| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 314 人关注过本帖
标题:新手求助》大神帮忙!~最后保存的不行!~无法加载,就大神指教
只看楼主 加入收藏
a402581217
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-1-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
新手求助》大神帮忙!~最后保存的不行!~无法加载,就大神指教
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>

typedef struct
{
    int year;      //年
    int month;    //月
    int day;      //日
} Date;

typedef struct
{
    char bookname[40];        //图书名
    char isbn[20];          //ISBN号
    char bookid[20];          //书号
    Date publishdate;        //出版日期
    char publisher[20];     //出版社
    char authors[40];       //作者
    float price;         //价格
} Book;

typedef struct{
    int current;
    Book book[100];
}mlibrary;
mlibrary library;//存储图书信息
Book book[100];
int current = 0;

//int total = 0;

int insert()//录入图书函数
{
    system("cls");
    if(current>=100)
    {
        printf("录入的数据已满,不再录入\n");
        return 0;
    }
    printf("请输入图书名称:\t");
    scanf("%s",book[current].bookname);
    //gets(book[current].book_name);
    printf("请输入作者名字:\t");
    //gets("%s",book[current].authors);//
    scanf("%s",book[current].authors);
    printf("请输入出版日期(格式:年-月-日):\t");
    scanf("%4d-%2d-%2d",&book[current].publishdate.year,&book[current].publishdate.month,&book[current].publishdate.day);
    printf("请输入出版社:");
    scanf("%s",book[current].publisher);
    printf("请输入书号:\t");
    scanf("%s",book[current].bookid);
    printf("请输入ISBN号:\t");
    scanf("%s",book[current].isbn);
    printf("请输入价格¥:\t");
    scanf("%f",&book[current].price);
    printf("录入成功!\n");
    current++;
    return 0;
}

void list()//列出所有图书信息
{
    int j;
    printf("当前一共有%d本书\n",current);
    printf("       书名   作者  出版日期   出版社   书号     ISBN号   价格\n");
    for(j = 0;j<current;j++)
    {
        printf("%10s",book[j].bookname);
        printf("%7s",book[j].authors);
        printf("%6d-%d-%d",book[j].publishdate.year,book[j].publishdate.month,book[j].publishdate.day);
        printf("%8s",book[j].publisher);
        printf("%8s",book[j].bookid);
        printf("%8s",book[j].isbn);
        printf("       ¥:%f    ",book[j].price);
        printf("\n");
    }
}

