堆&链表&中式表达式及实现

程序摘抄自黄国瑜的数据结构

/*program name:stack_expre.c
function:input the expression and caculatte the value
data:13-8-4
author:tt
*/

#include <stdio.h>
#include <stdlib.h>
/*defines a structure type*/
struct s_node
{
	int data;
	struct s_node *next;
};

typedef struct s_node s_list;
typedef  s_list *link;
link operator=NULL;
link operand=NULL;

/*push the data in the stack*/
link push(link stack,int value)
{
	link newnode;

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

/*take the data out of the 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;
	return NULL;
}

/*check the stack whether is empty*/
int empty(link stack)
{
	if(stack==NULL)
		return 1;
	else
		return 0;
}

/*determine the input whether is operator */
int is_operator(char operator)
{
	switch (operator)
	{
		case '+': case '-': case '/': case '*':return 1;
		default :return 0;
	}
}

/*determine the priority of the operators*/
int priority(char operator)
{
	switch (operator)
	{
		case '+': case '-':return 1;
		case '/': case '*':return 2;
		default : return 0;
	}
}
/*calculation result of two numbers*/
int two_reuslt (int operator ,int operand1,int operand2)
{
	switch (operator){
		case '+': return (operand2 + operand1);
		case '-': return (operand2 - operand1);
		case '*': return (operand2 * operand1);
		case '/': return (operand2 / operand1);
	}
}

int main(void)
{
	char expression[50];
	int position =0;
	int op=0;
	int operand1=0;
	int operand2=0;
	int evaluate=0;

	printf("input the inorder expression:");
	fgets(expression,50,stdin);
	printf("%s\n",expression);

	while(expression[position]!='\0'&& expression[position]!='\n'){
		if(is_operator(expression[position])){
			if(!empty(operator)){
				while(!empty(operator)&&priority(expression[position])<=priority(operator->data)){
					operand=pop(operand,&operand1);
					operand=pop(operand,&operand2);
					operator=pop(operator,&op);
					operand=push(operand,two_reuslt(op,operand1,operand2));

				}
			}

			operator=push(operator,expression[position]);
			position++;
		}
		else{
			operand=push(operand,expression[position]-48);
			position++;
		}
	}
	while(!empty(operator)){
		operator=pop(operator,&op);
		operand=pop(operand,&operand1);
		operand=pop(operand,&operand2);
		operand=push(operand,two_reuslt(op,operand1,operand2));	
	}
	
	operand=pop(operand,&evaluate);
	printf("the expression result is '%d'",evaluate);
	return 0;

}


1.开始程序为判断语句为:

	while(priority(expression[position])<=priority(operator->data)&&empty(operator))
前面的运算符小于后边的运算符时都是正确的,大于等于时都错了。在执行2+6-4时出现段错误.在执行到处理“-”时,判断“-”和“+”的优先级,结果是相等且此时操作符栈不为空。条件满足执行2+6,此时符号的堆已经为空。而程序还会再判断符号的优先级,在执行到operator->data是就会报段错误。修改后将!empty(operator)提到前面。

2.程序开始出现的错误如下:

这两个错误是相关联的,下图显示的是相关的函数没有实现。导致在函数的引用时发生错误。error:在调用时把函数名写错了。

3.gets函数存在bug,修改成fgets()函数。

.

4.在执行时出现了非法指针。具体原因没找到。重新输了一遍程序,就好了。需要继续解决~~~


解决问题的过程能掌握很多新知识,发现问题,就想办法解决问题,就像美白不留死角~~~






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值