永发信息网

有关堆栈问题

答案:2  悬赏:80  手机版
解决时间 2021-04-23 00:50
  • 提问者网友:抽煙菂渘情少年
  • 2021-04-22 06:04

#include "stdio.h"
#include "stdlib.h"

struct s_node
{
int data;
struct s_node * next;
};
typedef struct s_node s_list;
typedef s_list * link;
link operator1 = NULL;
link operand = NULL;

link push(link stack, int value); //存入堆栈数据
link pop(link stack, int *value); //从堆栈中取出数据
int empty(link stack); //检查堆栈是否为空
int is_operator(char operator2); //判断是否为运算符
int priority(char operator2); //判断运算符的优先权
int two_result(int operator2, int operand1, int operand2); //计算任两个操作数的值

//主程序:输入中序表示式后计算出表达式的结果值

int main()
{
char expression[50];
int position = 0; //表达式位置
int op = 0; //运算符
int operand1 = 0; //前操作数
int operand2 = 0; //后操作数
int evaluate = 0; //运算结果

printf("\nPlease input the inorder expression : ");
gets(expression);

while(expression[position] != '\0' && expression[position] != '\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator1))
{
while(priority(expression[position]) <= priority(operator1->data) && !empty(operator1))
{
operand = pop(operand, &operand1);
operand = pop(operand, &operand2);
operator1 = pop(operator1, &op);
operand = push(operand, two_result(op, operand1, operand2));
}
}
operator1 = push(operator1, expression[position]);
}
else
operand = push(operand, expression[position] - 48);
position++;
}

while(!empty(operator1))
{
operator1 = pop(operator1, &op);
operand = pop(operand, &operand1);
operand = pop(operand, &operand2);
operand = push(operand, two_result(op, operand1, operand2));
}
operand = pop(operand, &evaluate);
printf("The expression [ %s ] result is ' %d ' ",expression,evaluate);

return 0;
}

//存入堆栈数据

link push(link stack, int value)
{
link newnode;

newnode = (link)malloc(sizeof(s_list));
if(newnode == NULL)
{
printf("\nMemory allocation failure!!");
return NULL;
}
newnode->data = value;
newnode->next = stack;
stack = newnode;

return stack;
}

//从堆栈中取出数据

link pop(link stack, int *value)
{
link top;

if(stack != NULL)
{
top = stack;
stack = stack->next;
*value = top->data;
free(top);
return stack;
}
else
*value = -1;
}

//检查堆栈是否为空

int empty(link stack)
{
if(stack == NULL)
return 1;
else
return 0;
}

//判断是否为运算符

int is_operator(char operator2)
{
switch(operator2)
{
case '+': case '-': case '*': case '/': return 1;
default: return 0;
}
}

//判断运算符的优先权

int priority(char operator2)
{
switch(operator2)
{
case '+': case '-': return 1;
case '*': case '/': return 2;
default: return 0;
}
}

//计算任两个操作数的值

int two_result(int operator2, int operand1, int operand2)
{
switch(operator2)
{
case '+': return (operand2 + operand1);
case '-': return (operand2 - operand1);
case '*': return (operand2 * operand1);
case '/': return (operand2 / operand1);
}
}

验证数据:8*9-4*2
我编译链接运行没问题 可是当我输完验证数据 8*9-4*2 按回车后 出现了"0x0040e482"指令引用的"0x00000000"内存。该内存不能为"read”。

请问高手我的这个程序问题出现在哪里啊??急急急!!

最佳答案
  • 五星知识达人网友:举杯邀酒敬孤独
  • 2021-04-22 07:06

&&先后顺序的问题


while(expression[position] != '\0' && expression[position] != '\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator1))
{
while( !empty(operator1) && priority(expression[position]) <= priority(operator1->data)) //改成这样
{
operand = pop(operand, &operand1);
operand = pop(operand, &operand2);
operator1 = pop(operator1, &op);
operand = push(operand, two_result(op, operand1, operand2));
}
}
operator1 = push(operator1, expression[position]);
}
else
operand = push(operand, expression[position] - 48);
position++;
}

全部回答
  • 1楼网友:几近狂妄
  • 2021-04-22 07:20
程序这么长,你还是先调试运行,把发生错误的那行找出来,缩小怀疑范围,这样别人就容易分析出错的原因。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