Cod sursa(job #995211)
Utilizator | Data | 8 septembrie 2013 00:50:22 | |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 80 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 10.56 kb |
# include <cstring>
# include <iostream>
# include <fstream>
# include <string>
# include <stack>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
char s[100010];
stack<char> operatori;
stack<int> numere;
int main()
{
f >> s;
char *ptr = s;
s[strlen(s) + 1] = '\0';
s[strlen(s)] = ')';
operatori.push('(');
while (ptr - &s[0] < strlen(s)) {
char aux;
int nr1, nr2;
/*if (!isdigit(*ptr))
cout << *ptr;*/
switch (*ptr) {
case '(':
operatori.push('(');
break;
case '/':
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
operatori.push('/');
break;
case '*':
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
operatori.push('*');
break;
case '-':
while (operatori.top() == '*' || operatori.top() == '/') {
aux = operatori.top();
operatori.pop();
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (aux == '*') {
numere.push(nr1 * nr2);
} else {
numere.push(nr1 / nr2);
}
}
if (operatori.top() == '-') {
operatori.pop();
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
numere.push(nr1 - nr2);
}
operatori.push('-');
break;
case '+':
while (operatori.top() == '*' || operatori.top() == '/') {
aux = operatori.top();
operatori.pop();
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (aux == '*') {
numere.push(nr1 * nr2);
} else {
numere.push(nr1 / nr2);
}
}
if (operatori.top() == '-') {
operatori.pop();
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
numere.push(nr1 - nr2);
}
operatori.push('+');
break;
case ')':
aux = operatori.top();
operatori.pop();
while (aux != '(') {
switch (aux) {
case '+':
while (operatori.top() == '*' || operatori.top() == '/') {
aux = operatori.top();
operatori.pop();
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (aux == '*') {
numere.push(nr1 * nr2);
} else {
numere.push(nr1 / nr2);
}
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
numere.push(nr1 + nr2);
break;
case '-':
while (operatori.top() == '*' || operatori.top() == '/') {
aux = operatori.top();
operatori.pop();
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (aux == '*') {
numere.push(nr1 * nr2);
} else {
numere.push(nr1 / nr2);
}
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
numere.push(nr1 - nr2);
break;
case '*':
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
numere.push(nr1 * nr2);
break;
case '/':
if (operatori.top() == '/' || operatori.top() == '*') {
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
if (operatori.top() == '/') {
numere.push(nr1 / nr2);
} else {
numere.push(nr1 * nr2);
}
operatori.pop();
}
nr2 = numere.top();
numere.pop();
nr1 = numere.top();
numere.pop();
numere.push(nr1 / nr2);
break;
}
aux = operatori.top();
operatori.pop();
}
break;
default:
int num = 0;
while (*ptr >= '0' && *ptr <= '9') {
num = num * 10 + (*ptr - '0');
ptr++;
}
numere.push(num);
//cout << num;
ptr--;
break;
}
ptr++;
}
//cout << endl;
/*while (!numere.empty()) {
cout << numere.top() << ' ';
numere.pop();
}
cout << endl;
while (!operatori.empty()) {
cout << operatori.top() << ' ';
operatori.pop();
}*/
g << numere.top();
return 0;
}