日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Java用链表写图书管理_C语言链表实现图书管理系统

發布時間:2024/7/5 windows 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java用链表写图书管理_C语言链表实现图书管理系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前參照網上的資料用鏈表實現了圖書管理系統,包括簡單的增刪改查功能以及借書還書功能,我是VC6.0下寫的一個控制臺程序,格式參照的網上的。在動手編碼之前,你需要理清自己的思路。首先,需要確定圖書館里系統中主要有那幾個對象,這里我寫了學生對象和圖書對象。不妨在紙上寫出或畫出它們主要包括哪些屬性以及其可能的對應關系,這里根據不同人的要求會有所不同。清楚這些之后,就可以設計學生和圖書的數據結構,比如這里我用的結構體存儲其信息。然后就需要考慮,我想要哪些功能,除了基本的增刪改查之外,我還想要哪些功能?比如借書、還書,我怎么表示這之間的關系?可以通過圖書的屬性來記錄該書的狀態,及是否被借走,誰借了。主要就是這個思路,圖書的增刪改查是通過鏈表實現的,當然也可以用數組實現,只不過那會浪費較多的空間。

// MyLibManSys.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "iostream"

struct book{

int id;

char title[20];

char author[20];

double price;

char state[20];

int student_id;

char student_name[20];

struct book* next;

};

struct student{

int id;

char name[20];

char sex[10];

char borrow_book[30];

struct student* next;

};

void Print_Book(struct book *head_book);

void Print_Student(struct student *head_student);

struct book *Create_New_Book();/*創建新的圖書庫*/

struct student *Create_New_Student();/*創建新的學生庫*/

struct book *Insert_Book(struct book *head_book,struct book *new_book);/*增加圖書,逐個添加*/

//void Insert_Book(struct book *head_book,struct book *new_book);/*增加圖書,逐個添加*/

//函數的參數是一個指針時,不要在函數體內部改變指針所指的地址,那樣毫無作用,需要修改的只能是指針所指向的內容。即應把指針當作常量

struct student *Insert_Student(struct student *head_student,struct student *new_student);/*增加學生,逐個添加*/

struct book *Search_Book_ById(int id,struct book *head_book);

struct book *Search_Book_ByTitle(char *title,struct book *head_book);

struct book *Search_Book_ByPrice(double price_h,double price_l,struct book *head_book);

//bool Delete_Book(int id,book* head_book);

struct book* Delete_Book(int id,book* head_book);

struct student *Search_Student(int id,struct student *head_student);

struct student* Delete_Student(int id,student* head_student);

void Lent_Book(int id,int student_id,struct book *head_book,struct student *head_student);

void Back_Book(int id,int student_id,struct book *head_book,struct student *head_student);

int main()

