算数表达式栈

算术表达式(可以算多位数)

#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1//申请空间失败
#include<conio.h>
#include<dos.h> 
typedef int ElemType;
typedef int Status; 
typedef struct Stack_Node{
	ElemType data;
	struct Stack_Node *next;
}Stack_Node;
typedef struct {
	Stack_Node *top;
	Stack_Node *bottom;
	int length;
}StackList;
StackList Establish()//建栈 
{
	StackList S;
	 S.top=(Stack_Node*)malloc(sizeof(Stack_Node));
	 
	if(!S.top)
	{
		printf("Failed to open up space!\n");
		exit(1);//建站失败 
	}
	S.bottom=S.top;
	S.bottom->next=NULL;
	return S; 
} 
void Push(StackList* S,ElemType e)
{
	
	Stack_Node *newnode=(Stack_Node*)malloc(sizeof(Stack_Node));
	if(!newnode)
	{
		printf("Failed to open up space!");
		exit(1);
	}
	S->top->data=e;
	newnode->next=S->top;
	S->top=newnode;
	 
}
ElemType Pop(StackList* S)//出栈 未判断是否为空 
{
	   Stack_Node *p;//暂时指向top 
     	ElemType e;
     	p=S->top->next;
     	e=p->data;
	    S->top->next=p->next;
		 free(p);
		return e;
	
}
ElemType GetTop(StackList* S)//出栈 未判断是否为空 
{
	   Stack_Node *p;//暂时指向top 
     	ElemType e;
     	p=S->top->next;
     	e=p->data;
		return e;
	
}
Status Precede(ElemType e1,ElemType e2)
{
	int optr[7][7]={//1表示大于,2表示<,-1表示= 
		1, 1, 2, 2, 2, 1, 1,
		1, 1, 2, 2, 2, 1, 1,
		1, 1, 1, 1, 2, 1, 1,
		1, 1, 1, 1, 2, 1, 1,
		2, 2, 2, 2, 2, 0, -1,
		1, 1, 1, 1, -1, 1, 1,
		2, 2, 2, 2, 2, -1, 0 
	};
	//printf("ee%d\n",optr[1][1]);
	int op[7]={'+','-','*','/','(',')','#'};//用于查找运算符的序号
	int i=0;  
	while(op[i]!=e1)
	{
		i++;
	}
	 
	int j=0;  
	while(op[j]!=e2)
	{
		j++;
	}
//	printf("##%d %d %d\n",i,j,optr[i][j]);
	return optr[i][j];
}
/*
Status In(ElemType e)
{
	if()
	{
		return 0;
	}
	else{
		return 1;
	}
}*/
ElemType Operate(ElemType a,char theta,ElemType b)
{
	if(theta=='+')
	{
		return a+b;
	}
	else if(theta=='-')
	{
		return a-b;
	}
	else if(theta=='*')
	{
		return a*b;
	}
	else if(theta=='/')
	{
		return a/b;
	}
}
int Getin(int *f,char ch[],int *k)
{
	int p=0;
	
	if(ch[*k]>='0'&&ch[*k]<='9')
    {
	   p=p*10+ch[*k]-'0';
	  *k=*k+1;
        while(ch[*k]>='0'&&ch[*k]<='9')
	  {
	      //printf(" a  %c\n",a);
	      p=p*10+ch[*k]-'0';
	      *k=*k+1;
	  }
	  *f=0;
	 // *k=*k-1;//再退回去 
	   	
	}
	else{
		*f=1;
		p=(int)ch[*k]; 
		*k=*k+1;
	}
	   //printf("%d",p);
	   return p;
}
int  EvaluateExpression(char* ch)
{
		ElemType c;
		int b, a;
		int flag=0; 
		char theta;
		int k=0;//位置 
	   StackList S1;//操作符栈
	   StackList S2;//操作数栈
    	S1= Establish();
	  S2= Establish();
	   //c='#'
	  Push(&S1,'#');
	  c=Getin(&flag,ch,&k);
	  
  while(c!='#'||GetTop(&S1)!='#')
  {
			if(flag==0) //如果是运算数 
			{
				Push(&S2,c);
				c=Getin(&flag,ch,&k);
			}
			else{
				//printf("%c %c\n",c,GetTop(&S1));
			//	printf("%d\n",Precede(GetTop(&S1),c));
				switch(Precede(GetTop(&S1),c))
				{
					case 2:
						Push(&S1,c);
						c=Getin(&flag,ch,&k);
						break;
				    case 0:
				    		c=Pop(&S1);
				    		c=Getin(&flag,ch,&k);
				    		break;
				   case 1:
				   theta=Pop(&S1);
				   	a=Pop(&S2);
				    b=Pop(&S2);
					Push(&S2,Operate(b,theta,a));
					break;	
				}
			}
  }
	
  return GetTop(&S2);	
 } 
 int main()
 {
 	int value;
 	char ch[1000];
 	printf("请输入算数式(最后请以“#”结束)\n");
	 scanf("%s",ch);
 	value=EvaluateExpression(ch);
 	printf("%d\n",value);
 	return 0;
 }

