c小问题,跪求跪求

qzha274 2009-09-01 05:47:04
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

int lowestBid;

// A Tree is a pointer to its root node
typedef struct treeRep *Tree;
typedef struct treeRep Node;

// A Tree is simply a pointer to its root node
// What we need to define here is the Node structure
struct treeRep {
int bid;
bool unique;
Node *left;
Node *right;
};

// Private function to create new Nodes
Node *newNode(int bid)
{
Node *n;
n = malloc(sizeof(struct treeRep));
assert(n != NULL);
n->bid = bid;
n->left = NULL;
n->right = NULL;
n->unique = true;
return n;
}

// Add a new bid into a Tree
Tree treeInsert(Tree t, int bid)
{
if (t == NULL){
t = newNode(bid);
if(bid < lowestBid){
lowestBid = bid;
printf("new lowest unique bid!");
} else {
printf("unique but not lowest");
}
}else {
int diff = bid - t->bid;

if (diff == 0){
t->unique = false; // already in Tree
printf("not unique");
}else if (diff < 0)
t->left = treeInsert(t->left, bid);
else if (diff > 0)
t->right = treeInsert(t->right, bid);
}

return t;
}

int main(int argc, char **argv)
{
int bid;
Tree t = NULL;

while(scanf("%d",&bid) == 1){
printf("Bid of %d",bid);
treeInsert(t,bid);
printf(" [best bid: %d]\n",lowestBid);
}


exit(0);
}
-bash-3.2$ gcc -Wall -Werror -o lowestUniqueBid lowestUniqueBid.c
lowestUniqueBid.c:17: error: expected specifier-qualifier-list before 'bool'
lowestUniqueBid.c: In function 'newNode':
lowestUniqueBid.c:29: error: 'Node' has no member named 'left'
lowestUniqueBid.c:30: error: 'Node' has no member named 'right'
lowestUniqueBid.c:31: error: 'Node' has no member named 'unique'
lowestUniqueBid.c:31: error: 'true' undeclared (first use in this function)
lowestUniqueBid.c:31: error: (Each undeclared identifier is reported only once
lowestUniqueBid.c:31: error: for each function it appears in.)
lowestUniqueBid.c: In function 'treeInsert':
lowestUniqueBid.c:50: error: 'struct treeRep' has no member named 'unique'
lowestUniqueBid.c:50: error: 'false' undeclared (first use in this function)
lowestUniqueBid.c:53: error: 'struct treeRep' has no member named 'left'
lowestUniqueBid.c:53: error: 'struct treeRep' has no member named 'left'
lowestUniqueBid.c:55: error: 'struct treeRep' has no member named 'right'
lowestUniqueBid.c:55: error: 'struct treeRep' has no member named 'right'
-bash-3.2$
...全文
1025 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wissup 2009-09-02
  • 打赏
  • 举报
回复
另:在完整的结构定义出现之前,允许typedef struct treeRep Node;这样的不完整声明,所以这里语法没有错误。
wissup 2009-09-02
  • 打赏
  • 举报
回复
首先:ANSI C中没有bool这个关键字的,那是C++里边有的的,不过C99里有个_Bool;

其次,你把bool--->int,,true / false---> 1 / 0,后,在VC++ 2008 编译器下编译通过了。
qzha274 2009-09-01
  • 打赏
  • 举报
回复
我暴汗
c里没bool这个type?
xx的太久没写了,还是Eclipse好用啊要是有智能提示早就看出问题了
不知名小ITer 2009-09-01
  • 打赏
  • 举报
回复
这么多明显的错误,自己尝试着改改错误呗!
写代码,改错误,必须的!
asimay 2009-09-01
  • 打赏
  • 举报
回复

typedef struct treeRep Node;
struct Node {
int bid;
bool unique;
Node *left;
Node *right;
}Node,*Tree;


sdd0124 2009-09-01
  • 打赏
  • 举报
回复
学习了。
jackyjkchen 2009-09-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qzha274 的回复:]
引用 1 楼 jackyjkchen 的回复:
Node的定义呢?

typedef struct treeRep Node; 这样不行吗?
[/Quote]
定义要在引用之前……
asimay 2009-09-01
  • 打赏
  • 举报
回复
颠倒感觉也不妥,你还是整合到一块吧。
qzha274 2009-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jackyjkchen 的回复:]
Node的定义呢?
[/Quote]
typedef struct treeRep Node; 这样不行吗?
asimay 2009-09-01
  • 打赏
  • 举报
回复

// A Tree is simply a pointer to its root node
// What we need to define here is the Node structure
struct treeRep {
int bid;
bool unique;
Node *left;
Node *right;
};

// A Tree is a pointer to its root node
typedef struct treeRep *Tree;
typedef struct treeRep Node;


颠倒一下
jackyjkchen 2009-09-01
  • 打赏
  • 举报
回复
Node的定义呢?

69,382

社区成员

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

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