自己寫的運算式求值.

來源:互聯網
上載者:User

#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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.