佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
查看: 1410|回复: 9

Assessment 问题 ! 请帮帮忙 !

[复制链接]
发表于 26-7-2009 08:14 AM | 显示全部楼层 |阅读模式
Write a C program to read a list of books from a file     and create a linked list to store them. The purpose of this program is to     keep track of the books kept in a shelf. The shelf is a three-tier shelf     and each tier can store up to 4500 pages maximum. The books are kept in     alphabetical order (ascending).
Please     refer to the following image:-
               
Your programshould provide the following functions:-
    • Print the detail of all books.
    • Insert new book to the shelf in alphabetical order.      However, if the first-tier is full, you must put the book into the second      tier.
    • Remove a book from the list.
    • Search for a book using the book title, return the tier      where the book is placed.
    • Generate warning if the shelf is 75% full.
    • Store all the books information back into a file      for future use.

以上是问题。

以下是我的 code。


  1. #include
  2. #include
  3. #include

  4. int sum=0, sum2=0, sum3=0, x=0, y=0, z=0, i=0;

  5. typedef struct string{
  6.         char title[30];
  7. }StrinG;

  8. typedef struct bk{
  9.         char title[30];
  10.         int pages;
  11.         struct bk *next;
  12. }BooK;


  13. // Functions
  14. BooK* searchBooK (BooK* header, BooK **pred, BooK **cur, StrinG title); // To find book

  15. BooK *deleteBooK (BooK *header, BooK *pred, BooK *cur);                 // To delete book

  16. BooK *checkNdelete (BooK *header);                                      

  17. void tierset(int pages);

  18. void locatePosition(BooK* header,BooK** pred,BooK** cur, StrinG title);  // To locate the position for the book (ascending order)

  19. BooK* insertBooK(BooK* header,BooK* pred, BooK* cur, StrinG title, int pages);

  20. BooK* buildFrFile(BooK *header);

  21. void printDetail (BooK *header);                                        // To display all the books

  22. void saveIntoFile(BooK* header);

  23. void displayOption();


  24. // Main function
  25. int main(){
  26.         BooK *header=NULL, *pred=NULL, *cur=NULL;
  27.         bool permission=1;
  28.         StrinG title;
  29.         int option, pages, n=0;
  30.        
  31.         header=buildFrFile(header);
  32.        
  33.         displayOption();
  34.        
  35.         while(permission){
  36.                 if(n > 2)
  37.                         printf("\nPlease enter 0 to back to the option.\n");

  38.                     printf("Please select the option : ");
  39.                         scanf("%d",&option);
  40.                        
  41.     switch(option){

  42.     case 0: displayOption();
  43.                 n=0;
  44.                 break;

  45.     case 1: printDetail(header);
  46.                 break;

  47.         case 2: printf("Please enter the title of the book :\n");
  48.                 scanf("\n%[^\n]", title.title);
  49.                 i=1;
  50.                 searchBooK (header, &pred, &cur, title);
  51.                 printf("\n\n%d\n\n",i);
  52.                 system("PAUSE");
  53.                
  54.                 if( (i >= y) && ((z == 0)||(i < z)) ){
  55.                 printf("The book is located at the 2nd tier.\n\n");
  56.                 }else if(i >= z && z > 0){
  57.                 printf("The book is located at the 3rd tier.\n\n");
  58.                 }else{
  59.                 printf("the book is located at the 1st tier.\n\n");
  60.                 }
  61.                 i=1;
  62.                 break;
  63.        
  64.         case 3: printf("Please enter the title of the book :\n");
  65.                 scanf("\n%[^\n]", title.title);
  66.                 printf("The total pages of the book : ");
  67.                 scanf("%d",&pages);
  68.                 i=0;
  69.                 header=insertBooK(header, pred, cur, title, pages);
  70.                 printf("The book is successfully inserted.\n\n");
  71.                
  72.                 if( i <= y){
  73.                         y++;
  74.                         z++;
  75.                 }else if( i < z ){                   // To change the 1st tier's book number
  76.                         z++;
  77.                 }
  78.                 break;
  79.        
  80.         case 4: i = 1;
  81.                 checkNdelete(header);
  82.                 if( i <= y){
  83.                         y--;
  84.                         z--;
  85.                 }else if( i < z ){                   // To change the 1st tier's book number
  86.                         z--;
  87.                 }
  88.                 i=1;
  89.                 break;
  90.        
  91.         case 5: printf("The total pages used : %d \n", sum+sum2+sum3);
  92.                 break;
  93.        
  94.         case 6: saveIntoFile(header);
  95.                 break;
  96.        
  97.         case 7: saveIntoFile(header);
  98.        
  99.         case 8: permission=0;
  100.        
  101.         case 9: printf("\n\n%d\t%d\n\n",y,z);    // To jote down the rank of the 1st tier's book
  102.         }
  103.         n++;
  104.         }
  105.        
  106.         return 0;
  107. }


  108. BooK* searchBooK (BooK* header, BooK **pred, BooK **cur, StrinG title){   // To find book
  109.         *pred = NULL;
  110.         *cur = header;
  111.        
  112.         while(*cur != NULL){
  113.                 if(strcmpi(title.title , (*cur)->title) == 0){
  114.                         return *cur;
  115.                 }

  116.                 *pred = *cur;
  117.                
  118.                 *cur = (*cur)->next;
  119.                 i++;
  120.         }
  121.        
  122.         return NULL;
  123. }


  124. BooK *deleteBooK (BooK *header, BooK *pred, BooK *cur){
  125.         if(pred == NULL){
  126.                 header = cur->next;
  127.         }else
  128.                 pred->next = cur->next;
  129.         free (cur);

  130.         return header;
  131. }


  132. BooK *checkNdelete (BooK *header){
  133.         StrinG title;
  134.         BooK *cur=NULL, *pred=NULL;
  135.         int i = 1;
  136.        
  137.         printf(" Please enter the title of the book that you wish to delete : ");
  138.         scanf("\n%[^\n]",title.title);
  139.        
  140.         if( (searchBooK(header, &pred, &cur, title)) == NULL ){
  141.                 printf("Unable to search the title of the book.\n");
  142.         }else{
  143.                 printf("The book has been deleted\n\n");
  144.                 header = deleteBooK (header, pred, cur);
  145.         }
  146.        
  147.         return header;
  148. }


  149. void tierset(int pages){
  150.         sum+=pages;
  151.        
  152.         if(sum3 > 0){
  153.                 sum3+=pages;
  154.                 sum-=pages;
  155.         }else if(sum2 > 0){
  156.                 sum2+=pages;
  157.                 sum-=pages;
  158.         }
  159.        
  160.         if(sum2 > 4500){
  161.                 sum2-=pages;
  162.                 sum3+=pages;
  163.                 z=x+1;
  164.         }else if(sum>4500){
  165.                 sum-=pages;
  166.                 sum2+=pages;
  167.                 y=x+1;
  168.                
  169.                 if( (sum + sum2 + sum3) > 10125){
  170.                         printf("Exceeded 75% of the space!\n");
  171.                 }else if( (sum + sum2 + sum3) >= 13000){
  172.                         printf("The 3 tiers' slots are FULL!\n");
  173.                         printf("Please remove a book!\n");
  174.                 }
  175.         }
  176. }


  177. void locatePosition(BooK* header,BooK** pred,BooK** cur, StrinG title){
  178.         *cur = header;
  179.         *pred = NULL;
  180.        
  181.         while(*cur){
  182.                 if (strcmpi((*cur)->title , title.title) > 0){
  183.                         return;
  184.                 }
  185.                
  186.                 *pred=*cur;
  187.                 *cur=(*cur)->next;
  188.                 i++;
  189.         }
  190.        
  191.         return;
  192. }


  193. BooK* insertBooK(BooK* header,BooK* pred, BooK* cur, StrinG title, int pages){
  194.         BooK* newBooK = (BooK*)malloc(sizeof(BooK));           // To allocate new struct book
  195.         strcpy(newBooK->title , title.title);                  // To enter title into latest created struct
  196.         newBooK->pages = pages;                                // To enter pages
  197.         newBooK->next = NULL;                                  // To predefine NULL (to avoid error accured)
  198.        
  199.         tierset(pages);
  200.        
  201.         locatePosition(header, &pred, &cur, title);            // To obtain the ascending position
  202.        
  203.         if (pred == NULL){                                     // If it is totally no data
  204.                 newBooK->next = header;                            // To enter as the 1st node
  205.                 header = newBooK;
  206.         }else{
  207.                 newBooK->next = cur;                               // To enter between pred and cur
  208.                 pred->next = newBooK;
  209.         }
  210.         x++;

  211.         return header;
  212. }


  213. BooK* buildFrFile(BooK *header){
  214.         StrinG title;
  215.         FILE *rack;
  216.         int pages;
  217.         BooK *cur=NULL, *pred=NULL;
  218.        
  219.         if(! (rack = fopen("testfile.txt","r")) ){
  220.                 printf("Error in opening file\n");
  221.                 exit(210);
  222.         }
  223.        
  224.         while( (fscanf(rack,"%[^\n]",title.title)) != EOF){
  225.                 fscanf(rack,"%d\n\n",&pages);
  226.                 header=insertBooK(header, pred, cur, title, pages);
  227.         }
  228.        
  229.         return header;
  230. }


  231. void printDetail (BooK *header){
  232.         BooK* mover=header;
  233.         int i=1;
  234.        
  235.         printf("\nThe book rack has :\n");
  236.        
  237.         while (mover){
  238.                 printf("Book %d : %s\n",i, mover->title);
  239.                 printf("%d Pages\n", mover->pages);
  240.                 mover = mover->next;                 // To move a node forward
  241.                 i++;
  242.         }
  243.         printf("\n");
  244. }


  245. void saveIntoFile(BooK* header){
  246.         FILE *rack;
  247.         BooK *mover=header;
  248.        
  249.         if(! (rack = fopen("filetesting.txt","w")) ){
  250.                 printf("Error in opening file\n");
  251.                 exit(210);
  252.         }
  253.        
  254.         while (mover){
  255.                 fprintf(rack,"%s\n", mover->title);
  256.                 fprintf(rack,"%d\n", mover->pages);
  257.                 mover = mover->next;
  258.         }
  259. }


  260. void displayOption(){
  261.         printf("[1]. Display the books stored\n");
  262.         printf("[2]. Search book\n");
  263.         printf("[3]. Enter new book\n");
  264.         printf("[4]. Delete book\n");
  265.         printf("[5]. View total pages used\n");
  266.         printf("[6]. Save into file and continue.\n");
  267.         printf("[7]. Save into file and exit.\n");
  268.         printf("[8]. Exit without save\n");
  269. }
