表达式求值问题

           实验内容实验内容:表达式求值问题。这里限定的表达式求值问题是:用户输入一个包含“+”、“-”、“*”、“/”、正整数和圆括号的合法数学表达式,计算该表达式的运算结果。算术表达式求值过程是:STEP 1:先将算术表达式转换成后缀表达式。 STEP 2:然后对该后缀表达式求值。实验说明:在设计相关算法中用到栈,这里采用顺序栈存储结构。 中缀表达式exp ==》后缀表达式postexp伪代码如下: 对后缀表达式postexp求值伪代码如下: 实验内容一、顺序栈的基本操作定义了两个顺序栈,一种为char类型,主要适用于将前缀表达式转换为后缀表达式,另一种为double类型,用于后缀表达式求值。其中实现了栈的一些出栈入栈的基本操作,方便实现表达式转

转化和求值。

f1bacb60a69640bab50e97129795db59.png

7a80c0a2e65a4b1997a315998371202d.png

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. #define MaxSize 100
  5. typedef char ElemType;
  6. typedef double ElemType1;
  7. typedef struct {
  8.  ElemType data[MaxSize];
  9.  int top;
  10. }SqStack;
  11. typedef struct {
  12.  ElemType1 data[MaxSize];
  13.  int top;
  14. }SqStack1;
  15. void InitSqStack(SqStack*& s) {
  16.  s = new SqStack;
  17.  s->top = -1;
  18. }
  19. bool Push(SqStack*& s, ElemType e) {
  20.  if (s->top == MaxSize - 1)
  21.   return false;
  22.  s->top++;
  23.  s->data[s->top] = e;
  24.  return true;
  25. }
  26. void Pop(SqStack*& s, ElemType& e) {
  27.  if (s->top == -1)
  28.   return;
  29.  e = s->data[s->top];
  30.  s->top--;
  31. }
  32. void InitSqStack1(SqStack1*& s) {
  33.  s = new SqStack1;
  34.  s->top = -1;
  35. }
  36. void Push1(SqStack1*& s, ElemType1 e) {
  37.  if (s->top == MaxSize - 1)
  38.   return;
  39.  s->top++;
  40.  s->data[s->top] = e;
  41. }
  42. void Pop1(SqStack1*& s, ElemType1& e) {
  43.  if (s->top == -1)
  44.   return;
  45.  e=s->data[s->top];
  46.  s->top--;
  47. }
  48. bool StackEmpty(SqStack* op) {
  49.  return op->top == -1;
  50. }
  51. bool GetTop1(SqStack1* op, ElemType1& e) {
  52.  if (op->top == -1)
  53.   return false;
  54.  e = op->data[op->top];
  55.  return true;
  56. }
  57. ElemType Precede(ElemType a, ElemType b) {//a是op栈顶运算符,b是表达式的运算符。
  58.  ElemType f=' ';//定义f存放返回的符号
  59.  switch (b) {//表达式符号
  60.  case'+':
  61.   if (a == '(' || a == '=')//>表示退栈运算符,并存放入postexp
  62.    f = '<';
  63.   else
  64.    f = '>';
  65.   break;
  66.  case'-':
  67.   if (a == '(' || a == '=')
  68.    f = '<';
  69.   else
  70.    f = '>';
  71.   break;
  72.  case'*':
  73.   if (a == '(' || a == '+' || a == '-' || a == '=')//乘除的优先级比加减高
  74.    f = '<';
  75.   else
  76.    f = '>';
  77.   break;
  78.  case'/':
  79.   if (a == '(' || a == '+' || a == '-' || a == '=')
  80.    f = '<';
  81.   else
  82.    f = '>';
  83.   break;
  84.  case'('://表示一个子表达式的开始,直接将其进栈
  85.    f = '<';
  86.   break;
  87.  case')'://表示一个表达式的结束,需要计算表达式的值,出栈并存入postexp
  88.   if (a == '(' || a == '=')
  89.    f = '=';
  90.   else
  91.    f = '>';
  92.  }
  93.  return f;
  94. }
  95. bool GetTop(SqStack* op, ElemType& e) {
  96.  if (op->top == -1)
  97.   return false;
  98.  e = op->data[op->top];
  99.  return true;
  100. }
  101. void trans(char* exp, char postexp[]) {
  102.  //将算术表达式exp转换成后缀表达式postexp
  103.  char e;
  104.  SqStack* op; //定义并初始化运算符栈op;
  105.  int i=0;//作为访问postexp的下标
  106.  InitSqStack(op);
  107.  Push(op, '=');
  108.  while (*exp != '\0') {
  109.   if (*exp >= '0' && *exp <= '9') {
  110.    //将后续的所有数字均依次存放到postexp中, 并以字符'#'标志数值串结束;
  111.    while (*exp >= '0' && *exp <= '9') {
  112.     postexp[i++] = *exp;
  113.     exp++;
  114.    }
  115.    postexp[i++] = '#';
  116.   }
  117.   else
  118.   switch (Precede(op->data[op->top], *exp)) {
  119.   case'<'://栈顶运算符优先级低
  120.    Push(op, *exp);
  121.    exp++; //将ch进栈;  从exp读取下字符ch;
  122.    break;
  123.   case'='://只有栈顶运算符为'(',ch为')'的情况
  124.    Pop(op, e);
  125.    exp++;//退栈; 从exp读取下字符ch;
  126.    break;
  127.   case'>':
  128.    //栈顶运算符应先执行,所以出栈并存放到postexp中
  129.    Pop(op, e);
  130.    postexp[i++] = e;//退栈运算符并将其存放到postexp中
  131.    break;
  132.   }
  133.  }
  134.  while (op->data[op->top]!='=') {
  135.   //若字符串exp扫描完毕,则将运算符栈op中'='之前的所有运算符依次出栈并存放到
  136.   //postexp中。最后得到后缀表达式postexp; 
  137.   Pop(op, e);
  138.   postexp[i++] = e;
  139.  }
  140.  postexp[i] = '\0';
  141. }
  142. ElemType1 compvalue(char* postexp) {
  143.  double a, b, c, d, e;
  144.  SqStack1* op;
  145.  InitSqStack1(op);
  146.  while (*postexp != '\0') {//postexp字符串未遍历完时循环
  147.   switch (*postexp) {
  148.   case'+'://若ch为“+”,则从数值栈st中退栈两个运算数,相加后进栈st中。
  149.    Pop1(op, a);
  150.    Pop1(op, b);
  151.    c = b + a;
  152.    Push1(op, c);
  153.    break;
  154.   case'-':// 若ch为“-”,则从数值栈st中退栈两个运算数,相减后进栈st中。
  155.    Pop1(op, a);
  156.    Pop1(op, b);
  157.    c = b - a;
  158.    Push1(op, c);
  159.    break;
  160.   case'*'://若ch为“*”,则从数值栈st中退栈两个运算数,相乘后进栈st中。
  161.    Pop1(op, a);
  162.    Pop1(op, b);
  163.    c = b * a;
  164.    Push1(op, c);
  165.    break;
  166.   case'/'://若ch为“/”,则从数值栈st中退栈两个运算数,相除后进栈st中
  167.    Pop1(op, a);
  168.    Pop1(op, b);
  169.    if (a != 0) {//若除数为零,则提示相应的错误信息
  170.     c = b / a;
  171.     Push1(op, c);
  172.     break;
  173.    }
  174.    else
  175.     exit(0);
  176.    break;
  177.   default:
  178.    d = 0;//若字符串postexp扫描完毕,则数值栈op中的栈顶元素就是表达式的值。
  179.    while (*postexp >= '0' && *postexp <= '9') {
  180.     d = 10 * d + *postexp - '0';
  181.     postexp++;
  182.    }
  183.    Push1(op, d);
  184.    break;
  185.   }
  186.   postexp++;
  187.  }
  188.  GetTop1(op, e);//取栈顶元素e
  189.  return e;
  190. }
  191. int main() {
  192.  char exp[MaxSize];
  193.  cin >> exp;
  194.  int a = strlen(exp);
  195.  exp[a] = '\0';
  196.  char postexp[MaxSize];
  197.  trans(exp, postexp);
  198.  cout << "中缀:" << exp << endl;
  199.  cout << "后缀:" << postexp << endl;
  200.  cout << "value:" << compvalue(postexp) << endl;
  201. }

e7325b1203644a4eb1b478cd9df3edc2.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值