用栈编写算术表达式,符号不能入栈,求调试程序
#include <iostream>#include<stdio.h>
using namespace std;
#include<stdlib.h>
#define MAX 100
typedef struct Stack //存放符号栈
{
char date[MAX];
int top;
}SeqStack;
void SeqStackInit(SeqStack *s)//初始化
{
s->top=0;
}
void SeqStackPush(SeqStack *s,char x)//入栈
{
if (s->top==MAX)
{
printf("栈满\n");
}
s->date[s->top]=x;
s->top++;
}
char SeqStackPop(SeqStack *s)//出栈
{
char x;
if(s->top==0)
printf("栈空\n");
s->top--;
x=s->date[s->top];
return x;
}
char SeqStackGetTop(SeqStack *s)//栈顶元素
{
if(s->top!=0)
return (s->date[s->top-1]);
}
typedef struct fstack /*存放数字栈*/
{
double date[MAX];
int top;
}fstack;
void fStackInit(fstack *s)//初始化
{
s->top=0;
}
void fStackPush(fstack *s ,int x)//入栈
{
s->date[s->top]=x;
s->top++;
}
int fStackPop(fstack *s)//出栈
{
int x;
s->top--;
x=s->date[s->top];
return x;
}
int fStackGetTop(fstack *s)//栈顶元素
{
if(s->top!=0)
return (s->date[s->top-1]);
}
int In(char c) /*问题2:解决In函数问题:判断C是否为运算符是返回1否则返回0*/
{
char ch[7]={'+','-','*','/','#','(',')'};
int i;
for(i = 0; i < 7; i++)
if(c == ch[i])
return 1;
return 0;
}
char Proceed(char op,char c) /*op为栈顶元素,c为当前读入的运算符,比较二者的优先级*/
{ /*问题1:解决Proceed函数*/
char h;
if(op=='(' && c==')' || op=='#' && c=='#' )
h = '=';
else
if(op=='+' || op=='-') /*栈顶元素为‘+’或‘-’的时候*/
switch(c)
{
case '+':
case '-':
case ')':
case '#': h = '>'; break;
case '*':
case '/':
case '(': h = '<';
}
else
if(op=='*' || op=='/') /*栈顶元素为‘*’或‘/’的时候*/
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case ')':
case '#': h = '>'; break;
case '(': h = '<';
}
else
if(op=='(') /*栈顶元素为‘(’的时候*/
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '(': h = '<'; break;
case '#': printf("Error!\n"); exit(0);
}
else
if(op==')') /*栈顶元素为‘)’的时候*/
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '#': h = '>'; break;
case '(': printf("Error!\n"); exit(0);
}
else
if(op=='#') /*栈顶元素为‘#’的时候*/
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '(': h = '<'; break;
case ')': printf("Error!\n"); exit(0);
}
return h;
}
/* 问题3:解决Operate函数*/
int Operate(int a,char r,int b) /*返回由aRb的值 */
{
int s;
int d1 = a;
int d2 = b; /*把字符ab变为对应数字*/
switch(r)
{
case '+': s = d1+d2; break;
case '-': s = d2-d1; break;
case '*': s = d1*d2; break;
case '/': s = d2/d1;
}
return (s+'0');
}
int CalcuExp()
{
char theta;
char a[80];
SeqStack optr;
int c,b;
int i,s;i=0;s=0;
SeqStackInit(&optr);
SeqStackPush(&optr,'#');
fstack opnd;
fStackInit(&opnd);//数字初始化
printf(" 请输入表达式并以‘#’结束:\n");
gets(a);
while(a[i]!='#' || SeqStackGetTop(&optr)!='#'){
if(!In(a[i])){
if(a[i] >= '0' && a[i] <= '9')
{
s=s+a[i]-'0';
while (!In(a[++i]))
{
s=s*10;
s=s+a[i]-'0';
}
fStackPush(&opnd,s);
s=0;}
}
else{
switch(Proceed(SeqStackGetTop(&optr),a[+i]))//比较
{
case '<':SeqStackPush(&optr,a[i]);
i++;
break;
case'=':SeqStackPop(&optr);
i++;
break;
case'>':theta=SeqStackPop(&optr);
b=fStackPop(&opnd);
c=fStackPop(&opnd);
fStackPush(&opnd,Operate(c,theta,b));
break;
}
}
return (fStackGetTop(&opnd));
}
}
int main()
{int v;
v = CalcuExp();
printf("The result is:%d\n",v);
return 0;
}