久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
自己寫的C語言圖書管理系統程序 綜合使用了數組,指針,結構體的知識
[打印本頁]
作者:
chenyinhu
時間:
2019-11-19 10:34
標題:
自己寫的C語言圖書管理系統程序 綜合使用了數組,指針,結構體的知識
綜合使用了數組,指針,結構體的知識
結果截圖如下
Screenshot_20191119-102837.png
(160.55 KB, 下載次數: 82)
下載附件
2019-11-19 10:34 上傳
源代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
系統名稱:圖書管理系統
系統功能:增加圖書 刪除圖書 查找圖書 查看圖書 借書 還書 個人借書情況等
作者:陳銀虎
完成時間:2019年11月19日
*/
struct Book
{
char name[32]; //書名
int count; //本數
int flag; //是否可借 1表示可借
};
typedef struct Book book;
struct Student
{
char name[32]; //學生名
int id; //學生學號
char *book[5]; //已經借的書
int count; //借的本數
};
typedef struct Student stu;
book *g_book[1024] = {0}; //圖書館所有書
stu *g_student[1024] = {0}; //圖書館記錄的所有借書的學生
int g_BookCount = 0; //圖書館書的本數
int g_StudentCount = 0; //借書的人數
void menu()
{
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");
}
/*
函數描述:圖書館初始化,存放一些圖書
函數參數:無
函數返回值:無
*/
void init()
{
int i;
for (i = 0; i < 9; i++)
{
g_book[i] = (book *)malloc(sizeof(book) * 1);
if (NULL == g_book[i])
{
printf("mallco failrue!\n");
}
g_book[i]->flag = 1; //表示可借
g_BookCount++;
}
strcpy(g_book[0]->name, "數學");
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, "C語言");
strcpy(g_book[7]->name, "單片機");
strcpy(g_book[8]->name, "嵌入式");
g_book[0]->count = 3;
g_book[1]->count = 5;
g_book[2]->count = 1;
g_book[3]->count = 3;
g_book[4]->count = 5;
g_book[5]->count = 10;
g_book[6]->count = 2;
g_book[7]->count = 15;
g_book[8]->count = 7;
}
/*
函數描述:顯示圖書館所有書本
函數參數:無
函數返回值:無
*/
void ShowBook()
{ printf("\t\t借閱情況:\n");
printf("\t\t圖書館總藏書數:%d\n",g_BookCount);
printf("\t\t總借閱人數:%d\n",g_StudentCount);
printf("\t\t書本情況:\n");
int i;
for (i = 0; i < g_BookCount; i++)
{
printf("\t\t 書名:%s 本數 %d 是否可借 %s\n", g_book[i]->name, g_book[i]->count,
(g_book[i]->flag == 1) ? "可借" : "不可借");
}
printf("\n");
printf("\t\t具體個人的借閱情況請在返回主菜單后到借閱情況\n");
sleep(4);
}
/*
函數描述: 查詢圖書館的所有書本
函數參數: 無
函數返回值:無
*/
void SearchBook()
{ char s[32]={0};
printf("\t\t輸入你要查詢的書名:\n");
scanf("%s",s);
int i=TraverseBook(s);
if (i == -1) //未查詢到結果
{
printf("\t\t圖書館內無此書!\n");
return;
}
else
{
if (g_book[i]->flag == 1) //可借
{
printf("\t\t可借,還有%d本。\n",g_book[i]->count);
}
else
{
printf("\t\t此書已被借完!\n"); //不可借
}
}
}
/*
函數描述:根據id查找學生是否存在
函數參數:學號
函數返回值:學生存在返回下標 學生不存在返回-1
*/
int TraverseStudent(int id)
{
int i;
for (i = 0; i < g_StudentCount; i++)
{
if (id == g_student[i]->id)
{
// printf("存在!"); //調試用
return i;
}
}
return -1;
}
/*
函數描述:根據書名判斷書是否存在
函數參數:書名
函數返回值:存在則返回書本的下標,不存在返回-1
*/
int TraverseBook(char *n)
{
int i;
for (i = 0; i < g_BookCount; i++)
{
if (strcmp(n, g_book[i]->name) == 0)
{
return i;
}
}
return -1;
}
/*
函數描述:增加書籍
函數參數:無
函數返回值:無
*/
void AddBook()
{
printf("\t\t請輸入要增加的書籍名稱:\n");
char name[32] = {0};
int num;
scanf ("%s",name);
//判斷書籍是否已經存在
int result = TraverseBook(name);
if (result != -1)
{
printf("\t\t該書已存在,請輸入你要增加的數量:\n");
scanf("%d",&num);
g_book[result]->count +=num;
printf("書籍增加成功,該書現在有%d本\n",g_book[result]->count);
return;
}
//書籍不存在,分配空間
else
{ g_BookCount ++;
g_book[g_BookCount - 1] = (book *)malloc(sizeof(book)*1);
if (NULL == g_book[g_BookCount - 1])
{
printf("malloc failure!\n");
}
g_book[g_BookCount -1]->flag = 1;
}
strcpy(g_book[g_BookCount -1]->name,name);
printf("\t\t請輸入要增加的本數:\n");
scanf ("%d",&num);
g_book[g_BookCount - 1]->count = num;
printf("\t\t增加成功,該書現在有%d本\n",num);
return;
}
/*
函數描述:刪除輸入的書目
函數參數:無
函數返回值:無
*/
void DeleteBook()
{
int i,result=0;
printf("\t\t請輸入要刪除的書目:\n");
char name[32]={0};
scanf ("%s",name);
result = TraverseBook(name);
if (-1 == result)
{
printf("\t\t書本不存在,刪除失敗!\n");
return ;
}
else
{
for (i = result;i < g_BookCount;i++)
{
g_book[i] = g_book[i+1];
}
g_BookCount--;
printf("\t\t刪除成功!\n");
}
}
/*
函數描述:借書部分
函數參數:無
函數返回值:無
*/
void BorrowBook()
{
int id = 0;
printf("\t\t請輸入學號:\n");
scanf("%d", &id);
//遍歷g_student數組,判斷學生是否借過書
int result = TraverseStudent(id); //判斷id是否存在
int sturesult = result;
if (result == -1) //不存在,給新的學生申請內存
{
g_StudentCount ++;
g_student[g_StudentCount - 1] = (stu *)malloc(sizeof(stu) * 1);
if (NULL == g_student[g_StudentCount - 1])
{
printf("malloc fialure!\n");
return;
}
printf("\t\t要借哪本書:\n");
char name[32] = {0};
scanf("%s", name);
//判斷書在不在
result = TraverseBook(name);
if (-1 == result) //書本不存在
{
printf("\t\t書本不存在,借書失敗\n");
return;
}
else //書本存在
{
if (g_book[result]->flag == 0)
{
printf("\t\t該書已經被借完!\n");
}
else
{
g_book[result]->count--;
if (0 == g_book[result]->count)
{
g_book[result]->flag = 0; //書本不可借
}
g_student[g_StudentCount - 1]->id = id; //記錄學生的學號
g_student[g_StudentCount - 1]->book[g_student[g_StudentCount - 1]->count] = (char *)malloc(sizeof(char) * 128);
if (NULL == g_student[g_StudentCount - 1]->book)
{
printf("malloc failure!\n");
return;
}
strcpy(g_student[g_StudentCount - 1]->book[g_student[g_StudentCount - 1]->count], name); //保存書名
g_student[g_StudentCount - 1]->count++; //借書的本數加一
printf("\t\t借書成功\n");
}
}
}
//存在 判斷借書是否達到上限
else
{
if (g_student[sturesult]->count == 5 )
{
printf("借書已達上限!\n");
return;
}
else
{
printf("\t\t要借哪本書?\n");
char name1[32]={0};
scanf("%s",name1);
//判斷書是否存在
int result1 = TraverseBook(name1);
{
if (-1==result1)//書本不存在
{
printf("\t\t書本不存在,借書失敗!\n");
return;
}
else
{ if (g_book[result1]->flag == 0 )
{
printf("\t\t該書已經被借完!\n");
}
else
{ //書本存在
g_book[result1]->count--;
if (0 == g_book[result1]->count)
{
g_book[result]->flag = 0; //書本不可借
}
// printf("\t\t測試段錯誤用\n");
//給這個學生的書名分配內存
g_student[sturesult]->book[g_student[sturesult]->count] = (char*)malloc(sizeof(char)*128);
if (NULL == g_student[sturesult]->book)
{
printf("malloc failure!\n");
return;
}
strcpy(g_student[sturesult]->book[g_student[sturesult]->count],name1);
/*sturesult: 由于前部分result一開始代表的是學號下標,后來改成了書名下標,這里取學號下標保存 */
g_student[sturesult]->count++;
printf("\t\t借書成功!\n");
}
}
}
}
}
}
/*
函數描述:根據學號以及書名確定還書情況
函數參數:無
函數返回值:無
*/
void Payback()
{
int id,sturesult,bookresult=-1,i;
char name[32] = {0};
printf("\t\t請輸入學號:\n");
scanf("%d",&id);
//判斷學號是否存在
sturesult = TraverseStudent(id);
if (sturesult == -1)
{
printf("\t\t學號有誤!\n");
return;
}
else
{
printf("\t\t你要還哪本書?\n");
scanf ("%s",name);
//判斷書是否存在
for(i = 0;i < g_student[sturesult]->count;i++)
{ //判斷書名與所借書名是否相同
if (strcmp(name,g_student[sturesult]->book[i]) == 0)
{
bookresult = i;
break;
}
}
if ( bookresult == -1)
{
printf("\t\t你沒有借過這本書!\n");
}
else
{
//學生書名刪除
for (i = bookresult;i<g_student[sturesult]->count;i++)
{
g_student[sturesult]->book[i]=g_student[sturesult]->book[i+1];
}
g_student[sturesult]->count--;//學生借書冊數減一
int x = TraverseBook(name);
g_book[x]->count++; //圖書館書加一
printf("\t\t還書成功!\n");
}
}
}
/*
函數描述:根據輸入學號查看該學生的借書情況
函數參數:無
函數返回值:無
*/
void Status()
{
int id,result,i;
printf("\t\t請輸入你的學號:\n");
scanf("%d",&id);
result= TraverseStudent(id);
if (-1 == result)
{
printf("\t\t該學號不存在!\n");
}
else
{
printf("\t\t借書數量:%d\n",g_student[result]->count);
for(i = 0;i < g_student[result]->count;i++)
{
printf("\t\t書名:%s\n",g_student[result]->book[i]);
}
}
}
int main()
{
char choice[2] = {0};
init();
while (1)
{
menu();
scanf("%s", choice);
switch(choice[0])
{
case '1':
AddBook();
sleep(4);
break;
case '2':
DeleteBook();
sleep(4);
break;
case '3':
ShowBook();
break;
case '4':
SearchBook();
sleep(4);
break;
case '5':
BorrowBook();
sleep(4);
break;
case '6':
Payback();
sleep(4);
break;
case '7':
Status();
sleep(4);
break;
case '8':
exit(0);
break;
}
}
return 0;
}
復制代碼
作者:
faithblue
時間:
2020-6-2 15:01
能發源碼嘛
歡迎光臨 (http://m.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
福利小视频在线观看
|
欧美一区二区三区在线观看
|
欧美亚洲一区二区三区
|
99一区二区
|
亚洲成人毛片
|
国产一区精品视频
|
日本欧美久久久久免费播放网
|
国产在线天堂
|
国产欧美日韩综合
|
日韩手机在线视频
|
午夜激情影视
|
欧美色综合天天久久综合精品
|
91看片在线观看
|
综合一区二区三区
|
波多野结衣乳巨码无在线观看
|
人人爱人人插
|
亚洲男人天堂av
|
午夜激情在线观看
|
精东影业一区二区三区
|
国产成人精品三级麻豆
|
性做久久久久久
|
欧美在线观看视频
|
天天草天天射
|
亚洲男人在线
|
www.com国产
|
四虎在线免费观看视频
|
www.久久爱
|
国产一级片网站
|
精品国产欧美
|
久操精品视频
|
中文字幕日韩一区
|
亚洲在线视频
|
国产一区二区免费看
|
久久久久久久久久久国产
|
四虎永久在线视频
|
成人一级毛片
|
日韩欧美一区二区在线观看
|
最新国产在线视频
|
久久久亚洲精品视频
|
涩涩在线
|
亚洲美女在线视频
|