{

struct book* head_book,*p_book;

struct student* head_student, *p_student;

int choice, f, id, student_id;

int m = 1;

char name[20],sex[10];

char title[20];

double price_h,price_l,price;

char author[20];

int size_book=sizeof(struct book);

int size_student=sizeof(struct student);

printf("\n歡迎您第一次進入圖書管理系統!\n\n");

printf("----->[向導]----->[新建圖書庫]\n\n");

printf("注意:當輸入圖書編號為0時,進入下一步.\n\n");

head_book=Create_New_Book();

system("cls");

//Print_Book(head_book);

printf("\n歡迎您第一次進入圖書管理系統!\n\n");

printf("----->[向導]----->[新建會員庫]\n\n");

printf("注意:當輸入會員學號為0時,進入主菜單.\n\n");

head_student=Create_New_Student();

system("cls");

//Print_Student(head_student);

do{

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("\n");

printf("\t\t\t[1]:借書辦理\t");printf(" [6]:還書辦理\n");

printf("\n");

printf("\t\t\t[2]:查詢圖書\t");printf(" [7]:查詢學生\n");

printf("\t\t\t[3]:添加圖書\t");printf(" [8]:添加學生\n");

printf("\t\t\t[4]:刪除圖書\t");printf(" [9]:刪除學生\n");

printf("\t\t\t[5]:遍歷圖書\t");printf("[10]:遍歷學生\n\n");

printf("\t\t\t〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n\n");

printf("\t\t\t0:退出\n\n");

printf("請選擇<0~10>:");

scanf("%d",&choice);

switch(choice){

case 0:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("\n謝謝您的使用!\n\n");

break;

case 1:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("輸入借出圖書編號:\n");

scanf("%d",&id);

printf("輸入借入學生學號:\n");

scanf("%d",&student_id);

Lent_Book(id,student_id,head_book,head_student);

break;

case 2:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("1.按編號查詢\n\n");

printf("2.按名稱查詢\n\n");

printf("3.按價格區間查詢\n\n");

printf("0.返回主菜單\n\n");

printf("請選擇:");

scanf("%d",&f);

if(f==1){

printf("請輸入查詢圖書編號:");

scanf("%d",&id);

printf("相關信息如下:\n\n");

head_book=Search_Book_ById(id,head_book);

break;

}

else if(f==2){

getchar();

printf("請輸入查詢圖書名稱:");

gets(title);

printf("相關信息如下:\n\n");

head_book=Search_Book_ByTitle(title,head_book);

break;

}

else if(f==3){

printf("請輸入最高價格:");

scanf("%lf",&price_h);

printf("請輸入最低價格:");

scanf("%lf",&price_l);

printf("相關信息如下:\n\n");

head_book=Search_Book_ByPrice(price_h,price_l,head_book);

break;

}

else if(f==0){

break;

}

break;

case 3:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("請輸入圖書編號:");

scanf("%d",&id);

printf("請輸入圖書名稱:");

scanf("%s",title);

printf("請輸入作者名字:");

scanf("%s",author);

printf("請輸入單價:");

scanf("%lf",&price);

printf("\n");

struct book *ptr_b;

for(ptr_b=head_book;ptr_b;ptr_b=ptr_b->next)

{

if(ptr_b->id==id)

{

printf("此編號圖書已存在\n");

m=0;

break;

}

}

if(m){

p_book=(struct book *)malloc(size_book);

strcpy(p_book->title,title);

p_book->id=id;

p_book->price=price;

p_book->student_id=-1;

strcpy(p_book->author,author);

strcpy(p_book->state,"存在");

strcpy(p_book->student_name,"待定");

// head_book=Insert_Book(head_book,p_book);

Insert_Book(head_book,p_book);

printf("\n添加圖書成功!\n\n");

}

break;

case 4:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("輸入刪除圖書編號:\n");

scanf("%d",&id);

/*if(Delete_Book(id,head_book)){

printf("\n刪除圖書成功!\n\n");

}else{

printf("刪除失敗");

}*/

head_book = Delete_Book(id,head_book);

break;

case 5:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

Print_Book(head_book);

break;

case 6:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("輸入歸還圖書編號:\n");

scanf("%d",&id);

printf("輸入歸還學生學號:\n");

scanf("%d",&student_id);

Back_Book(id,student_id,head_book,head_student);

break;

case 7:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("請輸入查詢學生學號:");

scanf("%d",&id);

printf("相關信息如下:\n\n");

head_student=Search_Student(id,head_student);

break;

case 8:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("請輸入學生編號:");

scanf("%d",&id);

printf("請輸入學生姓名:");

scanf("%s",name);

printf("請輸入學生性別:");

scanf("%s",sex);

printf("\n");

struct student *ptr_s;

for(ptr_s=head_student;ptr_s;ptr_s=ptr_s->next)

{

if(ptr_s->id==id)

{

printf("此學號學生已存在\n");

m=0;

break;

}

}

if(m){

p_student=(struct student *)malloc(size_student);

p_student->id=id;

strcpy(p_student->name,name);

strcpy(p_student->sex,sex);

strcpy(p_student->borrow_book,"無");

head_student=Insert_Student(head_student,p_student);

printf("\n添加學生成功!\n\n");

}

break;

case 9:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

printf("輸入刪除學生學號:\n");

scanf("%d",&id);

head_student = Delete_Student(id,head_student);

break;

case 10:

system("cls");

printf("\n\t\t\t〓〓〓〓〓圖書管理系統〓〓〓〓〓\n\n");

Print_Student(head_student);

}

}while(choice!=0);

return 0;

}

struct book *Create_New_Book(){

struct book *head_book,*p_book;

int id, tag;

double price;

char title[20],author[20];

int size_book=sizeof(struct book);

head_book=NULL;

printf("請輸入圖書編號:");

scanf("%d",&id);

printf("請輸入圖書名稱:");

scanf("%s",title);

printf("請輸入作者名字:");

scanf("%s",author);

printf("請輸入單價:");

scanf("%lf",&price);

printf("\n");

while(true){

p_book=(struct book *)malloc(size_book);

strcpy(p_book->title,title);

p_book->id=id;

p_book->price=price;

p_book->student_id=-1;

strcpy(p_book->author,author);

strcpy(p_book->state,"存在");

strcpy(p_book->student_name,"待定");

head_book=Insert_Book(head_book,p_book);

printf("是否繼續?繼續輸入1,退出按任意鍵\n");

scanf("%d",&tag);

if(tag!=1){

break;

}

printf("請輸入圖書編號:");

scanf("%d",&id);

printf("請輸入圖書名稱:");

scanf("%s",title);

printf("請輸入作者名字:");

scanf("%s",author);

printf("請輸入單價:");

scanf("%lf",&price);

printf("\n");

}

return head_book;

}

