分享到:
发表于 2010-06-15 15:57:43 楼主 | |
const int maxstack=30; typedef double Stack_operand; typedef char Stack_operate; enum Error_code{overflow,success,underflow}; enum Error_code outcome; class Stack { public: Stack(); bool empty() const; bool full() const; Error_code pop(); //出栈 private: int count; }; class Stack_d:public Stack {public: Error_code top(Stack_operand &item_d) const; //取操作数栈顶元素 Error_code push(const Stack_operand &item_d); //操作数栈压栈 private: Stack_operand entry[maxstack]; }; class Stack_t:public Stack {public: Error_code top(Stack_operate &item_t) const; //取运算符栈栈顶元素 Error_code push(const Stack_operate &item_t); //运算符栈压栈 private: Stack_operate entry[maxstack]; }; 【caculator.cpp文件】 #include #include #include"stack.h" //以下为Stack类定义 Stack::Stack() //constructor {count=0;} bool Stack::empty() const {bool outcome=true; if(count>o)outcome=false; return outcome; } bool Stack::full() const; {bool outcome=false; if(count=maxstack-1) outcome=true; return outcome; } Error_code Stack::pop(); {Error_code outcome=success; if(count=0) outcome=underflow; else --count; return outcome; } Error_code Stack_d::top(Stack_operand &item_d)const; //取操作数栈顶元素item_d {Stack_operand &item_d; Error_code outcome=success; if(count=0) outcome=underflow; else item_d=entry[count-1]; return outcome; } Error_code Stack_t::top(Stack_operate &item_t)const; //取运算符栈栈顶元素item_t {Stack_operate &item_t Error_code outcome=success; if(count=0) outcome=underflow; else item_t=entry[count-1]; return outcome; } Error_code Stack_d::push(const Stack_operand &item_d); //将数据item_d压入操作数栈中 {const Stack_operand &item_d Error_code outcome=success; if(count>=maxtrack) outcome=overflow; else entry[count++]=item_d; return outcome; } Error_code Stack_t::push(const Stack_operate &item_t); //将数据item_t压入运算符栈中 {const Stack_operate &item_t Error_code outcome=success; if(count>=maxtrack) outcome=overflow; else entry[count++]=item_t; return outcome; } //以下为调用函数定义 int judge(char ch) //判断函数:判断ch是运算符号还是运算数据 {if(ch='0'||ch='1'||ch='2'||ch='3'||ch='4'||ch='5'||ch='6'||ch='7'||ch='8'||ch='9'||ch='.') return 1; if(ch='('||ch=')'||ch='+'||ch='-'||ch='*'||ch='/'||ch='#') return 0; } int qiumi(int a,int b) //求幂函数:a的b次幂(既10的b次幂)(目的:将数组转化为十进制数后压栈) {int a,b; int i,temp=1; for(int i=0;i{temp*=a;} return temp; } double deci_sim(int t) //求值:将数组转化为十进制数后压栈回运算数据 {int mi[10],q; double operand=0; for(q=0;t=0;q++,t--) { mi[q]=int qiumi(10,q) operand=operand+temp[t]*mi[q]; } return operand; double deci_comp(int n) {int n double sum; double mi[10],operand_xs; //小数点后面转化成十进制数 int p; for(p=-1;j+1=i;p--) { double temp[10]; mi[p]=int qiumi(10,p); operand_xs=operand_xs+temp[j+1]*mi[p]; j=j+1; } sum=deci_sim(n)+operand_xs; return sum; } char compare(t.top,ch) //判断函数:判断运算符栈运算符号的优先级 { if (t.top=='+'&&ch='+')return '>'; if (t.top=='+'&&ch='-')return '>'; if (t.top=='-'&&ch='+')return '>'; if (t.top=='-'&&ch='-')return '>'; if (t.top=='*'&&ch='*' return '>'; if (t.top=='/'&&ch='/')return '>'; if (t.top=='*'&&ch='/')return '>'; if (t.top=='/'&&ch='*')return '>'; if (t.top=='*'&&ch='+')return '>'; if (t.top=='*'&&ch='-')return '>'; if (t.top=='/'&&ch='+')return '>'; if (t.top=='/'&&ch='-')return '>'; if (t.top=='+'&&ch=')')return '>'; if (t.top=='+'&&ch='#')return '>'; if (t.top=='-'&&ch=')')return '>'; if (t.top=='-'&&ch='#')return '>'; if (t.top=='*'&&ch=')')return '>'; if (t.top=='*'&&ch='#')return '>'; if (t.top=='/'&&ch=')')return '>'; if (t.top=='/'&&ch='#')return '>'; if (t.top==')'&&ch='+')return '>'; if (t.top==')'&&ch='-')return '>'; if (t.top==')'&&ch='*')return '>'; if (t.top==')'&&ch='/')return '>'; if (t.top==')'&&ch='(')return '>'; if (t.top==')'&&ch='#')return '>'; if (t.top=='+'&&ch='*')return '<'; if (t.top=='+'&&ch='/')return '<'; if (t.top=='+'&&ch='(')return '<'; if (t.top=='-'&&ch='*')return '<'; if (t.top=='-'&&ch='/')return '<'; if (t.top=='-'&&ch='(')return '<'; if (t.top=='*'&&ch='(')return '<'; if (t.top=='/'&&ch='(')return '<'; if (t.top=='('&&ch='+')return '<'; if (t.top=='('&&ch='-')return '<'; if (t.top=='('&&ch='*')return '<'; if (t.top=='('&&ch='/')return '<'; if (t.top=='('&&ch='(')return '<'; if (t.top=='#'&&ch='+')return '<'; if (t.top=='#'&&ch='-')return '<'; if (t.top=='#'&&ch='*')return '<'; if (t.top=='#'&&ch='/')return '<'; if (t.top=='#'&&ch='(')return '<'; if (t.top=='('&&ch=')')return '='; if (t.top=='#'&&ch='#')return '='; } double execute(Stack_operand &a,Stack_operate &op,Stack_operand &b) {double a,b; char op; switch {case'op='+'':return b+a;break; case'op='-'':return b-a;break; case'op='*'':return b*a;break; case'op='/'':return b/a;break; } } } 【main.cpp文件】 typedef char Stack_entry; #include "stack.h" #include #include using namespace std; int main() { Stack_d d; //定义一个操作数栈d Stack_t t; //定义一个运算符栈t char ch; double sum_f; t.push('#'); //将#压入运算符栈栈底 cout<<"Please input an espression(Ending with #)"< while(ch!='#'||t.top(Stack_operate &item_t)!='#') {if(judge(ch)=1) //判断ch是否为操作数 {int i; int m=1 int temp[10]; for(i=0;(i<=9)&&(m=1);i++) {temp=ch; ch=getchar(); if(judge(ch)=1) m=1; else {m=-1;i--;} } int j; double sum_m; if (for(j=0;j sum_m=double deci_comp(j-1); //有小数点,进入函数deci_comp,进行十进制转换 } else {double deci_sim(i); //无小数点,进入函数deci_sim,进行十进制转换 int t=i; sum_m=double deci_sim(i); } d.push(sum_m); } else {if(judge(ch)=0) {switch(compare(t.top,ch)) {case'<':t.push(ch); ch=getchar();break; case'=':t.pop(ch); ch=getchar();break; case'>':Stack_operand &a,&b Stack_operate &op d.top(&a); d.top(&b); t.top(&op); double v,&u=v; v=execute(a,op,b); d.push(&u); break; } } else ch=getchar(); } } sum_f=t.top(Stack_operand &item_d); //若运算符栈是两个#,则直接输出操作数栈栈顶 return sum_f; } 在while语句中的判断条件总是编译不过去,请问是什么问题! 还有,我的算法可能比较繁琐 请问有没有更好的算法?? |
|
楼主热贴
个性签名:无
|
针对ZOL星空(中国)您有任何使用问题和建议 您可以 联系星空(中国)管理员 、 查看帮助 或 给我提意见