首先是顺序栈的定义 stack.h /* * Copyright (c) 2010,hnu-software * All rights reserved. * * 文件名称:Stack.h * 文件标识:.h * 摘 要:顺序栈的声明 * * 当前版本:1.1 * 作 者:www * 完成日期: */ #ifndef STACK_H #define STACK_H #define STACK_INIT_SIZE 100 #include<iostream> using namespace std; template <class type> class stack { public: stack(int = STACK_INIT_SIZE); ~stack() { delete []stackptr; } bool push(const type &); bool pop(type &); bool isempty()const; bool isfull()const; type getTopValue(); private: int size; int top; type* stackptr; }; template <class type> stack<type>::stack(int length) { size=length; //防止输入不合理的数字 top=-1; stackptr=new type[size]; } template<class type> bool stack<type>::isempty()const { return top==-1; } /// template<class type> bool stack<type>::isfull()const { return top==size-1; } // template<class type> bool stack<type>::push(const type &data) { if(!isfull()) { stackptr[++top]=data; return true; } return false; } / template<class type> bool stack<type>::pop(type &popvalue) { if(!isempty()) { popvalue=stackptr[top--]; return true; } return false; } template<class type> type stack<type>::getTopValue() { type temp; if(!isempty()) { temp=stackptr[top]; } return temp; } #endif 计算器的实现 cal.cpp /* * Copyright (c) 2010,hnu-software * All rights reserved. * * 文件名称:cal.cpp * 文件标识:.cpp * 摘 要:计算器实现,支持小数,小括号 * * 当前版本:1.1 * 作 者:www * 完成日期: */ #include<iostream> #include "stack.h" using namespace std; bool issign(char );//判断c中的值是否为运算符,如果是则返回1 char Precede(char c1,char c2);//比较运算符之间的优先级 double Operate(double a,char theta, double b);//计算a theta b 的值 double EvaluateExpression(char m[]);//表达式求值 void inputExpression(); int main() { char c[60]; cout<<"please input the expression and end with '=': "<<endl; cin>>c; double result=0; result=EvaluateExpression(c); cout<<"the result is :"<<result; system("pause"); return 0; } // bool issign(char c) { if(c!='+'&&c!='-'&&c!='*'&&c!='/'&&c!='='&&c!='('&&c!=')') return 0; else return 1; } char Precede(char c1,char c2) { switch(c1) { case '+':if(c2=='+'||c2=='-'||c2==')'||c2=='=') return '>'; else return '<';break; case '-':if(c2=='+'||c2=='-'||c2==')'||c2=='=') return '>'; else return '<';break; case '*':if(c2=='(') return '<'; else return '>';break; case '/':if(c2=='(') return '<'; else return '>';break; case '(':if(c2==')') return '='; else return '<';break; case ')': return '>';break; case '=':if(c2=='=') return '='; else return '<';break; default:return 0; } } double Operate(double a,char theta, double b) { switch(theta) { case '+': return a+b;break; case '-': return a-b;break; case '*': return a*b;break; case '/': if(b!=0) {return a/b;break;} else cout<<"分母不能为零!,输入错误"<<endl; default: return 0; } } double EvaluateExpression(char m[]) { stack<double>number; //存放表达式的数值 stack<char>sign;//存放表达式的符号 sign.push('=');//把等号存入符号栈中,用来判断表达式已经计算完毕 double stored_a,stored_b;//已经存入stack<int>number的值 int i=0;//m的下标 char signal;//保存符号 while(sign.getTopValue()!='=') { if(!issign(m[i])) { char*p=&m[i]; while(!issign(m[i])||m[i]=='.') { i++; } number.push(atof(p)); //如果不是运算符,则转换为浮点型并且压入存放数字的栈中 } else { switch( Precede(sign.getTopValue() , m[i])) { case '<': sign.push(m[i]);i++;break; case '=':sign.pop(signal);i++;break; case '>': sign.pop(signal); number.pop(stored_a); number.pop(stored_b); number.push(Operate(stored_b , signal , stored_a)); break; } } } double result; number.pop(result); return result; }