Pagini recente » Cod sursa (job #2215071) | Cod sursa (job #2099659) | Cod sursa (job #918888) | Cod sursa (job #2573219) | Cod sursa (job #2843830)
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
#define EMPTY -1
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
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;
}
int 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() {
char *i,*p;
char n1;
struct stack x;
empty(&x);
string q;
fin >> 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) || isalpha(*y)) {
string c1="";
while (isdigit(*y) || isalpha(*y)) {
c1=c1+*y;
y++;
}
int z=stoi(c1);
// cout << z << '\n';
push2(&stk,z);
}
else {
op1=pop2(&stk);
op2=pop2(&stk);
// cout << op1 << " " << op2 << " " << *y << " ";
switch(*y) {
case '+':
result = op2+op1;
break;
case '-':
result = op2-op1;
break;
case '*':
result = op2*op1;
break;
case '/':
result = op2/op1;
break;
}
// cout << result << endl;
push2(&stk,result);
//display2(stk);
}
y++;
}
result=pop2(&stk);
fout << result;
}
int main() {
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
evaluare();
}