当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 4 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了
帖子发表于 : 2010-04-28 22:53 

注册: 2010-03-09 14:02
帖子: 64
送出感谢: 0 次
接收感谢: 0 次
我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了,得改什么
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
}TREE;
TREE *CreateBittree()
{
char ch;
TREE *b;
ch=getchar();
if(ch=='@') b=NULL;
else
{
b=(TREE *)malloc(size(TREE));
b->data=ch;
b->lchild=CreateBittree();
b->rchild=CreateBittree();
}
return b;
}
void re_PreOrder(TREE *tree)
{
if(tree!=NULL){
printf("%c",tree->data);
re_PreOrder(tree->lchild);
re_PreOrder(tree->rchild);
}
}
void re_MidOrder(TREE *tree){
if(tree!=NULL){
re_MidOrder(tree->lchild);
printf("%c",tree->data);
re_MidOrder(tree->rchild);
}
}
void re_PostOrder(TREE *tree){
if(tree!=NULL)
re_PostOrder(tree->lchild);
re_PostOrder(tree->rchild);
printf("%c",tree->data);
}
void main(){
TREE *b;
printf("用先序输入一个二叉树,Input the bittree,using @ replace the empty child tree\n");
b=CreataBittree();
printf("先序序列为:\n");
re_PreOrder(&b);
printf("中序序列为:\n");
re_MidOrder(&b);
printf("后序序列为:\n");
}

然后cc的错误
zouvi@zouvi-laptop:Desktop$ gcc vim.c
vim.c: In function ‘CreateBittree’:
vim.c:17: error: expected expression before ‘TREE’
vim.c: In function ‘main’:
vim.c:48: warning: assignment makes pointer from integer without a cast
vim.c:50: warning: passing argument 1 of ‘re_PreOrder’ from incompatible pointer type
vim.c:24: note: expected ‘struct TREE *’ but argument is of type ‘struct TREE **’
vim.c:52: warning: passing argument 1 of ‘re_MidOrder’ from incompatible pointer type
vim.c:32: note: expected ‘struct TREE *’ but argument is of type ‘struct TREE **’

晕死,今天刚从机房做完的数据结构实验,会到ubuntu下就不行了,倍受打击


_________________
ThinkPad T60 with ubuntu 10.10 and windows xp sp3


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了
帖子发表于 : 2010-04-29 6:50 
头像

注册: 2008-07-27 8:51
帖子: 717
地址: 广西玉林|广东深圳
系统: Ubuntu/Windows
送出感谢: 1
接收感谢: 3
写得错误一大堆 :em06
b=(TREE *)malloc(size(TREE)); // size 还是 sizeof ??

定义 CreateBittree
但下面引用 CreataBittree :em06 手误吧
……


_________________
走过去了也便有了路
http://www.ptpt52.com/


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了
帖子发表于 : 2010-04-29 13:32 

注册: 2010-03-09 14:02
帖子: 64
送出感谢: 0 次
接收感谢: 0 次
:em06
多谢提醒,错的不少,回来我是凭记忆些的,很多地方都错了,改后的程序如下
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
}TREE;
TREE *CreateBittree()
{
char ch;
TREE *b;
ch=getchar();
if(ch=='@') b=NULL;
else
{
b=(TREE *)malloc(sizeof(TREE));
b->data=ch;
b->lchild=CreateBittree();
b->rchild=CreateBittree();
}
return b;
}
void re_PreOrder(TREE *tree)
{
if(tree!=NULL){
printf("%c",tree->data);
re_PreOrder(tree->lchild);
re_PreOrder(tree->rchild);
}
}
void re_MidOrder(TREE *tree){
if(tree!=NULL){
re_MidOrder(tree->lchild);
printf("%c",tree->data);
re_MidOrder(tree->rchild);
}
}
void re_PostOrder(TREE *tree){
if(tree!=NULL)
re_PostOrder(tree->lchild);
re_PostOrder(tree->rchild);
printf("%c",tree->data);
}
void main(){
TREE *b;
printf("Input the bittree,using @ replace the empty child tree\n");
b=CreateBittree();
printf("\n先序序列为:");
re_PreOrder(b);
printf("\n中序序列为:");
re_MidOrder(b);
// printf("\n后序序列为:");
// re_PostOrder(b);
}

