请教各位高手,谢谢.
我用二叉树求表达式的计算,如:11+12*(13-7)-14/5;
但是程序调试在初始化模块CreatBiTree(BiTree T)的时候,出现了死循环。请各位高手看一下,怎么解决。谢谢。
(以下是源代码)
#include "conio.h"
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
typedef struct {
float opnd;
char optr;
}Data;
typedef struct {
Data data;
int flag;
}TElemType;
typedef struct BTNode {
TelemType data;
struct BTNode *lchild,*rchild;
}*BiTree;
void CreatBiTree(BiTree &T);
void Make(BiTree &S,TElemType e);
float value(BiTree T);
void main()
{
int choise;
BiTree T;
start:
cout << " 1.Creat Binary Tree. " << endl;
cout << " 2.Traverse(LDR). " << endl;
cout << " 3.Traverse(DLR). " << endl;
cout << " 4.Traverse(LRD). " << endl;
cout << " 5.Calculate The Binary Tree." << endl;
cout << "choise:";
cin >> choise;
if (choise == 1)
{ CreatBiTree(T); goto start; }
if (choise == 2)
if (choise == 3)
if (choise == 4)
if (choise == 5)
{ value(T); goto start;}
}
void CreatBiTree(BiTree &T)
{
TElemType e; //initial
char str[10];
BiTree s;
static int i = 1;
cout << "Creat The No." << i << "TreeNode? (y/n)"; //input value
flushall();
cin >> str;
while (!( str[0] == 'y'|| str[0] == 'n')) //deal with the false condition
{
cout << "Input Wrong! Please input again:" ;
cin >> str;
}
if ( str[0] == 'y' )
{
cout << "\nPlease input value.";
flushall();
cin >> str;
if (str[0]=='+'||str[0]=='-'||str[0]=='*'||str[0]=='/')
{
e.flag = 1;
e.data.optr = str[0];
}
else
{ e.flag = 0;
e.data.opnd = atof(str);
}
Make(s,e);
T=s;
i++;
CreatBiTree( s -> lchild );
CreatBiTree( s -> rchild );
}
else
{
cout << " Do you want to continue?(y/n) " << endl;
cin >> str;
if ( str[0] == 'y' ) i++;
}
}