Pagini recente » Cod sursa (job #1237858) | Cod sursa (job #966977) | Cod sursa (job #58866) | Cod sursa (job #1709140) | Cod sursa (job #2050069)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("expresie.in");
ofstream g("expresie.out");
int main()
{
char s[100000], fp[100000], st[100000], x;
unsigned i=1, vf=-1, k=0;
long int rez, stv[100000];
s[0]='(';
while (f>>x) s[i++]=x;
s[i]=')';
for (i=0; i<strlen(s); i++)
{ if (s[i]=='(' || s[i]=='*' || s[i]=='/') { st[++vf]=s[i]; continue; }
if (s[i]>='0' && s[i]<='9') {
while (s[i]>='0' && s[i]<='9') fp[k++]=s[i++];
i--;
fp[k++]=' ';
continue; }
if (s[i]==')') {
while (st[vf]!='(') { fp[k++]=st[vf--]; fp[k++]=' '; }
vf--;
continue; }
if (s[i]=='+' || s[i]=='-') {
if (st[vf]=='*' || st[vf]=='/')
{ while (st[vf]=='*' || st[vf]=='/' || st[vf]=='+' || st[vf]=='-') { fp[k++]=st[vf--]; fp[k++]=' '; } }
st[++vf]=s[i];
continue; }
}
vf=-1;
for (i=0; i<=k; i++)
{ if (fp[i]==' ') continue;
if (fp[i]>='0' && fp[i]<='9')
{ rez=0;
while (fp[i]>='0' && fp[i]<='9') rez=rez*10+(fp[i++]-'0');
stv[++vf]=rez;
continue; }
if (fp[i]=='+') { stv[--vf]=stv[vf]+stv[vf+1]; continue; }
if (fp[i]=='-') { stv[--vf]=stv[vf]-stv[vf+1]; continue; }
if (fp[i]=='/') { stv[--vf]=stv[vf]/stv[vf+1]; continue; }
if (fp[i]=='*') { stv[--vf]=stv[vf]*stv[vf+1]; continue; }
}
g<<stv[vf];
f.close();
g.close();
return 0;
}