二叉树表达规定形式的代数表达式并计算表达式结果(无明显缺陷版本)
生活随笔
收集整理的這篇文章主要介紹了
二叉树表达规定形式的代数表达式并计算表达式结果(无明显缺陷版本)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <stdio.h>
//仔細(xì)分析有代表性的實(shí)例,發(fā)現(xiàn)規(guī)律.
typedef struct node????????// 結(jié)點(diǎn)類型定義????
{????
??float data;????
??bool sign;
????????struct node *lchild;????
????????struct node *rchild;????
}BTNode;????
BTNode *create(char *str,int n)????????// 創(chuàng)建代數(shù)表達(dá)式對(duì)應(yīng)的二叉樹????
{????
??BTNode *root = NULL;???? //始終指向當(dāng)前已經(jīng)生成的二叉樹的根
????????BTNode *p = NULL;???????????? //始終指向當(dāng)前待處理結(jié)點(diǎn)
????????float ch;????
????????int j = 0;????
????????ch = str[j];????
??while(ch != '\0' && j < n)????
??{????
????if(ch == '0' || ch == '1' ||ch == '2' ||ch == '3'||ch == '4' ||
??????ch == '5' || ch == '6' || ch == '7' || ch == '8'|| ch == '9')????
????{????
????????
??????p = new BTNode;????
????????????????????????p->data = ch;????
????????????????????????p->sign = false;
??????if(root == NULL)????????
????????root = p;????
??????else if(root->rchild == NULL)????????
????????root->rchild = p;????
??????else????????
????????root->rchild->rchild=p;????????
????}
????????else if(ch == '+' || ch == '-')
????????{
??????p = new BTNode;????
??????p->data = ch;????
??????p->sign = true;
??????p->rchild = NULL;????
??????p->lchild = root;????
??????root = p;????
??????root->rchild = NULL;????
????????}
????????else if(ch == '*' || ch == '/')
????????{
??????if(root->data >= '0' && root->data <= '9' || root->data == '*' || root->data == '/')????
??????{????????
????????p=new BTNode;????
????????p->data = ch;????
????????p->sign = true;
????????p->rchild = NULL;????
????????p->lchild = root;????
????????root = p;????
??????}????
??????else????
??????{????
????????p = new BTNode;????
????????p->data = ch;????
????????p->sign = true;
????????p->lchild = root->rchild;????
????????root->rchild = p;????
????????p->rchild = NULL;????
??????}????
????????}????
????????j++;????
????????ch=str[j];????
??}????
??return root;????
}????
float result(BTNode *b) //計(jì)算表達(dá)式????
{????
????float n1,n2;????
????if(b->data >= '0' && b->data <= '9')????
????????return (b->data - '0');????
????n1 = result(b->lchild);//計(jì)算左子表達(dá)式
????n2 = result(b->rchild);//計(jì)算右子表達(dá)式????
????
????if (b->sign == true && b->data == '+')
???? b->data = n1 + n2;
????else if (b->sign == true && b->data == '-')
???? b->data = n1 - n2;????
????else if (b->sign == true && b->data == '*')
???? b->data = n1 * n2;????????
????else if (b->sign == true && b->data == '/')
???? b->data = n1 / n2;
????return b->data;????
}????
void main()????
{????
??while(true)
??{
????BTNode static *b=NULL,*q=NULL;????
????char str[100];
????printf("請輸入符合要求的代數(shù)表達(dá)式:");????
????scanf("%s",str);????
????int n=0,i=0;????
????while(str[i++] != '\0')????
??????n++;????
????b = create(str,n);????
????printf("該表達(dá)式的值為:%f\n", result(b));????
??}
}
//仔細(xì)分析有代表性的實(shí)例,發(fā)現(xiàn)規(guī)律.
typedef struct node????????// 結(jié)點(diǎn)類型定義????
{????
??float data;????
??bool sign;
????????struct node *lchild;????
????????struct node *rchild;????
}BTNode;????
BTNode *create(char *str,int n)????????// 創(chuàng)建代數(shù)表達(dá)式對(duì)應(yīng)的二叉樹????
{????
??BTNode *root = NULL;???? //始終指向當(dāng)前已經(jīng)生成的二叉樹的根
????????BTNode *p = NULL;???????????? //始終指向當(dāng)前待處理結(jié)點(diǎn)
????????float ch;????
????????int j = 0;????
????????ch = str[j];????
??while(ch != '\0' && j < n)????
??{????
????if(ch == '0' || ch == '1' ||ch == '2' ||ch == '3'||ch == '4' ||
??????ch == '5' || ch == '6' || ch == '7' || ch == '8'|| ch == '9')????
????{????
????????
??????p = new BTNode;????
????????????????????????p->data = ch;????
????????????????????????p->sign = false;
??????if(root == NULL)????????
????????root = p;????
??????else if(root->rchild == NULL)????????
????????root->rchild = p;????
??????else????????
????????root->rchild->rchild=p;????????
????}
????????else if(ch == '+' || ch == '-')
????????{
??????p = new BTNode;????
??????p->data = ch;????
??????p->sign = true;
??????p->rchild = NULL;????
??????p->lchild = root;????
??????root = p;????
??????root->rchild = NULL;????
????????}
????????else if(ch == '*' || ch == '/')
????????{
??????if(root->data >= '0' && root->data <= '9' || root->data == '*' || root->data == '/')????
??????{????????
????????p=new BTNode;????
????????p->data = ch;????
????????p->sign = true;
????????p->rchild = NULL;????
????????p->lchild = root;????
????????root = p;????
??????}????
??????else????
??????{????
????????p = new BTNode;????
????????p->data = ch;????
????????p->sign = true;
????????p->lchild = root->rchild;????
????????root->rchild = p;????
????????p->rchild = NULL;????
??????}????
????????}????
????????j++;????
????????ch=str[j];????
??}????
??return root;????
}????
float result(BTNode *b) //計(jì)算表達(dá)式????
{????
????float n1,n2;????
????if(b->data >= '0' && b->data <= '9')????
????????return (b->data - '0');????
????n1 = result(b->lchild);//計(jì)算左子表達(dá)式
????n2 = result(b->rchild);//計(jì)算右子表達(dá)式????
????
????if (b->sign == true && b->data == '+')
???? b->data = n1 + n2;
????else if (b->sign == true && b->data == '-')
???? b->data = n1 - n2;????
????else if (b->sign == true && b->data == '*')
???? b->data = n1 * n2;????????
????else if (b->sign == true && b->data == '/')
???? b->data = n1 / n2;
????return b->data;????
}????
void main()????
{????
??while(true)
??{
????BTNode static *b=NULL,*q=NULL;????
????char str[100];
????printf("請輸入符合要求的代數(shù)表達(dá)式:");????
????scanf("%s",str);????
????int n=0,i=0;????
????while(str[i++] != '\0')????
??????n++;????
????b = create(str,n);????
????printf("該表達(dá)式的值為:%f\n", result(b));????
??}
}
轉(zhuǎn)載于:https://blog.51cto.com/howin45/315936
總結(jié)
以上是生活随笔為你收集整理的二叉树表达规定形式的代数表达式并计算表达式结果(无明显缺陷版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SharePoint 2007讨论板(D
- 下一篇: 发布一个C#实现的Asterisk的管理