头文件:
#ifndef EVALUATE_H
#define EVALUATE_H
#include<cstdlib>
#include<stack>
using std::stack ;
#include<iostream>
using std::begin ;
using std::endl;
using std::end;
using std::cout;
const int PN = 9 ;
const char opePrerity[PN][PN]{
// 当前运算符
// + - * / ! ^ ( ) \0
/*栈 + */ '>','>','<','<','<','<','<','>','>',
/*顶 - */ '>','>','<','<','<','<','<','>','>',
/*运 * */ '>','>','>','>','<','<','<','>','>',
/*算 / */ '>','>','>','>','<','<','<','>','>',
/*符 ! */ '>','>','>','>','>','>',' ','>','>',
/* ^ */ '>','>','>','>','<','>','<','>','>',
/* ( */ '<','<','<','<','<','<','<','=',' ',
/* ) */ ' ',' ',' ',' ',' ',' ',' ',' ',' ',
/* \0 */ '<','<','<','<','<','<','<',' ','=',
};
const char optor[9] = {'+','-','*','/','!','^','(',')','\0'} ;
int findIndex(char ch1) // 返回运算符在字符数组的位置
{
for(int i = 0 ; i < 9 ; i++)
if(ch1 == optor[i])
return i ;
return -1 ;
}
char orderBetween(char ch1 ,char ch2 ){ // 返回比较两个运算符的优先级,返回两个运算符在二维数组中的元素
int i = findIndex(ch1) ;
int j = findIndex(ch2) ;
if(i < 0 || j < 0 )
exit(0) ;
return opePrerity[i][j];
}
double calcu(char ch , double nu){ // 计算阶乘 !
int k = 1 ;
while(nu>=1){
k *= nu;
--nu ;
}
return k ;
}
double calcu(double nu1,char ch , double nu2){ // 计算二元运算符 + - * / ^乘方
switch(ch){
case '*' : return nu1 * nu2 ; break ;
case '/' : return nu1 / nu2 ; break ;
case '+' : return nu1 + nu2 ; break ;
case '-' : return nu1 - nu2 ; break ;
case '^' : {
double k = 1 ;
while(nu2--)
k *= nu1 ;
return k ;
break ;
}
default : return 0 ; break ;
}
}
double calculate(const char *str) // 计算表达式的值 ,表达式之间不要有空格
{
stack<double> operd; // 操作数栈
stack<char> oper ; //操作符栈
oper.push('\0');
while(!oper.empty()){
if(isdigit(*str)){// 读取str中连续数值的情况。
double d = atof(str) ; // 调用atof将字符串转化为double类型的数字,但是指针位置不变
operd.push(d) ;
while(isdigit(*str) || *str == '.') // 移动指针到非数字或'.'的位置
str++;
}
if(!isdigit(*str)&& *str != '.')
switch(orderBetween(oper.top(),*str)){
// 栈顶运算符优先级低,推迟运算,运算符进栈
case '<' :oper.push(*str) ; str++ ; break ;
// 优先级相等 (当前为右括号或者,‘\0’
case '=' : oper.pop() ; str++ ; break ; // 拖括号并接收下一个字符
// 优先级高于栈顶运算符,开始运算
case '>' : {
char ch = oper.top() ; // 取出栈顶运算符
oper.pop() ; // 删除栈顶运算符
if('!' == ch){ // 一元运算符
double opd1 = operd.top() ;
operd.pop() ;
operd.push(calcu(ch,opd1));//将计算结果压入数据栈中
}
else { //二元运算符
double opd2 = operd.top() ; // 第二个操作数
operd.pop() ;
double opd1 = operd.top() ; //第一个操作数
operd.pop() ;
operd.push(calcu(opd1,ch,opd2)) ; // 将计算结果压入数据栈中
}
break ;
}
}
}
return operd.top() ;
}
#endif // EVALUATE_H
<span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">主函数</span></span><span style="font-weight: bold; font-size: 14px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">:</span>
<strong>#include <iostream>
#include<string>
#include"evaluate.h"
using namespace std;
int main()
{
string s1 ;
getline(cin,s1) ;
cout<<s1<<" = "<<calculate(s1.c_str())<<endl;
return 0;
}</strong>
代码中使用了atof() 字符串转化为浮点型数字的库函数: 详情请见:
http://blog.csdn.net/tsinfeng/article/details/5844838