码迷,mamicode.com
首页 > 其他好文 > 详细

中缀表达式

时间:2017-01-28 22:09:14      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:stream   false   case   int   i++   else   strlen   bre   oid   

一个晚上+初一走人家时YY

终于想出了自己的傻逼错误

思路如下:

1.中缀转表达式树

2.dfs求值

贴上丑陋不堪的代码

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 
  6 
  7 using namespace std;
  8 
  9 //**************************************
 10 struct Node
 11 {
 12     int v;
 13     char ch;
 14     Node* lch;
 15     Node* rch;
 16     Node(){
 17         v = 0;
 18         ch = 0;
 19         lch = NULL;
 20         rch = NULL;
 21     }
 22 };
 23 
 24 Node* newnode()
 25 {
 26     return new Node();
 27 }
 28 
 29 void deletenode(Node* head)
 30 {
 31     if(head == NULL) return;
 32     if(head->lch != NULL) deletenode(head->lch );
 33     if(head->rch != NULL) deletenode(head->rch );
 34     delete head;
 35 }
 36 //********************************************
 37 const int maxn = 200000;
 38 char op[maxn];
 39 Node* root = newnode();
 40 
 41 bool Onlyint(int start,int end)
 42 {
 43     for(int i = start;i < end;i++)
 44     {
 45         if(!(op[i] >= 0&&op[i] <= 9)) return false;
 46     }
 47     return true;
 48 }
 49 
 50 void build_tree(int start,int end,Node* p)
 51 {
 52     int flag = 0,c1 = -1,c2 = -1;
 53     int v=0;
 54     if(Onlyint(start,end))
 55     {
 56         sscanf(&op[start],"%d",&v);
 57         p->v = v;
 58         return ;
 59     }
 60     for(int i = start;i < end;i++)
 61     {
 62         switch(op[i])
 63         {
 64             case (:flag++;break;
 65             case ):flag--;break;
 66             case +:if(flag == 0) c1 = i;break;
 67             case -:if(flag == 0) c1 = i;break;
 68             case *:if(flag == 0) c2 = i;break;
 69             case /:if(flag == 0) c2 = i;break;
 70         }
 71     }
 72     if(c1 < 0) c1 = c2;
 73     if(c1 < 0){
 74         build_tree(start+1,end-1,p);
 75         return ;    
 76     }else{
 77         p->ch = op[c1];
 78         Node* lch = newnode();
 79         Node* rch = newnode();
 80         p->lch = lch;
 81         p->rch = rch;
 82         build_tree(start,c1,lch);
 83         build_tree(c1+1,end,rch);
 84     }
 85 }
 86 
 87 int dfs(Node* p)
 88 {
 89     if(p->lch == NULL&&p->rch == NULL) return p->v;
 90     int val = 0;
 91     switch(p->ch)
 92     {
 93         case +:val = dfs(p->lch) + dfs(p->rch);break;
 94         case -:val = dfs(p->lch) - dfs(p->rch);break;
 95         case *:val = dfs(p->lch) * dfs(p->rch);break;
 96         case /:val = dfs(p->lch) / dfs(p->rch);break;
 97     }
 98     return val;
 99 }
100 
101 int main()
102 {
103     scanf("%s",op);
104     Node* p =newnode();
105     p = root;
106     build_tree(0,strlen(op),p);    
107     cout<<dfs(p);    
108     deletenode(p);
109     return 0;
110 }

就酱吧

中缀表达式

标签:stream   false   case   int   i++   else   strlen   bre   oid   

原文地址:http://www.cnblogs.com/RSQ-2000/p/6354511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!