int query()//查询图书函数
{
    char choice;
    char name[40];
    char authors[40];
    char publisher[20];
    char isbn[20];
    char bookid[20];

    int i;
    //显示程序基本信息
    //printf("        请选择查询关键字\n");

    while(1)
    {
        printf("\n");
        printf("                   请选择查询关键字\n");
        printf("                1:按图书名\n");
        printf("                2:按图书作者\n");
        printf("                3:按出版社\n");
        printf("                4:按ISBN号\n");
        printf("                5:按图书号\n");

        printf("\n");

        printf("        请选择:");
        fflush(stdin);
        choice = getch();
        printf("\n\n");

        switch(choice)
        {
            case '1':
                //insert();
                system("cls");
                printf("请输入您要查询的书的名字:\t");
                fflush(stdin);
                gets(name);

                for(i=0;i<current;i++)
                {
                    if( strcmp(book[i].bookname,name) == 0  )
                    {
                        //输出第i本书的信息。
                        printf("已找到该书。\n");
                        printf("       书名   作者  出版日期   出版社   书号     ISBN号   价格\n");
                        printf("%10s",book[i].bookname);
                        printf("%7s",book[i].authors);
                        printf("%6d-%d-%d",book[i].publishdate.year,book[i].publishdate.month,book[i].publishdate.day);
                        printf("%8s",book[i].publisher);
                        printf("%8s",book[i].bookid);
                        printf("%8s",book[i].isbn);
                        printf("       ¥:%f    ",book[i].price);
                        printf("\n");
                        return 1;
                    }
                }
                printf("无该书\n");
                return 0;
                break;

            case '2':
                system("cls");
                printf("请输入您要查询的作者名字:\t");
                fflush(stdin);
                gets(authors);

                for(i=0;i<current;i++)
                {
                    if( strcmp(book[i].authors,authors) == 0  )
                    {
                        //输出第i本书的信息。
                        printf("已找到该书。\n");
                        printf("       书名   作者  出版日期   出版社   书号     ISBN号   价格\n");
                        printf("%10s",book[i].bookname);
                        printf("%7s",book[i].authors);
                        printf("%6d-%d-%d",book[i].publishdate.year,book[i].publishdate.month,book[i].publishdate.day);
                        printf("%8s",book[i].publisher);
                        printf("%8s",book[i].bookid);
                        printf("%8s",book[i].isbn);
                        printf("       ¥:%f    ",book[i].price);
                        printf("\n");
                        return 1;
                    }
                }
                printf("无该书\n");
                return 0;

                 break;
            case '3':
                system("cls");
                printf("请输入您要查询的出版社:\t\n");
                fflush(stdin);
                gets(publisher);

                for(i=0;i<current;i++)
                {
                    if( strcmp(book[i].publisher,publisher) == 0  )
                    {
                        //输出第i本书的信息。
                        printf("已找到该书。\n");
                        printf("       书名   作者  出版日期   出版社   书号     ISBN号   价格\n");
                        printf("%10s",book[i].bookname);
                        printf("%7s",book[i].authors);
                        printf("%6d-%d-%d",book[i].publishdate.year,book[i].publishdate.month,book[i].publishdate.day);
                        printf("%8s",book[i].publisher);
                        printf("%8s",book[i].bookid);
                        printf("%8s",book[i].isbn);
                        printf("       ¥:%f    ",book[i].price);
                        printf("\n");
                        return 1;
                    }
                }
                printf("无该书\n");
                return 0;
        case '4':
                system("cls");
                printf("请输入您要查询的ISBN号查询:\t\n");
                fflush(stdin);
                gets(isbn);

                for(i=0;i<current;i++)
                {
                    if( strcmp(book[i].isbn,isbn) == 0  )
                    {
                        //输出第i本书的信息。
                        printf("已找到该书。\n");
                        printf("       书名   作者  出版日期   出版社   书号     ISBN号   价格\n");
                        printf("%10s",book[i].bookname);
                        printf("%7s",book[i].authors);
                        printf("%6d-%d-%d",book[i].publishdate.year,book[i].publishdate.month,book[i].publishdate.day);
                        printf("%8s",book[i].publisher);
                        printf("%8s",book[i].bookid);
                        printf("%8s",book[i].isbn);
                        printf("       ¥:%f    ",book[i].price);
                        printf("\n");
                        return 1;
                    }
                }
                printf("无该书\n");
                return 0;

                 break;
        case '5':
                system("cls");
                printf("请输入您要查询的图书号:\t\n");
                fflush(stdin);
                gets(bookid);

                for(i=0;i<current;i++)
                {
                    if( strcmp(book[i].bookid,bookid) == 0  )
                    {
                        //输出第i本书的信息。
                        printf("已找到该书。\n");
                        printf("       书名   作者  出版日期   出版社   书号     ISBN号   价格\n");
                        printf("%10s",book[i].bookname);
                        printf("%7s",book[i].authors);
                        printf("%6d-%d-%d",book[i].publishdate.year,book[i].publishdate.month,book[i].publishdate.day);
                        printf("%8s",book[i].publisher);
                        printf("%8s",book[i].bookid);
                        printf("%8s",book[i].isbn);
                        printf("       ¥:%f    ",book[i].price);
                        printf("\n");
                        return 1;
                    }
                }
                printf("无该书\n");
                return 0;

                 break;


            default:
                printf("非法选择,请重新选择\n");
        }
    }


    return 0;    //程序返回

}

