首页 > 代码库 > project 2016 11 20 树的多项式

project 2016 11 20 树的多项式

#include <iostream>
#include <stack>
#include <cmath>
#include <sstream>

using namespace std;

bool isoptr(char ch) {
 if(ch == ‘+‘ || ch == ‘-‘ || ch == ‘*‘ || ch ==  ‘/‘ || ch == ‘^‘ || ch == ‘s‘ || ch == ‘c‘ || ch == ‘l‘ || ch == ‘n‘) return true;
 else return false;
}

bool isopnd(string s) {
 stringstream temp;
 double num;
 char ch;
 temp << s;
 if(!(temp >> num)) return false; // 若无法转换,则字符串不是数据
 else return true;
}

double convert(string s) {
 double sum = 0;
 stringstream temp;
 temp << s;
 temp >> sum;
 return sum;
}

string convert_str(double num) {
 stringstream temp;
 string str;
 temp << num;
 temp >> str;
 return str;
}

int optrpri(string s)
{
 switch(s[0]) {
  case ‘+‘ : return 1;
  case ‘-‘ : return 1;
  case ‘*‘ : return 2;
  case ‘/‘ : return 2;
  case ‘^‘ : return 3;
  case ‘s‘ : return 4;
  case ‘c‘ : return 4;
  case ‘l‘ : return 4;
 }
 
}

struct Bit {
 string data;
 Bit* left = NULL;
 Bit* right = NULL;
};

Bit* ReadExpr(string exp) {
 stack <Bit*> datast;
 int cnt = exp.length()-1;
 while(cnt >= 0) {
  if(exp[cnt] == ‘ ‘) cnt--;
  else if(!isoptr(exp[cnt])) {
   int i = cnt;
   string temp;
   while(!isoptr(exp[i]) && i >= 0 && exp[i] != ‘ ‘) {
    temp = exp[i] + temp;
    i--;
   }
   if(exp[i] == ‘-‘) {
    temp = exp[i] + temp;
    cnt = i-1;
   } else cnt = i;
   Bit* p = new Bit;
   p->data = http://www.mamicode.com/temp;
   datast.push(p);
  } else if(exp[cnt] == ‘n‘ || exp[cnt] == ‘s‘) {
   if(exp[cnt-1] == ‘l‘ && exp[cnt] == ‘n‘) {
    Bit* p = new Bit;
    p->data = "http://www.mamicode.com/ln";
    if(!datast.empty()) {
     p->right = datast.top();
     datast.pop();
    }
    datast.push(p);
    cnt -= 2;
   } else if(exp[cnt-2] == ‘s‘ && exp[cnt-1] == ‘i‘ && exp[cnt] == ‘n‘) {
    Bit* p = new Bit;
    p->data = "http://www.mamicode.com/sin";
    if(!datast.empty()) {
     p->right = datast.top();
     datast.pop();
    }
    datast.push(p);
    cnt -= 3;
   } else if(exp[cnt-2] == ‘c‘ && exp[cnt-1] == ‘o‘ && exp[cnt] == ‘s‘) {
    Bit* p = new Bit;
    p->data = "http://www.mamicode.com/cos";
    if(!datast.empty()) {
     p->right = datast.top();
     datast.pop();
    }
    datast.push(p);
    cnt -= 3;
   }
  } else {
   Bit* p = new Bit;
   p->data = http://www.mamicode.com/exp[cnt];
   if(!datast.empty()) {
    p->left = datast.top();
    datast.pop();
   }
   if(!datast.empty()) {
    p->right = datast.top();
    datast.pop();
   }
   datast.push(p);
   cnt--;
  }
 }
 Bit* root = new Bit;
 root = datast.top();
 datast.pop();
 return root;
};

void WriteExpr(Bit* root) { // 中序遍历
 if(root) {
  if(root->left) {
   if((!isopnd(root->left->data) && (optrpri(root->data) > optrpri(root->left->data))) || root->left->data[0] == ‘^‘) {
    cout << " (";
    WriteExpr(root->left);
    cout << " )";
   } else WriteExpr(root->left);
  }
  
  cout << " " << root->data;
  
  if(root->right) {
   if((!isopnd(root->right->data) && (optrpri(root->data) >= optrpri(root->right->data))) || root->right->data[0] == ‘^‘) {
    cout << " (";
    WriteExpr(root->right);
    cout  << " )";
   } else WriteExpr(root->right);
  }
 }
}

