You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct book{
char name[100];
char author[100];
char publisher[100];
char date[20];
double price;
int quantity;
}; //定义书类型的结构体
book aa[100]={{"周子越教你学高数","周子越","湖南工业大学出版社","2019.01.08",88,5000},{"失乐园","渡边淳一","文化艺术出版社","2005.04.01",24,100},{"C语言程序设计","刘强","电子工业出版社","2018.02.28",49,150},{"D语言程序设计","刘强","电子工业出版社","2018.02.28",49,150},{"E语言程序设计","刘强","电子工业出版社","2018.02.28",49,150}};//初始化五本书
int pp=6; //书的总数(控制循环次数)
FILE *fp;
void buy();
void start();
void print();
void update();
void print(){ //显示书籍存货清单函数
int i;
system("cls");
system("color E0");
fp=fopen("booklist.txt","r");
for(i=0;i<pp;i++)
{
fscanf(fp,"%s%s%s%s%lf%d",aa[i].name,aa[i].author,aa[i].publisher,aa[i].date,aa[i].price,aa[i].quantity);
printf("\n\t\t\t%d------《%s》\n",i+1,aa[i].name);
}
printf("按任意键回到主界面");
getch();
system("cls");
start();
}
void buy(){ //购买书籍函数
int i,k,s;
char name[100];
char author[100];
system("cls");
system("color 9F");
printf("\n\t\t\t\t请输入要查找的书名:");
scanf("%s",name);
for(i=0;i<pp;i++)
{
if(strcmp(aa[i].name,name)==0)
{
printf("\n\t\t\t\t书籍信息:\n\t\t\t书名:%s\n\t\t\t作者:%s\n\t\t\t出版社:%s\n\t\t\t出版日期:%s\n\t\t\t价格:%5.2lf\\\n\t\t\t库存:%d\n",aa[i].name,aa[i].author,aa[i].publisher,aa[i].date,aa[i].price,aa[i].quantity);
printf("\n\t\t\t\t请输入您需要购买的数量:");
scanf("%d",&s);
if(aa[i].quantity-s<0)
{
printf("所需数量不在库存范围内!");
printf("按任意键返回购买界面");
getch();
buy();
}
else
{
int j;
aa[i].quantity=aa[i].quantity-s;
fp = fopen("booklist.txt","w");
for(j=0;j<pp;j++)
{
fwrite(&aa[j],sizeof(book),1,fp);
}
fclose(fp);
}
printf("\n\t\t\t\t总价格为:%5.2lf元,感谢您的购买!\n\t\t\t按y继续购买其他书籍,按其它任意键回到主界面\n",aa[i].price*s);
k=getch();
if(k==121)
buy();
else
start();
}
}
printf("\n\t\t\t未查询到此书籍信息!\n\t\t\t按1返回主界面其他任意键重新输入书籍名");
k=getch();
if(k==49)
{
start();
}
else
buy();
}
void update(){ //添加及修改书籍信息函数
int i,k,s;
system("cls");
system("color BD");
printf("\n\n\t\t\t请输入您要使用的功能");
printf("\n\n\t\t\t1------添加一本书籍信息\n\n\t\t\t2------修改一本书籍信息\n\n\t\t\t3------返回上一级\n");
k=getch();
if(k==49)
{
system("cls");
printf("\n\t\t\t请输入您要将添加的书籍放在清单的序号范围在1~%d间",pp);
scanf("%d",&s);
if(s>pp||s<1)
{
printf("\n\t\t\t范围输入错误!按任意键返回上一级");
getch();
update();
}
else
{
for(i=pp;i>=s;i--)
{
aa[i+1]=aa[i];
}
aa[s].price=0;
aa[s].quantity=0;
printf("\n\t\t\t请输入您要添加的书籍的名称:");
scanf("%s",aa[s].name);
printf("\n\t\t\t请输入您要添加的书籍的作者:");
scanf("%s",aa[s].author);
printf("\n\t\t\t请输入您要添加的书籍的出版社:");
scanf("%s",aa[s].publisher);
printf("\n\t\t\t请输入您要添加的书籍的出版日期:");
scanf("%s",aa[s].date);
printf("\n\t\t\t请输入您要添加的书籍的价格:");
scanf("%lf",&aa[s].price);
printf("\n\t\t\t请输入您要添加的书籍的库存:");
scanf("%d",&aa[s].quantity);
printf("\n\t\t\t\t书籍信息添加成功!");
pp++;
fp = fopen("booklist.txt","w");
for(i=0;i<pp;i++)
{
fwrite(&aa[i],sizeof(book),1,fp);
}
fclose(fp);
printf("\n\t\t\t\t按y查看书籍清单其它任意键返回主界面");
k=getch();
if(k==121)
print();
else
start();
}
}
else if(k==50)
{
system("cls");
printf("\n\t\t\t请输入您要修改的书籍所在清单的序号范围请在1~%d间",pp);
scanf("%d",&s);
if(s>pp||s<1)
{
printf("\n\t\t\t范围输入错误!按任意键返回上一级");
getch();
update();
}
else
{
printf("\n\t\t\t请输入您要修改的书籍的名称:");
scanf("%s",aa[s-1].name);
printf("\n\t\t\t请输入您要修改的书籍的作者:");
scanf("%s",aa[s-1].author);
printf("\n\t\t\t请输入您要修改的书籍的出版社:");
scanf("%s",aa[s-1].publisher);
printf("\n\t\t\t请输入您要修改的书籍的出版日期:");
scanf("%s",aa[s-1].date);
printf("\n\t\t\t请输入您要修改的书籍的价格:");
scanf("%lf",&aa[s-1].price);
printf("\n\t\t\t请输入您要修改的书籍的库存:");
scanf("%d",&aa[s-1].quantity);
printf("\n\t\t\t\t书籍信息修改成功!");
fp = fopen("booklist.txt","w");
for(i=0;i<pp;i++)
{
fwrite(&aa[i],sizeof(book),1,fp);
}
fclose(fp);
printf("\n\t\t\t\t按y查看书籍清单其它任意键返回主界面");
k=getch();
if(k==121)
print();
else
start();
}
}
else if(k==51)
start();
else
update();
}
void start(){ //开始选择功能界面函数
system("color CE");
system("cls");
printf("\n\t\t\t欢迎使用书店存货清单!");
printf("\n\t\t\t请选择您需要使用的功能!");
printf("\n\n\t\t\t\t1------查看书籍清单\n\n\t\t\t\t2------购买书籍\n\n\t\t\t\t3------添加、修改书籍信息\n\n\t\t\t\t4------退出");
int k;
k=getch();
if(k==49)
print();
else if(k==50)
buy();
else if(k==51)
update();
else if(k==52)
exit(0);
else
start();
}
int main(){ //主函数
start();
}