📄 图书管理系统.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bookInfo{
int num;
char* name;
char* publish;
int price;
};
struct bookType{
bookInfo bo[20];
int num;
}book;
void DisplayMenu();
int choiceItem();
void Init(struct bookType*);
void PrintInfo();
void Print_aInfo(int);
void Input_aInfo(struct bookInfo*);
void Input_allInfo(struct bookType*,int num);
int search();
void search2();
void search3();
void Modify();
void main()
{
int choice,index,c,MUNMAX;
int *n[10];
Init(&book);
do{
choice=choiceItem();
switch (choice){
case 0:printf("\nWelcome to");break;
case 1:printf("How much do you want to enter?\n");
scanf("%d",&c);
MUNMAX=c;
Input_allInfo(&book,MUNMAX);break;
case 2:Input_allInfo(&book,1); break;
case 3:if ((index=search())!=-1)
Print_aInfo(index);
else printf("\n No eixst the book published by this publish.");
break;
case 4:PrintInfo();break;
case 5:search2();break;
case 6:search3();break;
case 7:Modify();break;
}
}while(choice);
}
void DisplayMenu()
{
printf("\n============================ MENU ================================");
printf("\n Input more books' informations ................................ 1");
printf("\n Input a book's informations ................................... 2");
printf("\n Input a number and print books' informations .................. 3");
printf("\n Show all the books..............................................4");
printf("\n Input a name and print books' informations .................... 5");
printf("\n Input a publish and print and books' information .............. 6");
printf("\n Modify book's information ..................................... 7");
printf("\n exit system ................................................... 0");
printf("\nchoice,please:");
}
int choiceItem()
{
int choice;
do{
DisplayMenu();
scanf("%d",&choice);
}while(choice<0||choice>7);
return choice;
}
void Init(struct bookType* s)
{
s->num=0;
}
void Print_aInfo(int index)
{
printf("\n%4d %-16s",book.bo[index].num,book.bo[index].name);
printf("%4s%6.1d",book.bo[index].publish,book.bo[index].price);
}
void PrintInfo()
{
int i;
if(book.num==0) {
printf("\nNo book.");
return;
}
for (i=0;i<book.num;i++){
printf("\n%4d %-16s",book.bo[i].num,book.bo[i].name);
printf("%4s%6.1d",book.bo[i].publish,book.bo[i].price);
}
}
void Input_aInfo(struct bookInfo* as)
{
printf("\nEnter the number:");
scanf("%d",&as->num);
as->name=(char*)malloc(30);
printf("\nEnter the name:");
scanf("%s",as->name);getchar();
as->publish=(char*)malloc(30);
printf("\nEnter the publish:");
scanf("%s",as->publish);
printf("\nEnter the price:");
scanf("%d",&as->price);
}
void Input_allInfo(struct bookType* s,int num)
{ int i,MUNMAX;
printf("\nEnter %d infomations.\n",num);
for (i=0;i<num;i++){
if (s->num==MUNMAX){
printf("\nOverflow.");
break;
}
Input_aInfo(&s->bo[s->num++]);
}
}
int search()
{
int num,i;
printf("\nEnter num:");
scanf("%d",&num);
for (i=0;i<book.num;i++)
if (book.bo[i].num==num)
return i;
return -1;
}
void search2()
{
int h,i,j,k,sign=0,sign1=0;
char as[15];
printf("\nEnter the name of book you look for:");
scanf("%s",as);
for(h=0;h<book.num;h++)
for(i=0;book.bo[h].name[i];i++)
{
sign=0;
for(j=i,k=0;book.bo[h].name[j]==as[k];j++,k++)
if(!as[k+1])
{sign=1;
sign1=1;}
if(sign){
printf("\n%4d %-16s",book.bo[h].num,book.bo[h].name);
printf("%4s%6.1d",book.bo[h].publish,book.bo[h].price);
}
}
if(!sign1) printf("\nsorry,do not exist a book with this name;");
}
void search3()
{
int i,j,a=0,b=0;
char as[10];
printf("\nEnter the publish of the book you look for:");
scanf("%s",as);
a=strlen(as);
for(i=0;book.bo[i].name!=0;i++)
{for(j=0,b=0;book.bo[i].publish[j]!=0;j++)
{if(book.bo[i].publish[j]==as[j])
b++;}
if(a==b){
printf("\n%4d %-16s",book.bo[i].num,book.bo[i].name);
printf("%4s%6.1d",book.bo[i].publish,book.bo[i].price);
}
}
}
void Modify()
{ int a,i,j,sig=0;
printf("\nEnter the number:");
scanf("%d",&a);
for(i=0;i<book.num;i++)
if(book.bo[i].num==a)
{
sig=1;
break;
}
if(sig==1)
{
for(j=i;j<book.num;j++)
book.bo[j]=book.bo[j+1];
book.num--;
printf("\nbook have deleted.");
}
else
printf("\nsorry,do not exist book with this number.");
}
/*书名不要为空,否则有BUG 书名不可含空格。。。。*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -