自己写的表达式求值.

#include <iostream>
#include <stdio.h>
using namespace std;
char priority[7][7] = { {'>', '>', '<','<', '<', '>', '>'},
      {'>','>', '<', '<', '<', '>','>'},
      {'>','>', '>','>', '<','>','>'},
      {'>','>', '>', '>', '<', '>', '>'},
      {'<','<', '<', '<','<', '=', 'E'},
      {'>', '>', '>', '>','E', '>','>'},
      {'<','<','<','<','<','E', '='}};

 

struct Node
{
 int info;
 Node *next;
};

void push(Node **head, int info)  
{
 Node *temp = NULL;
 temp = (Node*)malloc(sizeof(Node));
// assert(temp!=NULL);
 temp->info = info;
 temp->next = *head;
 *head = temp;
}


bool pop(Node **head, int *info)
{
  if(*head !=NULL)
     {
   Node *temp;
  *info = (*head)->info ;
   temp = *head;
   *head = (*head)->next;
   free(temp);
   return true;
  }
  return false;
}

void peek(Node **head, int *info)
{

 if(*head)
  *info = (*head)->info ;


 

 

}
void makempty(Node **head)
{
 Node *temp;
 while(*head!=NULL)
 {
  temp = *head;
  *head = (*head)->next;
  free(temp);
 }
 *head = NULL;
}

bool is_empty(Node **head)
{
 return (*head) ? true : false;
}

bool is_operand(int ch)          //判断用户输入的是否是一个操作数.
{
     if(ch>=48 && ch<= 56)
  {
   return true;
  }
  return false;
}


int compute(int op, int left, int right)
{
 switch(op)
 {
 case '+':
  {
   return (left)+right;
   break;
  }
 case '-':
  {
   return left-right;
   break;
  }
 case '*':
  {
   return left*right;
   break;
  }
 case '/':
  {
   return left/right;
   break;
  }
 default:
  {
   return -1;
  }

 }
}

int is_priority(int left_o, int right_o)
{
 int row;
 int colum;
 switch(left_o)
 {
 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(right_o)
 {
 case '+':
  colum = 0;
  break;
 case '-':
  colum = 1;
  break;
 case '*':
  colum = 2;
  break;
 case '/':
  colum = 3;
  break;
 case '(':
  colum = 4;
     break;
 case ')':
  colum = 5;
  break;
 case '#':
  colum = 6;
  break;
 default:
  break;
 }
 return priority[row][colum];
}

int main()
{
 int c;

 int pri;
 int left_o;
 int a;
 int b;
 char e, d;
    struct Node *op = NULL;
 Node *operand = NULL;
 c = getchar();
 push(&op, '#');
 while(c!='#' || is_empty(&op))
 {
  if(is_operand(c))
        {
    c= c-48;
   push(&operand, c);
   c = getchar();
  }
  else
  { 
   peek(&op, &left_o);
      e = left_o;
   d = c;
   switch(pri = is_priority( left_o, c))
   {
   case '<':
    {
     push(&op, c);
     c=getchar();
     break;
    }
   case '=':
    {
     pop(&op, &left_o);   //=号出栈
     
     break;
    }
   case '>':
    {
     pop(&op, &left_o);
     pop(&operand, &a);
     pop(&operand, &b);
     push(&operand,compute( left_o, a, b));
     break;
    }
   }
  }
       
 }
 pop(&operand, &left_o);
 cout<<left_o<<endl;
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值