struct student *Create_New_Student(){

struct student *head_student,*p_student;

int id, tag;

char sex[10];

char name[20];

int size_student=sizeof(struct student);

head_student=NULL;

printf("請輸入學生編號:");

scanf("%d",&id);

printf("請輸入學生姓名:");

scanf("%s",name);

printf("請輸入學生性別:");

scanf("%s",sex);

printf("\n");

while(true){

p_student=(struct student *)malloc(size_student);

p_student->id=id;

strcpy(p_student->name,name);

strcpy(p_student->sex,sex);

strcpy(p_student->borrow_book,"無");

head_student=Insert_Student(head_student,p_student);

printf("是否繼續?繼續輸入1,退出按任意鍵\n");

scanf("%d",&tag);

if(tag!=1){

break;

}

printf("請輸入學生編號:");

scanf("%d",&id);

printf("請輸入學生姓名:");

scanf("%s",name);

printf("請輸入學生性別:");

scanf("%s",sex);

printf("\n");

}

return head_student;

}

struct book *Insert_Book(struct book *head_book,struct book *new_book){

struct book *p,*q;

p=q=head_book;

if(head_book==NULL){ //單向鏈表為空的情況

head_book=new_book;

new_book->next = NULL;

}else{

while((new_book->id>p->id)&&(p->next!=NULL)){

q = p;

p = p->next;

}

if(new_book->id<=p->id){

new_book->next=p;

if(head_book==p)

head_book=new_book;

else

q->next = new_book;

}else{

p->next=new_book;

new_book->next=NULL;

}

}

return head_book;

};

struct student *Insert_Student(struct student *head_student,struct student *new_student){

struct student *p,*q;

p=q=head_student;

if(head_student==NULL){ //單向鏈表為空的情況

head_student=new_student;

new_student->next = NULL;

}else{

while((new_student->id>p->id)&&(p->next!=NULL)){

q = p;

p = p->next;

}

if(new_student->id<=p->id){

new_student->next=p;

if(head_student==p)

head_student=new_student;

else

q->next = new_student;

}else{

p->next=new_student;

new_student->next=NULL;

}

}

return head_student;

}

struct book *Search_Book_ById(int id,struct book *head_book){

struct book *ptr_book = head_book;

int flag=0;

while(ptr_book!=NULL)

{

if(ptr_book->id==id){

printf("圖書編號:%d\n",ptr_book->id);

printf("圖書名稱:%s\n",ptr_book->title);

printf("圖書單價:%.2lf\n",ptr_book->price);

printf("圖書作者:%s\n",ptr_book->author);

printf("存在狀態:%s\n",ptr_book->state);

printf("借書人姓名:%s\n",ptr_book->student_name);

printf("學號:%d\n",ptr_book->student_id);

printf("\n");

flag++;

}

if(flag>0)

{

break;

}

ptr_book = ptr_book->next;

}

if(flag==0){

printf("暫無此圖書信息!\n\n");

}

return head_book;

};

struct book *Search_Book_ByTitle(char *title,struct book *head_book){

struct book *ptr_book = head_book;

int flag=0;

while(ptr_book!=NULL)

{

if(strcmp(ptr_book->title,title)==0){

printf("圖書編號:%d\n",ptr_book->id);

printf("圖書名稱:%s\n",ptr_book->title);

printf("圖書單價:%.2lf\n",ptr_book->price);

printf("圖書作者:%s\n",ptr_book->author);

printf("存在狀態:%s\n",ptr_book->state);

printf("借書人姓名:%s\n",ptr_book->student_name);

printf("學號:%d\n",ptr_book->student_id);

printf("\n");

flag++;

}

if(flag>0)

{

break;

}

ptr_book = ptr_book->next;

}

if(flag==0){

printf("暫無此圖書信息!\n\n");

}

return head_book;

};

struct book *Search_Book_ByPrice(double price_h,double price_l,struct book *head_book){

struct book *ptr_book = head_book;

int flag=0;

while(ptr_book!=NULL)

{

if(ptr_book->price>=price_l&&ptr_book->price<=price_h){

printf("圖書編號:%d\n",ptr_book->id);

printf("圖書名稱:%s\n",ptr_book->title);

printf("圖書單價:%.2lf\n",ptr_book->price);

printf("圖書作者:%s\n",ptr_book->author);

printf("存在狀態:%s\n",ptr_book->state);

printf("借書人姓名:%s\n",ptr_book->student_name);

printf("學號:%d\n",ptr_book->student_id);

printf("\n");

flag++;

}

ptr_book = ptr_book->next;

}

if(flag==0){

printf("暫無此圖書信息!\n\n");

}

return head_book;

}