void Assign(Bit* root, char V, int c = 0) {
 if(root) {
  char ch = root->data[0];
  if(ch == V) {
   if(c >= 0) {
    char temp = c + ‘0‘;
    root->data = http://www.mamicode.com/temp;
   } else {
    char temp = (0-c) + ‘0‘;
    string str = "-" + temp;
    root->data = http://www.mamicode.com/str;
   }
  }
  Assign(root->left, V, c);
  Assign(root->right, V, c);
 } 
}

double Value(Bit* root){
 if(root) {
  char ch = root->data[0];
  if(!root->left && !root->right && isopnd(root->data)) return convert(root->data);
  else {
   switch (ch) {
    case ‘+‘ : return Value(root->left) + Value(root->right);
    case ‘-‘ : return Value(root->left) - Value(root->right);
    case ‘*‘ : return Value(root->left) * Value(root->right);
    case ‘/‘ : return Value(root->left) / Value(root->right);
    case ‘^‘ : return pow(Value(root->left), Value(root->right));
    case ‘s‘ : return sin(Value(root->right));
    case ‘c‘ : return cos(Value(root->right));
    case ‘l‘ : return log(Value(root->right));
   }
  }
 }
}

Bit* CompoundExpr(char optr, Bit* root1, Bit* root2){
 if(root1 && root2) {
  Bit* root = new Bit;
  root->data = http://www.mamicode.com/optr;
  root->left = root1;
  root->right = root2;
  return root;
 } else {
  return NULL;
 }
}

Bit* Diff(Bit* root, char V) {
 if(root) {
  if(isopnd(root->data) || (!isopnd(root->data) && !isoptr(root->data[0]) && root->data[0] != V)) {
   root->data = "http://www.mamicode.com/0";
   return root;
  } else if(root->data[0] == V) {
   root->data = "http://www.mamicode.com/1";
   return root;
  } else if(isoptr(root->data[0]) && root->right) {
   char ch = root->data[0];
   switch (ch) {
    case ‘+‘:
     {
      Bit* l = new Bit;
      *l = *(root->left);
      Bit* r = new Bit;
      *r = *(root->right);
      return CompoundExpr(ch, Diff(l, V), Diff(r, V));
     }
    case ‘-‘:
     {
      Bit* l = new Bit;
      *l = *(root->left);
      Bit* r = new Bit;
      *r = *(root->right);
      return CompoundExpr(ch, Diff(l, V), Diff(r, V));
     }
    case ‘*‘:
     {
      Bit* l = new Bit;
      *l = *(root->left);
      Bit* r = new Bit;
      *r = *(root->right);
      return CompoundExpr(‘+‘, CompoundExpr(‘*‘, root->left , Diff(r , V)),
        CompoundExpr(‘*‘, Diff(l, V),  root->right));
     }
    case ‘/‘:
     {
      Bit* l = new Bit;
      *l = *(root->left);
      Bit* r = new Bit;
      *r = *(root->right);
      return CompoundExpr(‘-‘, CompoundExpr(‘/‘, Diff(l, V) , root->right),
        CompoundExpr(‘*‘,
        CompoundExpr(‘/‘, root->left, CompoundExpr(‘*‘, root->right, root->right )),  Diff(r, V)));
     }
    case ‘^‘:
     {
      Bit* l = new Bit;
      *l = *(root->left);
      Bit* r = new Bit;
      *r = *(root->right);
      
      Bit* p = new Bit;
      p->data = "http://www.mamicode.com/ln";
      p->right = root->left;
      
      return CompoundExpr(‘*‘, CompoundExpr(‘^‘, root->left, root->right),
       CompoundExpr(‘+‘, CompoundExpr(‘*‘, Diff(r, V), p),
       CompoundExpr(‘/‘, CompoundExpr(‘*‘, root->right, Diff(l, V)), root->left)));
     }
    case ‘s‘:
     {
      Bit* p = new Bit;
      *p = *root;
      Bit* r = new Bit;
      *r = *(root->right);
      p->data = "http://www.mamicode.com/cos";
      return CompoundExpr(‘*‘, Diff(r, V), p);
     }
    case ‘c‘:
     {
      Bit* p = new Bit;
      *p = *root;
      Bit* r = new Bit;
      *r = *(root->right);
      p->data = "http://www.mamicode.com/sin";
      Bit* q = new Bit;
      q->data = "http://www.mamicode.com/-1";
      return CompoundExpr(‘*‘, CompoundExpr(‘*‘, q, Diff(r, V)), p);
     }
    case ‘l‘:
     {
      Bit* r = new Bit;
      *r = *(root->right);
      return CompoundExpr(‘/‘, Diff(r, V), root->right);
     }
   }
  }
 }
}
 
