运行试试,帮忙改个错吧,指针和结构
程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #define CHAR 20 //CHAR 输入名字的最大长度(包括图书名) //包含的图书信息 typedef struct book_ifo{ // int num; char writer[CHAR]; char name[CHAR]; double price; struct book_ifo *next; }BOOK; BOOK *book; int size = sizeof(BOOK); BOOK *creat(); //录入 void browse(); //浏览(将链表顺序输出)共用排序函数 BOOK *search(); //查询 void edit(int ); //删除与修改 BOOK *store(BOOK *head); //存储 BOOK *output(int choice); int check(int num); //检测 BOOK *Head(BOOK *); //存储头指针以便其它函数调用 void modify(int ); //修改 void del(); //删除 BOOK *read(); void add(); FILE *fp; //主函数 int main() { int choice; //声明变量 BOOK *head; printf("\n \t \t \t 欢迎您进入图书管理系统!\n\n"); do{ printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); printf("\t\t\t[1]:信息录入\t");printf("[3]:查询图书\n\n"); printf("\t\t\t[2]:浏览图书\t");printf("[4]:修改与删除\n\n"); printf("\t\t\t[5]:添加图书\n\n"); printf("\t\t\t〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n\n"); printf("\t\t\t0:退出\n\n"); printf("Enter your choice:"); //此处判断输入是否合法(未实现) scanf("%d",&choice); switch(choice) { //复合语句开始 case 1: //录入 if((fp = fopen("bookinfo.txt","r")) != NULL){ printf("File already has been created!\n"); printf("已进入添加图书选项\n"); add(); } else head = creat(); break; case 2: printf("浏览\n"); //浏览 browse(); break; case 3: printf("查询\n"); //查询 head = search(); break; case 4: printf("删除与修改\n"); //删除与修改 printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); printf("\t\t\t[1]修改 \t [2]删除\n\t\t\t[0]exit!\n\n"); printf("please enter your choice!\n"); scanf("%d",&choice); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! edit(choice); break; case 5: add(); break; case 0: printf("exit!\n"); break; default: printf("Please enter right choice 0--4:\n\t"); //输入错误提示 } }while(choice != 0); return 0; } //录入函数 BOOK *creat() { BOOK *head, *p, *tail; head = tail = NULL; int num; char writer[CHAR],name[CHAR]; double price; printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); printf("enter 0 end input!\n"); printf("please enter book's number:\n"); printf("\n\t\t\t********************************\n\n"); scanf("%d", &num); do{ p=(BOOK *)malloc(size); if(head == NULL) head = p; else tail->next = p; tail = p; p->next = NULL; p->num = num; printf("enter book's name:\n"); scanf("%s",name); strcpy(p->name,name); printf("enter writer's name:\n"); scanf("%s",writer); strcpy(p->writer,writer); printf("enter price:\n"); scanf("%lf",&price); printf("\n\t**************************\n\n"); p->price = price; printf("enter next book's number:\n"); scanf("%d",&num); }while(num != 0); store(head); //存储 free(p); //释放 return head; } //浏览函数 void browse() { int num; float price; char writer[CHAR], name[CHAR]; FILE *fp; printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); if((fp = fopen("bookinfo.txt","r")) == NULL){ printf("error: open fail!\n"); exit(0); } printf("\t\tnumber\t\t writer\t\t name\t\t price\n"); while(! feof(fp)) { fscanf(fp,"%d %s %s %f",&num,writer,name,&price); printf("\t\t%d\t\t %s\t\t %s\t\t %.2f\n", num, writer, name, price); }; if(fclose(fp)){ printf("close error!\n"); exit(0); } } //将文件内容读入结构用于便利,查找 BOOK *read() { BOOK *ptr, *head, *tail; head = tail = NULL; int num; char writer[CHAR], name[CHAR]; double price; if((fp = fopen("bookinfo.txt","r")) == NULL){ printf("open fail!\n"); exit(0); } while(! feof(fp)) { ptr=(BOOK *)malloc(size); if(head == NULL) head = ptr; else tail->next = ptr; tail = ptr; fscanf(fp,"%d %s %s %lf",&num, writer, name, &price); ptr->num = num; strcpy(ptr->writer, writer); strcpy(ptr->name, name); ptr->price = price; } tail = NULL; return head; } //查询函数 BOOK *search() { int choice, num; char writer[CHAR], name[CHAR]; printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); printf("\t\t\t请选择查询方式:\n"); printf("\t\t\t[1]number\t[2]name\t[3]writer\n\t\t\t[0]EXIT!\n"); scanf("%d",&choice); switch(choice) { case 1: printf("请输入图书编号:"); scanf("%d",&num); printf("按编号查找:\n"); break; case 2: printf("请输入书名:\n"); scanf("%s", name); printf("按书名查找:\n"); break; case 3: printf("请输入作者姓名:"); scanf("%s", writer); printf("按作者查找:\n"); break; default: printf("Please enter right choice 0--3:\n\t"); }; return 0; } //修改与删除菜单 !!!!!!!!!!!!!!!!!!!!!!!!待完成!!!!!!!!!!!!!!!!!! void edit(int choice) { int number; do{ switch(choice) { case 1: printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); printf("\t\t\t输入要修改的图书编号\n\n"); scanf("%d",&number); modify(number); //**************修改*************** break; case 2: printf("删除\n"); printf("\n\t\t\t〓〓〓〓〓图书管理系统〓〓〓〓〓\n\n"); printf("\t\t\t输入要删除的图书编号\n\n"); scanf("%d", &number); break; default: printf("Please enter right choice 0--2:\n\t"); }; }while(number != 0); } void modify(int number) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! { BOOK *ptr, *ptr1; int num, n = 1; char name[CHAR], writer[CHAR]; ptr = ptr1 = read(); do{ if(ptr->num == number){ printf("!"); printf("enter num:\n"); scanf("%d", &num); printf("enter name:\n"); scanf("%s", name); printf("enter writer:\n"); scanf("%s", writer); if ((fp = fopen("boohinfo.txt", "wb")) == NULL) { printf("cannot open file\n"); exit(1); } if(fwrite(ptr1, sizeof(struct book_ifo), 1, fp) != 1){ printf("write error!\n"); exit(1); } fclose(fp); } // else if(ptr == NULL) printf("\t无此图书!\n"); // else{ n++; ptr = ptr->next; // } }while(ptr == NULL); } BOOK *store(BOOK *head) //存储 { FILE *fp; BOOK *ptr, *p; p = head; if((fp = fopen("bookinfo.txt","w")) == NULL){ printf("File open error!\n"); exit(0); } //检测输入的信息是否已存在(书目num) for(ptr = head;ptr;ptr = ptr->next) fprintf(fp,"%d %s %s %6.2f\n",ptr->num,ptr->writer,ptr->name,ptr->price); if(fclose(fp)){ printf("Can not close the file!\n"); exit(0); } return head; } void add() { int num; char writer[CHAR], name[CHAR], ch[1]; double price; if((fp = fopen("bookinfo.txt","a+")) == NULL){ printf("open fail!\n"); exit(0); } else { printf("请输入新图书的编号:\n"); scanf("%d", &num); // check(num); printf("请输入新图书名:"); scanf("%s", name); printf("请输入新图书作者:"); scanf("%s", writer); printf("请输入新图书价格:"); scanf("%lf", &price); fprintf(fp, "%d %s %s %f", num, writer, name, price); } if(fclose(fp)){ printf("Can not close the file!\n"); exit(0); } printf("是否继续添加? (y/n)"); scanf("%s", ch); if(strcmp(ch, "y") == 0) //!!!!!!!!!!!!!!!!!!!!!!!!! add(); else{ main(); exit(0); } } int check(int num) { BOOK *ptr; for(ptr = read(); ptr != NULL; ptr = ptr->next){ if(ptr->num == num){ printf("编号为%d的图数已存在!\n\n", num); main(); exit(0); } } return 0; }
为什么不能修改呢;
该怎么修改?