#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
//#include "information.h"
#define MAXNUM 1000
//图书数据类型
typedef struct book
{ char isbn[25]; /*书号*/
char bkname[30]; /*书名*/
char author[25]; /*作者*/
float price; /*单价*/
char press[28]; /*出版社*/
int num; /*库存册数*/
}BOOK;
//int TotalBook;
//用线性表保存图书信息
typedef struct{
BOOK bk[MAXNUM];
int TotalBook;
}BookList;
BookList booklist;
void note(){
/*注释:
作者:lilililimu
数据:bookdata.txt
书号格式:XXX-XXX-XXX-XXX-XXX
例:978-7-5063-6543-7
*/
}
main()
{
//函数原型
void ReadData();//读取文件
void WriteData();//写入文件
void showbooklist();//图书列表
int showsel();//菜单界面
int bookquery_scanf_float(char s[]);//小数录入
int bookbubblesort_showsel();//图书排序菜单
void bookbubblesort();//图书排序
int bookin_verification_isbn(char s[]);//书号格式验证
int bookin_exist(char key[]);//根据书号验证图书是否已存在于书库
int bookquery_scanf(char s[]); //整数录入
int bookin();//图书入库
int bookdelete_showsel();//删除图书显示
int bookdelete_delete(int location) ;//删除图书删除
int bookdelete();//删除图书
void bookquery_key(char s[],char key[]);//获得查询的key值
int bookquery_showsel(); //图书查询显示函数
void bookquery_location_show(int i,int flag);//通过查询到的位置显示图书信息
int bookquery_string(char key[],int sel,int location); //通过key查询图书
void bookquery();//图书查询
int booksale();//图书销售
int bookscreening_showsel();//图书筛选菜单
void bookscreening();//图书筛选
void salesranking();//销量排行榜
void modifytheprice();//修改价格
//主程序
ReadData();
int sel;
//Initial(&booklist);//信息初始化
/*test===============================================================================
/* test================================================================================*/
do{system("cls");sel=showsel();
switch(sel)
{case 0:break;
case 1: system("cls");showbooklist();getch();break;//已完成
case 2: system("cls");bookin();getch();break;
case 3: system("cls");booksale();getch();break;
case 4: system("cls");bookdelete();getch();break;
case 5: system("cls");bookquery();getch();break;
case 6:system("cls");bookbubblesort();getch();break;
case 7: system("cls");bookscreening();getch();break;
case 8:system("cls");modifytheprice();getch();break;
default:printf("\t\t\t指令不正确!");getch();fflush(stdin);system("cls");break;
}
}while(sel);
WriteData();
}
//读取文件
void ReadData(){
FILE *fp;
if ((fp=fopen(".\\bookdata.txt","rb"))==NULL)
{printf("Can not open the file!\n");return;}
fread(&booklist.TotalBook,sizeof(int),1,fp);
fread(booklist.bk,sizeof(BOOK),booklist.TotalBook,fp);
fclose(fp);
}
//写入文件
void WriteData(){
FILE *fp;
if ( (fp=fopen("bookdata.txt","wb"))==NULL)
{printf("Can not open the file!\n");return;}
fwrite(&booklist.TotalBook, sizeof(int) ,1 ,fp);
fwrite(booklist.bk,sizeof(BOOK),booklist.TotalBook,fp);
fclose(fp);
}
//图书列表
void showbooklist(){
int i;
printf("\t\t%-5s%-20s%-29s%-20s%-8s%-8s%-20s%\n",
"序号","书号","书名","作者","价格","数量","出版社");
printf("\t\t----------------------------------------------------------------------------------------------------\n");
for (i=0;i<booklist.TotalBook;i++)
printf("\t\t%-5d%-20s%-29s%-20s%-8.2f%-8d%-20s%\n\n",i+1,booklist.bk[i].isbn,booklist.bk[i].bkname,
booklist.bk[i].author,booklist.bk[i].price,booklist.bk[i].num,booklist.bk[i].press);
printf("\n\t\t按任意键退出!");
}
//菜单界面
int showsel(){
int sel;
printf("\t\t简易书店管理系统\n");
printf("\t\t设计者:184 李俊豪\n");
printf("\t\t2020/6/9 周三\n");
printf("\t---------------------------------\n");
printf("\t\t0. 退出程序\n");
printf("\t\t1. 图书列表\n");
printf("\t\t2. 图书入库\n");
printf("\t\t3. 图书销售\n");
printf("\t\t4. 删除图书\n");
printf("\t\t5. 图书查询\n");
printf("\t\t6. 图书排序\n");
printf("\t\t7. 图书筛选\n");
printf("\t\t8. 价格修改\n");
printf("\t----------------------------------\n");
printf("\t\t请输入要选择的功能编号:");
int ret=scanf("%d",&sel);
if(ret) return sel;
else{
return 15;
}
}
//通过查询到的位置显示图书信息
void bookquery_location_show(int i,int flag){
if(flag==0){
printf("\t\t%-5s%-20s%-29s%-20s%-8s%-8s%-20s\n",
"序号","书号","书名","作者","价格","数量","出版社");}
printf("\t\t%-5d%-20s%-29s%-20s%-8.2f%-8d%-20s\n",flag+1,booklist.bk[i].isbn,booklist.bk[i].bkname,
booklist.bk[i].author,booklist.bk[i].price,booklist.bk[i].num,booklist.bk[i].press);
}
//根据书号验证图书是否已存在于书库
int bookin_exist(char key[]) {
int i;
for(i=0;i<booklist.TotalBook;i++){
if(strcmp(key,booklist.bk[i].isbn)==0){
return i;
}
}
return -1;
}
//小数录入
int bookquery_scanf_float(char s[]){
float digital;
int ret=0;
int record=0;
do{
digital=-3;
if(record==0){
printf("\t\t请输入%s价格:(或者输入'-1'放弃录入!)\n\t\t",s);
}else{
printf("\t\t价格格式有误!请重新输入价格:(或者输入'-1'放弃录入!)\n\t\t");
}
record++;
ret=scanf("%f",&digital);
fflush(stdin);
}while((!ret||digital<0)&&digital!=-1);
if(digital==-1){
//printf("\t\t图书入库失败!");
return -1;
}else{
return digital;
}
}
//整数录入
int bookquery_scanf(char s[]){
int ret=0;
int record=0;
float digital=-1;
do{
if(record==0){
printf("\t\t请输入书本的%s:(或者输入'-1'放弃!)\n\t\t",s);
}else{
printf("\t\t%s输入有误!请重新输入:(或者输入'-1'放弃!)\n\t\t",s);
}
record++;
ret=scanf("%f",&digital);
if(ret==0){
digital=-5;
}
if((int)digital==digital&&ret&&digital>0){
ret=1;
}else if(digital==-1){
ret=1;
}else{
ret=0;
}
fflush(stdin);
}while(!ret);
if(digital==-1){
//printf("放弃出售!");
return -1;
}else{
return digital;
}
}
//图书入库
int bookin(){
printf("\t\tBook In...\n");
int record;
char flag='y';
while(flag=='y'){
char key[25]="";
int exist=-1;
record=0;
do{
printf("*********************************************************\n");
if(record==0){
printf("\t\t请输入书号:(或者输入'n'放弃录入!)\n\t\t");
}else{
printf("\t\t书号格式不正确!请重新输入书号:(或者输入'n'放弃录入!)\n\t\t");
}
record++;
scanf("%s",&key);
if(key[0]=='n'){
flag='n';
}
}while(!bookin_verification_isbn(key)&&flag=='y');
if(flag!='y'){
printf("\t\t图书入库失败!");
return 0;
}else{
exist=bookin_exist(key);
if(exist!=-1){
//printf("图书已存在,请输入需要增加的数量:\n");
bookquery_location_show(exist,0);
float digital=-1;
digital=bookquery_scanf("数量");
if(digital==-1){
printf("\t\t图书入库失败!");
return 0;
}else{
booklist.bk[exist].num+=digital;
printf("\t\t图书录入成功!\n");
bookquery_location_show(exist,0);
printf("\t\t是否继续录入?输入y/n.\n\t\t");
scanf("%c",&flag);
if(flag=='y'){
continue;
}else{
printf("\t\t请按任意键返回主界面!");
return 0;
}
}
}else{
strcpy(booklist.bk[booklist.TotalBook].isbn,key);
}
}
printf("\t\t新书录入!\n\t\t请输入作者:或者输入'@'放弃录入!\n\t\t");
scanf("%s",&booklist.bk[booklist.TotalBook].author);
if(booklist.bk[booklist.TotalBook].author[0]=='@'){
printf("\t\t图书入库失败!");
return 0;
}
printf("\t\t请输入书名:或者输入'@'放弃录入!\n\t\t");
scanf("%s",&booklist.bk[booklist.TotalBook].bkname);
if(booklist.bk[booklist.TotalBook].bkname[0]=='@'){
printf("\t\t图书入库失败!");
return 0;
}
printf("\t\t请输入出版社:或者输入'@'放弃录入!\n\t\t");
scanf("%s",&booklist.bk[booklist.TotalBook].press);
if(booklist.bk[booklist.TotalBook].press[0]=='@'){
printf("\t\t图书入库失败!");
return 0;
}
float digital;
int ret=0;
record=0;
digital=bookquery_scanf_float("");
if(digital==-1){
printf("\t\t图书入库失败!");
return 0;
}else{
booklist.bk[booklist.TotalBook].price=digital;
}
digital=-1;
ret=0;
record=0;
digital=bookquery_scanf("数量");
if(digital==-1){
printf("\t\t图书入库失败!");
return 0;
}else{
booklist.bk[booklist.TotalBook].num=digital;
}
booklist.TotalBook+=1;
printf("\t\t图书录入成功!\n");
bookquery_location_show(booklist.TotalBook-1,0);
printf("\t\t是否继续录入?输入y/n.\n\t\t");
scanf("%c",&flag);
fflush(stdin);
}
printf("\t\t按任意键返回主界面!");
return 0;
}
//获得查询的key值
void bookquery_key(char s[],char key[]){
printf("\t\t请输入需要查询的%s:",s);
scanf("%s",key);
}
//删除图书显示
int bookdelete_showsel(){
int sel;
printf("\t\t删除系统\n");
printf("\t---------------------------------\n");
printf("\t\t0. 退出程序\n");
printf("\t\t1. 删除指定书籍\n");
printf("\t\t2. 删除全部数量为零的书籍\n");
printf("\t----------------------------------\n");
printf("\t\t请输入要选择的功能编号:");
int ret=scanf("%d",&sel);
if(ret) return sel;
else{
return 15;
}
}
//删除图书删除
int bookdelete_delete(int location){
int i;
if(location<0||location>booklist.TotalBook) return -1;
for(i=location;i<booklist.TotalBook;i++){
booklist.bk[i]=booklist.bk[i+1];
}
booklist.TotalBook--;
return 1;
}
//删除图书
int bookdelete(){
printf("\t\tBook Delete...\n");
int sel;
int location=-1;
int resault[5];
int flag=0;
char choose;
int i;
char key[25];
do{
system("cls");sel=bookdelete_showsel();
switch(sel)
{case 0:printf("\t\t按任意键退出!");break;
case 1:
bookquery_key("书号",key);
flag=0;
do{
location=bookquery_string(key,3,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
resault[flag]=location;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
if(flag>0){
do{
location=bookquery_scanf("序号");
}while(location>flag&&location!=-1);
if(location==-1) {
printf("放弃删除!");
return 0;
}else{
if(booklist.bk[resault[location-1]-1].num==0){
flag=bookdelete_delete(resault[location-1]-1);
if(flag==1){
printf("\t\t删除成功!");
}
}else{
printf("\t\t图书未售空,无法从书库中移除!");
}
}
}
getch();
break;
case 2:
flag=0;
for(i=0;i<booklist.TotalBook;i++)
if(booklist.bk[i].num==0){
bookquery_location_show(flag,i);
flag++;
}
printf("\t\t是否全部删除?输入y/n:");
fflush(stdin);
choose=getchar();
if(choose=='y'){
for(flag=0;flag<booklist.TotalBook;flag++){
if(booklist.bk[flag].num==0){
bookdelete_delete(flag);
}
}
printf("\t\t删除成功!");
}else{
printf("\t\t放弃删除!");
}
getch();
break;
default:printf("\t\t\t指令不正确!");getch();fflush(stdin);system("cls");break;
}
}while(sel);
}
//图书查询
void bookquery(){
printf("/t/t/tBook query...");
int sel;
do{
system("cls");sel=bookquery_showsel();
char key[30]="";
int location=-1;
int flag=0;
switch(sel){
case 0:printf("\t\t按任意键退出!");break;
case 1:
bookquery_key("书名",key);
do{
location=bookquery_string(key,1,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
getch();
break;
case 2:
flag=0;
location=-1;
bookquery_key("作者",key);
do{
location=bookquery_string(key,2,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
getch();
break;
case 3:
flag=0;
location=-1;
bookquery_key("书号",key);
do{
location=bookquery_string(key,3,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
getch();
break;
case 4:
flag=0;
location=-1;
bookquery_key("出版社",key);
do{
location=bookquery_string(key,4,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
getch();
break;
default:printf("\t\t\t指令不正确!");getch();fflush(stdin);system("cls");break;
}
}while(sel);
}
//图书销售
int booksale(){
printf("\t\tBook Sale...\n");
char key[30];
int resault[5];
int location=-1;
int flag=0;
printf("\t\t请输入你要出售的书名:或者输入'@'放弃录入!\n\t\t");
scanf("%s",key);
if(key[0]=='@'){
printf("\t\t放弃出售!");
return 0;
}
do{
location=bookquery_string(key,1,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
resault[flag]=location;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
if(flag>0){
do{
location=bookquery_scanf("序号");
}while(location>flag&&location!=-1);
if(location==-1) {
printf("\t\t放弃出售!");
return 0;
}else{
do{
flag=bookquery_scanf("数量");
if(flag>booklist.bk[resault[location-1]-1].num){
printf("\t\t需要出售数量大于书库中书本数量,请重新输入!\n");
}
}while(flag!=-1&&flag>booklist.bk[resault[location-1]-1].num);
if(flag==-1) {
printf("\t\t放弃出售!");
return 0;
}
}
}
if(flag>0&&booklist.bk[resault[location-1]-1].num!=0){
booklist.bk[resault[location-1]-1].num-=flag;
bookquery_location_show(resault[location-1]-1,0);
printf("\t\t出售成功!");
if(booklist.bk[resault[location-1]-1].num==0){
printf("\n\t\t书本已售空,是否从书库移除?y/n:");
scanf("%s",key);
if(key[0]=='y'){
bookdelete_delete(resault[location-1]-1);
printf("\t\t移除成功!按任意键退出!");
}else printf("\t\t请按任意键退出!");
}
}
return 0;
}
//图书排序菜单
int bookbubblesort_showsel(){
int sel;
printf("\t\t排序系统\n");
printf("\t---------------------------------\n");
printf("\t\t0. 退出程序\n");
printf("\t\t1. 根据数量由大到小排序\n");
printf("\t\t2. 根据数量由小到大排序\n");
printf("\t\t3. 根据价格由大到小排序\n");
printf("\t\t4. 根据价格由小到大排序\n");
printf("\t\t5. 根据作者姓名排序\n");
printf("\t----------------------------------\n");
printf("\t\t请输入要选择的功能编号:");
int ret=scanf("%d",&sel);
if(ret) return sel;
else{
return 15;
}
}
//图书排序
void bookbubblesort()
{
int i;
int j;
int sel;
BOOK temp;
do{
system("cls");sel=bookbubblesort_showsel();
switch(sel)
{case 0:printf("\t\t按任意键退出!");break;
case 1:
if (booklist.TotalBook == 0)
{
printf("\t\t书库为空!");
return;
}
for (i = 0; i < booklist.TotalBook-1;i++)
for (j = 0; j < booklist.TotalBook-1;j++)
if (booklist.bk[j].num<booklist.bk[j+1].num)
{
{
temp = booklist.bk[j];
booklist.bk[j]= booklist.bk[j+1];
booklist.bk[j+1] = temp;
}
}
printf("\t\t排序完成!");
getch();
break;
case 2:
if (booklist.TotalBook == 0)
{
printf("\t\t书库为空!");
return;
}
for (i = 0; i < booklist.TotalBook-1;i++)
for (j = 0; j < booklist.TotalBook-1;j++)
if (booklist.bk[j].num>booklist.bk[j+1].num)
{
{
temp = booklist.bk[j];
booklist.bk[j]= booklist.bk[j+1];
booklist.bk[j+1] = temp;
}
}
printf("\t\t排序完成!");
getch();
break;
case 3:
if (booklist.TotalBook == 0)
{
printf("\t\t书库为空!");
return;
}
for (i = 0; i < booklist.TotalBook-1;i++)
for (j = 0; j < booklist.TotalBook-1;j++)
if (booklist.bk[j].price<booklist.bk[j+1].price)
{
{
temp = booklist.bk[j];
booklist.bk[j]= booklist.bk[j+1];
booklist.bk[j+1] = temp;
}
}
printf("\t\t排序完成!");
getch();
break;
case 4:
if (booklist.TotalBook == 0)
{
printf("\t\t书库为空!");
return;
}
for (i = 0; i < booklist.TotalBook-1;i++)
for (j = 0; j < booklist.TotalBook-1;j++)
if (booklist.bk[j].price>booklist.bk[j+1].price)
{
{
temp = booklist.bk[j];
booklist.bk[j]= booklist.bk[j+1];
booklist.bk[j+1] = temp;
}
}
printf("\t\t排序完成!");
getch();
break;
case 5:
if (booklist.TotalBook == 0)
{
printf("\t\t书库为空!");
return;
}
for (i = 0; i < booklist.TotalBook-1;i++)
for (j = 0; j < booklist.TotalBook-1;j++)
if (strcmp(booklist.bk[j].author,booklist.bk[j+1].author)>0)
{
{
temp = booklist.bk[j];
booklist.bk[j]= booklist.bk[j+1];
booklist.bk[j+1] = temp;
}
}
printf("\t\t排序完成!");
getch();
break;
default:printf("\t\t\t指令不正确!");getch();fflush(stdin);system("cls");break;
}
}while(sel);
}
//图书筛选菜单
int bookscreening_showsel(){
int sel;
printf("\t\t筛选系统\n");
printf("\t---------------------------------\n");
printf("\t\t0. 退出程序\n");
printf("\t\t1. 根据价格区间筛选\n");
printf("\t\t2. 根据数量区间筛选\n");
printf("\t----------------------------------\n");
printf("\t\t请输入要选择的功能编号:");
int ret=scanf("%d",&sel);
if(ret) return sel;
else{
return 15;
}
}
//图书筛选
void bookscreening(){
/*根据价格区间,同一作者,同一出版商筛选图书,方便顾客购买,功能可依赖于图书查询*/
printf("\t\tBook screening...");
int sel;
do{
float max=-1;
float min=-1;
int i;
int flag=0;
system("cls");sel=bookscreening_showsel();
switch(sel)
{case 0:printf("\t\t按任意键退出!");break;
case 1:
max=bookquery_scanf_float("最大");
if(max!=-1){
min=bookquery_scanf_float("最小");
}
if(max>min&&max!=-1&&min!=-1){
for(i=0;i<booklist.TotalBook;i++){
if(booklist.bk[i].price<=max&&booklist.bk[i].price>=min){
bookquery_location_show(i,flag);
flag++;
}
}
}
if(flag==0){
printf("\t\t没有符合条件的数据!按任意键退出!\n");
}
getch();
break;
case 2:
max=bookquery_scanf("最大数量");
if(max!=-1){
min=bookquery_scanf("最小数量");
}
if(max>min){
for(i=0;i<booklist.TotalBook;i++){
if(booklist.bk[i].num<=max&&booklist.bk[i].num>=min){
bookquery_location_show(i,flag);
flag++;
}
}
}
if(flag==0){
printf("\t\t没有符合条件的数据!按任意键退出!\n");
}
getch();
break;
default:printf("\t\t\t指令不正确!");getch();fflush(stdin);system("cls");break;
}
}while(sel);
}
//图书销量排行榜
void salesranking(){
printf("salesranking...");
}
//书号格式验证
int bookin_verification_isbn(char s[]){
int flag=1;
int i;
int num=0;
char s1[13];//数字书号
if(strlen(s)!=17){//位数验证
flag*=0;
}
for(i=0;i<17;i++){
if(isdigit(s[i])){
num++;
}
}
if(num!=13){
flag*=0;
}
char s2=s[0];
if(isdigit(s2)==0){
flag*=0;
}
s2=s[16];
if(isdigit(s2)==0){
flag*=0;
}
return flag;
}
// 图书查询显示函数
int bookquery_showsel(){
int sel;
printf("\t\t图书查询系统\n");
printf("\t---------------------------------\n");
printf("\t\t0. 退出系统\n");
printf("\t\t1. 以书名查询\n");
printf("\t\t2. 以作者查询\n");
printf("\t\t3. 以书号查询\n");
printf("\t\t4. 以出版社查询\n");
printf("\t----------------------------------\n");
printf("\t\t请输入要选择的功能编号:");
int ret=scanf("%d",&sel);
if(ret) return sel;
else{
return 15;
}
}
//通过key查询图书
int bookquery_string(char key[],int sel,int location){
switch(sel){
case 1:
for(location;location<booklist.TotalBook;location++){
if(strcmp(key,booklist.bk[location].bkname)==0){
return location;
}
}break;
case 2:
for(location;location<booklist.TotalBook;location++){
if(strcmp(key,booklist.bk[location].author)==0){
return location;
}
}break;
case 3:
for(location;location<booklist.TotalBook;location++){
if(strcmp(key,booklist.bk[location].isbn)==0){
return location;
}
}break;
case 4:
for(location;location<booklist.TotalBook;location++){
if(strcmp(key,booklist.bk[location].press)==0){
return location;
}
}break;
}
return -1;
}
//修改价格
void modifytheprice(){
char key[25];
float digital;
int flag=0;
int resault[5];
int location=-1;
bookquery_key("书号",key);
do{
location=bookquery_string(key,3,location);
if(location!=-1){
bookquery_location_show(location,flag);
location+=1;
resault[flag]=location;
flag++;
}
}while(location!=-1);
if(flag==0){
printf("\t\t未查到所需信息!");
}
if(flag>0){
do{
location=bookquery_scanf("序号");
}while(location>flag&&location!=-1);
if(location==-1) {
printf("\t\t放弃修改!");
getch();
return ;
}else{
digital=bookquery_scanf_float("");
if(digital!=-1){
booklist.bk[resault[location-1]-1].price=digital;
bookquery_location_show(resault[location-1]-1,1);
printf("\t\t修改成功!");
}
}
}
return;
}
C语言课程设计图书馆管理系统
最新推荐文章于 2024-03-19 10:37:53 发布