#include #include #include #include #include #include using namespace std; struct function { char const * name ; double (* call)(double) ; } ; function table [] = { "cos",&cos, "sin",&sin, "abs",&fabs, "tan",&tan, "arccos",&acos, "arcsin",&asin, "arctan",&atan, "exp",&exp, "ln",&log, "log",&log10, "sqrt",&sqrt } ; template void F2(string & str, Iterator begin, Iterator end) { Iterator i, temp, i1, beg; double x, d; string s, ss; for ( ;; ) { int count = 0; for (i = str.begin ();i != str.end ();++i) { if( isalpha(*i) ) { ++count; beg = i; while (isalpha(*i)) ++i; s(beg,i+1); }else if (*i == '(') { temp = i; while (*i != ')') ++i; ss(temp+1,i); x = atof(ss.c_str ()); } ostringstream sss; sss << d ; string s1 = sss.str(); str.replace (beg,i,s1) ; break; } for (int a = 0;a < sizeof(table)/sizeof (table [0]);++a) { if (!strcmp (table [a].name,s.c_str())) d = table [a].call(x) ; } if (count == 0) return ; } } template void F1(string & str, Iterator begin, Iterator end) { Iterator i, temp, i1; float f; for ( ;; ) { int count = 0; for (i = str.begin ();i != str.end ();++i) { if (*i == '(') { ++count; temp = i; } else if (*i == ')') { ++count; f=F(temp+1,i); ostringstream ss; ss << f ; string s = ss.str(); str.replace (temp,i+1,s) ; break; } } if (count == 0) return ; } } template float F(Iterator begin, Iterator end) { Iterator i; if ((i = find(begin,end,'+')) != end) { return F(begin,i) + F(i+1,end) ; }else if ((i = find(begin,end,'-')) != end) { return F(begin,i) - F(i+1,end) ; }else if ((i = find(begin,end,'*')) != end) { return F(begin,i) * F(i+1,end) ; }else if ((i = find(begin,end,'/')) != end) { return F(begin,i) / F(i+1,end) ; }else if ((i = find(begin,end,'^')) != end) { return pow(F(begin,i),F(i+1,end)) ; }else { string str1(begin,end) ; istringstream stream(str1); float i; stream >> i; return i ; } } int main() { string str,str1; float a; getline(cin,str); F1(str,str.begin(),str.end()); a=F(str.begin(),str.end()); cout << "Ats: " << a << endl; cin.get(); }