只能输入个位数


#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1//申请空间失败
#include<conio.h>
#include<dos.h> 
typedef int ElemType;
typedef int Status; 
typedef struct Stack_Node{
	ElemType data;
	struct Stack_Node *next;
}Stack_Node;
typedef struct {
	Stack_Node *top;
	Stack_Node *bottom;
	int length;
}StackList;
StackList Establish()//建栈 
{
	StackList S;
	 S.top=(Stack_Node*)malloc(sizeof(Stack_Node));
	 
	if(!S.top)
	{
		printf("Failed to open up space!\n");
		exit(1);//建站失败 
	}
	S.bottom=S.top;
	S.bottom->next=NULL;
	return S; 
} 
void Push(StackList* S,ElemType e)
{
	
	Stack_Node *newnode=(Stack_Node*)malloc(sizeof(Stack_Node));
	if(!newnode)
	{
		printf("Failed to open up space!");
		exit(1);
	}
	S->top->data=e;
	newnode->next=S->top;
	S->top=newnode;
	 
}
ElemType Pop(StackList* S)//出栈 未判断是否为空 
{
	   Stack_Node *p;//暂时指向top 
     	ElemType e;
     	p=S->top->next;
     	e=p->data;
	    S->top->next=p->next;
		 free(p);
		return e;
	
}
ElemType GetTop(StackList* S)//出栈 未判断是否为空 
{
	   Stack_Node *p;//暂时指向top 
     	ElemType e;
     	p=S->top->next;
     	e=p->data;
		return e;
	
}
Status Precede(ElemType e1,ElemType e2)
{
	int optr[7][7]={//1表示大于,2表示<,-1表示= 
		1, 1, 2, 2, 2, 1, 1,
		1, 1, 2, 2, 2, 1, 1,
		1, 1, 1, 1, 2, 1, 1,
		1, 1, 1, 1, 2, 1, 1,
		2, 2, 2, 2, 2, 0, -1,
		1, 1, 1, 1, -1, 1, 1,
		2, 2, 2, 2, 2, -1, 0 
	};
	//printf("ee%d\n",optr[1][1]);
	int op[7]={'+','-','*','/','(',')','#'};//用于查找运算符的序号
	int i=0;  
	while(op[i]!=e1)
	{
		i++;
	}
	 
	int j=0;  
	while(op[j]!=e2)
	{
		j++;
	}
//	printf("##%d %d %d\n",i,j,optr[i][j]);
	return optr[i][j];
}
Status In(ElemType e)
{
	if(e>'0'&&e<'9')
	{
		return 0;
	}
	else{
		return 1;
	}
}
ElemType Operate(ElemType a,char theta,ElemType b)
{
	if(theta=='+')
	{
		return a+b;
	}
	else if(theta=='-')
	{
		return a-b;
	}
	else if(theta=='*')
	{
		return a*b;
	}
	else if(theta=='/')
	{
		return a/b;
	}
}
int  EvaluateExpression()
{
		ElemType c;
		int b, a;
		char theta;
	   StackList S1;//操作符栈
	   StackList S2;//操作数栈
    	S1= Establish();
	  S2= Establish();
	   //c='#'
	  Push(&S1,'#');
	  	  c=getchar();
	  
  while(c!='#'||GetTop(&S1)!='#')
  {
   
  	if(!(c=='#'||c=='('||c==')'||c=='+'||c=='-'||c=='*'||c=='/'||c>='0'&&c<='9')){
		printf("你的输入不符合计算器规则!");
		exit(1);
		}
			if(!In(c)) 
			{
				Push(&S2,c-'0');
				c=getchar();
			}
			else{
				//printf("%c %c\n",c,GetTop(&S1));
			//	printf("%d\n",Precede(GetTop(&S1),c));
				switch(Precede(GetTop(&S1),c))
				{
					case 2:
						Push(&S1,c);
						c=getchar();
						break;
				    case 0:
				    		c=Pop(&S1);
				    		c=getchar();
				    		break;
				   case 1:
				   theta=Pop(&S1);
				   	a=Pop(&S2);
				    b=Pop(&S2);
					Push(&S2,Operate(b,theta,a));
					break;	
				}
			}
  }
	
  return GetTop(&S2);	
 } 
 int main()
 {
 	int value;
 	printf("请输入算数式(最后请以“#”结束)\n");
 	value=EvaluateExpression();
 	printf("%d\n",value);
 	return 0;
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值