Bit* MergeConst(Bit* root) {
 if(root) {
  if(!isopnd(root->data) && root->left && root->right && isopnd(root->left->data) && isopnd(root->right->data)) {
   switch(root->data[0]) {
    case ‘+‘ :
     {
      root->data = http://www.mamicode.com/convert_str(convert(root->left->data) + convert(root->right->data));
      root->left = NULL;
      root->right = NULL;
      return root;
     }
    case ‘-‘ :
     {
      root->data = http://www.mamicode.com/convert_str(convert(root->left->data) - convert(root->right->data));
      root->left = NULL;
      root->right = NULL;
      return root;
     }
    case ‘*‘ :
     {
      root->data = http://www.mamicode.com/convert_str(convert(root->left->data) * convert(root->right->data));
      root->left = NULL;
      root->right = NULL;
      return root;
     }
    case ‘/‘ :
     {
      root->data = http://www.mamicode.com/convert_str(convert(root->left->data) / convert(root->right->data));
      root->left = NULL;
      root->right = NULL;
      return root;
     }
    case ‘^‘ :
     {
      root->data = http://www.mamicode.com/convert_str(pow(convert(root->left->data), convert(root->right->data)));
      root->left = NULL;
      root->right = NULL;
      return root;
     }
   }
  } else {
   Bit* l = NULL;
   Bit* r = NULL;
   if(root->left) l = MergeConst(root->left);
   if(root->right) r = MergeConst(root->right);
   if(r&&l) {
    root->left = l;
    root->right = r;
    return MergeConst(root);
   } else return root;
  }
 }
}
 

int main()
{
 string s;
 
 while(getline(cin, s) && s != "") {
  Bit* root = ReadExpr(s);
  WriteExpr(root);
  cout << endl;
//  cout << Value(root) << endl;
  Bit* p = Diff(root, ‘x‘);
  WriteExpr(root);
  cout << endl;
  WriteExpr(p);
  //cout << endl;
//  Assign(root, ‘x‘, 3);
//  cout << Value(root) << endl;
//  Bit* temp = new Bit;
//  temp = CompoundExpr(‘/‘, root, root);
//  WriteExpr(temp);
//  cout << endl;
//  cout << Value(temp) << endl;

  //Bit* q = MergeConst(root);
  //WriteExpr(MergeConst(Diff(ReadExpr(s),‘x‘)));
  cout << endl;
 }
}

 