int update()//修改图书信息
{
    int n;
    char name[40];
    printf("请输入需要修改的图书名称:\t");
    gets (name);
    fflush(stdin);
    for (n=0;n<=current;n++)
    {
        if(strcmp(book[n].bookname,name) ==0);     //判断是否修改的名字相同
        {
        printf("请输入新的图书名称:\t");
        scanf("%s",book[n].bookname);
        printf("请输入新的作者名字:\t");
        scanf("%s",book[n].authors);
        printf("请输入新的出版日期(格式:年-月-日):\t");
        scanf("%4d-%2d-%2d",&book[n].publishdate.year,&book[n].publishdate.month,&book[n].publishdate.day);
        printf("请输入新的出版社:\t");
        scanf("%s",book[n].publisher);
        printf("请输入新的书号:\t");
        scanf("%s",book[n].bookid);
        printf("请输入新的ISBN号:\t");
        scanf("%s",book[n].isbn);
        printf("请输入新的价格¥:\t");
        scanf("%f",&book[n].price);
        printf ("修改成功!\n");
        return 0;
        }
    }
    printf("该图书不存在。\n");
}


void del()//删除图书
{
        int i=0;
        system("cls");
        char bookname[40];
        printf("请输入您要删除的书的名字:");
        fflush(stdin);
        gets(bookname);

        for( i=0;i<current;i++)
            {
                if( strcmp(book[i].bookname,bookname) == 0  )
                {
                        //删除第i本书的信息。


                        int j;
                        for(j=i+1;j<current;j++)
                        {
                            book[j-1] = book[j];
                        }

                        current--;
                        printf("已删除该书。\n");

                        break;
                    }
                }
                printf("无该书\n");


}


void save()//保存结构数组到文件
{
    FILE *fp;
    if((fp=fopen("CC.doc","w"))==NULL)
    {
        printf("\n不能保存图书信息\n");
        return;
    }
    fwrite(&mlibrary.current,sizeof(int),1,fp);
    fwrite(mlibrary.book,sizeof(mlibrary.book[0]),mlibrary.current,fp);
}

void load()//从文件加载图书信息到结构数组
{
    FILE*fp;
    char ch;
    if((fp=fopen("CC.doc","rt"))==NULL)
    {
        printf("\n无法读取文件");
        getch();
        exit(1);
    }
    ch=fgetc(fp);
    while(ch!=EOF)
    {
        putchar(ch);
        ch=fgetc(fp);


    }
    fclose(fp);
}




int main()
{
    char choice;

    //显示程序基本信息
    printf(" \t\t     图书信息管理系统,版本1.0,版权所有2010-2020\n");

    while(1)
    {
        printf("\n");
        printf("  \t\t 程序功能:\n");
        printf("  \t\t\t\t--------  1:录入图书信息    --------\n");
        printf("  \t\t\t\t********  2:查询图书信息    ********\n");
        printf("  \t\t\t\t********  3:修改图书信息    ********\n");
        printf("  \t\t\t\t********  4:删除图书信息    ********\n");
        printf("  \t\t\t\t********  5:列出所有图书信息********\n");
        printf("  \t\t\t\t********  6:保存图书信息    ********\n");
        printf("  \t\t\t\t********  7:加载图书信息    ********\n");
        printf("  \t\t\t\t--------   0:退出程序       --------\n");
        printf("\n");

        printf("        请选择:");
        fflush(stdin);
        choice = getch();
        printf("\n\n");

        switch(choice)
        {
            case '1':
                insert();
                break;

            case '2':
                 query();
                 break;

            case '3':
                update();
                break;

            case '4':
                del();
                break;

            case '5':
                list();
                break;

            case '6':
                save();
                break;

            case '7':
                load();
                break;

            case '0':
                if (book != NULL)
                {
                    free(book);
                }
                return 0;

            default:
                printf("非法选择,请重新选择\n");
        }
    }

    return 0;    //程序返回
}
   希望高手指点下

[ 本帖最后由 a402581217 于 2014-1-14 13:12 编辑 ]
搜索更多相关主题的帖子: include 出版社 price 
2014-01-14 11:55
a402581217
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-1-13
收藏
得分:0 
大神!~快出现吧!~新手求助啊!~
2014-01-14 12:05
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
自己调试 分模块调试

DO IT YOURSELF !
2014-01-14 13:34
快速回复:新手求助》大神帮忙!~最后保存的不行!~无法加载,就大神指教
数据加载中...
 
   



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

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