#include #include #include #include #include using namespace std; using namespace boost::spirit; //----------------------------------------------------------------------------- // スタック //----------------------------------------------------------------------------- stack stk; //stack stk; double x=0.0; void push(double n) //push(int n) { stk.push(n); } double pop() //int pop() { double t = stk.top(); //int t = stk.top(); stk.pop(); return t; } void clear() { stk = stack(); //stk = stack(); } //----------------------------------------------------------------------------- // セマンティックアクション //----------------------------------------------------------------------------- void PUSH(double n) //PUSH(int n) { stk.push( n ); } void ADD(const char* const, const char* const) { double a = pop();//int a = pop(); double b = pop();//int b = pop(); push(a + b); } void SUB(const char* const, const char* const) { double a = pop();//int a = pop(); double b = pop();//int b = pop(); push(b - a); } void MUL(const char* const, const char* const) { double a = pop();//int a = pop(); double b = pop();//int b = pop(); push(a * b); } void DIV(const char* const, const char* const) { double a = pop();//int a = pop(); double b = pop();//int b = pop(); push(b / a); } void POW(const char* const, const char* const) { double a = pop();//int a = pop(); double b = pop();//int b = pop(); push(pow(b,a)); } void PUSH_x(char val) //(char c) //PUSH(int n) { stk.push( x ); } void PUSH_SIN(char const *first, char const *last) { double a = pop();//int a = pop(); stk.push( sin(a) ); } //----------------------------------------------------------------------------- // 文法 //----------------------------------------------------------------------------- struct calculator : public grammar { template struct definition { rule expression, term, factor, group, func, variable; //definition(const calculator& self) //{ // // 構文定義 // group = '(' >> expression >> ')'; // factor = real_p[&PUSH] | group ; // term = factor >> *( ('*' >> factor)[&MUL] | ('/' >> factor)[&DIV] ); // expression = term >> *( ('+' >> term)[&ADD] | ('-' >> term)[&SUB] ); //} definition(const calculator& self) { // 構文定義 group = '(' >> expression >> ')'; factor = real_p[&PUSH] | group | func | variable; term = factor >> *( ('*' >> factor)[&MUL] | ('/' >> factor)[&DIV] | ('^' >> factor)[&POW] ); expression = term >> *( ('+' >> term)[&ADD] | ('-' >> term)[&SUB] ); func = ( str_p("sin") >> group )[&PUSH_SIN]; variable = ch_p('x')[&PUSH_x]; } // 開始記号を定義 const rule& start() const { return expression; } }; }; int main() { FILE *fp; char str[256]; if((fp = fopen( "eq1.txt", "r")) == NULL) { printf("EQUATION FILE OPEN ERROR\n"); } fscanf(fp, "%s", str); printf("Equation=%s\n", str); calculator calc; int i; for(i=0;i<=10;i++) { x = (double)i/10; printf(" x=%e",x); // 入力された文字列をcalcに入力し、space_p(空白、タブ、改行)を抜いて解析 parse_info<> r = parse(str, calc, space_p); if(r.full) // 入力された文字列が全て解析されていたら答えを出力 cout << " result = " << pop() << endl; else // 入力された文字列が全て解析されてないならエラーとみなす cout << "error" << endl; clear(); } return 0; }