数据结构课程设计-计算器 联系客服

发布时间 : 星期四 文章数据结构课程设计-计算器更新完毕开始阅读63d556fab9d528ea80c77918

}

}

++it;

if(!sym.empty()) { } exp=to; return true;

to=to+\; tem=sym.top(); while(!sym.empty()) { }

if(tem=='(') { }

to=to+tem+\; sym.pop();

if(sym.empty())break; tem=sym.top();

cout<<\输入错误:括号匹配有误\<

bool ExpressionHandler::doprofix(string profix,double& result) {

string numtemp; stack numstack; stringstream strm; double d,d1,d2;

for(string::iterator it=profix.begin();it!=profix.end();++it) {

switch (*it) { case

{ } {

if(numtemp!=\) {

numtemp+=*it; break; '1':case

'2':case

'3':case'4':case

'5':case

'6':case'7':case

'8':case'9':case '0':case '.':

case ' ':

if(numtemp.find('.')&&numtemp.find('.',(numtemp.find('.')+1))!=string::npos)

}

}

{ }

strm.str(numtemp); strm>>d;

numstack.push(d); numtemp=\; strm.str(\); strm.clear(); break;

cout<<\输入错误:小数点数目超出:\+numtemp<

break;

case '+':

{ } { } { } {

d2=numstack.top(); d2=numstack.top(); numstack.pop(); d1=numstack.top(); numstack.pop(); numstack.push(d1*d2); break;

d2=numstack.top(); numstack.pop(); d1=numstack.top(); numstack.pop(); numstack.push(d1-d2); break;

d2=numstack.top(); numstack.pop(); d1=numstack.top(); numstack.pop(); numstack.push(d1+d2); break;

case '-':

case '*':

case '/':

}

}

} { }

numstack.pop(); if(fabs(d2)<0.0000001) { }

d1=numstack.top(); numstack.pop(); numstack.push(d1/d2); break;

cout<<\输入错误:除数不能为0\<

case '%':

d2=numstack.top(); numstack.pop(); d1=numstack.top(); numstack.pop();

if((fabs(d2-(int)d2))<0.0000001&&(fabs(d1-(int)d1))<0.0000001) { } else { }

cerr<<\输入错误:求模操作只能作用于整数\<

numstack.push((double)(n1%n2)); break;

if(numtemp!=\) {

if(numtemp.find('.')&&numtemp.find('.',(numtemp.find('.')+1))!=string::npos)

{ }

strm.str(numtemp); strm>>d;

cout<<\输入错误:小数点数目超出:\+numtemp<

}

}

numstack.push(d);

if(numstack.empty()) { }

result=numstack.top(); return true;

cout<<\输入错误:表达式中没有合法运算数\<

文件三. Main.cpp

#include \ int main() { }

string str; double r;

cout<<\请输入一个含有加、减、乘、除或求模运算的表达式,中间不能有空格\<>str;

cout<<\运算结果:\<

if(eh.infixtoprofix(result)) { }

else cout<<\程序终止\<

if(eh.doprofix(result,r)) cout<

else cout<<\程序终止\<