0% found this document useful (0 votes)
45 views6 pages

Travail Pratique nr.4 SDA: Programmation de L'ordinateur

This document contains code for several programs related to stacks, binary trees, and logical expressions. It includes code to: 1) Evaluate logical expressions using a stack to pop operands and push results. 2) Convert binary numbers to decimal using a stack. 3) Create and print a min binary heap tree from an array of integers. 4) Push integers to a stack structure and pop them off to calculate binary expressions.

Uploaded by

Damean Alexandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

Travail Pratique nr.4 SDA: Programmation de L'ordinateur

This document contains code for several programs related to stacks, binary trees, and logical expressions. It includes code to: 1) Evaluate logical expressions using a stack to pop operands and push results. 2) Convert binary numbers to decimal using a stack. 3) Create and print a min binary heap tree from an array of integers. 4) Push integers to a stack structure and pop them off to calculate binary expressions.

Uploaded by

Damean Alexandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ministère de l'éducation de la République de Moldova

Université technique de Moldavie


Faculté d’Ordinateur,Informatique et Microélectronique
Filière francophone “Informatique”

Programmation de l’ordinateur

Travail pratique nr.4 SDA


Thème: Lucrarea de laborator №4.
Evaluare expresii de relatie si logice folosind stive si MinHeap binar

Effectué par l’étudiant(e) de gr FI-191 : Chebac Grigore

Vérifié par le professeur : Rusu Viorel

Chișinau 2020
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

double *Stack;
int sPtr;

void init()
{
Stack=new double[100];
sPtr=0;
}

void del()
{
delete [] Stack;
}

void Push(int z)
{
Stack[sPtr++]=z;
}

int Pop()
{
return Stack[--sPtr];
}

int eval(char *str)


{
int num1,num2;
int i = 0,j = 0,k = 0;
char Tmp[10],s;

for (j=0; j<10; j++) Tmp[j]=0; k=0;

for (i=0; i < strlen(str); i++)


{
s=str[i];

if (((s >= '0') && (s <= '9')))


Tmp[k++]=s;
else
if ((s == '>') || (s == '<') || (s == '#') || (s == '&') || (s == '|'))
{

Push(atof(Tmp));

for (j=0; j<10; j++) Tmp[j]=0; k=0;

switch (s)
{
case '>':
num1=Pop();
num2=Pop();
if(num1 > num2){
Push(1);
} else{
Push(0);
}
break;
case '<':
num2=Pop();
num1=Pop();
if(num1 < num2) {
Push(1);
} else{
Push(0);
}
break;
case '#':
num2=Pop();
num1=Pop();
if(num1 != num2) {
Push(1);
} else {
Push(0);
}
break;
}

}
}
return Pop();
}

int main()
{
char *exp="34#";
init();
cout << exp << " = ";
cout << eval(exp);
del();
return 0;
}

#include <iostream>
#include <cstring>
#include <cmath>"

using namespace std;

typedef struct stack{

char *str;
int num[20];
int top;
}stack;

char init(stack *tp){

tp->top = 0;

return 0;
}
void push_calc(stack *stk, int n_b){

if(stk->top < 20){

stk->num[stk->top] = n_b;
stk->top++;

} else{
cout << "Стек полон " << stk->top;
}
}

int pop_calc(stack *stk){

int end = 0;

if(stk->top > 0) {

stk->top--;
end = stk->num[stk->top];
return end;

} else{
cout << "Stack ne a les variables pas";
return 0;
}
}
//
***********************************************************************************************
********************

int calc(char op, int x,int y){

switch(op){
case '+':
x = x + y;
break;
case '-':
x = x - y;
break;
case '*':
x = x * y;
break;
case '/':
x = x / y;
break;
case '%':
x = x % y;
break;
default:
break;
}

int ost = 0, kappa = 0;

stack *stk;
stk = new(stack);

init(stk);
while (x != 0) {
ost = x % 2;
x = x / 2;
push_calc(stk,ost);
kappa++;
}

puts("");

while (kappa != 0){


printf(" %d", pop_calc(stk));
kappa--;
}
free(stk);

}
int main() {

int ost_n_b_1 = 0,ost_n_b_2 = 0, resul = 0, resul1 = 0, num_bin1 = 0, num_bin2 = 0;

int n_1[10], n_2[10];


int i = 0;

cin >> num_bin1 >> num_bin2;

while(num_bin1 != 0 && num_bin2 != 0) {

n_1[i] = ost_n_b_1 = num_bin1 % 10;


num_bin1 = num_bin1 / 10;

n_2[i] = ost_n_b_2 = num_bin2 % 10;


num_bin2 = num_bin2 / 10;
i++;
}

for (int j = 0; j < i ; ++j) {


if(n_1[j] == 1){
resul = resul + pow(2,j);
}
if(n_2[j] == 1){
resul1 = resul1 + pow(2,j);
}
}

cout << resul << " " << resul1;


puts(" ");

calc('+',resul,resul1);
return 0;
}

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define M 10
struct node{
int info;
struct node *l, *r;
};

struct node *tree = NULL;

void makeHeap(int a, struct node **t){

if((*t) == NULL){
*t = (struct node*)malloc(sizeof(struct node));
(*t)->info = a;
(*t)->l = (*t)->r = NULL;
return;
}
if(a > (*t)->info){
makeHeap(a, &(*t)->l);
} else {
makeHeap(a, &(*t)->r);
}

void printH (struct node *t)


{
if (t == NULL) return;
else {
printH(t->l);
printf("%d \n\t",t->info);
}
printH(t->r);
free(t);
}

void printT(struct node *t, int n,int r,int ind){

if(t == NULL) return;


printf("%d\n",t->info);
printT(t->l,++n,r,ind);
printT(t->r,n,r,ind);
}

int main () {
int a[M]={0,6,2,8,4,1,7,3,9,5};
int n=9,s;

for (int i = 0; i < n; ++i) {


s = a[i];
makeHeap(s, &tree);
}
printT(tree,n,1,3);
}

You might also like