求C语言高手编程 急急急!!!!
用C语言编写一个程序实现如下功能:1,创建一个图书结构体book。结构体应包含以下几个成员:图书编号,图书名称,作者名,出版社,图书价格。个成员变量名称自定。2,编写函数...
用C语言编写一个程序 实现如下功能:
1,创建一个图书结构体book。结构体应包含以下几个成员:图书编号,图书名称,作者名,出版社,图书价格。个成员变量名称自定。
2,编写函数newlib,用于新建一个文件以保存若干条图书信息。
3,编写函数appendbook,用于在创建好的文件中新增一条图书记录,包括图书编号,书名,作者名,出版社,图书价格等信息。
4,编写函数delbook,用于删除指定图书编号的相应记录。
5,编写函数searchbook,用于查询指定图书编号或者指定书名的相应记录。
6,对图书进行排序,分别编写函数sortbyid和sortbyprice,将图书记录按照编号的升序进行排序,以及按图书的价格的升序进行排序。
急急急!!!!!!做的好有追加 展开
1,创建一个图书结构体book。结构体应包含以下几个成员:图书编号,图书名称,作者名,出版社,图书价格。个成员变量名称自定。
2,编写函数newlib,用于新建一个文件以保存若干条图书信息。
3,编写函数appendbook,用于在创建好的文件中新增一条图书记录,包括图书编号,书名,作者名,出版社,图书价格等信息。
4,编写函数delbook,用于删除指定图书编号的相应记录。
5,编写函数searchbook,用于查询指定图书编号或者指定书名的相应记录。
6,对图书进行排序,分别编写函数sortbyid和sortbyprice,将图书记录按照编号的升序进行排序,以及按图书的价格的升序进行排序。
急急急!!!!!!做的好有追加 展开
1个回答
展开全部
还未完成,要先睡觉了,明天继续:
再更新一遍,还没全写好
/****************************************
用C语言编写一个程序 实现如下功能:
1,创建一个图书结构体book。结构体应包含以下几个成员:图书编号,图书名称,作者名,出版社,图书价格。个成员变量名称自定。
2,编写函数newlib,用于新建一个文件以保存若干条图书信息。
3,编写函数appendbook,用于在创建好的文件中新增一条图书记录,包括图书编号,书名,作者名,出版社,图书价格等信息。
4,编写函数delbook,用于删除指定图书编号的相应记录。
5,编写函数searchbook,用于查询指定图书编号或者指定书名的相应记录。
6,对图书进行排序,分别编写函数sortbyid和sortbyprice,将图书记录按照编号的升序进行排序,以及按图书的价格的升序进行排序。 ****************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define ID_LEN 8
#define NAME_LEN 20
#define AUTHOR_LEN 12
#define CONCERN_LEN 20
#define PRICE_LEN 1
#define FNAME_LEN 20
typedef struct book
{
char *book_id;
char *book_name;
char *book_author;
char *book_concern;
int *book_price;
} BOOK;
FILE *newlib(char *fname);
BOOK *appendbook(FILE *fp);
void delbook(FILE *fp, char *fname, char *book_id);
void searchbook(char *book_id_or_name);
void sortbyid(FILE *);
void sortbyprice(FILE *);
void booklib_out(FILE *);
int main()
{
FILE *booklib;
char *lib_name;
BOOK *newbook,*book1;
/*********************************
ÒÔÏÂΪº¯Êý²âÊÔ´úÂë
*********************************/
//Test newlib()
booklib = newlib(lib_name);
//Test appendbook()
newbook = appendbook(booklib);
//Test delbook();
/* book1 = newbook;
delbook(booklib,lib_name,book1->book_id);
//Test searchbook();
searchbook(book1->book_id);
searchbook(book1->book_name);
//Test sortbyid();
sortbyid(booklib);
booklib_out(booklib);
//Test sortbyprice();
sortbyprice(booklib);
booklib_out(booklib);*/
return 0;
}
FILE *newlib(char *fname)
{
FILE *fp = NULL;
char ch;
unsigned int n,i;
BOOK *newbook;
fname = (char *)malloc(FNAME_LEN*sizeof(char));
newbook = (BOOK *)malloc(1*sizeof(BOOK));
newbook->book_id = (char *)malloc(ID_LEN*sizeof(char));
newbook->book_name = (char *)malloc(NAME_LEN*sizeof(char));
newbook->book_author = (char *)malloc(AUTHOR_LEN*sizeof(char));
newbook->book_concern = (char *)malloc(CONCERN_LEN*sizeof(char));
newbook->book_price = (int *)malloc(PRICE_LEN*sizeof(int));
printf("Please input the book_lib's name:");
scanf("%s",fname);
printf("\n%s\n",fname);
fp = fopen(fname,"w+");
if(!fp)
{
printf("Book lib create fail!");
exit(0);
}
printf("\nPlease input how many books in the lib:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
printf("\nPlease input the %dth book's information:\n",i+1);
printf("%38s","book_id:");
scanf("%s",newbook->book_id);
fprintf(fp,"%s ",newbook->book_id);
getchar();
printf("%38s","book_name:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_name);
fprintf(fp,"%s ",newbook->book_name);*/
printf("%38s","book_author:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_author);
fprintf(fp,"%s ",newbook->book_author);*/
printf("%38s","book_concern:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_concern);
fprintf(fp,"%s ",newbook->book_concern);*/
printf("%38s","book_price:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp,"\n");
/*
scanf("%d",newbook->book_price);
fprintf(fp,"%d\n",*newbook->book_price);*/
}
return fp;
}
BOOK *appendbook(FILE *fp)
{
char ch;
BOOK *newbook;
newbook = (BOOK *)malloc(1*sizeof(BOOK));
newbook->book_id = (char *)malloc(ID_LEN*sizeof(char));
newbook->book_name = (char *)malloc(NAME_LEN*sizeof(char));
newbook->book_author = (char *)malloc(AUTHOR_LEN*sizeof(char));
newbook->book_concern = (char *)malloc(CONCERN_LEN*sizeof(char));
newbook->book_price = (int *)malloc(PRICE_LEN*sizeof(int));
printf("\nPlease input the new book's information:\n");
printf("%38s","book_id:");
scanf("%s",newbook->book_id);
fprintf(fp,"%s ",newbook->book_id);
getchar();
printf("%38s","book_name:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_name);
fprintf(fp,"%s ",newbook->book_name);*/
printf("%38s","book_author:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_author);
fprintf(fp,"%s ",newbook->book_author);*/
printf("%38s","book_concern:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_concern);
fprintf(fp,"%s ",newbook->book_concern);*/
printf("%38s","book_price:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp,"\n");
/*
scanf("%d",newbook->book_price);
fprintf(fp,"%d\n",*newbook->book_price);*/
return newbook;
}
void delbook(FILE *fp, char *fname, char *book_id)
{
char *s,ch;
FILE *fp1;
rename(fname,"f_backup.txt");
fp1 = fopen(fname,"w");
while( (fscanf(fp,"%s",s) != EOF) && ((ch = getchar()) != EOF) )
{
if( !strcmp(s,book_id) )
{
while( getchar() != '\n' )
;
}
if(ch != '\n' )
fprintf(fp1,"%s ",s);
else
fprintf(fp1,"\n");
}
}
/*
void searchbook(char *book_id_or_name)
{}
void sortbyid(FILE *fp)
{}
void sortbyprice(FILE *fp)
{}
void booklib_out(FILE *fp)
{}*/
再更新一遍,还没全写好
/****************************************
用C语言编写一个程序 实现如下功能:
1,创建一个图书结构体book。结构体应包含以下几个成员:图书编号,图书名称,作者名,出版社,图书价格。个成员变量名称自定。
2,编写函数newlib,用于新建一个文件以保存若干条图书信息。
3,编写函数appendbook,用于在创建好的文件中新增一条图书记录,包括图书编号,书名,作者名,出版社,图书价格等信息。
4,编写函数delbook,用于删除指定图书编号的相应记录。
5,编写函数searchbook,用于查询指定图书编号或者指定书名的相应记录。
6,对图书进行排序,分别编写函数sortbyid和sortbyprice,将图书记录按照编号的升序进行排序,以及按图书的价格的升序进行排序。 ****************************************/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define ID_LEN 8
#define NAME_LEN 20
#define AUTHOR_LEN 12
#define CONCERN_LEN 20
#define PRICE_LEN 1
#define FNAME_LEN 20
typedef struct book
{
char *book_id;
char *book_name;
char *book_author;
char *book_concern;
int *book_price;
} BOOK;
FILE *newlib(char *fname);
BOOK *appendbook(FILE *fp);
void delbook(FILE *fp, char *fname, char *book_id);
void searchbook(char *book_id_or_name);
void sortbyid(FILE *);
void sortbyprice(FILE *);
void booklib_out(FILE *);
int main()
{
FILE *booklib;
char *lib_name;
BOOK *newbook,*book1;
/*********************************
ÒÔÏÂΪº¯Êý²âÊÔ´úÂë
*********************************/
//Test newlib()
booklib = newlib(lib_name);
//Test appendbook()
newbook = appendbook(booklib);
//Test delbook();
/* book1 = newbook;
delbook(booklib,lib_name,book1->book_id);
//Test searchbook();
searchbook(book1->book_id);
searchbook(book1->book_name);
//Test sortbyid();
sortbyid(booklib);
booklib_out(booklib);
//Test sortbyprice();
sortbyprice(booklib);
booklib_out(booklib);*/
return 0;
}
FILE *newlib(char *fname)
{
FILE *fp = NULL;
char ch;
unsigned int n,i;
BOOK *newbook;
fname = (char *)malloc(FNAME_LEN*sizeof(char));
newbook = (BOOK *)malloc(1*sizeof(BOOK));
newbook->book_id = (char *)malloc(ID_LEN*sizeof(char));
newbook->book_name = (char *)malloc(NAME_LEN*sizeof(char));
newbook->book_author = (char *)malloc(AUTHOR_LEN*sizeof(char));
newbook->book_concern = (char *)malloc(CONCERN_LEN*sizeof(char));
newbook->book_price = (int *)malloc(PRICE_LEN*sizeof(int));
printf("Please input the book_lib's name:");
scanf("%s",fname);
printf("\n%s\n",fname);
fp = fopen(fname,"w+");
if(!fp)
{
printf("Book lib create fail!");
exit(0);
}
printf("\nPlease input how many books in the lib:");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
printf("\nPlease input the %dth book's information:\n",i+1);
printf("%38s","book_id:");
scanf("%s",newbook->book_id);
fprintf(fp,"%s ",newbook->book_id);
getchar();
printf("%38s","book_name:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_name);
fprintf(fp,"%s ",newbook->book_name);*/
printf("%38s","book_author:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_author);
fprintf(fp,"%s ",newbook->book_author);*/
printf("%38s","book_concern:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_concern);
fprintf(fp,"%s ",newbook->book_concern);*/
printf("%38s","book_price:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp,"\n");
/*
scanf("%d",newbook->book_price);
fprintf(fp,"%d\n",*newbook->book_price);*/
}
return fp;
}
BOOK *appendbook(FILE *fp)
{
char ch;
BOOK *newbook;
newbook = (BOOK *)malloc(1*sizeof(BOOK));
newbook->book_id = (char *)malloc(ID_LEN*sizeof(char));
newbook->book_name = (char *)malloc(NAME_LEN*sizeof(char));
newbook->book_author = (char *)malloc(AUTHOR_LEN*sizeof(char));
newbook->book_concern = (char *)malloc(CONCERN_LEN*sizeof(char));
newbook->book_price = (int *)malloc(PRICE_LEN*sizeof(int));
printf("\nPlease input the new book's information:\n");
printf("%38s","book_id:");
scanf("%s",newbook->book_id);
fprintf(fp,"%s ",newbook->book_id);
getchar();
printf("%38s","book_name:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_name);
fprintf(fp,"%s ",newbook->book_name);*/
printf("%38s","book_author:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_author);
fprintf(fp,"%s ",newbook->book_author);*/
printf("%38s","book_concern:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp," ");
/*
scanf("%s",newbook->book_concern);
fprintf(fp,"%s ",newbook->book_concern);*/
printf("%38s","book_price:");
while( (ch = getchar())!='\n' )
fputc(ch,fp);
fprintf(fp,"\n");
/*
scanf("%d",newbook->book_price);
fprintf(fp,"%d\n",*newbook->book_price);*/
return newbook;
}
void delbook(FILE *fp, char *fname, char *book_id)
{
char *s,ch;
FILE *fp1;
rename(fname,"f_backup.txt");
fp1 = fopen(fname,"w");
while( (fscanf(fp,"%s",s) != EOF) && ((ch = getchar()) != EOF) )
{
if( !strcmp(s,book_id) )
{
while( getchar() != '\n' )
;
}
if(ch != '\n' )
fprintf(fp1,"%s ",s);
else
fprintf(fp1,"\n");
}
}
/*
void searchbook(char *book_id_or_name)
{}
void sortbyid(FILE *fp)
{}
void sortbyprice(FILE *fp)
{}
void booklib_out(FILE *fp)
{}*/
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询