怎么把小甲鱼的中缀变后缀和逆波兰表达式结合
//SqStack头文件 建立了两个栈#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED
#endif // SQSTACK_H_INCLUDED
#include<iostream>
using namespace std;
#include<stdlib.h>
#include "Status.h"
#define MAXSIZE 80 //栈的储存空间初始分配量
typedef char SElemtype;
typedef double SSElemtype;
typedef struct
{
SElemtype *base; //栈底指针
SElemtype *top; //栈顶指针
int stacksize; //最大容量
}SqStack;
Status InitStack(SqStack &S) //栈的初始化
{
S.base=new SElemtype;//分配最大容量为MAXSIZE的数组空间
if(!S.base)exit(OVERFLOW); //存储分配失败
S.top=S.base; //初始为空栈
S.stacksize=MAXSIZE; //stacksize设置为栈的最大容量MAXSIZE
return OK;
}
Status Push(SqStack &S,SElemtype e) //入栈
{
if((S.top-S.base)==S.stacksize) //判断栈满
return ERROR;
else
{
*S.top=e;
++S.top; //栈顶指针加1
}
return OK;
}
Status Pop(SqStack &S,SElemtype &e)//出栈
{
if(S.top==S.base) //栈空
return ERROR;
else
{
--S.top;
e=*S.top; //栈顶指针减1
}
return OK;
}
Status StackLen(SqStack S) //求栈长
{
return(S.top-S.base);
}
int GotTop(SqStack S) //取栈顶元素
{
if(S.top!=S.base)
return *(S.top-1);
return 0;
}
void SqStackTraverse(SqStack S)//遍历
{
SElemtype *p;
p=S.base;
while(S.top!=p)
{
cout<<*p++<<" ";
}
cout<<endl;
}
//数字栈
typedef struct
{
SSElemtype *bases;
SSElemtype *tops;
int Stacksizes;
}SqStacks;
Status InitStacks(SqStacks &SS) //初始化
{
SS.bases=new SSElemtype;
if(!SS.bases) exit(OVERFLOW);
SS.tops=SS.bases;
SS.Stacksizes=MAXSIZE;
return OK;
}
Status Pushs(SqStacks &SS,SSElemtype &e) //入栈
{
if((SS.tops-SS.bases)==SS.Stacksizes)
{
return ERROR;
}
else
{
*SS.tops=e;
++SS.tops;
}
return OK;
}
Status Pops(SqStacks &SS,SSElemtype &e) //出栈
{
if(SS.bases==SS.tops)
return ERROR;
else
{
--SS.tops;
e=*SS.tops;
}
return OK;
}
Status SqStackSlen(SqStacks &SS) //求栈长
{
return(SS.tops-SS.bases);
}
//////////////////main
//中缀后缀转换
int main()
{
SqStack S;
SqStacks SS;
SElemtype e,c;
InitStack(S);
cout<<"请输入中缀表达式,以 '#' 作为结尾!";
cin>>c;
while('#'!=c)
{
while('0'<= c && '9'>= c)
{
cout<<c;
cin>>c;
if(c<'0'||c>'9')
{
cout<<" ";
}
}
if(')'==c)
{
Pop(S,e);
while('('!=e)
{
cout<<e<<" ";
Pop(S,e);
}
}
else if('+'==c||'-'==c)
{
if(!StackLen(S))
{
Push(S,c);
}
else
{
do
{
Pop(S,e);
if('('==e)
{
Push(S,e);
}
else
{
cout<<e<<" ";
}
}while(StackLen(S) && '('!=e);
Push(S,c);
}
}
else if( '*'==c || '/'==c )
{
if(StackLen(S))
{
Pop(S,e);
while('('!=e&&'+'!=e&&'-'!=e)
{
cout<<e<<" ";
Pop(S,e);
}
Push(S,e);
}
Push(S,c);
}
else if( '*'==c || '/'==c )
{
if(StackLen(S))
{
Pop(S,e);
while('('!=e&&'+'!=e&&'-'!=e)
{
cout<<e<<" ";
Pop(S,e);
}
Push(S,e);
}
Push(S,c);
}
else if('('==c)
{
Push(S,c);
}
else if('#'==c)
{
break;
}
else
{
cout<<"\n输入格式错误!\n";
return INFEASIBLE;
}
cin>>c;
}
while(StackLen(S))
{
Pop(S,e);
cout<<e<<" ";
}
return 0;
}
//逆波兰
int main()
{
SqStacks SS;
char c;
double d,f,t;
char str; //最大缓冲区
int i=0;
InitStacks(SS);
cout<<"请输入后缀表达式:"<<endl;
cin>>c;
while('#'!= c)
{
while(isdigit(c) || '.'==c) //过滤数字
{
str=c;
str='\0';
if(i>=10)
{
cout<<"输入单个数据过大!";
return ERROR;
}
c=cin.get();
if(c==' ')
{
d=atof(str);
Pushs(SS,d);
i=0;
break;
}
}
switch(c)
{
case '+':
Pops(SS,f);
Pops(SS,d);
t=d+f;
Pushs(SS,t);
break;
case '-':
Pops(SS,f);
Pops(SS,d);
t=d-f;
Pushs(SS,t);
break;
case '*':
Pops(SS,f);
Pops(SS,d);
t=d*f;
Pushs(SS,t);
break;
case '/':
Pops(SS,f);
Pops(SS,d);
if(f!=0)
{
t=d/f;
Pushs(SS,t);
}
else
{
cout<<"\n输入错误!除数为零!"<<endl;
return ERROR;
}
}
cin>>c;
}
Pops(SS,d);
cout<<"最终的计算结果为"<<d<<endl;
return 0;
}
//Status头文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
怎么把小甲鱼的中缀变后缀和逆波兰表达式结合?——写一个逆波兰计算器就好了~视频好像有说过...
页:
[1]