表达式求值问题

#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10

typedef struct {  
 char *base;
 char *top;
 int stacksize;
} stack; //字符栈

int InitStack(stack *s) {    //初始化栈
 s->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
 if (!s->base) {
  exit(1);
 } else {
 s->top = s->base;
 s->stacksize = STACK_INIT_SIZE; 
 }
}

char GetTop(stack *s) {   //用e返回栈顶元素
 if (s->base == s->top) {
  printf("栈已空/n");
 } else { 
 return(*(s->top - 1));
 }
}

void Pop(stack *s, char *e) {  //返回栈顶元素,注意此处e的值需要改变,因该使用指针
 if (s->base == s->top) {
  printf("栈已空/n");
 } else {
  *e = *--s->top;
 }
}

int Push(stack *s, char e) {   //将e入栈
 if (s->top - s->base >= s->stacksize) {
  s->base = (char *)realloc(s->base, (s->stacksize + STACK_INCREMENT) * sizeof(char));
  if (!s->base) {
   exit(1);
  } else {
   s->top = s->base + s->stacksize;
   s->stacksize += STACK_INCREMENT;
  }
 } else {
 *s->top++ = e; 
 }
}

char JudgePrecede(char symbol1, char symbol2) {
//优先级判断算法 
 char pre[7][7] = {{'>', '>', '<', '<', '<', '>', '>'},  //运算符优先级设置有二维数组完成
                             {'>', '>', '<', '<', '<', '>', '>'},
                             {'>', '>', '>', '>', '<', '>', '>'},
                             {'>', '>', '>', '>', '<', '>', '>'},
                             {'<', '<', '<', '<', '<', '=', ' '},
                             {'>', '>', '>', '>', ' ', '>', '>'},
                             {'<', '<', '<', '<', '<', ' ', '='}
                           } ;
                  
 int row, line;
 
 switch (symbol1) {
  case '+':
   row = 0;
   break;
  case '-':
   row = 1;
   break;
  case '*':
   row = 2;
   break;
  case '/':
   row = 3;
   break;
  case '(':
   row = 4;
   break;
  case ')':
   row = 5;
   break;
  case '#':
   row = 6;
   break; 
  default:
   break;
 }
 
 switch (symbol2) {
  case '+':
   line = 0;
   break;
  case '-':
   line = 1;
   break;
  case '*':
   line = 2;
   break;
  case '/':
   line = 3;
   break;
  case '(':
   line = 4;
   break;
  case ')':
   line = 5;
   break;
  case '#':
   line = 6;
   break; 
  default:
   break;
 }     
 
 return(pre[row][line]);    
    
}

int IfOperator(char c) {
//判断c是否为运算符
 if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '#') {
  return(1);
 } else {
  return(0);
 } 
}

char Operation(char operand1, char operator, char operand2) {
//四则运算
 switch  (operator) {
  case '+':
   return(operand1  + operand2 - '0'); //0的ASC2为048
   break;
  case '-':
   return(operand1 - operand2 + '0');
   break;
  case '*':
   return((operand1 - '0') * (operand2 - '0') + '0');
   break;
  case '/':
   return((operand1 - '0') / (operand2 - '0') + '0');
   break;
  default:
   break;
 }
}

void EvaluateExpression(char *e) {
//算法描述,以#结尾,用e返回表达式的值
 stack OPND;  //OPTR为运算符栈,OPND为操作数栈
 stack OPTR;
 InitStack(&OPTR);
 InitStack(&OPND);
 Push(&OPTR, '#');
 
 char  operator, a, b, c;
 c = getchar();
 while (c != '#' || GetTop(&OPTR) != '#') {
  if (!IfOperator(c)) {
   Push(&OPND, c);
   c = getchar();
  } else {
   switch (JudgePrecede(GetTop(&OPTR), c)) {
    case '<':
     Push(&OPTR, c);
     c = getchar();
     break;
    case '=':
     Pop(&OPTR, &operator);
     c = getchar();
     break;
    case '>':
     Pop(&OPTR, &operator);
     Pop(&OPND, &b);
     Pop(&OPND, &a);
     Push(&OPND, Operation(a, operator, b));
     break;
    default:
     break;
   }
  }
 }
 *e = GetTop(&OPND);
 
}

main(){
 
 
 printf("输入一条表达式,以'#'结尾,回车/n");
 
 char e;
 EvaluateExpression(&e);
 printf("%d/n", e - '0');
 
 system("pause");
 
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值