1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
using namespace std;
enum TOKEN
{
PARBEG, PAREND, PLUS, TIMES, SIN
}
typedef int Iterator;
int begin, end, oper;
double rakna(Iterator begin, Iterator end);
{
double res;
int has_type;
if(begin != end && begin+1!=end && *begin==TOKEN_PARBEG && *(end-1)==TOKEN_PAREND)
{
return rakna(begin+1,end-1,0);
}
if(oper==0)
{
Iterator next=begin, last=next;
res=0.0;
while(last!=end)
{
++last;
next=find_if(next,end,has_type(TOKEN_PLUS));
res+=rakna(last, next, 1);
}
}
else if(oper==1)
{
Iterator next=begin,last=next;
res=1.0;
while(last!=end)
{
++last;
next=find_if(next,end,has_type(TOKEN_TIMES));
res*=rakna(last,next,2);
}
}
else if(oper==2)
{
if(begin->type==TOKEN_SIN)
{
res=sin(rakna(begin+1,end,3));
}
}
else
{
assert(begin+1==end);
assert(begin->has_value());
res=begin->value;
}
return res;
}
|