输入一个值后报错显示位置冲突,我想了想可能是字符数组那里出现了问题,但不知道怎么解决。小伙伴们帮忙看看!!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_SIZE 1000
typedef struct my_stack {
int* base; //指向栈底
int* top;//指向栈顶
int stacksize;
}Stack;
void init(Stack& st) /// 初始化栈st
{
st.base = (int*)malloc(sizeof(int) * MAX_SIZE);
if (!st.base) {
exit(0);
}
st.top = st.base;
st.stacksize = MAX_SIZE;
}
bool full(Stack& st) /// 判断是否栈满
{
if (st.top-st.base>=st.stacksize) {
return 1;
}
else {
return 0;
}
}
bool empty(Stack& st) /// 判断是否栈空
{
if (st.base == st.top) {
return 1;
}
else {
return 0;
}
}
void push(Stack& st, int e) /// e进栈
{
*st.top = e;
st.top++;
}
int pop(Stack& st, int& e) /// 出栈,由参数e带回
{
st.top--;
return *st.top;
}
int main()
{
Stack s;
init(s);
char str[MAX_SIZE];
int d, e;
scanf_s("%s", str);
while (strcmp(str,"=")!=0)
{
if (strcmp(str, "/") == 0) {
pop(s, e);
pop(s, d);
push(s, e / d);
}
else if (strcmp(str, "%") == 0) {
pop(s, e);
pop(s, d);
push(s, e %d);
}
else if (strcmp(str, "+") == 0) {
pop(s, e);
printf("%d\n", e);
pop(s, d);
printf("%d\n", d);
push(s, e + d);
}
else if (strcmp(str, "-") == 0)
{
pop(s, e);
pop(s, d);
push(s, e - d);
}
else if (strcmp(str, "*") == 0) {
pop(s, e);
pop(s, d);
push(s, e * d);
}
else {
int a = atoi(str);
push(s, a);
printf("%d\n", a);
}
scanf_s("%s", str);
}
pop(s, d);
printf("%d", *s.top);
return 0;
}