表达式直接求值

本文探讨了表达式直接求值的原理和实现,通过实验代码展示了如何进行表达式的计算,并给出了运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


实验代码:

#include <stdio.h>
#include<stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAX_STACK 100
typedef int SElemType;
typedef int Status;

typedef struct{
        SElemType base[MAX_STACK];
        int top; //指示栈顶位置
}SqStack;

void InitStack(SqStack *S) {  
	S->top=-1;   
} 

void Push(SqStack *S,SElemType e){  
		if (S->top == MAX_STACK-1) 
           printf("\nStack is full"); 
         else {
		 	S->base[++S->top]=e;
		 }
           
}
void Pop(SqStack *S,SElemType *e){  
		if (S->top == MAX_STACK-1) 
           printf("\nStack is empty"); 
         else { 
		 	*e=S->base[S->top--];
		 }
}

SElemType GetTop(SqStack S) {  
         if (S.top == -1)  
           printf("\nStack is empty");
         else 
           return S.base[S.top];
}      
void printStack(SqStack S){
	printf("\n");
	while(S.top!=-1){
		printf("%d ",S.base[S.top--]);
	}
}
int StackEmpty(SqStack S){
	return S.top==-1?1:0; 
}
int In(char c){
	if(c>='0'&&c<='9')
		return 0;
}
int comp[255][255];//存储了符号优先级的数组 
int Precede(SElemType a,SElemType b){//判断谁的优先级高 
	return comp[a][b];
}
SElemType Operate(SElemType a,SElemType o,SElemType b){//对a b进行四则运算 
	SElemType answer;
	switch(o){
		case '+': answer=a+b-'0';break;
		case '-': answer=a-b+'0';break;
		case '*': answer=(a-'0')*(b-'0')+'0';break;
		case '/': answer=(a-'0')/(b-'0')+'0';break;
	}
	return answer;
}
char EvaluateExpression() {//直接对表达式求值 
	
     SqStack OPND,OPTR; 	
     SElemType c,x,theta; SElemType a,b; 
     InitStack(&OPTR); Push(&OPTR,'#');    
     InitStack(&OPND); c=getchar();
     while(c!='#'||GetTop(OPTR)!='#') {
       if (!In(c)) 
         { Push(&OPND,c);c=getchar();  } 
       else 
         switch (Precede(GetTop(OPTR),c)){ 
		  case -1: Push(&OPTR,c); c=getchar(); break; 
          case 0: Pop(&OPTR,&x);  c=getchar();break; 
          case 1: Pop(&OPTR,&theta); Pop(&OPND,&b); Pop(&OPND,&a);      
                    Push(&OPND,Operate(a,theta,b));
                    break;
        }
      }
      c=GetTop(OPND);
	  return c; 
}

int main(){
	 //-1 0 1  小 等 大 
	 comp['+']['+']=1,comp['+']['-']=1,comp['+']['*']=-1,
	 comp['+']['/']=-1,comp['+']['(']=-1,comp['+'][')']=1,
	 comp['+']['#']=1,
	 
	 comp['-']['+']=1,comp['-']['-']=1,comp['-']['*']=-1,
	 comp['-']['/']=-1,comp['-']['(']=-1,comp['-'][')']=1,
	 comp['-']['#']=1,
	 
	 comp['*']['+']=1,comp['*']['-']=1,comp['*']['*']=1,
	 comp['*']['/']=1,comp['*']['(']=-1,comp['*'][')']=1,
	 comp['*']['#']=1,
	 
	 comp['/']['+']=1,comp['/']['-']=1,comp['/']['*']=1,
	 comp['/']['/']=1,comp['/']['(']=-1,comp['/'][')']=1,
	 comp['/']['#']=1,
	 
	 comp['(']['+']=-1,comp['(']['-']=-1,comp['(']['*']=-1,
	 comp['(']['/']=-1,comp['(']['(']=-1,comp['('][')']=0,
	 comp['(']['#']=99999,
	 
	 comp[')']['+']=1,comp[')']['-']=1,comp[')']['*']=1,
	 comp[')']['/']=1,comp[')']['(']=99999,comp[')'][')']=1,
	 comp[')']['#']=1,
	 
	 comp['#']['+']=-1,comp['#']['-']=-1,comp['#']['*']=-1,
	 comp['#']['/']=-1,comp['#']['(']=-1,comp['#'][')']=999,
	 comp['#']['#']=0;
    SElemType answer=EvaluateExpression();
	printf("\nanswerc = %c\n",answer);
	return 0;
}
/*
样例输入:2*(3-2)-1*2/2+3# 
*/ 


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值