下面是代码,还请指正。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef char element;
typedef int num;
#define error -1
struct snode{
element *data;
int top;
int maxsize; //堆栈最大容量
};
struct tnode{
num *data;
int top;
int maxsize;
};
typedef struct snode *sqstack;
typedef sqstack stackchar;
typedef struct tnode *sqstack1;
typedef sqstack1 stacknum;
char *s_gets(char *st,int n);
//首先初始化,然后满,空检查,然后装,出,读栈顶
stackchar initialize_char(int maxsize)
{
stackchar s = (stackchar)malloc(sizeof(struct snode));
s->data = (element *)malloc(maxsize * sizeof(element));
s->top = -1;
s->maxsize = maxsize;
return s;
}
stacknum initialize_num(int maxsize)
{
stacknum s = (stacknum)malloc(sizeof(struct tnode));
s->data = (num *)malloc(maxsize * sizeof(num));
s->top = -1;
s->maxsize = maxsize;
return s;
}
bool stackempty(sqstack s)
{
if(s->top == -1)
return true;
else
return false;
}
bool pushchar(stackchar s,element x)
{
if(s->top == s->maxsize - 1)
{
printf("堆栈满pushchar");
return false;
}
s->data[++s->top] = x;
return true;
}
element popchar(stackchar s)
{
if(stackempty(s))
{
printf("堆栈空popchar");
return error;
}
return (s->data[(s->top)--]);
}
element gettopchar(stackchar s)
{
if(stackempty(s))
{
printf("栈空gettopchar");
return error;
}
return (s->data[s->top]);
}
bool pushnum(stacknum p,num e)
{
if(p->top == p->maxsize - 1);
{
printf("栈满!pushnum");
return false;
}
p->data[++p->top] = e;
return true;
}
num popnum(stacknum p)
{
if(p->top == -1)
{
printf("栈空popnum");
return error;
}
else
return p->data[(p->top)--];
}
num gettopnum(stacknum p)
{
if(p->top == -1)
{
printf("栈空gettopnum");
return error;
}
return p->data[p->top];
}
void fun(stacknum p,char e)
{
num temp1,temp2;
temp1 = popnum(p);
temp2 = popnum(p);
switch(e)
{
case '+':
pushnum(p,temp1 + temp2);
break;
case '-':
pushnum(p,temp1 - temp2);
break;
case '*':
pushnum(p,temp1 * temp2);
break;
case '/':
if(temp2 != 0.0)
pushnum(p,(double)temp1 / temp2);
else
{
printf("错误:除法分母位0;\n");
}
break;
default:
printf("符号输入错误!");
}
}
char readstack1(stackchar s)
{
while(s->top != -1)
printf("%c",s->data[s->top--]);
}
void readstack2(stacknum s)
{
while(s->top != -1)
printf("%d",s->data[s->top--]);
}
int main()
{
int t;
int i;//循环变量
printf("请输入maxsize:");
scanf("%d",&t);
stackchar s = initialize_char(t);
stacknum q = initialize_num(t);
char infix[t];
int temp;
char oper;//临时算子
fflush(stdin);
//gets(infix);
/*for(int i = 0;i < t;i++)
printf("%c",infix[i]);*/ //检查infix问题,发现infix无法读入字符
//开始遍历
for(;;)
{
printf("请输入中缀表达式:");
//s_gets(infix,t);
gets(infix);
for(i = 0;infix[i] != '\0';i++)
{
if(infix[i] >= '0' && infix[i] <= '9')
{
temp = infix[i] - '0';//char转int
while(infix[i+1] != '\0')
{
if(infix[i+1] >= '0' && infix[i+1] <= '9')
{
temp = temp * 10 + infix[i+1] - '0';//多位数字转换
i++;
}
else
break;
}
pushnum(q,temp);//数字压栈?这里一直报错
}
else if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/')
{
switch(infix[i])
{
case '+':
if(s->data[s->top - 1] != '+' && s->data[s->top - 1] != '-' && s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')//检查前面的算子与这个算子优先度
pushchar(s,'+');
else
{
while(s->top > -1 && s->data[s->top - 1] != '(')//检查括号
{
oper = popchar(s);
fun(q,oper);
}
pushchar(s,'+');
};
break;
case '-':
if(s->data[s->top - 1] != '+' && s->data[s->top - 1] != '-' && s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')
pushchar(s,'-');
else
{
while(s->top > -1 && s->data[s->top - 1] != '(')
{
oper = popchar(s);
fun(q,oper);
}
pushchar(s,'-');
};
break;
case '*':
if(s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')
pushchar(s,'*');
else
{
while(s->top > -1 && s->data[s->top - 1] != '(')
{
oper = popchar(s);
fun(q,oper);
}
pushchar(s,'*');
};
break;
case '/':
if(s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')
pushchar(s,'/');
else
{
while(s->top > -1 && s->data[s->top - 1] != '(')
{
oper = popchar(s);
fun(q,oper);
}
pushchar(s,'/');
};
break;
case '(':
pushchar(s,'(');
break;
case ')'://当出栈信号
while(s->data[s->top - 1] != '(')
{
oper = popchar(s);
fun(q,oper);
}
oper = popchar(s);//如果不行,直接出),不管
default:
printf("error!");
}
}
}
while(s->top > -1)
{
oper = popchar(s);
fun(q,oper);
}
printf("\t\t%s = %.2f",infix,gettopnum(q));
printf("\n");
system("pause");
}
}
//处理输入
char *s_gets(char *st,int n)
{
char *ret_val;
char *find;
ret_val = fgets(st,n,stdin);
if(ret_val)
{
find = strchr(st,'\n');
if(find)
*find = '\0';
else
while(getchar() != '\n')
continue;
}
return ret_val;
}
Q:
1.哪怕输入字符,比如2+5,但infix没有字符,只有2与5,改成gets(),问题没有解决
2.栈的top,maxsize没有问题,但是pushnum一直报错,式子也没有结果,初步怀疑是data里面的问题,但是不知道怎么解决(这里在初始函数加入pushnum,popnum之类的是为了辨别)
简介:
在了解并尝试写出中缀表达式转后缀表达式并求值的程序之时,我看到了下面这位 超级小白龙 的代码,试图将栈转化成给定容量分配的栈,并且允许用户输入一个生成栈的最大值,然而却遇到了诸多问题,弄了一晚不知道如何改善。
后记:
解决了
1.top指针的问题,不能减少
2.分号问题
3.一些表述问题,比如分母为0的情况再处理