LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 588|回复: 2

在linux为什么会运行错误?而在windows正常

[复制链接]
发表于 2004-5-25 17:47:44 | 显示全部楼层 |阅读模式
在linux下.运行时,"请问根结点是否有左子树?(Y/N):"和"请问根结点是否有右子树?(Y/N):"会同时出现,前面的根本没有输入.为什么?难道一个程序中不能出现两三个getchar或scanf??

  1. #include <stdio.h>

  2. typedef struct btnode
  3. {
  4.   int data;
  5.   struct btnode *lchild;
  6.   struct btnode *rchild;
  7. } bitreptr;

  8. bitreptr* create(bitreptr* root);

  9. bitreptr* createHead()
  10. {
  11.   bitreptr *root;
  12.   int input;
  13.   char choose;
  14.   root = (bitreptr *) malloc (sizeof(bitreptr));
  15.   root -> lchild = NULL;
  16.   root -> rchild = NULL;
  17.   printf("输入根结点的数据:\t\t");
  18.   scanf("%d", &input);
  19.   root -> data = input;
  20.   printf("请问根结点是否有左子树?(Y/N):\t");
  21.   fflush(stdin);
  22.   choose=getchar();
  23.   if(choose=='Y' || choose=='y')
  24.    root->lchild = create(root->lchild);
  25.   printf("请问根结点是否有右子树?(Y/N):\t");
  26.   fflush(stdin);
  27.   choose=getchar();
  28.   if(choose=='Y' || choose=='y')
  29.    root -> rchild = create(root->rchild);

  30.   return root;
  31. }

  32. bitreptr* create(bitreptr* root)
  33. {
  34.   int input;
  35.   char choose;
  36.   root = (bitreptr *) malloc (sizeof(bitreptr));
  37.   root -> lchild = NULL;
  38.   root -> rchild = NULL;
  39.   printf("输入当前结点内的数据:\t\t");
  40.   scanf("%d", &input);
  41.   root -> data = input;
  42.   printf("请问当前结点是否有左子树?(Y/N):\t");
  43.   fflush(stdin);
  44.   choose=getchar();
  45.   if(choose=='Y' || choose=='y')
  46.    root->lchild = create(root->lchild);
  47.   printf("请问当前结点是否有右子树?(Y/N):\t");
  48.   fflush(stdin);
  49.   choose=getchar();
  50.   if(choose=='Y' || choose=='y')
  51.    root -> rchild = create(root->rchild);
  52.   return (root);
  53. }

  54. void outputTree(bitreptr* pbnode,int totalSpace)
  55. {int i;
  56. if(pbnode!=NULL)
  57. {
  58.   totalSpace+=5;
  59.   outputTree(pbnode->rchild,totalSpace);
  60.   for(i=0;i<totalSpace;i++) printf(" ");
  61.   printf("%d\n",pbnode->data);
  62.   outputTree(pbnode->lchild,totalSpace);
  63. }
  64. }

  65. void preorder (bitreptr *root)
  66. {
  67.   if (root == NULL)
  68.     return;
  69.   printf("%d ", root -> data);
  70.   preorder(root -> lchild);
  71.   preorder(root -> rchild);
  72. }

  73. void inorder (bitreptr *root)
  74. {
  75.   if (root == NULL)
  76.     return;
  77.   inorder(root -> lchild);
  78.   printf("%d ", root -> data);
  79.   inorder(root -> rchild);
  80. }

  81. void postorder (bitreptr *root)
  82. {
  83.   if (root == NULL)
  84.     return;
  85.   postorder(root -> lchild);
  86.   postorder(root -> rchild);
  87.   printf("%d ", root -> data);
  88. }

  89. int max(int a, int b)
  90. {
  91. if(a>=b) return a;
  92. else return b;
  93. }

  94. int depth_bintree (bitreptr *root)
  95. {
  96.   int depth, depthl, depthr;
  97.   if (root == NULL)
  98.     depth = 0;
  99.   else
  100.     {
  101.       depthl = depth_bintree(root -> lchild);
  102.       depthr = depth_bintree(root -> rchild);
  103.       depth = (max(depthl, depthr) + 1);
  104.     }
  105.   return (depth);
  106. }

  107. int countleaf (bitreptr *root)
  108. {
  109.   static int count=0;
  110.   if ((root -> lchild == NULL) && (root -> rchild == NULL))
  111.       count++;
  112.   else if((root -> lchild != NULL) && (root -> rchild == NULL))
  113.    countleaf(root -> lchild);
  114.   else if((root -> lchild == NULL) && (root -> rchild != NULL))
  115.       countleaf(root -> rchild);
  116.   else if((root -> lchild != NULL) && (root -> rchild != NULL))
  117.   {
  118.    countleaf(root->lchild);
  119.    countleaf(root->rchild);
  120.   }
  121.   return (count);
  122. }
  123. int countall(bitreptr *root)
  124. {
  125. static int count=0;
  126. if((root -> lchild == NULL) && (root -> rchild == NULL))
  127.   count++;
  128. else if((root -> lchild != NULL) && (root -> rchild == NULL))
  129. {
  130.   count++;
  131.   countall(root -> lchild);
  132. }
  133. else if((root -> lchild == NULL) && (root -> rchild != NULL))
  134. {
  135.   count++;
  136.   countall(root -> rchild);
  137. }
  138. else if((root -> lchild != NULL) && (root -> rchild != NULL))
  139. {
  140.   count++;
  141.   countall(root->lchild);
  142.   countall(root->rchild);
  143. }
  144. return count;
  145. }

  146. int main ()
  147. {
  148.   bitreptr *root;
  149.   int leaves,depth;
  150.   int allnodes;
  151.   int totalSpace=0;
  152.   root = createHead();
  153.   printf("我们构造的这棵二叉树的直观图为:\n");
  154.   outputTree(root,totalSpace);
  155.   printf("\n先根遍历:\n");
  156.   preorder(root);
  157.   printf("\n中根遍历:\n");
  158.   inorder(root);
  159.   printf("\n后根遍历:\n");
  160.   postorder(root);
  161.   depth = depth_bintree(root);
  162.   printf("\n树的深度为:\t%d",depth);
  163.   leaves = countleaf(root);
  164.   printf("\n叶子总数为:\t%d", leaves);
  165.   allnodes =countall(root);
  166.   printf("\n总的结点数目为:\t%d\n",allnodes);
  167. }
复制代码
发表于 2004-5-25 20:14:16 | 显示全部楼层
这是因为unix的终端是行缓冲的。
http://www.linuxsir.cn/forum.php ... =%D0%D0%BB%BA%B3%E5
发表于 2004-5-26 21:51:19 | 显示全部楼层

ft..

fflush(stdin),你刷新stdin流做什么??
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表