| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 497 人关注过本帖
标题:运行试试,帮忙改个错吧,指针和结构
只看楼主 加入收藏
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:8 
运行试试,帮忙改个错吧,指针和结构
程序代码:
#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;
}

为什么不能修改呢;
图片附件: 游客没有浏览图片的权限,请 登录注册

该怎么修改?
2012-04-05 22:14
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
2012-04-05 22:40
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
因该是把文件里的内容读入结构时出错了
2012-04-05 22:46
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:3 
偶在犹豫中,是帮你改错呢?还是让你学习调试?
授人以渔,还是授人以鱼?

[ 本帖最后由 hellovfp 于 2012-4-6 16:00 编辑 ]

我们都在路上。。。。。
2012-04-06 15:59
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
回复 4楼 hellovfp
高数我怎么调试吧
2012-04-06 21:45
瓦力
Rank: 2
等 级:论坛游民
帖 子:41
专家分:27
注 册:2011-11-18
收藏
得分:0 
偶也是很爱学习滴
2012-04-06 21:45
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 

我们都在路上。。。。。
2012-04-07 10:34
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
读写文件的基础知识:看闲版的。
https://bbs.bccn.net/thread-360404-1-1.html
33楼 如何设计学生管理系统
34楼 写文件
35楼 读文件

我们都在路上。。。。。
2012-04-07 10:36
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:3 

授人以渔,不授人以鱼。
2012-04-07 13:15
快速回复:运行试试,帮忙改个错吧,指针和结构
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031766 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved