Cod sursa(job #2843821)

Utilizator alex.prohnitchiAlex Prohnitchi alex.prohnitchi Data 2 februarie 2022 23:25:01
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.31 kb
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
#define EMPTY -1

using namespace std;

typedef long long ll;


struct stack {
	char data[10005];
	int top;
};
struct stack2 {
	int data[10005];
	int top;
};

void push(struct stack* s, char item) {
	++s->top;
	s->data[s->top]=item;	
}


bool isempty(struct stack* s) {
	if (s->top==EMPTY)return true;
	else return false;
}
void empty(struct stack* s) {
	s->top=EMPTY;
}

void display(struct stack s) {
	while(s.top!=EMPTY) {
		cout << s.data[s.top];
		s.top--;
	}	
	cout << endl;
}
char pop(struct stack* s) {
	char ret=(char)EMPTY;
	if (!isempty(s)) {
		ret=s->data[s->top];
		--s->top;	
	}
	return ret;
}


void push2(struct stack2* s, int item) {
	++s->top;
	s->data[s->top]=item;	
}


bool isempty2(struct stack2* s) {
	if (s->top==EMPTY)return true;
	else return false;
}
void empty2(struct stack2* s) {
	s->top=EMPTY;
}

void display2(struct stack2 s) {
	while(s.top!=EMPTY) {
		cout << s.data[s.top];
		s.top--;
	}	
	cout << endl;
}
char pop2(struct stack2* s) {
	int ret=EMPTY;
	if (!isempty2(s)) {
		ret=s->data[s->top];
		--s->top;	
	}
	return ret;
}
bool isOperator(char c) {
	return (c=='/' || c=='+' || c=='-' || c=='*'); 
}

int priority(char c) {
	if (c=='+' || c=='-')return 1;
	else if (c=='/' || c=='*')return 2;
}
void evaluare() {
	ifstream cin("evaluare.in");
	ofstream cout("evaluare.out");
	char *i,*p;
	char n1;
	struct stack x;
	empty(&x);
	string q;
	cin >> q;
	ll n=(int)q.size();
	char infix[n+5]={0},postfix[2*n+5]={0};
	for (int i=0; i<n; i++) {
		infix[i]=q[i];
	}
	i=&infix[0];
	p=&postfix[0];
	
	while(*i) {
		//cout << i;
			
			if (isdigit(*i) || isalpha(*i)) {
				while (isdigit(*i) || isalpha(*i)) {
					*p=*i;
					i++;
					p++;
						
				}
				*p=' ';
				p++;
				
			}
			
			if (*i=='(') {
				push(&x,*i);
				i++;
			}
		//	display(x);
			if (*i==')') {
				
				n1=pop(&x);
				while (n1!='(') {
					*p=n1;
					p++;
					*p=' ';
					p++;
					//cout << n1;
					n1=pop(&x);
				}
				i++;
			}
			if (isOperator(*i)) {
				if (isempty(&x))push(&x,*i);
				else {
					n1=pop(&x);
					if(n1!='(')while (priority(n1)>=priority(*i)) {
						*p=n1;
						p++;
						*p=' ';
						p++;
						n1=pop(&x);
					}
					push(&x,n1);
					push(&x,*i);
				}
				i++;
			}	
	}
	while (!isempty(&x)) {
		n1=pop(&x);
		*p=n1;
		p++;
		*p=' ';
		p++;
	}
	
	*p='\0';
//	for(int i=0; i<2*n;i++)cout<<postfix[i];
//	cout << '\n';
	int op1,op2,result;
	char *y;
	struct stack2 stk;
	
	y=&postfix[0];
	
	empty2(&stk);
	
	while (*y != '\0') {
		while (*y==' ' || *y=='\t')y++;
		if (isdigit(*y)) { 
			string c1="";
			while (isdigit(*y)) {
				c1=c1+*y;
				y++;
			}
			int z=stoi(c1);
		//	cout << z << '\n';
			push2(&stk,z);
		}
		else {
			op1=pop2(&stk);
			op2=pop2(&stk);
			switch(*y) {
				case '+':
					result = op2+op1;
					break;
				case '-':
					result = op2-op1;
					break;
				case '*':
					result = op2*op1;
					break;
				case '/':
					result = op2/op1;
					break;
			}
			push2(&stk,result);
		}
		y++;
	}
	result=pop2(&stk);
	cout << result;
	
}
int main() {
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	evaluare();

}