Pagini recente » Cod sursa (job #2978404) | Cod sursa (job #1741213) | Cod sursa (job #2503646) | Cod sursa (job #2825992) | Cod sursa (job #377059)
Cod sursa(job #377059)
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define LMAX 3
/*
*
*/
using namespace std;
int value[30];
string FEcuation[30], FExpresion;
const string op[]={ "+-", "*/", "" };
string::const_iterator it, iend, it2, iend2;
ofstream out("apel.out");
inline int evaluate( int a, int b, char op )
{
switch( op )
{
case '+' : return a+b; break;
case '*' : return a*b; break;
case '/' : return a/b; break;
case '-' : return a-b; break;
}
exit(1);
}
int evaluate( vector<int>* arrity, int level )
{char p;
int x, y;
if( LMAX == level )
{
if( '(' == *it ) //we have a knew expresion in the brackets
{
++it; //go over (
x=evaluate( arrity, 0); //evaluate the new expresion;
++it;//go over )
}
else if( *it >= 'a' && *it <= 'z' ) //if it's a variable
x=(*arrity)[*it-'a']; //get it's value
else if( *it >= '0' && *it <= '9' )//if it's number
for( x=0; it < iend && *it >= '0' && *it <= '9'; ++it )
x=x*10+*it-'0'; //get the number
}
else for( x=evaluate( arrity, level+1); it < iend && op[level].end() != find( op[level].begin(), op[level].end(), *it ); x=y )
{
y=evaluate( x, evaluate( arrity, level+1), *it++ );
}
return x;
}
int main()
{vector<int> arrity;
ifstream in("evaluare.in");
in>>FExpresion;
it=FExpresion.begin();
iend=FExpresion.end();
ofstream out("evaluare.out");
out<<evaluate( &arrity, 0 );
}