没有主函数的最后两行时没问题,运行结果也没问题,为什么加了后面两行后就有问题了呢?

这是没加入后两行后的显示结果:
zouvi@zouvi-laptop:Desktop$ gcc vim.c
zouvi@zouvi-laptop:Desktop$ ./a.out
Input the bittree,using @ replace the empty child tree
abd@e@@f@@cg@@@

先序序列为:abdefcg
中序序列为:debfagc
zouvi@zouvi-laptop:Desktop$

这是加入后的显示结果:
zouvi@zouvi-laptop:Desktop$ gcc vim.c
zouvi@zouvi-laptop:Desktop$ ./a.out
Input the bittree,using @ replace the empty child tree
abd@e@@f@@cg@@@

先序序列为:abdefcg
中序序列为:debfagc
段错误
zouvi@zouvi-laptop:Desktop$

试着用gbd调试(不是很会用)

zouvi@zouvi-laptop:Desktop$ gcc -g vim.c -o vim
zouvi@zouvi-laptop:Desktop$ gdb vim
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zouvi/Desktop/vim...done.
(gdb) break main
Breakpoint 1 at 0x80485be: file vim.c, line 47.
(gdb) break re_PostOrder
Breakpoint 2 at 0x8048580: file vim.c, line 40.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x080485be in main at vim.c:47
2 breakpoint keep y 0x08048580 in re_PostOrder at vim.c:40
(gdb) r
Starting program: /home/zouvi/Desktop/vim

Breakpoint 1, main () at vim.c:47
47 printf("Input the bittree,using @ replace the empty child tree\n");
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/zouvi/Desktop/vim

Breakpoint 1, main () at vim.c:47
47 printf("Input the bittree,using @ replace the empty child tree\n");
(gdb)
晕,又回来了,这个gdb该怎么用的,怎么看程序哪错了呢,这个程序错哪了呢?


_________________
ThinkPad T60 with ubuntu 10.10 and windows xp sp3


页首
 用户资料  
 
4 楼 
 文章标题 : Re: 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了
帖子发表于 : 2010-04-29 13:43 

注册: 2010-03-09 14:02
帖子: 64
送出感谢: 0 次
接收感谢: 0 次
结果是这样的
Breakpoint 2, main () at vim.c:47
47 printf("Input the bittree,using @ replace the empty child tree\n");
(gdb)
(gdb) n
Input the bittree,using @ replace the empty child tree
48 b=CreateBittree();
(gdb) n
abd@e@@f@@cg@@@
49 printf("\n先序序列为:");
(gdb) n

50 re_PreOrder(b);
(gdb) n
51 printf("\n中序序列为:");
(gdb) n
先序序列为:abdefcg
52 re_MidOrder(b);
(gdb) n
53 printf("\n后序序列为:");
(gdb) n
中序序列为:debfagc
54 re_PostOrder(b);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x804b008) at vim.c:40
40 if(tree!=NULL)
(gdb) n
41 re_PostOrder(tree->lchild);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x804b018) at vim.c:40
40 if(tree!=NULL)
(gdb) n
41 re_PostOrder(tree->lchild);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x804b028) at vim.c:40
40 if(tree!=NULL)
(gdb) n
41 re_PostOrder(tree->lchild);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x0) at vim.c:40
40 if(tree!=NULL)
(gdb) n
42 re_PostOrder(tree->rchild);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x08048597 in re_PostOrder (tree=0x0) at vim.c:42
42 re_PostOrder(tree->rchild);
(gdb)

最后的Program received signal SIGSEGV, Segmentation fault.
貌似re_PostOrder,这个程序写错了,改怎么改呢?


_________________
ThinkPad T60 with ubuntu 10.10 and windows xp sp3


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 4 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:Bing [Bot] 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译