C语言:书店管理系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C语言:书店管理系统
#include "stdio.h"
#include <string.h>
#define MAXNUM 1000
typedef struct
{char isbn[10]; /*书号*/
char bkname[20]; /*书名*/
char author[10]; /*作者*/
float price; /*单价*/
int num; /*数量(单位:册)*/
} BOOK;
int bknum;
BOOK bk[MAXNUM];
rdata()
{
FILE *fp;
if ((fp=fopen("e:\\lth019\\bkstore\\bookdata.txt","rb"))==NULL)
{printf("Can not open the file!\n");return;}
fread(&bknum,sizeof(int),1,fp);
fread(bk,sizeof(BOOK),bknum,fp);
fclose(fp);
}
bklist()
{int i;
clrscr();
printf(" Isbn bkname author price num\n"); printf("----------------------------------------------------------------\n");
for(i=0;i<bknum;i++)
putout();
printf("-------------------------BOOK LIST END---------------------------\n");
printf("Press any key to return...");getch();
}
void wdata()
{
FILE *fp;
if ( (fp=fopen("e:\\lth019\\bkstore\\bookdata.txt","wb"))==NULL) {printf("Can not open the file!\n");return;}
fwrite(&bknum , sizeof(int) , 1 , fp);
fwrite(bk, sizeof(BOOK), bknum, fp);
fclose(fp);
}
void bookin()
{
char s[10],bn[20],au[10];
int i,nn;
float pp;
printf("input isbn(000 to end input):");
scanf("%s",s);
while(strcmp(s,"000")!=0)
{
if ((i=found(s))<bknum)
{ printf("Have found!\n");
putout();
printf("input the number:");
scanf("%d",&nn);
bk[i].num+=nn;
{
putout();
getch();
}
printf("input isbn(000 to end input):");
scanf("%s",s);
}
else
{
printf("A new book input,\nPlease input bkname,author,price,num\n");
strcpy(bk[i].isbn,s);
scanf("%s%s",bn,au);
strcpy(bk[i].bkname,bn);
strcpy(bk[i].author,au);
scanf("%f%d",&pp,&nn );
bk[i].price=pp;
bk[i].num=nn;
bknum++;
{
putout();
getch();
}
printf("input isbn(000 to end input):");
scanf("%s",s);break;}
}
}
showsold(BOOK sold[],int n)
{int i;
printf("\n\n\n -------------The Books are selling-----------------\n");
for(i=0;i<n;i++)
printf(" %-10s%-20s%-10s%-8.2f%-8d\n",
sold[i].isbn,sold[i].bkname,sold[i].author,sold[i].price,sold[i].num);
}
int found(char isbn[])
{int i;
for(i=0;i<bknum;i++)
if(strcmp(isbn,bk[i].isbn)==0) break;
return i;
}
void booksale()
{ char isbn[10];
BOOK sold[100];
int soldnum=0,i;
float total=0;
clrscr();
printf("Please input isbn(000 to end input):");
scanf("%s",isbn);
while(strcmp(isbn,"000")!=0)
{
if((i=found(isbn))<bknum)
if(bk[i].num>0)
sold[soldnum++]=bk[i],bk[i].num--;
else
{printf("\Not enough!Pressany key to continue...");getch();} else
{printf("\Not enough!Pressany key to continue...");getch();}
clrscr();
for(i=0,total=0;i<soldnum;i++)
total+=sold[i].price;
if(soldnum)
{ showsold(sold,soldnum);
printf(" ----------------------------------------------\n");
printf("\n The total %8.2f\n\n\n",total);
}
printf("\n input isbn(000 to end input):");
scanf("%s",isbn);
}
}
void search()
{ char s[10]; char found; char isbn[20];
char num[10]; char au[20]; char name[20];
int t,i;
printf("=================\n");
printf(" 1.search with isbn \n");
printf(" 2.search with author \n");
printf(" 3.search with bookname \n");
printf("=================\n");
printf(" please chose one way : ");
scanf("%d",&t);
if(t==1)
{ printf(" please input isbn : ");
scanf("%s",&s);
for(found=0,i=0 ; i<bknum ; i++)
if(strcmp(s,bk[i].isbn)==0)
{printf("Have found!\n");
putout();
found++;
}
else continue;
if(!found) printf("Not found!");
getch();
}
if(t==2)
{ printf(" please input author: ");
scanf("%s",au);
for(found=0,i=0;i<bknum;i++)
if(strcmp(au,bk[i].author)==0)
{
printf("Have found!\n");
putout();
found++;
}
else continue;
if(!found) printf("Not found!");
getch();
}
if(t==3)
{ printf(" please input bookname: ");
scanf("%s",name);
for(found=0,i=0;i<bknum;i++)
if(strcmp(bk[i].bkname,name)==0)
{
printf("Have found!\n");
putout();
found++;
}
else continue;
if(!found) printf("Not found!");
getch();
}}
Delete()
{ char s[10],t;
int found=0,i,j;
printf("please input isbn of the book:");scanf("%s",s);
for(i=0 ; i<bknum ; i++)
if( strcmp(s,bk[i].isbn)==0 )
break;
if (i<bknum)
{ found=1;
printf("Found!\n");
putout();
printf("Are you sure to delete?(Y/N):"); flushall(); scanf("%c",&t);
if(t=='y'||t=='Y')
{ for(;i<=bknum;i++)
bk[i]=bk[i+1];
}
bknum--;
}
else printf("This book did't delete!");getch();
}
putout()
{ int i;
printf(" %-10s%-20s%-10s%-8.2f%-8d\n",
bk[i].isbn,bk[i].bkname,bk[i].author,bk[i].price,bk[i].num); }
int showsel()
{int sel;
clrscr();
printf("--------------------------ManiMenus-------------------------\n\n"); printf(" 0. Exit\n");
printf(" 1. BookList\n");
printf(" 2. BookIn\n") ;
printf(" 3. BookSale\n");
printf(" 4. Save\n ");
printf(" 5. Delete\n");
printf(" 6. Search\n");
printf("\n------------------------------------------------------------\n"); printf(" Input the selection:");
scanf("%d",&sel);
return sel;
}
main()
{
int sel; char yes;
clrscr();
rdata();
sel=showsel();
while(1)
{ switch(sel){
case 0:printf("Are sure to Save?(Y/N) ");
flushall();
scanf("%c",&yes);
if(yes=='Y'||yes=='y') { wdata(); exit(0); }
exit(0);
case 1:bklist();break;
case 2:bookin();break;
case 3:booksale();break;
case 4:wdata();break;
case 5:Delete();break;
case 6:search();break;
default :break;
}
sel=showsel();
}
}。

相关文档
最新文档