#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”。
请问高手我的这个程序问题出现在哪里啊??急急急!!