复制代码



我compile后有14个error
找到一整天还是找不到 error。

以下是 error details。

1>.\beta1.c(43) : error C2065: 'bool' : undeclared identifier
1>.\beta1.c(43) : error C2146: syntax error : missing ';' before identifier 'permission'
1>.\beta1.c(43) : error C2065: 'permission' : undeclared identifier
1>.\beta1.c(44) : error C2275: 'StrinG' : illegal use of this type as an expression
1>        .\beta1.c(9) : see declaration of 'StrinG'
1>.\beta1.c(44) : error C2146: syntax error : missing ';' before identifier 'title'
1>.\beta1.c(44) : error C2065: 'title' : undeclared identifier
1>.\beta1.c(45) : error C2143: syntax error : missing ';' before 'type'
1>.\beta1.c(52) : error C2065: 'n' : undeclared identifier
1>.\beta1.c(56) : error C2065: 'option' : undeclared identifier
1>.\beta1.c(68) : error C2224: left of '.title' must have struct/union type
1>.\beta1.c(70) : error C2440: 'function' : cannot convert from 'int' to 'StrinG'
1>.\beta1.c(70) : warning C4024: 'searchBooK' : different types for formal and actual parameter 4
1>.\beta1.c(85) : error C2224: left of '.title' must have struct/union type
1>.\beta1.c(87) : error C2065: 'pages' : undeclared identifier
1>.\beta1.c(89) : error C2440: 'function' : cannot convert from 'int' to 'StrinG'
1>.\beta1.c(89) : warning C4024: 'insertBooK' : different types for formal and actual parameter 4
1>.\beta1.c(229) : warning C4996: 'strcpy' was declared deprecated
1>        Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'


