|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Luker 于 2018-12-24 18:34 编辑
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define STACKINCREMENT 10 //栈的最大容量
#define addition '+'
#define division '/'
#define subtraction '-'
#define multiplication '*'
typedef char ElemType;
typedef struct //栈的结构体
{
ElemType *bottom; //栈底指针
ElemType *top; //栈顶指针
int stackSize; //栈的大小
}sqStack;
void InitStack(sqStack *s); //创建一个最大容量为STASCKINCREMENT的栈
void Push(sqStack *s,ElemType e); //入栈
void Pop(sqStack *s,ElemType *e); //出栈
int StackLen(sqStack *s); //返回栈的当前容量
void Cleanup(sqStack *s); //清空栈
int main(void)
{
sqStack s;
ElemType end,c,e;
int first,second;
printf("请以后缀表达式的形式输入你要进行的计算(以回车键作为运算结束的标识).\n");
scanf("%c",&c);
while(c!='\n') //判断输入是否为结束计算的标识
{
if(c==addition || c==division || c==subtraction ||c==multiplication) //当输入为这四个运算符中的其中一个时
{
Pop(&s,&e); //出栈并将其值赋给second
second=e-'0';
Pop(&s,&e); //出栈并将其值赋给first
first=e-'0';
if(c==addition) //加法运算
{
end=first+second+48;
}
if(c==division) //除法运算
{
end=first/second+48;
}
if(c==subtraction) //减法运算
{
end=first-second+48;
}
if(c==multiplication) //乘法运算
{
end=first*second+48;
}
Push(&s,end); //将运算的结果压入栈中
}
else
{
Push(&s,c); //压栈
}
scanf("%c",&c); //获取下一个输入
}
Pop(&s,&e); //出栈
printf("计算结果:%d",e-'0'); //显示最后的结果
Cleanup(&s); //清空栈
return 0;
}
void InitStack(sqStack *s)
{
s->bottom=(ElemType *)malloc(STACKINCREMENT*sizeof(ElemType));
if(!s->bottom) //申请失败时
{
exit(0);
}
s->top=s->bottom;
s->stackSize=STACKINCREMENT;
}
void Push(sqStack *s,ElemType e)
{
if(s->top-s->bottom>=s->stackSize) //若栈已满
{
s->bottom=(ElemType *)realloc(s->bottom,(s->stackSize+STACKINCREMENT)*sizeof(ElemType)); //增加栈的容量
if(!s->bottom) //申请失败时
{
exit(0);
}
}
*(++s->top)=e;
}
void Pop(sqStack *s,ElemType *e)
{
if(s->top==s->bottom) //栈为空时
{
exit(0);
}
*e=*s->top;
s->top--;
}
int StackLen(sqStack *s)
{
return (s->top-s->bottom);
}
void Cleanup(sqStack *s)
{
int LEN=s->stackSize;
int i;
for(i=0;i<LEN;i++)
{
free(s->bottom);
s->bottom++;
}
s->bottom=s->top=NULL;
s->stackSize=0;
}
一个通过栈实现的逆波兰计算器,调试过程跳出这个弹窗,请问是什么意思?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define STACKINCREMENT 10 //栈的最大容量
#define addition '+'
#define division '/'
#define subtraction '-'
#define multiplication '*'
typedef char ElemType;
typedef struct //栈的结构体
{
ElemType *bottom; //栈底指针
ElemType *top; //栈顶指针
int stackSize; //栈的大小
}sqStack;
void InitStack(sqStack *s); //创建一个最大容量为STASCKINCREMENT的栈
void Push(sqStack *s, ElemType e); //入栈
void Pop(sqStack *s, ElemType *e); //出栈
int StackLen(sqStack *s); //返回栈的当前容量
void Cleanup(sqStack *s); //清空栈
int main(void)
{
sqStack s;
InitStack(&s); // 这里
ElemType end, c, e;
int first, second;
printf("请以后缀表达式的形式输入你要进行的计算(以回车键作为运算结束的标识).\n");
scanf("%c", &c);
while(c != '\n') //判断输入是否为结束计算的标识
{
if(c == addition || c == division || c == subtraction || c == multiplication) //当输入为这四个运算符中的其中一个时
{
Pop(&s, &e); //出栈并将其值赋给second
second = e - '0';
Pop(&s, &e); //出栈并将其值赋给first
first = e - '0';
if(c == addition) //加法运算
{
end = first + second + 48;
}
if(c == division) //除法运算
{
end = first / second + 48;
}
if(c == subtraction) //减法运算
{
end = first - second + 48;
}
if(c == multiplication) //乘法运算
{
end = first * second + 48;
}
Push(&s, end); //将运算的结果压入栈中
}
else
{
Push(&s, c); //压栈
}
scanf("%c", &c); //获取下一个输入
}
Pop(&s, &e); //出栈
printf("计算结果:%d", e - '0'); //显示最后的结果
// 这个函数存在逻辑问题,自己好好想一想为什么
//Cleanup(&s); //清空栈
return 0;
}
void InitStack(sqStack *s)
{
s->bottom = (ElemType *)malloc(STACKINCREMENT * sizeof(ElemType));
if(!s->bottom) //申请失败时
{
exit(0);
}
s->top = s->bottom;
s->stackSize = STACKINCREMENT;
}
void Push(sqStack *s, ElemType e)
{
if(s->top - s->bottom >= s->stackSize) //若栈已满
{
s->bottom = (ElemType *)realloc(s->bottom, (s->stackSize + STACKINCREMENT) * sizeof(ElemType)); //增加栈的容量
if(!s->bottom) //申请失败时
{
exit(0);
}
}
*(++s->top) = e;
}
void Pop(sqStack *s, ElemType *e)
{
if(s->top == s->bottom) //栈为空时
{
exit(0);
}
*e = *s->top;
s->top--;
}
int StackLen(sqStack *s)
{
return (s->top - s->bottom);
}
/* 这个函数不对
void Cleanup(sqStack *s)
{
int LEN = s->stackSize;
int i;
for(i = 0; i<LEN; i++)
{
free(s->bottom);
s->bottom++;
}
s->bottom = s->top = NULL;
s->stackSize = 0;
}*/
|
-
-
|