|
查看: 1410|回复: 9
|
Assessment 问题 ! 请帮帮忙 !
[复制链接]
|
|
|
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。
- #include
- #include
- #include
- int sum=0, sum2=0, sum3=0, x=0, y=0, z=0, i=0;
- typedef struct string{
- char title[30];
- }StrinG;
- typedef struct bk{
- char title[30];
- int pages;
- struct bk *next;
- }BooK;
- // Functions
- BooK* searchBooK (BooK* header, BooK **pred, BooK **cur, StrinG title); // To find book
- BooK *deleteBooK (BooK *header, BooK *pred, BooK *cur); // To delete book
- BooK *checkNdelete (BooK *header);
- void tierset(int pages);
- void locatePosition(BooK* header,BooK** pred,BooK** cur, StrinG title); // To locate the position for the book (ascending order)
- BooK* insertBooK(BooK* header,BooK* pred, BooK* cur, StrinG title, int pages);
- BooK* buildFrFile(BooK *header);
- void printDetail (BooK *header); // To display all the books
- void saveIntoFile(BooK* header);
- void displayOption();
- // Main function
- int main(){
- BooK *header=NULL, *pred=NULL, *cur=NULL;
- bool permission=1;
- StrinG title;
- int option, pages, n=0;
-
- header=buildFrFile(header);
-
- displayOption();
-
- while(permission){
- if(n > 2)
- printf("\nPlease enter 0 to back to the option.\n");
- printf("Please select the option : ");
- scanf("%d",&option);
-
- switch(option){
- case 0: displayOption();
- n=0;
- break;
- case 1: printDetail(header);
- break;
- case 2: printf("Please enter the title of the book :\n");
- scanf("\n%[^\n]", title.title);
- i=1;
- searchBooK (header, &pred, &cur, title);
- printf("\n\n%d\n\n",i);
- system("PAUSE");
-
- if( (i >= y) && ((z == 0)||(i < z)) ){
- printf("The book is located at the 2nd tier.\n\n");
- }else if(i >= z && z > 0){
- printf("The book is located at the 3rd tier.\n\n");
- }else{
- printf("the book is located at the 1st tier.\n\n");
- }
- i=1;
- break;
-
- case 3: printf("Please enter the title of the book :\n");
- scanf("\n%[^\n]", title.title);
- printf("The total pages of the book : ");
- scanf("%d",&pages);
- i=0;
- header=insertBooK(header, pred, cur, title, pages);
- printf("The book is successfully inserted.\n\n");
-
- if( i <= y){
- y++;
- z++;
- }else if( i < z ){ // To change the 1st tier's book number
- z++;
- }
- break;
-
- case 4: i = 1;
- checkNdelete(header);
- if( i <= y){
- y--;
- z--;
- }else if( i < z ){ // To change the 1st tier's book number
- z--;
- }
- i=1;
- break;
-
- case 5: printf("The total pages used : %d \n", sum+sum2+sum3);
- break;
-
- case 6: saveIntoFile(header);
- break;
-
- case 7: saveIntoFile(header);
-
- case 8: permission=0;
-
- case 9: printf("\n\n%d\t%d\n\n",y,z); // To jote down the rank of the 1st tier's book
- }
- n++;
- }
-
- return 0;
- }
- BooK* searchBooK (BooK* header, BooK **pred, BooK **cur, StrinG title){ // To find book
- *pred = NULL;
- *cur = header;
-
- while(*cur != NULL){
- if(strcmpi(title.title , (*cur)->title) == 0){
- return *cur;
- }
- *pred = *cur;
-
- *cur = (*cur)->next;
- i++;
- }
-
- return NULL;
- }
- BooK *deleteBooK (BooK *header, BooK *pred, BooK *cur){
- if(pred == NULL){
- header = cur->next;
- }else
- pred->next = cur->next;
- free (cur);
- return header;
- }
- BooK *checkNdelete (BooK *header){
- StrinG title;
- BooK *cur=NULL, *pred=NULL;
- int i = 1;
-
- printf(" Please enter the title of the book that you wish to delete : ");
- scanf("\n%[^\n]",title.title);
-
- if( (searchBooK(header, &pred, &cur, title)) == NULL ){
- printf("Unable to search the title of the book.\n");
- }else{
- printf("The book has been deleted\n\n");
- header = deleteBooK (header, pred, cur);
- }
-
- return header;
- }
- void tierset(int pages){
- sum+=pages;
-
- if(sum3 > 0){
- sum3+=pages;
- sum-=pages;
- }else if(sum2 > 0){
- sum2+=pages;
- sum-=pages;
- }
-
- if(sum2 > 4500){
- sum2-=pages;
- sum3+=pages;
- z=x+1;
- }else if(sum>4500){
- sum-=pages;
- sum2+=pages;
- y=x+1;
-
- if( (sum + sum2 + sum3) > 10125){
- printf("Exceeded 75% of the space!\n");
- }else if( (sum + sum2 + sum3) >= 13000){
- printf("The 3 tiers' slots are FULL!\n");
- printf("Please remove a book!\n");
- }
- }
- }
- void locatePosition(BooK* header,BooK** pred,BooK** cur, StrinG title){
- *cur = header;
- *pred = NULL;
-
- while(*cur){
- if (strcmpi((*cur)->title , title.title) > 0){
- return;
- }
-
- *pred=*cur;
- *cur=(*cur)->next;
- i++;
- }
-
- return;
- }
- BooK* insertBooK(BooK* header,BooK* pred, BooK* cur, StrinG title, int pages){
- BooK* newBooK = (BooK*)malloc(sizeof(BooK)); // To allocate new struct book
- strcpy(newBooK->title , title.title); // To enter title into latest created struct
- newBooK->pages = pages; // To enter pages
- newBooK->next = NULL; // To predefine NULL (to avoid error accured)
-
- tierset(pages);
-
- locatePosition(header, &pred, &cur, title); // To obtain the ascending position
-
- if (pred == NULL){ // If it is totally no data
- newBooK->next = header; // To enter as the 1st node
- header = newBooK;
- }else{
- newBooK->next = cur; // To enter between pred and cur
- pred->next = newBooK;
- }
- x++;
- return header;
- }
- BooK* buildFrFile(BooK *header){
- StrinG title;
- FILE *rack;
- int pages;
- BooK *cur=NULL, *pred=NULL;
-
- if(! (rack = fopen("testfile.txt","r")) ){
- printf("Error in opening file\n");
- exit(210);
- }
-
- while( (fscanf(rack,"%[^\n]",title.title)) != EOF){
- fscanf(rack,"%d\n\n",&pages);
- header=insertBooK(header, pred, cur, title, pages);
- }
-
- return header;
- }
- void printDetail (BooK *header){
- BooK* mover=header;
- int i=1;
-
- printf("\nThe book rack has :\n");
-
- while (mover){
- printf("Book %d : %s\n",i, mover->title);
- printf("%d Pages\n", mover->pages);
- mover = mover->next; // To move a node forward
- i++;
- }
- printf("\n");
- }
- void saveIntoFile(BooK* header){
- FILE *rack;
- BooK *mover=header;
-
- if(! (rack = fopen("filetesting.txt","w")) ){
- printf("Error in opening file\n");
- exit(210);
- }
-
- while (mover){
- fprintf(rack,"%s\n", mover->title);
- fprintf(rack,"%d\n", mover->pages);
- mover = mover->next;
- }
- }
- void displayOption(){
- printf("[1]. Display the books stored\n");
- printf("[2]. Search book\n");
- printf("[3]. Enter new book\n");
- printf("[4]. Delete book\n");
- printf("[5]. View total pages used\n");
- printf("[6]. Save into file and continue.\n");
- printf("[7]. Save into file and exit.\n");
- printf("[8]. Exit without save\n");
- }
复制代码
我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 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 05:33 PM
|
显示全部楼层
|
你所犯的错误都是基础中的基础,初级中的初级。重新再看清楚你的error message,里面已经说得很清楚了。 |
|
|
|
|
|
|
|
|
|
|
发表于 26-7-2009 05:43 PM
|
显示全部楼层
这次真的的很难下,其实难是难在 function。 |
|
|
|
|
|
|
|
|
|
|
发表于 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来干吗?
先画好界线,分配好工作,设下骨架,最后才填肉嘛 |
|
|
|
|
|
|
|
|
|
|
发表于 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到半死? |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|