所以请各位高手帮帮忙哦。
查出 error 在哪里?
也可以 suggest 有更好/适合的 coding
小的在此感激不尽。


p/s:  任何疑问也可以直接 pm 我 msn

yip_han88@hotmail.com

谢谢

[ 本帖最后由 Nickeolosophy 于 26-7-2009 08:22 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 26-7-2009 08:24 AM | 显示全部楼层
因为技术问题

前3个的 include 是

stdio.h
stdlib.h
string.h
回复

使用道具 举报

发表于 26-7-2009 08:32 AM | 显示全部楼层
我弄了整晚,刚刚搞掂,现在要去睡觉了。
祝你好运。
回复

使用道具 举报

 楼主| 发表于 26-7-2009 08:55 AM | 显示全部楼层
原帖由 龍卷春 于 26-7-2009 08:32 AM 发表
我弄了整晚,刚刚搞掂,现在要去睡觉了。
祝你好运。


你也是做一样的吗?
教教我嘛!
pm  我可以吗 ?
回复

使用道具 举报

发表于 26-7-2009 05:33 PM | 显示全部楼层
你所犯的错误都是基础中的基础,初级中的初级。重新再看清楚你的error message,里面已经说得很清楚了。
回复

使用道具 举报

发表于 26-7-2009 05:43 PM | 显示全部楼层
这次真的的很难下,其实难是难在 function。
回复

使用道具 举报

Follow Us
发表于 28-7-2009 09:54 PM | 显示全部楼层
樓主, 你寫 Code 都是醬全部寫完才 Compile/Debug 的麼?
純屬好奇, 通常我只有在很有信心的情況下才這麼做
回复

使用道具 举报

发表于 29-7-2009 08:31 PM | 显示全部楼层
原帖由 eddom 于 28-7-2009 09:54 PM 发表
樓主, 你寫 Code 都是醬全部寫完才 Compile/Debug 的麼?
純屬好奇, 通常我只有在很有信心的情況下才這麼做


写一半就compile来干吗?
先画好界线,分配好工作,设下骨架,最后才填肉嘛
回复

使用道具 举报


ADVERTISEMENT

发表于 29-7-2009 10:36 PM | 显示全部楼层
原帖由 yeenfei 于 29-7-2009 08:31 PM 发表 写一半就compile来干吗?先画好界线,分配好工作,设下骨架,最后才填肉嘛

所以我的意思是說,填肉的時候都會填一塊compile一塊喽 , 少code還好, 多的時候不是要debug到半死?
回复

使用道具 举报

发表于 29-7-2009 10:36 PM | 显示全部楼层
原帖由 yeenfei 于 29-7-2009 08:31 PM 发表 写一半就compile来干吗?先画好界线,分配好工作,设下骨架,最后才填肉嘛

所以我的意思是說,填肉的時候都會填一塊compile一塊喽 , 少code還好, 多的時候不是要debug到半死?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT


本周最热论坛帖子本周最热论坛帖子

ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2026 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 2-5-2026 03:24 AM , Processed in 0.070491 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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