主要实现图书管理系统
通过定义两个结构体(图书信息、学生信息)
有如下几个基本功能:
具体代码如下:
/*******************************
功能 :图书管理系统
时间 : 2019-10-17
作者 :茅XX
*******************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10 //一个人的借阅上限
struct Book
{
char name[32]; //书名
int count; //本数
int flag; //借阅情况,1代表可借,0表示不可借
};
typedef struct Book book;
struct Student
{
char name[32]; //学生名
int id ; //学号
char* book[MAX]; //已经借阅的书
int count; //借的本数
};
typedef struct Student stu;
book* g_book[1024] = {0}; //图书馆的所有书
stu * g_stu[1024] = {0}; //图书馆记录的所有借书的同学
int g_bookcount = 0; //图书馆书的本数
int g_booktype = 0; //图书馆的书的类型数
int g_stucount = 0; //借书的人数
void init () //图书馆初始化
{
int i = 0, j = 0;
for ( i = 0; i < 10; i++)
{
g_book[i] = (book *)malloc (sizeof(book) * 1);
if(NULL == g_book[i])
{
printf("malloc failure!\n");
}
g_book[i]->flag = 1; //表示可借
g_book[i]->count = 1; //初始化
g_bookcount++; //总书+1
g_booktype ++; //书本类型数+1
}
for ( i = 0; i < 5; i++)
{
g_stu[i] = (stu *)malloc (sizeof(stu) * 1);
g_stu[i]-> book[0] = (char *)malloc (sizeof (char) * 32);
if(NULL == g_stu[i])
{
printf("malloc failure!\n");
}
g_stu[i]->id = i + 1; //学号
g_stu[i]->count ++; //初始化
g_stucount ++; //借书人数+1
}
strcpy(g_book[0]->name, "C 和指针");
strcpy(g_book[1]->name, "大学语文");
strcpy(g_book[2]->name, "高等数学");
strcpy(g_book[3]->name, "线性代数");
strcpy(g_book[4]->name, "电路原理");
strcpy(g_book[5]->name, "大学英语");
strcpy(g_book[6]->name, "电子技术");
strcpy(g_book[7]->name, "电机拖动");
strcpy(g_book[8]->name, "电路设计");
strcpy(g_book[9]->name, "大学物理");
strcpy(g_stu[0]->name, "张");
strcpy (g_stu[0] -> book[0], "大学英语");
strcpy(g_stu[1]->name, "李");
strcpy (g_stu[1] -> book[0], "高等数学");
strcpy(g_stu[2]->name, "茅");
strcpy (g_stu[2] -> book[0], "电路原理");
strcpy(g_stu[3]->name, "刘");
strcpy (g_stu[3] -> book[0], "电路设计");
strcpy(g_stu[4]->name, "唐");
strcpy (g_stu[4] -> book[0], "大学英语");
}
void login() // 界面主菜单函数,传递值空,返回值空
{
int i;
system ("clear");
printf ("\t\t|***********************************************|\n");
printf ("\t\t|1.增加图书 2.删除图书 |\n");
printf ("\t\t|3.图书总览 4.查找图书 |\n");
printf ("\t\t|5.借书 6.还书 |\n");
printf ("\t\t|7.借书情况 8.退出系统 |\n");
printf ("\t\t|***********************************************|\n");
for (i = 0; i < g_booktype; i++)
{
if (g_book[i]->count <= 0)
{
g_book[i]->count = 0;
g_book[i]->flag =0;
}
}
}
//***************1.增加图书
int Add_Book()
{
printf ("输入增加的图书的名称 数量\n");
char ch[32] = {0};
int num = 0, i, flag = 0;
scanf ("%s %d", ch, &num);
for(i = 0; i < g_booktype; i++)
{
if(strcmp(g_book[i]->name , ch) == 0)
{
flag = 1;
break;
}
}
if(flag == 1)
{
g_book[i]->count = num + g_book[i]->count;
g_bookcount =g_bookcount + num;
return 0;
}
else
{
g_book[g_booktype] = (book *)malloc (sizeof (book) * 1);
if (NULL == g_book[g_booktype])
{
printf ("malloc failure!\n");
}
g_book[g_booktype] -> count += num ;
g_book[g_booktype] -> flag = 1;
strcpy (g_book[g_booktype] -> name, ch);
g_bookcount += num;
g_booktype++;
}
return 0;
}
//***************2.删除图书
int Del_Book()
{
int i = 0, input = 0, j=0;
char choice ;
char ch[32] = {0};
printf ("输入删除图书的名称 数量\n");
scanf ("%s%d", ch, &input);
printf ("\t\t\t查询结果\n");
printf ("\t书名\t\t数量\t\t借阅情况\n");
for (i = 0; i < g_booktype; i++)
{
if (strcmp (g_book[i]->name, ch) == 0)
{
printf ("\t%s\t%d",g_book[i]->name, g_book[i]->count);
if(g_book[i]->flag > 1 )
printf (" \t\t可借");
else printf (" \t\t不可借");
printf ("\n");
break;
}
}
if (g_booktype == i)
{
printf ("\t无\t\t无\t\t无");
return 0;
}
printf ("是否删除%s\tY ? N\n", ch);
getchar();
scanf ("%c", &choice);
printf ("\n");
while (choice != 'Y' && choice != 'y'&& choice != 'N' && choice != 'n')
{
printf ("选择错误,重新输入\n");
scanf ("%c", &choice);
}
if (choice == 'Y' || choice == 'y')
{
if (input < g_book[i]->count)
{
g_book[i]->count -= input;
g_bookcount -= input;
}
else
{
if (input > g_book[i] -> count)
printf ("删除数目超载\n");
for (j = i; j < g_booktype - 1; j++)
{
g_book[j]->count = g_book[j + 1]->count;
g_book[j]->flag = g_book[j]->flag;
strcpy (g_book[j]->name,g_book[j + 1]->name);
}
g_bookcount -= input;
g_booktype -= 1;
g_book[g_booktype]->count = 0;
free (g_book[g_booktype]);
}
printf("删除成功\n");
}
else printf("撤销删除\n");
return 0;
}
//***********************3.图书总览
void Show_Book()
{
int i;
printf ("\t书名\t\t数量\t\t借阅情况\n");
for (i = 0; i < g_booktype;i++)
{
printf ("\t%s\t%d",g_book[i]->name, g_book[i]->count);
if(g_book[i]->flag == 1 )
printf (" \t\t可借");
else printf (" \t\t不可借");
printf ("\n");
}
printf ("\t一共有%d种类型的书\n", g_booktype);
}
//****************************4.查找图书 //输入值 书名 返回值 书库中的实际位置
int Find_Book(char ch[])
{
int i = 0;
printf ("\t\t\t查询结果\n");
printf ("\t书名\t\t数量\t\t借阅情况\n");
for (i = 0; i < g_booktype; i++)
{
if (strcmp (g_book[i]->name, ch) == 0)
{
printf ("\t%s\t%d",g_book[i]->name, g_book[i]->count);
if(g_book[i]->flag == 1 )
printf (" \t\t可借");
else printf (" \t\t不可借");
printf ("\n");
break;
}
}
if (g_booktype == i)
{
printf ("\t无\t\t无\t\t无\n");
return 0;
}
return i + 1;
}
//*************************************5.借书
int Borrow_Book()
{
int number = 0, i, book_num, num, temp, cemp;
char ch[32] = {0};
printf ("输入学生学号:\n");
scanf ("%d", &number);
printf ("\n");
num = Find_Student(number);
if (num == g_stucount)
{
printf ("录入新信息:\t输入姓名\n");
printf ("\t\t");
scanf ("%s", ch);
g_stu[g_stucount] = (stu * )malloc (sizeof (stu) * 1);
if (NULL == g_stu[g_stucount])
{
printf ("malloc Failure !\n");
}
strcpy(g_stu[g_stucount]->name, ch);
g_stu[g_stucount]-> id = number;
g_stucount ++;
printf ("学生基础信息登录完毕\n");
}
printf("输入借阅的数量:");
scanf ("%d", &book_num);
if((book_num + g_stu[num]->count) > MAX )
{
printf ("超出借阅上限\n");
return;
}
else
{
cemp = g_stu[num] -> count;
for (i = g_stu[num]->count; i < book_num + g_stu[num] -> count; i++)
{
printf ("输入书名:");
scanf ("%s", ch);
temp = Find_Book(ch);
while (!temp) //图库中无输入
{
printf ("\n图库中无输入所需图书,重新输入:");
scanf("%s", ch);
temp = Find_Book(ch);
}
if(g_book[temp - 1] -> flag == 1) //表示可借
{
g_stu[num]->book[cemp] = (char *)malloc(sizeof (char) * 32);
if ( NULL == g_stu[num]->book[cemp])
{
printf ("malloc failure!\n");
}
strcpy(g_stu[num]->book[cemp],ch);
g_book[temp - 1] -> count--;
cemp++;
}
else printf ("图书不可借!!\n");
}
g_stu[num] -> count = cemp;
}
printf ("\n\n");
Find_Student(number);
return 0;
}
//************************************查找同学的借书情况(内存操作)
int Find_Student (int a) //输入值学号
{ //返回学生库实际地址-1 (数组地址)
int i, j;
printf ("\t姓名\t学号\t已借阅的书名\n");
for (i = 0; i < g_stucount; i++ )
{
if(g_stu[i]->id == a)
{
break;
}
}
if (i == g_stucount)
{
printf("\t\t数据库中无输入学生信息\n");
return g_stucount;
}
else
{
printf ("\t%s\t%d\t%s\n", g_stu[i]->name, g_stu[i]->id,g_stu[i]->book[0]);
for (j = 1; j < g_stu[i]->count; j++)
printf ("\t\t\t%s\n", g_stu[i]->book[j]);
return i;
}
}
//*******************************6.还书
void Repay_Book()
{
char ch [32] = {0};
int number, temp, book_number, cemp;
int i, j, flag;
printf ("输入学号:");
scanf ("%d", &number);
temp = Find_Student(number); //该同学在数据库里的位置
printf ("输入归还的图书数量:");
scanf ("%d", &book_number);
while (book_number > g_stu[temp] -> count)
{
printf ("超出上限,重新输入\n");
scanf ("%d", &book_number);
}
for (i = 0; i < book_number; i++)
{
flag = 0;
while (1)
{
printf ("输入归还的书籍名称:");
scanf ("%s", ch);
for (j = 0; j < g_stu[temp]-> count; j++)
{
if (strcmp (g_stu[temp]->book[j], ch) == 0)
{
flag = 1;
break;
}
}
if ( flag == 1) break;
}
g_stu[temp]->count --;
free (g_stu[temp]->book[j]);
g_bookcount++;
cemp = Find_Book(ch);
g_book[cemp - 1]->count ++;
}
printf ("\n\n");
Find_Student(number);
}
//*******************************7.借书情况
void Find_Borrow()
{
int i, j;
printf ("\t姓名\t学号\t书名\n");
for (i = 0; i < g_stucount; i++)
{
printf ("\t%s\t%d\t%s\n",g_stu[i]->name, g_stu[i]->id, g_stu[i]->book[0]);
for (j = 1; j < g_stu[i] -> count;j++)
printf ("\t\t\t%s\n", g_stu[i]->book[j]);
printf ("\n");
}
}
int main ()
{
char choice = '0';
char ch[32] = {0};
init(); //初始化
while (1)
{
login (); //主菜单
scanf ("%c", &choice);
switch (choice)
{
case '1' : Add_Book(); //增加图书
break;
case '2' : Del_Book(); //删除图书
break;
case '3' : Show_Book(); //图书总览
break;
case '4' : printf ("输入图书名称:");
scanf ("%s", ch);
Find_Book(ch); //查找图书
break;
case '5' : Borrow_Book(); //借书
break;
case '6' : Repay_Book(); //还书
break;
case '7' : Find_Borrow(); //借阅总览
break;
case '8' : exit(0); //退出系统
break;
default : printf ("输入有误,请重新输入选项\n");
break;
}
getchar();
printf("输入回车继续执行\n");
getchar();
}
return 0;
}