请教各位高手,谢谢.

rex2000 2003-04-21 10:27:17
我用二叉树求表达式的计算,如: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++;

}

}
...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
diabloqin 2003-04-22
  • 打赏
  • 举报
回复
mark
zhaoao 2003-04-22
  • 打赏
  • 举报
回复
gz

70,033

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