用C语言设计一个图书管理系统(高手请进)
发布网友
发布时间:2022-04-30 23:28
我来回答
共3个回答
热心网友
时间:2022-06-20 10:17
我写的,基本符合你最基本的要求,不知道合适不合适,自己没有电脑,这些代码还是在网吧写的...在TC2.0调试通过的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 100
struct book
{
char booknum[8];
char bookname[15];
char author[12];
char PubHouse[15];
char PubTime[11];
}bookinfor[NUM];
char Initialization()/*界面初始化*/
{
char ch;
printf(" ********Library Management System********\n1.Show all the book information\n2.Add book information\n3.Delete book information\n4.Search book information\n5.Exit\nPlease select:");
do
{
ch=getch();
if(ch>='1'&&ch<='5')
{
printf("%c\n",ch);
getchar();
return ch;
}
}while(1);
}
void showall()/*显示所有*/
{
int i=0;
FILE *fp;
system("cls");
if((fp=fopen("C:\\bookinfo.txt","r"))==NULL)
{
printf("ERROR:cannot open file\n");
getchar();
return;
}
printf("booknum bookname author PubHouse Pubtime\n");
while(!feof(fp))
{fread(&bookinfor[i],sizeof(struct book),1,fp);
printf("%-8s %-15s %-12s %-15s %-11s\n",bookinfor[i].booknum,bookinfor[i].bookname,bookinfor[i].author,bookinfor[i].PubHouse,bookinfor[i].PubTime);
i++;
if(!i%20)
{
printf("Press any key to continue...");
getch();
}
}
fclose(fp);
printf("Press any key to return");
getch();
return;
}
void addinfor()/*增加数据*/
{
int i=0,b=0;
char ch;
FILE *fp;
do
{
system("cls");
printf("booknum:");
gets(bookinfor[i].booknum);
printf("bookname:");
gets(bookinfor[i].bookname);
printf("author:");
gets(bookinfor[i].author);
printf("PubHouse:");
gets(bookinfor[i].PubHouse);
printf("PubTime:");
gets(bookinfor[i].PubTime);
printf("Saved!Continue?(Y/N)");
do
{
ch=getch();
if(ch=='Y'||ch=='y'||ch=='N'||ch=='n')
{
printf("%c\n",ch);
getchar();
break;
}
}while(1);
i++;
}while(ch=='Y'||ch=='y');
if((fp=fopen("C:\\bookinfo.txt","a+"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
for(b=0;b<i;b++)
if(fwrite(&bookinfor[b],sizeof(struct book),1,fp)!=1)
printf("file write error\n");
fclose(fp);
return;
}
void delinfor()/*删除信息*/
{
int i=0,b=0,k;
char delnum[8];
FILE *fp;
system("cls");
printf("Enter the Delete Book Num:");
gets(delnum);
if((fp=fopen("C:\\bookinfo.txt","r"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
while(!feof(fp))
{
fread(&bookinfor[i],sizeof(struct book),1,fp);
i++;
}
fclose(fp);
for(b=0;b<i;b++)
if(!strcmp(delnum,bookinfor[b].booknum))
{
for(k=b;k<i-1;k++)
bookinfor[k]=bookinfor[k+1];
if((fp=fopen("C:\\bookinfo.txt","w"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
for(i=0;i<k;i++)
if(fwrite(&bookinfor[i],sizeof(struct book),1,fp)!=1)
printf("file write error\n");
fclose(fp);
printf("Delete!Press any key to return...");
getchar();
return;
}
printf("Not Found!Press any key to return...");
getchar();
return;
}
void searchinfor()/*查找信息*/
{
char ch;
char searchbook[20];
int i=0;
FILE *fp;
system("cls");
printf("search mode:\n1.By booknum\n2.By bookname\nChoose:");
do
{
ch=getch();
if(ch=='1'||ch=='2')
{
printf("%c\n",ch);
getchar();
break;
}
}while(1);
if((fp=fopen("C:\\bookinfo.txt","r"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
if(ch=='1')
{
printf("Enter the booknum:");
gets(searchbook);
printf("booknum bookname author PubHouse Pubtime\n");
while(!feof(fp))
{
fread(&bookinfor[i],sizeof(struct book),1,fp);
if(!strcmp(searchbook,bookinfor[i].booknum))
{
printf("%-8s %-20s %-15s %-25s %-10s\n",bookinfor[i].booknum,bookinfor[i].bookname,bookinfor[i].author,bookinfor[i].PubHouse,bookinfor[i].PubTime);
printf("Press any key to return...");
getchar();
fclose(fp);
return;
}
i++;
}
}
if(ch=='2')
{
printf("Enter the bookname:");
gets(searchbook);
printf("booknum bookname author PubHouse Pubtime\n");
while(!feof(fp))
{
fread(&bookinfor[i],sizeof(struct book),1,fp);
if(!strcmp(searchbook,bookinfor[i].bookname))
{
printf("%-8s %-20s %-15s %-25s %-10s\n",bookinfor[i].booknum,bookinfor[i].bookname,bookinfor[i].author,bookinfor[i].PubHouse,bookinfor[i].PubTime);
printf("Press any key to return...");
getchar();
fclose(fp);
return;
}
i++;
}
}
system("cls");
printf("Not Found!Press any key to return...");
fclose(fp);
getchar();
return;
}
void main()
{
char ch;
do
{
system("cls");
ch=Initialization();
if(ch=='1') showall();
if(ch=='2') addinfor();
if(ch=='3') delinfor();
if(ch=='4') searchinfor();
if(ch=='5') break;
}while(1);
system("cls");
printf("Thank you for your use!");
getchar();
}
热心网友
时间:2022-06-20 10:17
。。。
这给150块钱别人也不一定愿意做啊,主要是花时间
热心网友
时间:2022-06-20 10:18
有偿可以联系 看ID