1 #include <iostream>
  2 #include <stack>
  3 #include <cmath>
  4 #include <sstream>
  5 using namespace std;
  6 bool isoptr(char ch) {
  7  if(ch == '+' || ch == '-' || ch == '*' || ch ==  '/' || ch == '^' || ch == 's' || ch == 'c' || ch == 'l' || ch == 'n') return true;
  8  else return false;
  9 }
 10 bool isopnd(string s) {
 11  stringstream temp;
 12  double num;
 13  char ch;
 14  temp << s;
 15  if(!(temp >> num)) return false; // 若无法转换,则字符串不是数据 
 16  else return true;
 17 } 
 18 double convert(string s) {
 19  double sum = 0;
 20  stringstream temp;
 21  temp << s;
 22  temp >> sum;
 23  return sum;
 24 }
 25 string convert_str(double num) {
 26  stringstream temp;
 27  string str;
 28  temp << num;
 29  temp >> str;
 30  return str;
 31 }
 32 int optrpri(string s) 
 33 {
 34  switch(s[0]) {
 35   case '+' : return 1;
 36   case '-' : return 1;
 37   case '*' : return 2;
 38   case '/' : return 2;
 39   case '^' : return 3;
 40   case 's' : return 4;
 41   case 'c' : return 4;
 42   case 'l' : return 4;
 43  }
 44  
 45 }
 46 struct Bit {
 47  string data;
 48  Bit* left = NULL;
 49  Bit* right = NULL;
 50 };
 51 Bit* ReadExpr(string exp) {
 52  stack <Bit*> datast;
 53  int cnt = exp.length()-1;
 54  while(cnt >= 0) {
 55   if(exp[cnt] == ' ') cnt--;
 56   else if(!isoptr(exp[cnt])) {
 57    int i = cnt;
 58    string temp;
 59    while(!isoptr(exp[i]) && i >= 0 && exp[i] != ' ') {
 60     temp = exp[i] + temp;
 61     i--;
 62    }
 63    if(exp[i] == '-') {
 64     temp = exp[i] + temp;
 65     cnt = i-1;
 66    } else cnt = i;
 67    Bit* p = new Bit;
 68    p->data = temp;
 69    datast.push(p);
 70   } else if(exp[cnt] == 'n' || exp[cnt] == 's') {
 71    if(exp[cnt-1] == 'l' && exp[cnt] == 'n') {
 72     Bit* p = new Bit;
 73     p->data = "ln";
 74     if(!datast.empty()) {
 75      p->right = datast.top();
 76      datast.pop();
 77     }
 78     datast.push(p);
 79     cnt -= 2; 
 80    } else if(exp[cnt-2] == 's' && exp[cnt-1] == 'i' && exp[cnt] == 'n') {
 81     Bit* p = new Bit;
 82     p->data = "sin";
 83     if(!datast.empty()) {
 84      p->right = datast.top();
 85      datast.pop();
 86     }
 87     datast.push(p);
 88     cnt -= 3; 
 89    } else if(exp[cnt-2] == 'c' && exp[cnt-1] == 'o' && exp[cnt] == 's') {
 90     Bit* p = new Bit;
 91     p->data = "cos";
 92     if(!datast.empty()) {
 93      p->right = datast.top();
 94      datast.pop();
 95     }
 96     datast.push(p);
 97     cnt -= 3; 
 98    }
 99   } else {
100    Bit* p = new Bit;
101    p->data = exp[cnt];
102    if(!datast.empty()) {
103     p->left = datast.top();
104     datast.pop();
105    }
106    if(!datast.empty()) {
107     p->right = datast.top();
108     datast.pop();
109    } 
110    datast.push(p); 
111    cnt--;
112   }
113  }
114  Bit* root = new Bit;
115  root = datast.top();
116  datast.pop();
117  return root;
118 };
119 void WriteExpr(Bit* root) { // 中序遍历 
120  if(root) {
121   if(root->left) {
122    if((!isopnd(root->left->data) && (optrpri(root->data) > optrpri(root->left->data))) || root->left->data[0] == '^') {
123     cout << " (";
124     WriteExpr(root->left);
125     cout << " )";
126    } else WriteExpr(root->left);
127   }
128   
129   cout << " " << root->data;
130   
131   if(root->right) {
132    if((!isopnd(root->right->data) && (optrpri(root->data) >= optrpri(root->right->data))) || root->right->data[0] == '^') {
133     cout << " (";
134     WriteExpr(root->right);
135     cout  << " )";
136    } else WriteExpr(root->right);
137   }
138  }
139 } 
140 void Assign(Bit* root, char V, int c = 0) {
141  if(root) {
142   char ch = root->data[0];
143   if(ch == V) {
144    if(c >= 0) {
145     char temp = c + '0';
146     root->data = temp;
147    } else {
148     char temp = (0-c) + '0';
149     string str = "-" + temp;
150     root->data = str; 
151    }
152   }
153   Assign(root->left, V, c);
154   Assign(root->right, V, c);
155  } 
156 }
157 double Value(Bit* root){
158  if(root) {
159   char ch = root->data[0];
160   if(!root->left && !root->right && isopnd(root->data)) return convert(root->data);
161   else {
162    switch (ch) {
163     case '+' : return Value(root->left) + Value(root->right);
164     case '-' : return Value(root->left) - Value(root->right);
165     case '*' : return Value(root->left) * Value(root->right);
166     case '/' : return Value(root->left) / Value(root->right);
167     case '^' : return pow(Value(root->left), Value(root->right));
168     case 's' : return sin(Value(root->right));
169     case 'c' : return cos(Value(root->right));
170     case 'l' : return log(Value(root->right));
171    }
172   } 
173  }
174 }
175 Bit* CompoundExpr(char optr, Bit* root1, Bit* root2){
176  if(root1 && root2) {
177   Bit* root = new Bit;
178   root->data = optr;
179   root->left = root1;
180   root->right = root2;
181   return root; 
182  } else {
183   return NULL;
184  }
185 }
186 Bit* Diff(Bit* root, char V) {
187  if(root) {
188   if(isopnd(root->data) || (!isopnd(root->data) && !isoptr(root->data[0]) && root->data[0] != V)) {
189    root->data = "0";
190    return root;
191   } else if(root->data[0] == V) {
192    root->data = "1";
193    return root;
194   } else if(isoptr(root->data[0]) && root->right) {
195    char ch = root->data[0];
196    switch (ch) {
197     case '+':
198      {
199       Bit* l = new Bit;
200       *l = *(root->left);
201       Bit* r = new Bit;
202       *r = *(root->right);
203       return CompoundExpr(ch, Diff(l, V), Diff(r, V));
204      }
205     case '-':
206      {
207       Bit* l = new Bit;
208       *l = *(root->left);
209       Bit* r = new Bit;
210       *r = *(root->right);
211       return CompoundExpr(ch, Diff(l, V), Diff(r, V));
212      }
213     case '*':
214      {
215       Bit* l = new Bit;
216       *l = *(root->left);
217       Bit* r = new Bit;
218       *r = *(root->right);
219       return CompoundExpr('+', CompoundExpr('*', root->left , Diff(r , V)),
220         CompoundExpr('*', Diff(l, V),  root->right));
221      }
222     case '/':
223      {
224       Bit* l = new Bit;
225       *l = *(root->left);
226       Bit* r = new Bit;
227       *r = *(root->right);
228       return CompoundExpr('-', CompoundExpr('/', Diff(l, V) , root->right),
229         CompoundExpr('*', 
230         CompoundExpr('/', root->left, CompoundExpr('*', root->right, root->right )),  Diff(r, V)));
231      }
232     case '^':
233      {
234       Bit* l = new Bit;
235       *l = *(root->left);
236       Bit* r = new Bit;
237       *r = *(root->right);
238       
239       Bit* p = new Bit;
240       p->data = "ln";
241       p->right = root->left;
242       
243       return CompoundExpr('*', CompoundExpr('^', root->left, root->right),
244        CompoundExpr('+', CompoundExpr('*', Diff(r, V), p), 
245        CompoundExpr('/', CompoundExpr('*', root->right, Diff(l, V)), root->left)));
246      }
247     case 's':
248      {
249       Bit* p = new Bit;
250       *p = *root;
251       Bit* r = new Bit;
252       *r = *(root->right);
253       p->data = "cos";
254       return CompoundExpr('*', Diff(r, V), p);
255      }
256     case 'c':
257      {
258       Bit* p = new Bit;
259       *p = *root;
260       Bit* r = new Bit;
261       *r = *(root->right);
262       p->data = "sin";
263       Bit* q = new Bit;
264       q->data = "-1";
265       return CompoundExpr('*', CompoundExpr('*', q, Diff(r, V)), p);
266      }
267     case 'l':
268      {
269       Bit* r = new Bit;
270       *r = *(root->right);
271       return CompoundExpr('/', Diff(r, V), root->right);
272      }
273    }
274   } 
275  }
276 } 
277  
278 Bit* MergeConst(Bit* root) {
279  if(root) {
280   if(!isopnd(root->data) && root->left && root->right && isopnd(root->left->data) && isopnd(root->right->data)) {
281    switch(root->data[0]) {
282     case '+' :
283      {
284       root->data = convert_str(convert(root->left->data) + convert(root->right->data));
285       root->left = NULL;
286       root->right = NULL;
287       return root;
288      }
289     case '-' :
290      {
291       root->data = convert_str(convert(root->left->data) - convert(root->right->data));
292       root->left = NULL;
293       root->right = NULL;
294       return root;
295      }
296     case '*' :
297      {
298       root->data = convert_str(convert(root->left->data) * convert(root->right->data));
299       root->left = NULL;
300       root->right = NULL;
301       return root;
302      }
303     case '/' :
304      {
305       root->data = convert_str(convert(root->left->data) / convert(root->right->data));
306       root->left = NULL;
307       root->right = NULL;
308       return root;
309      }
310     case '^' :
311      {
312       root->data = convert_str(pow(convert(root->left->data), convert(root->right->data)));
313       root->left = NULL;
314       root->right = NULL;
315       return root;
316      }
317    }
318   } else {
319    Bit* l = NULL;
320    Bit* r = NULL;
321    if(root->left) l = MergeConst(root->left);
322    if(root->right) r = MergeConst(root->right);
323    if(r&&l) {
324     root->left = l;
325     root->right = r;
326     return MergeConst(root);
327    } else return root;
328   }
329  }
330 }
331  
332 int main()
333 {
334  string s;
335  
336  while(getline(cin, s) && s != "") {
337   Bit* root = ReadExpr(s);
338   WriteExpr(root);
339   cout << endl;
340 //  cout << Value(root) << endl;
341   Bit* p = Diff(root, 'x');
342   WriteExpr(root);
343   cout << endl;
344   WriteExpr(p);
345   //cout << endl;
346 //  Assign(root, 'x', 3);
347 //  cout << Value(root) << endl;
348 //  Bit* temp = new Bit;
349 //  temp = CompoundExpr('/', root, root);
350 //  WriteExpr(temp);
351 //  cout << endl;
352 //  cout << Value(temp) << endl;
353   //Bit* q = MergeConst(root);
354   //WriteExpr(MergeConst(Diff(ReadExpr(s),'x')));
355   cout << endl;
356  }
357 }
358  
359 /*
360 #include<iostream>
361 #include<string>
362 #include<cstdio>
363 #include<stdio.h>
364 #include<stdlib.h>
365 #include<stack> 
366 #include<sstream>
367 #include<cmath>
368 using namespace std;
369  struct Node{                                                                                                            //构建一棵树 
370      string data;
371      Node *lchild=NULL;
372   Node *rchild=NULL;
373  };
374  
375 bool CharToNum(string s);
376 Node* CompoundExpr(char P, Node* E1, Node* E2);
377 double DoCharToNum(string s);
378 int Judge(char s);
379 int find(Node* root);
380 Node* CreatTree(string s);
381 Node* Merge(Node* root); 
382 Node* MergeConst(Node* root); 
383 void show(Node* root);
384 double GetValue(Node* root);
385 double Assign(string s,int a);
386 int error(string s);
387 string NumToStr(double num);
388 Node* Diff(Node* root, char V);
389 
390 bool CharToNum(string s) {                                                                                               //判断字符串是否可以转化为数据 
391  stringstream temp;
392  double num;
393  temp << s;
394  if(!(temp >> num)) return false;                                                                                     // 若无法转换,则字符串不是数据 
395  else return true; 
396 } 
397 string NumToStr(double num){                                                                                             //将数据转化为字符串
398  stringstream t;
399  string s;
400  t<<num;
401  t>>s;
402  return s;
403 }
404 double DoCharToNum(string s){                                                                                            //将字符串转化为数据 
405  stringstream t;
406  double num;
407  t<<s;
408  t>>num;
409  return num;
410  
411 }
412 int Judge(char s)                                                                                                       //对运算符进行分类并且判断 
413 {
414  switch(s) {
415   case '+' : return 1; 
416   case '-' : return 1; 
417   case '*' : return 2; 
418   case '/' : return 2; 
419   case '^' : return 2;
420   case 's' : return 4;
421   case 'n' : return 4;
422   case 'g' : return 4;
423   case ' ' : return -1;
424   case 'x' : return 3;
425   case 'X' : return 3;
426         default  : return 5;
427          
428  }
429  
430 }
431  
432 int Jud(string s){                                                                                                      //对于笼统的区分有用字符和无用字符 
433     if(s[0]=='+'||s[0]=='-'||s[0]=='*'||s[0]=='/'||s[0]=='s'||s[0]=='c'||s[0]=='l'||s[0]=='n') return 1;
434  else return 0;
435 }
436  
437 int error(string s){                                                                                                    //判断输入字符串是否是正确的 
438     int l = s.length();
439     int count = 0;
440  for(int i=0; i<l;i++){
441   if(s[i]=='-') {                                                                                                 //对于空格的处理,不计数 
442    while(Judge(s[i+1])==5){
443     i++;
444    }
445   }
446   if(Judge(s[i])==5){                                                                                             //对于一串数字的处理,算作一个数 
447    while(Judge(s[i+1])==5){
448     i++;
449    }
450   }
451   if(s[i]=='c'||s[i]=='l'||s[i]=='s') i = i + 3;                                                                 //对于sin cos log 的处理 
452   if(Judge(s[i])==1 || Judge(s[i])==2) 
453        count--;
454   if(Judge(s[i])==3 || Judge(s[i])==5) 
455        count++;
456  }  
457  if(count!=1){                                                                                                     //如果数字不是比二元运算符多一个,则出错 
458   cout<<"您输入的式子有问题,请确认后重新输入  ";
459   return 0;
460  } 
461  else return 1;
462 }
463  
464 Node* CompoundExpr(char P, Node* E1, Node* E2){                                                                      //两棵树相加,新增加一个根,把两棵树挂上去就好 
465  
466  if(E1 && E2) {
467   Node* root = new Node;
468   char t=P;
469      switch(t){
470        case '+' : root->data = "+"; break;
471        case '-' : root->data = "-"; break;
472           case '*' : root->data = "*"; break;
473           case '/' : root->data = "/"; break;
474        case '^' : root->data = "^"; break;
475      }
476   root->lchild = E1;
477   root->rchild = E2;
478   return root; 
479  } 
480  else return NULL;
481  
482 }
483 
484 Node* CreatTree(string s){                                                                                          //把多项式建立成二叉树 
485  stack<Node*> store;
486  int len = s.length()-1;
487  
488  while(len>=0){                                                                                                  //遍历字符串 
489   
490      if(s[len]==' ') { 
491    len--;                                                                                                 //跳过空格 
492   }
493   else if(Judge(s[len])==1||Judge(s[len])==2){                                                              //如果是字符,运算符 
494    Node* r = new Node;
495    string b="";
496    b = b + s[len];
497    len--;
498    r->data = b;
499    if(!s.empty()){                                                                                       //将树节点栈中两个元素接在一个新节点上 
500     r->lchild = store.top();
501     store.pop();
502    }
503    if(!s.empty()){
504     r->rchild = store.top();
505     store.pop();
506    }
507    store.push(r);                                                                                       //将新节点放回栈内 
508   }
509   else if(Judge(s[len])==4){                                                                        //若是数字,直接作为一个节点放入栈中 
510    Node* r = new Node;
511    string b="";
512    for(int i=len;i>=len-2;i--){
513     b = s[i] + b;
514    } 
515    len = len - 3;
516    r->data = b;
517    if(!s.empty()){
518     r->rchild = store.top();
519     store.pop();
520    } 
521    store.push(r);
522   } 
523   else if(Judge(s[len])==5){                                                                                //对于未知数的处理,也是直接入栈,但是不具备多位数,所以与数字分开表示 
524    
525    string t="";
526    t += s[len];
527    len--;
528    while(Judge(s[len])==5 && len!=-1){
529     t = s[len] + t;
530     len --;
531    }
532    
533    Node* a = new Node;
534       a->data = t;
535       store.push(a);
536    
537          if(s[len]=='-') {                                                                                     //+ -5 7有负数 
538                 Node* c = new Node;
539                 c->data = "0";
540                 store.push(c);
541          }
542   }
543   else if(Judge(s[len])==3 && len!=-1){
544    
545    string t="";
546    t += s[len]; 
547    len--;
548    Node* a = new Node;
549       a->data = t;
550       store.push(a); 
551   }
552  }
553  Node* root = new Node;
554  root = store.top();
555  store.pop();
556  return root;                                                                                                //返回一个最上层的节点,可以代表整棵树,作为一个根 
557 }
558 Node* Diff(Node* root, char V) {                                                                               //求导 
559  if(root) {
560   if(CharToNum(root->data) || (! CharToNum(root->data) && Jud(root->data)==0  && root->data[0] != V)) {
561    root->data = "0";                                                                                  // 数字求导返回0; 
562    return root;
563   } else if(root->data[0] == V) {
564    root->data = "1";                                                                                 //单独的x变量返回1; 
565    return root;
566   } else if(Jud(root->data)==1 && root->rchild) {                                                       //对于F(x) + - / * G(x)处理 
567    char ch = root->data[0];
568    switch (ch) {
569     case '+':
570      {
571       Node* l = new Node;
572       *l = *(root->lchild);
573       Node* r = new Node;
574       *r = *(root->rchild);
575       return CompoundExpr(ch, Diff(l, V), Diff(r, V));                                      //分别求导 
576      }
577     case '-':
578      {
579       Node* l = new Node;
580       *l = *(root->lchild);
581       Node* r = new Node;
582       *r = *(root->rchild);
583       return CompoundExpr(ch, Diff(l, V), Diff(r, V));
584      }
585     case '*':
586      {
587       Node* l = new Node;
588       *l = *(root->lchild);
589       Node* r = new Node;
590       *r = *(root->rchild);
591       return CompoundExpr('+', CompoundExpr('*', root->lchild , Diff(r , V)),                 //分部积分 
592         CompoundExpr('*', Diff(l, V),  root->rchild));
593      }
594     case '/':
595      {
596       Node* l = new Node;
597       *l = *(root->lchild);
598       Node* r = new Node;
599       *r = *(root->rchild);
600       return CompoundExpr('-', CompoundExpr('/', Diff(l, V) , root->rchild),CompoundExpr('*',  
601          CompoundExpr('/', root->lchild, CompoundExpr('*', root->rchild, root->rchild )),  Diff(r, V)));
602      }                                                                                             //对于除法 ,加负号以及 分子分母求导 
603     case '^':
604      {
605       Node* l = new Node;
606       *l = *(root->lchild);
607       Node* r = new Node;
608       *r = *(root->rchild);
609       
610       Node* p = new Node;
611       p->data = "ln";
612       p->rchild = root->lchild;                                                                 //对于开方的求导 
613                                             
614       return CompoundExpr('*', CompoundExpr('^', root->lchild, root->rchild),CompoundExpr('+', CompoundExpr('*', Diff(r, V), p), 
615                                                  CompoundExpr('/', CompoundExpr('*', root->rchild, Diff(l, V)), root->lchild)));
616      }
617     case 's':                                                                                        //对于sin的求导 
618      {
619       Node* p = new Node;
620       *p = *root;
621       Node* r = new Node;
622       *r = *(root->rchild);
623       p->data = "cos";                                                                        //变成cos 
624       return CompoundExpr('*', Diff(r, V), p);
625      }
626     case 'c':
627      {                                                                                           //对于cos的求导 
628       Node* l = new Node;
629       *l = *root;
630       Node* r = new Node;
631       *r = *(root->rchild);
632       l->data = "sin";                                                                       //变成sin 
633       Node* q = new Node;
634       q->data = "-1";
635       return CompoundExpr('*', CompoundExpr('*', q, Diff(r, V)), l);
636      }
637     case 'l':
638      {
639       Node* r = new Node;
640       *r = *(root->rchild);
641       return CompoundExpr('/', Diff(r, V), root->rchild);
642      }
643    }
644   } 
645  }
646 } 
647 void show(Node* root){                                                                                        //输出表达式的形式 
648   
649     if(root!=NULL){
650       
651      if(root->lchild){                                                                                     //先输出左子树 
652       if((!CharToNum(root->lchild->data) && (Judge(root->data[0]) > Judge(root->lchild->data[0]))) || root->lchild->data[0] == '^') {
653     cout << " (";
654     show(root->lchild);
655     cout << " )";
656    } else show(root->lchild);                                                                         //对于符号优先级做判断,加括号 
657   
658      }
659       
660      cout<<" "<< root->data;                                                                               //中间节点输出 
661      
662      if(root->rchild){
663       if((!CharToNum(root->rchild->data) && (Judge(root->data[0]) >= Judge(root->rchild->data[0]))) || root->rchild->data[0] == '^') {
664     cout << " (";
665     show(root->rchild);                                                                            //输出右子树 
666     cout << " )";
667    } else show(root->rchild); 
668      }
669      
670  }
671 }
672 int find(Node* root){                                                                                         //找到有数字的那些节点 
673  if(root){
674   if(Jud(root->data)==0) return 1;
675   if(Jud(root->lchild->data)==0) return 1;
676   else  find(root->lchild);
677  
678   if(Jud(root->rchild->data)==0) return 1;
679   else  find(root->rchild);
680   
681   return 0;
682  }
683 }
684 Node* Merge(Node* root){                                                                                      //化简的函数 
685     if(root) {
686   
687   if(!CharToNum(root->data) && root->lchild && root->rchild && CharToNum(root->lchild->data) && CharToNum(root->rchild->data)) {
688                                                                                                               //如果节点左右不空且可计算,则计算 
689    switch(root->data[0]) {
690     case '+' :
691      {
692       root->data = NumToStr(DoCharToNum(root->lchild->data) + DoCharToNum(root->rchild->data));
693       root->lchild = NULL;
694       root->rchild = NULL;
695       return root;
696      }
697     case '-' :
698      {
699       root->data =  NumToStr(DoCharToNum(root->lchild->data) - DoCharToNum(root->rchild->data));
700       root->lchild = NULL;
701       root->rchild = NULL;
702       return root;
703      }
704     case '*' :
705      {   
706       root->data = NumToStr(DoCharToNum(root->lchild->data) * DoCharToNum(root->rchild->data));
707       root->lchild = NULL;
708       root->rchild = NULL;
709       return root;
710      }
711     case '/' :
712      {
713       root->data = NumToStr(DoCharToNum(root->lchild->data) / DoCharToNum(root->rchild->data));
714       root->lchild = NULL;
715       root->rchild = NULL;
716       return root;
717      }
718     case '^' :
719      {
720       root->data = NumToStr(pow(DoCharToNum(root->lchild->data), DoCharToNum(root->rchild->data)));
721       root->lchild = NULL;
722       root->rchild = NULL;
723       return root;
724      }
725     
726    }
727   } 
728   
729   
730   else {                                                                                             //有未知数可以在分析未知数节点的左右子树 
731             
732    Node* l = NULL;
733    Node* r = NULL;
734    if(root->lchild) l = MergeConst(root->lchild);
735    if(root->rchild) r = MergeConst(root->rchild);
736    if(r&&l) {
737     root->lchild = l;
738     root->rchild = r;
739     return root;
740    } else return root;
741   }
742  }
743 }
744 Node* MergeConst(Node* root){                                                                                //对于上述式子多化简几次 
745  
746  for(int i=0;i<5;i++){
747   Merge(root);
748  }
749 }
750 double GetValue(Node* root){                                                                                 //求值函数 
751  if(root){
752   char a = root->data[0];
753   if(!root->rchild && !root->lchild &&CharToNum(root->data)) return DoCharToNum(root->data);          //如果节点只有一个数字,左右为空,则为为叶子节点,可以直接返回数字 
754   else{
755    switch(a){
756     case '+' : return GetValue(root->lchild) + GetValue(root->rchild);
757     case '-' : return GetValue(root->lchild) - GetValue(root->rchild);
758     case '*' : return GetValue(root->lchild) * GetValue(root->rchild);
759     case '/' : return GetValue(root->lchild) / GetValue(root->rchild);
760     case '^' : return pow(GetValue(root->lchild), GetValue(root->rchild));
761        case 's' : return sin(GetValue(root->rchild));
762     case 'c' : return cos(GetValue(root->rchild));
763     case 'l' : return log(GetValue(root->rchild));
764    } 
765   }
766  }
767 }
768 double Assign(string s,int a){                                                                               //对未知数赋值的函数,  
769  int l = s.length();
770  for(int i=0;i<l;i++){                                                                                    // 直接在原来的字符串基础上把未知数变成要代入的 
771   if(s[i]=='x'||s[i]=='X') s[i] = a +'0';                                                                 
772  }          
773  cout<<s<<endl;
774  return GetValue(CreatTree(s));                                                                           //求值 
775 }
776  
777 void jiemmian(){                                                                                             //界面的展示 
778  cout<<"             欢迎使用本计算器             "<<endl; 
779  cout<<"            本计算器有以下功能            "<<endl; 
780  cout<<"------------------------------------------"<<endl;
781     cout<<"1----创建表达式树     2----打印表达式树   "<<endl; 
782     cout<<"3----表达式树相加     4----得到表达式值   "<<endl;
783  cout<<"5----化简表达式       6----求导           "<<endl;
784  cout<<"7-----求sin           8----求cos          "<<endl; 
785  
786  cout<<"tips:"<<endl;
787    
788  cout<<"           输入 1 -----创建并打印树            "<<endl;
789  cout<<"           输入 2 -----创建并计算树相加        "<<endl;
790  
791   
792 }
793 int main(){
794  string s;
795  jiemmian();
796  cout<<endl;
797  cout<<"请输入一个前缀算术表达式   "; 
798  while(getline(cin,s)){
799   
800   if(error(s)==1) {
801    int t;
802    cout<<"请输入要操作的指令   : ";
803    cin>>t;
804    
805    if(t==1){
806     show(CreatTree(s));
807     cout<<endl;
808           cout<<"这个算式化简的结果为   ";
809              show(MergeConst(CreatTree(s)));
810     } 
811    if(t==2){
812       cout<<"请输入另一表达式"<<endl;
813       getchar();
814       string s1;
815       getline(cin,s1);
816                cout<<s1<<endl;
817                cout<<s<<endl; 
818       show(CompoundExpr('+', CreatTree(s), CreatTree(s1)));
819       cout<<"这个算式化简的结果为   ";
820       show(MergeConst(CompoundExpr('+', CreatTree(s), CreatTree(s1))));
821    }
822   }
823   else continue;
824   cout<<endl;
825   //show(MergeConst(Diff(MergeConst(CreatTree(s)),'x')));
826   //cout<<endl;
827   //cout<<"这个式子的导数为   "; 
828   //show(Diff(CreatTree(s),'x'));
829   //cout<<endl;
830   //cout<<"这个算式的结果为"<<GetValue(CreatTree(s));
831   //cout<<"这个算式的结果为"<< Assign(s,5);
832         //show(CompoundExpr('+', CreatTree(s), CreatTree(s)));
833         //cout<<endl;
834      //cout<<GetValue(CompoundExpr('+', CreatTree(s), CreatTree(s)));
835  } 
836  return 0;
837 }
838 */
839  

 

posted on 2016-11-26 21:55  任我主宰  阅读(395)  评论(0编辑  收藏  举报