Pagini recente » Cod sursa (job #3281851) | Cod sursa (job #496559) | Cod sursa (job #1307367) | Cod sursa (job #1165746) | Cod sursa (job #173601)
Cod sursa(job #173601)
#include <fstream.h>
#include <string.h>
//#include <conio.h>
ofstream fout("evaluare.out");
char ec[1000];
int ind, N;
void Citire()
{
ifstream fin("evaluare.in");
fin.get(ec, 1000);
N = strlen(ec);
fin.close();
}
int expr();
int factor();
int var();
int factor()
{
int t;
if (ec[ind] == '(' || ec[ind] == ')')
{
ind++;
t = expr();
ind++;
}
else
{
t=0;
while (ind<N && ec[ind] != '+' && ec[ind] != '-' && ec[ind] != '*' && ec[ind] != '(' && ec[ind] !=')')
{
t = t*10+ec[ind]-'0';
ind++;
}
//ind++;
}
return t;
}
int var()
{
int t = factor();
while (ind<=N && ec[ind] == '*' || ec[ind] == '/')
{
//ind++;
if (ec[ind] == '*'){ ind++;
t *= factor();
}
else
if (ec[ind] == '/'){ ind++;
t /= factor();
}
}
return t;
}
int expr()
{
int t = var();
while(ind<=N && ec[ind] == '+' || ec[ind] == '-')
{
if (ec[ind] == '+')
{
ind++;
t += var();
}
else
if (ec[ind] == '-')
{
ind++;
t -= var();
}
}
return t;
}
int main()
{
//clrscr();
Citire();
fout<<expr()<<"\n";
return 0;
//getch();
}