/*bool Delete_Book(int id,book* head_book){

bool flag=true;

struct book *p,*q;

p=q=head_book;

if(p->id==id&&p->next==NULL){

head_book=NULL;

}

while(p->id!=id&&p->next!=NULL){

q=p;

p=p->next;

}

if(p->id==id){

if(p==head_book){

head_book=p->next;

}else{

q->next=p->next;

}

free(p);

}else{

flag=false;

printf("找不到該書");

}

return flag;

};*/

struct book* Delete_Book(int id,book* head_book){

bool flag=true;

struct book *p,*q;

p=q=head_book;

while(p->id!=id&&p->next!=NULL){

q=p;

p=p->next;

}

if(p->id==id){

if(p==head_book){

head_book=p->next;

}else{

q->next=p->next;

}

free(p);

printf("刪除成功!\n");

}else{

flag=false;

printf("找不到該書");

}

return head_book;

};

struct student* Delete_Student(int id,student* head_student){

bool flag=true;

struct student *p,*q;

p=q=head_student;

while(p->id!=id&&p->next!=NULL){

q=p;

p=p->next;

}

if(p->id==id){

if(p==head_student){

head_student=p->next;

}else{

q->next=p->next;

}

free(p);

printf("刪除成功!\n");

}else{

flag=false;

printf("找不到該學生");

}

return head_student;

};

struct student *Search_Student(int id,struct student *head_student){

struct student *ptr_student = head_student;

int flag=0;

while(ptr_student!=NULL)

{

if(ptr_student->id==id){

printf("學號:%d\n",ptr_student->id);

printf("姓名:%s\n",ptr_student->name);

printf("性別:%s\n",ptr_student->sex);

printf("借書:%s\n",ptr_student->borrow_book);

printf("\n");

flag++;

}

if(flag>0)

{

break;

}

ptr_student = ptr_student->next;

}

if(flag==0){

printf("暫無此學生信息!\n\n");

}

return head_student;

};

void Lent_Book(int id,int student_id,struct book *head_book,struct student *head_student){

struct book* p=head_book;

struct student* q=head_student;

if(p==NULL||q==NULL){

printf("書本或學生不存在\n");

return;

}

while(p!=NULL&&q!=NULL){

if(p->id!=id){

p=p->next;

}

if(q->id!=student_id){

q=q->next;

}

if(p->id==id&&q->id==student_id){

break;

}

}

if(p==NULL||q==NULL){

printf("書本或學生不存在\n");

return;

}else{

if(strcmp(p->state,"存在")!=0){

printf("書已借出!抱歉!");

return;

}else{

p->student_id=student_id;

strcpy(p->student_name,q->name);

strcpy(q->borrow_book,p->title);

strcpy(p->state,"已借出");

printf("已成功借出!/n");

}

}

};

void Back_Book(int id,int student_id,struct book *head_book,struct student *head_student){

struct book* p=head_book;

struct student* q=head_student;

if(p==NULL||q==NULL){

printf("書本或學生不存在\n");

return;

}

while(p!=NULL&&q!=NULL){

if(p->id!=id){

p=p->next;

}

if(q->id!=student_id){

q=q->next;

}

if(p->id==id&&q->id==student_id){

break;

}

}

if(p==NULL||q==NULL){

printf("書本或學生不存在\n");

return;

}else{

if(strcmp(p->state,"存在")==0){

printf("書未借出!抱歉!");

return;

}else{

p->student_id=-1;

strcpy(p->student_name,"待定");

strcpy(q->borrow_book,"無");

strcpy(p->state,"存在");

printf("已成功歸還!/n");

}

}

};

void Print_Book(struct book *head_book){

struct book* p=head_book;

if(p==NULL){

printf("\n無記錄\n\n");

return;

}

printf("\n圖書編號\t圖書名稱\t圖書單價\t圖書作者\n\n");

while (p!=NULL)

{

printf("%d\t\t%s\t\t%.2lf\t\t%s\n\n",p->id,p->title,p->price,p->author);

p = p->next;

}

}

void Print_Student(struct student *head_student){

struct student* p=head_student;

if(p==NULL){

printf("\n無記錄\n\n");

return;

}

printf("\n學生姓名\t學生性別\t學生學號\n\n");

while (p!=NULL)

{

printf("%s\t\t%s\t\t%d\n",p->name,p->sex,p->id);

p = p->next;

}

}

代碼可以直接運行,這里我都是在控制臺上直接顯示的,如果想從文件讀取和向文件寫入學生和圖書信息,只需要把相應的printf和scanf部分改為文件操作。這個是很久之前寫的,詳細的函數以及功能講解這里就不介紹了。歡迎大家討論和指導。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java用链表写图书管理_C语言链表实现图书管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。