/*

#include<iostream>
#include<string>
#include<cstdio>
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<sstream>
#include<cmath>

using namespace std;

 struct Node{                                                                                                            //构建一棵树

     string data;
     Node *lchild=NULL;
  Node *rchild=NULL;

 };
 
bool CharToNum(string s);
Node* CompoundExpr(char P, Node* E1, Node* E2);
double DoCharToNum(string s);
int Judge(char s);
int find(Node* root);
Node* CreatTree(string s);
Node* Merge(Node* root);
Node* MergeConst(Node* root);
void show(Node* root);
double GetValue(Node* root);
double Assign(string s,int a);
int error(string s);
string NumToStr(double num);
Node* Diff(Node* root, char V);


bool CharToNum(string s) {                                                                                               //判断字符串是否可以转化为数据
 stringstream temp;
 double num;
 temp << s;
 if(!(temp >> num)) return false;                                                                                     // 若无法转换,则字符串不是数据
 else return true;
}

string NumToStr(double num){                                                                                             //将数据转化为字符串
 stringstream t;
 string s;
 t<<num;
 t>>s;
 return s;
}
double DoCharToNum(string s){                                                                                            //将字符串转化为数据
 stringstream t;
 double num;
 t<<s;
 t>>num;
 return num;
 
}

int Judge(char s)                                                                                                       //对运算符进行分类并且判断
{
 switch(s) {
  case ‘+‘ : return 1;
  case ‘-‘ : return 1;
  case ‘*‘ : return 2;
  case ‘/‘ : return 2;
  case ‘^‘ : return 2;
  case ‘s‘ : return 4;
  case ‘n‘ : return 4;
  case ‘g‘ : return 4;
  case ‘ ‘ : return -1;
  case ‘x‘ : return 3;
  case ‘X‘ : return 3;
        default  : return 5;
        
 }
 
}

 

int Jud(string s){                                                                                                      //对于笼统的区分有用字符和无用字符
    if(s[0]==‘+‘||s[0]==‘-‘||s[0]==‘*‘||s[0]==‘/‘||s[0]==‘s‘||s[0]==‘c‘||s[0]==‘l‘||s[0]==‘n‘) return 1;
 else return 0;
}

 

int error(string s){                                                                                                    //判断输入字符串是否是正确的
    int l = s.length();
    int count = 0;
 for(int i=0; i<l;i++){
  if(s[i]==‘-‘) {                                                                                                 //对于空格的处理,不计数
   while(Judge(s[i+1])==5){
    i++;
   }
  }
  if(Judge(s[i])==5){                                                                                             //对于一串数字的处理,算作一个数
   while(Judge(s[i+1])==5){
    i++;
   }
  }
  if(s[i]==‘c‘||s[i]==‘l‘||s[i]==‘s‘) i = i + 3;                                                                 //对于sin cos log 的处理
  if(Judge(s[i])==1 || Judge(s[i])==2)
       count--;
  if(Judge(s[i])==3 || Judge(s[i])==5)
       count++;
 } 
 if(count!=1){                                                                                                     //如果数字不是比二元运算符多一个,则出错
  cout<<"您输入的式子有问题,请确认后重新输入  ";
  return 0;
 }
 else return 1;
}

 

Node* CompoundExpr(char P, Node* E1, Node* E2){                                                                      //两棵树相加,新增加一个根,把两棵树挂上去就好
 
 if(E1 && E2) {
  Node* root = new Node;
  char t=P;
     switch(t){
       case ‘+‘ : root->data = "http://www.mamicode.com/+"; break;
       case ‘-‘ : root->data = "http://www.mamicode.com/-"; break;
          case ‘*‘ : root->data = "http://www.mamicode.com/*"; break;
          case ‘/‘ : root->data = "http://www.mamicode.com/"; break;
       case ‘^‘ : root->data = "http://www.mamicode.com/^"; break;
     }
  root->lchild = E1;
  root->rchild = E2;
  return root;
 }
 else return NULL;
 
}


Node* CreatTree(string s){                                                                                          //把多项式建立成二叉树
 stack<Node*> store;
 int len = s.length()-1;
 
 while(len>=0){                                                                                                  //遍历字符串
  
     if(s[len]==‘ ‘) {
   len--;                                                                                                 //跳过空格
  }
  else if(Judge(s[len])==1||Judge(s[len])==2){                                                              //如果是字符,运算符
   Node* r = new Node;
   string b="";
   b = b + s[len];
   len--;
   r->data = http://www.mamicode.com/b;
   if(!s.empty()){                                                                                       //将树节点栈中两个元素接在一个新节点上
    r->lchild = store.top();
    store.pop();
   }
   if(!s.empty()){
    r->rchild = store.top();
    store.pop();
   }
   store.push(r);                                                                                       //将新节点放回栈内
  }
  else if(Judge(s[len])==4){                                                                        //若是数字,直接作为一个节点放入栈中
   Node* r = new Node;
   string b="";
   for(int i=len;i>=len-2;i--){
    b = s[i] + b;
   }
   len = len - 3;
   r->data = http://www.mamicode.com/b;
   if(!s.empty()){
    r->rchild = store.top();
    store.pop();
   } 
   store.push(r);
  }
  else if(Judge(s[len])==5){                                                                                //对于未知数的处理,也是直接入栈,但是不具备多位数,所以与数字分开表示
   
   string t="";
   t += s[len];
   len--;
   while(Judge(s[len])==5 && len!=-1){
    t = s[len] + t;
    len --;
   }
   
   Node* a = new Node;
      a->data = http://www.mamicode.com/t;
      store.push(a);
   
         if(s[len]==‘-‘) {                                                                                     //+ -5 7有负数
                Node* c = new Node;
                c->data = "http://www.mamicode.com/0";
                store.push(c);
         }

  }
  else if(Judge(s[len])==3 && len!=-1){
   
   string t="";
   t += s[len];
   len--;
   Node* a = new Node;
      a->data = http://www.mamicode.com/t;
      store.push(a); 
  }
 }
 Node* root = new Node;
 root = store.top();
 store.pop();
 return root;                                                                                                //返回一个最上层的节点,可以代表整棵树,作为一个根

}

Node* Diff(Node* root, char V) {                                                                               //求导
 if(root) {
  if(CharToNum(root->data) || (! CharToNum(root->data) && Jud(root->data)==0  && root->data[0] != V)) {
   root->data = "http://www.mamicode.com/0";                                                                                  // 数字求导返回0;
   return root;
  } else if(root->data[0] == V) {
   root->data = "http://www.mamicode.com/1";                                                                                 //单独的x变量返回1;
   return root;
  } else if(Jud(root->data)==1 && root->rchild) {                                                       //对于F(x) + - / * G(x)处理
   char ch = root->data[0];
   switch (ch) {
    case ‘+‘:
     {
      Node* l = new Node;
      *l = *(root->lchild);
      Node* r = new Node;
      *r = *(root->rchild);
      return CompoundExpr(ch, Diff(l, V), Diff(r, V));                                      //分别求导
     }
    case ‘-‘:
     {
      Node* l = new Node;
      *l = *(root->lchild);
      Node* r = new Node;
      *r = *(root->rchild);
      return CompoundExpr(ch, Diff(l, V), Diff(r, V));
     }
    case ‘*‘:
     {
      Node* l = new Node;
      *l = *(root->lchild);
      Node* r = new Node;
      *r = *(root->rchild);
      return CompoundExpr(‘+‘, CompoundExpr(‘*‘, root->lchild , Diff(r , V)),                 //分部积分
        CompoundExpr(‘*‘, Diff(l, V),  root->rchild));
     }
    case ‘/‘:
     {
      Node* l = new Node;
      *l = *(root->lchild);
      Node* r = new Node;
      *r = *(root->rchild);
      return CompoundExpr(‘-‘, CompoundExpr(‘/‘, Diff(l, V) , root->rchild),CompoundExpr(‘*‘, 
         CompoundExpr(‘/‘, root->lchild, CompoundExpr(‘*‘, root->rchild, root->rchild )),  Diff(r, V)));
     }                                                                                             //对于除法 ,加负号以及 分子分母求导
    case ‘^‘:
     {
      Node* l = new Node;
      *l = *(root->lchild);
      Node* r = new Node;
      *r = *(root->rchild);
      
      Node* p = new Node;
      p->data = "http://www.mamicode.com/ln";
      p->rchild = root->lchild;                                                                 //对于开方的求导
                                           
      return CompoundExpr(‘*‘, CompoundExpr(‘^‘, root->lchild, root->rchild),CompoundExpr(‘+‘, CompoundExpr(‘*‘, Diff(r, V), p),
                                                 CompoundExpr(‘/‘, CompoundExpr(‘*‘, root->rchild, Diff(l, V)), root->lchild)));
     }
    case ‘s‘:                                                                                        //对于sin的求导
     {
      Node* p = new Node;
      *p = *root;
      Node* r = new Node;
      *r = *(root->rchild);
      p->data = "http://www.mamicode.com/cos";                                                                        //变成cos
      return CompoundExpr(‘*‘, Diff(r, V), p);
     }
    case ‘c‘:
     {                                                                                           //对于cos的求导
      Node* l = new Node;
      *l = *root;
      Node* r = new Node;
      *r = *(root->rchild);
      l->data = "http://www.mamicode.com/sin";                                                                       //变成sin
      Node* q = new Node;
      q->data = "http://www.mamicode.com/-1";
      return CompoundExpr(‘*‘, CompoundExpr(‘*‘, q, Diff(r, V)), l);
     }
    case ‘l‘:
     {
      Node* r = new Node;
      *r = *(root->rchild);
      return CompoundExpr(‘/‘, Diff(r, V), root->rchild);
     }
   }
  }
 }
}
void show(Node* root){                                                                                        //输出表达式的形式
 
    if(root!=NULL){
     
     if(root->lchild){                                                                                     //先输出左子树
      if((!CharToNum(root->lchild->data) && (Judge(root->data[0]) > Judge(root->lchild->data[0]))) || root->lchild->data[0] == ‘^‘) {
    cout << " (";
    show(root->lchild);
    cout << " )";
   } else show(root->lchild);                                                                         //对于符号优先级做判断,加括号
  
     }
     
     cout<<" "<< root->data;                                                                               //中间节点输出
    
     if(root->rchild){
      if((!CharToNum(root->rchild->data) && (Judge(root->data[0]) >= Judge(root->rchild->data[0]))) || root->rchild->data[0] == ‘^‘) {
    cout << " (";
    show(root->rchild);                                                                            //输出右子树
    cout << " )";
   } else show(root->rchild); 
     }
    
 }
}

int find(Node* root){                                                                                         //找到有数字的那些节点
 if(root){
  if(Jud(root->data)==0) return 1;
  if(Jud(root->lchild->data)==0) return 1;
  else  find(root->lchild);
 
  if(Jud(root->rchild->data)==0) return 1;
  else  find(root->rchild);
  
  return 0;
 }
}

Node* Merge(Node* root){                                                                                      //化简的函数
    if(root) {
  
  if(!CharToNum(root->data) && root->lchild && root->rchild && CharToNum(root->lchild->data) && CharToNum(root->rchild->data)) {
                                                                                                              //如果节点左右不空且可计算,则计算
   switch(root->data[0]) {
    case ‘+‘ :
     {
      root->data = http://www.mamicode.com/NumToStr(DoCharToNum(root->lchild->data) + DoCharToNum(root->rchild->data));
      root->lchild = NULL;
      root->rchild = NULL;
      return root;
     }
    case ‘-‘ :
     {
      root->data =http://www.mamicode.com/ NumToStr(DoCharToNum(root->lchild->data) - DoCharToNum(root->rchild->data));
      root->lchild = NULL;
      root->rchild = NULL;
      return root;
     }
    case ‘*‘ :
     {  
      root->data = http://www.mamicode.com/NumToStr(DoCharToNum(root->lchild->data) * DoCharToNum(root->rchild->data));
      root->lchild = NULL;
      root->rchild = NULL;
      return root;
     }
    case ‘/‘ :
     {
      root->data = http://www.mamicode.com/NumToStr(DoCharToNum(root->lchild->data) / DoCharToNum(root->rchild->data));
      root->lchild = NULL;
      root->rchild = NULL;
      return root;
     }
    case ‘^‘ :
     {
      root->data = http://www.mamicode.com/NumToStr(pow(DoCharToNum(root->lchild->data), DoCharToNum(root->rchild->data)));
      root->lchild = NULL;
      root->rchild = NULL;
      return root;
     }
    
   }
  }
  
  
  else {                                                                                             //有未知数可以在分析未知数节点的左右子树
           
   Node* l = NULL;
   Node* r = NULL;
   if(root->lchild) l = MergeConst(root->lchild);
   if(root->rchild) r = MergeConst(root->rchild);
   if(r&&l) {
    root->lchild = l;
    root->rchild = r;
    return root;
   } else return root;
  }
 }

}

Node* MergeConst(Node* root){                                                                                //对于上述式子多化简几次
 
 for(int i=0;i<5;i++){
  Merge(root);
 }
}
double GetValue(Node* root){                                                                                 //求值函数
 if(root){
  char a = root->data[0];
  if(!root->rchild && !root->lchild &&CharToNum(root->data)) return DoCharToNum(root->data);          //如果节点只有一个数字,左右为空,则为为叶子节点,可以直接返回数字
  else{
   switch(a){
    case ‘+‘ : return GetValue(root->lchild) + GetValue(root->rchild);
    case ‘-‘ : return GetValue(root->lchild) - GetValue(root->rchild);
    case ‘*‘ : return GetValue(root->lchild) * GetValue(root->rchild);
    case ‘/‘ : return GetValue(root->lchild) / GetValue(root->rchild);
    case ‘^‘ : return pow(GetValue(root->lchild), GetValue(root->rchild));
       case ‘s‘ : return sin(GetValue(root->rchild));
    case ‘c‘ : return cos(GetValue(root->rchild));
    case ‘l‘ : return log(GetValue(root->rchild));
   }
  }
 }
}

double Assign(string s,int a){                                                                               //对未知数赋值的函数, 
 int l = s.length();
 for(int i=0;i<l;i++){                                                                                    // 直接在原来的字符串基础上把未知数变成要代入的
  if(s[i]==‘x‘||s[i]==‘X‘) s[i] = a +‘0‘;                                                                
 }         
 cout<<s<<endl;
 return GetValue(CreatTree(s));                                                                           //求值
}
 
void jiemmian(){                                                                                             //界面的展示
 cout<<"             欢迎使用本计算器             "<<endl;
 cout<<"            本计算器有以下功能            "<<endl;
 cout<<"------------------------------------------"<<endl;
    cout<<"1----创建表达式树     2----打印表达式树   "<<endl;
    cout<<"3----表达式树相加     4----得到表达式值   "<<endl;
 cout<<"5----化简表达式       6----求导           "<<endl;
 cout<<"7-----求sin           8----求cos          "<<endl;
 
 cout<<"tips:"<<endl;
  
 cout<<"           输入 1 -----创建并打印树            "<<endl;
 cout<<"           输入 2 -----创建并计算树相加        "<<endl;
 
 
}

int main(){
 string s;
 jiemmian();
 cout<<endl;
 cout<<"请输入一个前缀算术表达式   ";
 while(getline(cin,s)){
  
  if(error(s)==1) {
   int t;
   cout<<"请输入要操作的指令   : ";
   cin>>t;
   
   if(t==1){
    show(CreatTree(s));
    cout<<endl;
          cout<<"这个算式化简的结果为   ";
             show(MergeConst(CreatTree(s)));
    }
   if(t==2){
      cout<<"请输入另一表达式"<<endl;
      getchar();
      string s1;
      getline(cin,s1);
               cout<<s1<<endl;
               cout<<s<<endl;
      show(CompoundExpr(‘+‘, CreatTree(s), CreatTree(s1)));
      cout<<"这个算式化简的结果为   ";
      show(MergeConst(CompoundExpr(‘+‘, CreatTree(s), CreatTree(s1))));
   }
  }
  else continue;
  cout<<endl;
  //show(MergeConst(Diff(MergeConst(CreatTree(s)),‘x‘)));
  //cout<<endl;
  //cout<<"这个式子的导数为   ";
  //show(Diff(CreatTree(s),‘x‘));

  //cout<<endl;
  //cout<<"这个算式的结果为"<<GetValue(CreatTree(s));
  //cout<<"这个算式的结果为"<< Assign(s,5);
        //show(CompoundExpr(‘+‘, CreatTree(s), CreatTree(s)));
        //cout<<endl;
     //cout<<GetValue(CompoundExpr(‘+‘, CreatTree(s), CreatTree(s)));
 }

 return 0;
}

*/

 

project 2016 11 20 树的多项式