刚刚做过,你自己也动手改下
有四个文件
//sqstack.h
#include <iostream>
#include <sstream>
using namespace std;
const int MAXSIZE=100;
template <typename ElemType>
struct SqStack{
ElemType base[MAXSIZE];
int top;
};
template <typename ElemType>
void initStack(SqStack<ElemType>& s){
s.top=0;
}
template <typename ElemType>
void clearStack(SqStack<ElemType>& s){
s.top=0;
}
template <typename ElemType>
bool isEmpty(SqStack<ElemType>& s){
return s.top==0;
}
template <typename ElemType>
bool isFull(SqStack<ElemType>& s){
return s.top==MAXSIZE;
}
template <typename ElemType>
ElemType peek(SqStack<ElemType>& s){
if(s.top==0){
cerr<<"Stack is empty!"<<endl;
exit(1);
}
return s.base[s.top-1];
}
template <typename ElemType>
void push(SqStack<ElemType>& s,const ElemType& e){
if(s.top==MAXSIZE){
cerr<<"Stack overflow!"<<endl;
exit(1);
}
s.base[s.top]=e;
++s.top;
}
template <typename ElemType>
ElemType pop(SqStack<ElemType>& s){
if(s.top==0){
cerr<<"Stack is empty!"<<endl;
exit(1);
}
--s.top;
ElemType temp=s.base[s.top];
return temp;
}
//change.cpp
#include "sqstack.h"
int precede(char op){
switch(op){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case '@':
default:
return 0;
}
}
void change(char* s1,char* s2){
//将字符串s1中的中缀表达式转换为在于s2中的后缀表达式
SqStack<char> r;
initStack(r);//初始化栈
push(r,'@');//给栈底放入'@'字符,它具有最低优先级
int i,j;
i=0;
j=0;
char ch=s1[i];
while(ch!='@'){
if(ch==' ')
ch=s1[++i];
else if(ch=='('){
push(r,ch);
ch=s1[++i];
}
else if(ch==')'){
while(peek(r)!='(')
s2[j++]=pop(r);
pop(r);
ch=s1[++i];
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
char w=peek(r);
while(precede(w)>=precede(ch)){
s2[j++]=w;
pop(r);
w=peek(r);
}
push(r,ch);
ch=s1[++i];
}
else{
while(isdigit(ch)||ch=='.'){
s2[j++]=ch;
ch=s1[++i];
}
s2[j++]=' ';
}
}
ch=pop(r);
while(ch!='@'){
if(ch=='('){
cerr<<"expression error!"<<endl;
exit(1);
}
else{
s2[j++]=ch;
ch=pop(r);
}
}
s2[j++]='@';
s2[j++]='\0';
}
//compute.cpp
#include "sqstack.h"
double compute(char* str)
{
SqStack<double> s;
initStack(s);
istringstream ins(str); //把str定义为string流对象ins,P82
char ch; //用于输入字符
double x; //用于输入浮点数
ins>>ch;
while(ch!='@')
{
switch(ch){
case '+':
x=pop(s)+pop(s);
break;
case '-':
x=pop(s); //pop(s)弹出减数
x=pop(s)-x; //pop(s)弹出被减数
break;
case '*':
x=pop(s)*pop(s);
break;
case '/':
x=pop(s);
if(x!=0.0)
x=pop(s)/x;
else {cerr<<"Divide by 0!"<<endl;
exit(1);}
break;
default:
ins.putback(ch); //把ch重新回送到输入流中
ins>>x; //从字符串输入流中读入一个浮点数
}
push(s,x); //把读入的数或进行相应运算的结果压入到s栈中
ins>>ch;
}
if(!isEmpty(s)){
x=pop(s);
if(isEmpty(s))
return x;
else{
cerr<<"expression error!"<<endl;
exit(1);
}
}
else{
cerr<<"Stack is emppty!"<<endl;
exit(1);
}
}
//main.cpp
#include "sqstack.h"
int checkString(string strin)
{ //检查输入的表达式是否正确,包括括号是否配对
//若正确,返回值为1;否则返回0;
char st[MAXSIZE/2]; //存放括号的栈
int top=0, //栈顶
i;
for(i=0;strin[i]!='\0';i++)
{
//如果是左括号,则入栈
if(strin[i]=='(')
{ //如果是左括号,则入栈
top++;
st[top]=strin[i];
continue;
}
//小括号
if(strin[i]==')') //小括号
{
if(st[top]=='(') //配对
{
top--; //出栈
continue;
}
else return 0; //不配对,返回0
}
//运算符
if(strin[i]=='+' || strin[i]=='-' || strin[i]=='*' || strin[i]=='/')
{ //如果有连续运算符则错误
if(strin[i+1]=='+' || strin[i+1]=='-' || strin[i+1]=='*' ||strin[i+1]=='/')
return 0; //错误,返回0
else
continue; //无连续运算符情况
}
//数字、小数点、逗号
if(('0'<=strin[i] && strin[i]<='9') || strin[i]=='.'||strin[i]==',')
continue;
}
if(top!=0) //括号不匹配,返回0
return 0;
return 1;
}
int precede(char op);
void change(char* s1,char* s2);
double compute(char* str);
void main(){
cout<<"请输入表达式的个数\n(每个表达式以@结尾,表达式之间用空格隔开,最后以#号结尾):"<<endl;
char s1[1000];
cin.get(s1,1000,'#');
istringstream stri(s1);
for(int i=0;i<2;i++)
{
char ss[30],s2[30];
stri>>ss;
change(ss,s2);
cout<<ss<<endl;
if(!checkString(ss)) cout<<"表达式错误!";
double r=compute(s2);
cout<<r<<endl;
}
}
废话上面的值能用识别括号,其他的字母是一个一个的判断,你自己改下吧!
这里有更先进的代码
http://www.pudn.com/downloads11/sourcecode/others/detail44493.html是有界面,你看的懂吗!
上面的程序也不看下,就直接运行,运行后也不看看,真怀疑是学了多少C++。