首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c prime plus 的一个题解

2013-03-22 
c prime plus 的一个例题本帖最后由 jamme007 于 2013-03-15 02:15:20 编辑终于学到第17章了, 由于代码太

c prime plus 的一个例题
本帖最后由 jamme007 于 2013-03-15 02:15:20 编辑 终于学到第17章了, 由于代码太多 这里就问一下 有这本书的前辈们吧
 大概496-498页 由例题17.3 17.4 17.5共同编译成的一个链表程序

 代码都输入进去后 发现 总提示错误
cannot convert 'Node*' to 'Node* const*'for argument '1' to 'bool ListIsFull(Node* const*)'
cannot convert 'Node*' to 'Node* const*'for argument '1' to 'bool ListIsEmpty(Node* const*)'
cannot convert 'Node*' to 'Node* const*'for argument '1' to 'void Traverse(Node* const*,void(*)(Item))'
cannot convert 'Node*' to 'Node* const*'for argument '1' to 'unsigned int ListItemCount(Node* const*)'
郁闷 仔细检查过 没有发现错打字了 ,, 哪位前辈告诉我一下是例题错了,还是我错了

例题中 有个list.h 内容的片段之一 是 
struct film
{
    char title[TSIZE];
    int rating;
}
typedef struct filmn Item;
typedef struct node
{
    Item item;
    struct node *next;
}Node;
typedef Node *List;
void InitializeList(List *plist);
bool ListIsEmpty(const List *plist);
bool ListIsFull(const List *plist);
这里ListIsFull的函数是这样写的 
bool ListIsFull(const List *plist)
{ Node *pt;
  bool full;
  pt=(Node *)malloc(sizeof(Node));
  if(pt==NULL)
     full=true;
  else
     full=false;
  free(pt);
  return full;
}
回到主程序films3.cpp看的话 就这些行总提示错误
if(ListIsFull(movies))
{  
   puts("the list is now full.");
  break;
}
发现 凡是带bool的函数都提示错误 。。 大侠们 请指教呀 

后面附上三个 源代码 list.h list.cpp films3.cpp
list.h

#ifndef LIST_H_
#define LIST_H_
#include "stdbool.h"
#define TSIZE 45

struct film
{
char title[TSIZE];
int rating;
};
typedef struct film Item;

typedef struct node
{
Item item;
struct node *next;
}Node;
typedef Node *List;
void InitializeList(List *plist);
bool ListIsEmpty(const List *plist);
bool ListIsFull(const List *plist);
unsigned int ListItemCount(const List *plist);
bool AddItem(Item item,List *plist);
void Traverse(const List *plist,void(*pfun)(Item item));
void EmptyTheList(List *plist);

#endif

list.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "list.h"
static void CopyToNode(Item item,Node *pnode);

void InitializeList(List *plist)
{
*plist=NULL;
}
bool ListIsEmpty(const List *plist)
{
if(*plist==NULL)
return true;
else
return false;
}
bool ListIsFull(const List *plist)
{
Node *pt;
bool full;
pt=(Node *)malloc(sizeof(Node));
if(pt==NULL)
full=true;
else
full=false;
free(pt);
return full;
}
unsigned int ListItemCount(const List *plist)


{
unsigned int count=0;
Node *pnode=*plist;

while(pnode!=NULL)
{
++count;
pnode=pnode->next;
}
return count;
}
bool AddItem(Item item,List *plist)
{
Node *pnew;
Node *scan=*plist;
pnew=(Node *)malloc(sizeof(Node));
if(pnew==NULL)
return false;

CopyToNode(item,pnew);
pnew->next=NULL;
if(scanf==NULL)
*plist=pnew;
else
{
while(scan->next!=NULL)
scan=scan->next;
scan->next=pnew;
}
return true;
}
void Traverse(const List *plist,void(*pfun)(Item item))
{
Node *pnode=*plist;
while(pnode!=NULL)
{
(*pfun)(pnode->item);
pnode=pnode->next;
}
}
void EmptyTheList(List *plist)
{
Node *psave;
while(*plist!=NULL)
{
psave=(*plist)->next;
free(*plist);
*plist=psave;
}
}
static void CopyToNode(Item item,Node *pnode)
{
pnode->item=item;
}


films3.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "list.h"
void showmovies(Item item);

int main(int argc, char *argv[])
{
List movies;
Item temp;

InitializeList(&movies);
if(ListIsFull(movies))
{
fprintf(stderr,"No memory available!bye!\n");
exit(1);
}
puts("enter first movie title:");
while(gets(temp.title)!=NULL&&temp.title[0]!='\0')
{
puts("enter ur rating <0-10>:");
scanf("%d",&temp.rating);
while(getchar()!='\n')
continue;
if(AddItem(temp,&movies)==false)
{
fprintf(stderr,"problem allocating memory\n");
break;
}
if(ListIsFull(movies))
{
puts("the list is now full.");
break;
}
puts("enter next movies title(empty line to stop");


}
if(ListIsEmpty(movies))
printf("no data entered.");
else
{
printf("here is the movie list:\n");
Traverse(movies,showmovies);
}
printf("you entered %d movies.\n",ListItemCount(movies));
EmptyTheList(&movies);
printf("bye!\n");

return 0;

}
void showmovies(Item item)
{
printf("movie:%s rating:%d\n",item.title,
item.rating);
}

[解决办法]
主要是films3.cpp中有许多应该传入&movies的地方,直接误用movies了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "list.h"
void showmovies(Item item);
 
int main(int argc, char *argv[])
{
    List movies;
    Item temp;
     
    InitializeList(&movies);
    if(ListIsFull(&movies))
    {
        fprintf(stderr,"No memory available!bye!\n");
        exit(1);
    }
    puts("enter first movie title:");


    while(gets(temp.title)!=NULL&&temp.title[0]!='\0')
    {
        puts("enter ur rating <0-10>:");
        scanf("%d",&temp.rating);
        while(getchar()!='\n')
            continue;
        if(AddItem(temp,&movies)==false)
        {
            fprintf(stderr,"problem allocating memory\n");
            break;
        }
        if(ListIsFull(&movies))
        {
            puts("the list is now full.");
            break;
        }
        puts("enter next movies title(empty line to stop");
         
         
    }
    if(ListIsEmpty(&movies))
        printf("no data entered.");
    else
    {
        printf("here is the movie list:\n");
        Traverse(&movies,showmovies);
    }
    printf("you entered %d movies.\n",ListItemCount(&movies));
    EmptyTheList(&movies);
    printf("bye!\n");
     
    return 0;
 
}
void showmovies(Item item)
{
    printf("movie:%s rating:%d\n",item.title,
            item.rating);
}

热点排行