国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++二叉樹應(yīng)用:計(jì)算表達(dá)式

2020-05-23 14:24:40
字體:
供稿:網(wǎng)友

昨天晚上,我花了大把的時(shí)間研究里面二叉樹應(yīng)用解決計(jì)算表達(dá)式的問題,一直就沒理解,主要是覺得是不是自己錯(cuò)了,又懶,不愿意自己把代碼敲到電腦里看看,結(jié)果浪費(fèi)了很多時(shí)間。所以還是提醒大家,代碼這種東西,有什么好多看的,覺得他錯(cuò)了就自己敲到電腦里去看看!其實(shí)也沒錯(cuò)太多,就是少了一些東西,導(dǎo)致原代碼里的括號(hào)完全沒有意義,也就是說,書中的代碼雖然考慮到了計(jì)算表達(dá)式中的括號(hào),卻什么都沒有做,而這其實(shí)只要稍稍改進(jìn):加一個(gè)flag存儲(chǔ)上次讀到的char,如果是‘)’的話,就要把左式當(dāng)成運(yùn)算數(shù)來計(jì)算。

  好了,把正確的代碼貼在下面:

  #include <iostream>

  using namespace std;

  class calc

  {

  enum Type {DATA, ADD, SUB, MULTI, DIV, OPAREN, CPAREN, EOL};

  struct node

  {

  Type type;

  int data;

  node *lchild, *rchild;

  node(Type t, int d=0, node *lc=NULL, node *rc=NULL)

  {

  type=t; data=d; lchild=lc; rchild=rc;

  }

  };

  node *root;

  node *create(char * &s);

  Type getToken (char * &s, int &value);

  int result (node *t);

  public:

  calc (char *s) {root=create(s);}

  int result() {if (root==NULL) return 0;

  return result(root);}

  };

  calc::node *calc::create(char * &s)

  {

  node *p, *root=NULL;

  Type returnType,flag=DATA;

  int value;

  while (*s)

  {

  flag=returnType;

  returnType=getToken(s,value);

  switch (returnType)

  {

  case DATA:

  case OPAREN:

  if (returnType == DATA) p=new node(DATA,value);

  else p=create(s);

  if (root==NULL) root=p;

  else if (root->rchild==NULL) root->rchild=p;

  else root->rchild->rchild=p;

  break;

  case CPAREN:

  case EOL: return root;

  case ADD:

  case SUB:

  root=new node(returnType,0,root);

  break;

  case MULTI:

  case DIV:

  if (root->type==DATA || root->type==MULTI || root->type==DIV || flag==OPAREN)

  root=new node(returnType,0,root);

  else root->rchild=new node(returnType,0,root->rchild);

  }

  }

  return root;

  }

  calc::Type calc::getToken(char *&s, int &data)

  {

  char type;

  while (*s==' ') ++s;

  if (*s>='0' && *s<='9')

  {

  data=0;

  while (*s>='0' && *s<='9') {data=data*10+ *s-'0'; ++s;}

  return DATA;

  }

  if (*s == '/0') return EOL;

  type =*s; ++s;

  switch(type)

  {

  case '+':return ADD;

  case '-':return SUB;

  case '*':return MULTI;

  case '/':return DIV;

  case '(':return OPAREN;

  case ')':return CPAREN;

  default: return EOL;

  }

  }

  int calc::result(node *t)

  {

  int num1,num2;

  if (t->type == DATA) return t->data;

  num1=result(t->lchild);

  num2=result(t->rchild);

  switch(t->type)

  {

  case ADD:t->data=num1+num2;break;

  case SUB:t->data=num1-num2;break;

  case MULTI: t->data=num1*num2;break;

  case DIV:t->data=num1/num2;break;

  }

  return t->data;

  }

  int main()

  {

  char equation[256];

  cin>>equation;

  //calc exp("3*(2+(6/2))");

  calc exp(equation);

  cout<<exp.result()<<endl;

  return 0;

  }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通渭县| 内黄县| 苏尼特右旗| 镇雄县| 沂南县| 上高县| 霍城县| 铜鼓县| 衡南县| 瑞金市| 贵定县| 哈巴河县| 阿城市| 昌乐县| 华蓥市| 永平县| 酒泉市| 怀来县| 渝北区| 昭通市| 绩溪县| 汾阳市| 灵山县| 兖州市| 黎川县| 正蓝旗| 治多县| 同心县| 根河市| 麟游县| 靖江市| 渑池县| 青铜峡市| 卢龙县| 金湖县| 滦平县| 博白县| 乌拉特后旗| 敦化市| 若羌县| 神木县|