|
常用链接
留言簿(1)
随笔分类
随笔档案
文章分类
文章档案
相册
搜索
最新评论
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
附上代码:
CAP.h 表达式的计算
//CAP.h 表达式的计算 #pragma once enum{NUMBER, OPERATOR, END, UNKNOWN}; class Token { public: double value; char opeor; int type; Token(double _value=0,char _opeor='#',int _type=UNKNOWN) :value(_value),opeor(_opeor),type(_type){} }; Token t[20]; int por(char ch) { switch(ch) { case '(': case '#': return 0; case '+': case '-': return 1; case '*': case '/': return 2; default:return -1; } } void Ctpostexp(string str,queue<Token>& q) { stack<char> s_ch; s_ch.push('#'); int i=0,index_t=0,sum=0; char ch=str[i]; while(i<str.size()) { sum=0; ch=str[i]; if((ch<='9'&&ch>='0')) { while((ch<='9'&&ch>='0')) { sum=sum*10+ch-'0'; ch=str[++i]; } t[index_t].type=NUMBER; t[index_t].value=sum; q.push(t[index_t]); ++index_t; } else { if(por(str[i])>por(s_ch.top())) { s_ch.push(str[i]); } else if(str[i]=='(') { s_ch.push(str[i]); } else if(str[i]==')') { while(s_ch.top()!='(') { t[index_t].type=OPERATOR; t[index_t].opeor=s_ch.top(); q.push(t[index_t]); s_ch.pop(); } s_ch.pop(); } else if(str[i]=='#') { while(s_ch.top()!='#') { t[index_t].type=OPERATOR; t[index_t].opeor=s_ch.top(); q.push(t[index_t]); s_ch.pop(); } s_ch.pop(); } else if(por(str[i])<=por(s_ch.top())) { t[index_t].type=OPERATOR; t[index_t].opeor=s_ch.top(); q.push(t[index_t]); s_ch.pop(); s_ch.push(str[i]); } ++i; ++index_t; } } } double Calpostexp(queue<Token>& q) { stack<double> s_int; while(!q.empty()) { double a=0,b=0; if(q.front().type==NUMBER) { s_int.push(q.front().value); q.pop(); } else if(q.front().type==OPERATOR) { char ch_op=q.front().opeor; switch(ch_op) { case '+': a=s_int.top(); s_int.pop(); b=s_int.top(); s_int.pop(); s_int.push(b+a); break; case '-': a=s_int.top(); s_int.pop(); b=s_int.top(); s_int.pop(); s_int.push(b-a); break; case '*': a=s_int.top(); s_int.pop(); b=s_int.top(); s_int.pop(); s_int.push(b*a); break; case '/': a=s_int.top(); s_int.pop(); b=s_int.top(); s_int.pop(); s_int.push(b/a); break; default:break; } q.pop(); } else { cout<<"error";break; }; } double num=s_int.top(); return num; }
exp.h 词法分析和语法分析
#pragma once #include<iostream> #include<fstream> #include <stack> #include<map> #include <queue> #include<vector> #include <string> #include <sstream> using namespace std; int kk=0; int qq=0;//语法分析 vector<int>synv; vector<string>res_prog; int expression(); int term(); int factor(); void scanner(string prog,vector<int>& synv,vector<string>& res_prog) { int syn=0,cur=0; string token; stringstream ss; int index_token=0; int sum=0; string rwtab[6]={"begin","if","then","while","do","end"}; char ch; do { sum=0; index_token=0; ss.str(""); token.clear(); ch=prog[cur++]; while(ch==' ') ch=prog[cur++]; if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')) { while((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')||(ch<='9'&&ch>='0')) { token.push_back(ch); ch=prog[cur++]; } --cur; syn=10; for(int i=0;i<6;++i) if(token==rwtab[i]) { syn=i+1; break; } } else if((ch<='9'&&ch>='0')) { while((ch<='9'&&ch>='0')) { sum=sum*10+ch-'0'; ch=prog[cur++]; } syn=11; --cur; } else { switch(ch) { case'<': index_token=0; token.push_back(ch); ch=prog[cur++]; if(ch=='>') { syn=21; token.push_back(ch); } else if(ch=='=') { syn=22; token.push_back(ch); } if(ch=='>') { syn=21; --cur; } break; case'>': index_token=0; token.push_back(ch); ch=prog[++cur]; if(ch=='=') { syn=24; token.push_back(ch); } else { syn=23; --cur; } break; case':': index_token=0; token.push_back(ch); ch=prog[cur++]; if(ch=='=') { syn=18; token.push_back(ch); } else { syn=17; --cur; } break; case'+': syn=13; token.push_back(ch); break; case'-': syn=14; token.push_back(ch); break; case'*': syn=15; token.push_back(ch); break; case'/': syn=16; token.push_back(ch); break; case';': syn=26; token.push_back(ch); break; case'(': syn=27; token.push_back(ch); break; case')': syn=28; token.push_back(ch); break; case'#': syn=0; token.push_back(ch); break; default:syn=-1; } } synv.push_back(syn); switch(syn) { case 11: //printf("(%d,%d)\n",syn,sum); ss<<sum; res_prog.push_back(ss.str()); break;//输出(数的二元组); case -1:printf("error\n"); break; default: //printf("(%d,%s)\n",syn,token.c_str()); res_prog.push_back(token); break;//输出(关键字begin的二元组);break; } } while(syn!=0); } int expression() { if(term()) return 1; while(synv[qq]==13||synv[qq]==14) { ++qq; if(term()) return 1; } if(synv[qq]==0&&kk!=1) { //cout<<"sus"<<endl; //fout<<"sus"<<endl; return 0; } else return 1; } int term() { if(factor()) return 1; while(synv[qq]==15||synv[qq]==16) { ++qq; if(factor()) return 1; } return 0; } int factor() { if(synv[qq]==10||synv[qq]==11) ++qq; else if(synv[qq]==27) { ++qq; expression(); if(synv[qq]==28) ++qq; else { //printf(") error\n"); kk=1; } } else { //printf("factor synax error\n"); //fout<<"factor synax error\n"; kk=1; return 1; } return 0; }
main
#include "stdafx.h" #include"exp.h" #include"CAP.h" ostringstream oss; int ww=0; int apend=0; void DelSpace(string& str) { char curr = 0; while (curr<str.size()) { if(' '==str.at(curr)) str.erase(curr,1); else curr++; } } void GetExpression(int a[], int n) { string token[] = {"+","-","*","/","+(" ,"-(","*(","/(",")+",")-",")*",")/"," "}; //string token[] = {"+","-","*","/"," "}; //static char token[] = {'+','-',' '}; oss.str(""); oss<<1<<token[a[0]]<<2<<token[a[1]]<<3<<token[a[2]]<<4<<token[a[3]] <<5<<token[a[4]]<<6<<token[a[5]]<<7<<token[a[6]]<<8<<token[a[7]]<<9; //9577次时出现异样 string str=oss.str(); DelSpace(str); str.append(1,'#'); synv.clear(); res_prog.clear(); qq=0; kk=0; scanner(str,synv,res_prog); ++ww; if(!expression()) { queue<Token>q; Ctpostexp(str,q); double result = Calpostexp(q); if (result == 100) {str.erase(str.size()-1,1); cout << str<< '=' << result << endl; //fout << str<< '=' << result << endl; } } } void DoEnum(int n, int curr, int to[]) { if (curr == n) { GetExpression(to, n); } else { for (int i = 0; i<13; ++i) { to[curr] = i; DoEnum(n, curr+1, to); } } } int _tmain(int argc, _TCHAR* argv[]) { int to[8]; DoEnum(8,0,to); return 0; }
|
|