c語言課程設計圖書管理系統
㈠ C語言課程設計:圖書管理系統。求完整的代碼參考。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct BookInfo
{
char loginname[10];
char bookname[20];
char author[20];
char number[5];
char date[10];
double price;
};
struct Node
{
struct BookInfo book;
struct Node *next;
};
void option();
void select();
Node *head;
Node *pt[10];
FILE *fp;
//創建鏈表
/*
Node *CrtNode(int n)
{
Node *head; //聲明頭指針head
Node *p,*s;
head=new Node; //創建頭結點由head指向(空的頭結點)
s=head;
cout<<"請輸圖書信息:"<<endl;
for(int i=0;i<n;i++)
{
p=new Node; //創建一個結點
cout<<"登錄名:";
cin.getline(p->book.loginname,10);
strcat(p->book.loginname,"\n");
cout<<"書名:";
cin.getline(p->book.bookname,20);
strcat(p->book.bookname,"\n");
cout<<"作者名:";
cin.getline(p->book.author,20);
strcat(p->book.author,"\n");
cout<<"分類號:";
cin.getline(p->book.number,5);
strcat(p->book.number,"\n");
cout<<"出版日期:";
cin>>p->book.date;
strcat(p->book.date,"\n");
cout<<"價格:";
cin>>p->book.price;
s->next=p; //把創建的結點由s的next指向
s=p; //指針s的指向向後移一個結點
cin.clear();
cin.sync();
}
p->next=NULL; //最後一個結點的next指向空
return head; //返回頭指針
}
*/
/*
strcat(p->book.loginname,"\n");
strcat(p->book.bookname,"\n");
strcat(p->book.author,"\n");
strcat(p->book.number,"\n");
strcat(p->book.date,"\n");
*/
//1.插入
void Insert(Node *head)
{
Node *p,*s;
s = head;
cout<<"請輸入圖書信息:"<<endl;
p=new Node;
cin.clear();
cout<<"登錄名:";
cin.getline(p->book.loginname,10);
strcat(p->book.loginname,"\n");
cin.clear();
cin.sync();
cout<<"書名:";
cin.getline(p->book.bookname,20);
strcat(p->book.bookname,"\n");
cin.clear();
cin.sync();
cout<<"作者名:";
cin.getline(p->book.author,20);
strcat(p->book.author,"\n");
cin.clear();
cin.sync();
cout<<"分類號:";
cin.getline(p->book.number,5);
strcat(p->book.number,"\n");
cin.clear();
cin.sync();
cout<<"出版日期(yyyy-mm-dd):";
cin>>p->book.date;
strcat(p->book.date,"\n");
cout<<"價格:";
cin>>p->book.price;
while(s->next)
s = s->next;
s->next = p;
p->next = NULL;
}
//初始化
Node *Initial()
{
Node *head;
head = new Node;
head->next = NULL;
return head;
}
//2.顯示所有信息
void Show(Node *head)
{
int i = 1;
Node *p;
//顯示除頭結點以後所有結點(因為創建時頭結點為空)
p=head->next;
if(p == NULL)
{
cout<<"系統沒有儲存任何圖書信息,請輸入圖書信息後再進行其他操作!"<<endl;
}
else
{
cout<<"*********下面是所有的圖書信息**********"<<endl;
while(p!=NULL)
{
cout<<" 圖書"<<i<<": "<<p->book.bookname<<endl;
cout<<"登錄名:"<<p->book.loginname;
cout<<"書名:"<<p->book.bookname;
cout<<"作者名:"<<p->book.author;
cout<<"分類號:"<<p->book.number;
cout<<"出版日期:"<<p->book.date;
cout<<"價格:"<<p->book.price;
cout<<endl;
p=p->next;
i++;
cout<<endl;
}
}
cout<<"請按回車鍵返回菜單。"<<endl;
cin.get();
}
//3.查找
int findauthor(const Node *head)
{
Node *ps;
char author[20];
int count = 0;
ps = head->next;
cout<<"請輸入作者名:";
cin.getline(author,20);
strcat(author,"\n");
while(ps)
{
if(strcmp(ps->book.author,author) == 0)
{
pt[0] = ps;
count++;
}
ps = ps->next;
}
if(count == 0)
cout<<"查找的圖書不存在!"<<endl;
return count;
}
int findbookname(const Node *head)
{
Node *ps;
char bookname[20];
int count = 0;
int i = 0;
ps = head->next;
cout<<"請輸入書名:";
cin.getline(bookname,20);
strcat(bookname,"\n");
while(ps)
{
if(strcmp(ps->book.bookname,bookname) == 0)
{
pt[i] = ps;
count++;
i++;
}
if(ps->next ==NULL)
break;
ps = ps->next;
}
if(count == 0)
cout<<"查找的圖書不存在!"<<endl;
return count;
}
void Showarray(int n)
{
cout<<"*********下面是所查找的圖書信息**********"<<endl;
for(int i=0;i<n;i++)
{
cout<<" 圖書"<<i+1<<": "<<pt[i]->book.bookname;
cout<<"登錄名:"<<pt[i]->book.loginname;
cout<<"書名:"<<pt[0]->book.bookname;
cout<<"作者名:"<<pt[i]->book.author;
cout<<"分類號:"<<pt[i]->book.number;
cout<<"出版日期:"<<pt[i]->book.date;
cout<<"價格:"<<pt[i]->book.price;
cout<<endl;
}
cin.get();
}
int Find(Node *head)
{
int n,num;
system("cls");
if(head->next == NULL)
{
cout<<"系統沒有儲存任何圖書信息,請輸入圖書信息後再進行其他操作!"<<endl;
cin.get();
}
else
{
cout<<"請選擇查找方式(1.按作者名查找 2.按書名查找):";
cin>>n;
cin.clear();
cin.sync();
switch(n)
{
case 1:
num = findauthor(head);
if(num != 0)
Showarray(num);
break;
case 2:
num = findbookname(head);
if(num !=0)
Showarray(num);
break;
default:
cout<<"輸入有誤,請重新輸入!"<<endl;
cin.get();
system("cls");
option();
select();
}
}
return num;
}
//4.修改圖書信息
void Modify(Node *head)
{
Node *p,*q,*s;
p = head->next;
int i,n;
if(p == NULL)
cout<<"系統沒有儲存任何圖書信息,請輸入圖書信息後再進行其他操作!"<<endl;
else
{
cout<<"請輸入需要更正信息的圖書名:"<<endl;
n = findbookname(head);
Showarray(n);
loop:
{
cout<<"請選擇對第幾本書的信息進行修改:";
}
cin>>i;
cin.clear();
cin.sync();
if(i > 0 & i <= n)
{
cout<<"******修改第"<<i<<"本書的其他信息******"<<endl;
cout<<"登錄名:";
cin.getline(pt[i-1]->book.loginname,10);
strcat(pt[i-1]->book.loginname,"\n");
cin.clear();
cin.sync();
cout<<"作者名:";
cin.getline(pt[i-1]->book.author,20);
strcat(pt[i-1]->book.author,"\n");
cin.clear();
cin.sync();
cout<<"分類號:";
cin.getline(pt[i-1]->book.number,5);
strcat(pt[i-1]->book.number,"\n");
cin.clear();
cin.sync();
cout<<"出版日期(yyyy-mm-dd):";
cin>>pt[i-1]->book.date;
strcat(pt[i-1]->book.date,"\n");
cout<<"價格:";
cin>>pt[i-1]->book.price;
}
else
{
cout<<"選擇的書的序號有誤!";
goto loop;
}
}
cin.get();
}
//5.刪除圖書信息
void Deletebook(Node *head)
{
Node *p,*q;
char bookname[20];
int count = 0;
int i = 0;
p = head;
q = p->next;
if(q == NULL)
cout<<"系統沒有任何圖書信息!"<<endl;
else
{
cout<<"請輸入書名:";
cin.getline(bookname,20);
strcat(bookname,"\n");
while(q != NULL)
{
q = p->next;
if(strcmp(q->book.bookname,bookname) == 0)
{
p->next = q->next;
delete q;
count++;
}
p = q;
q = q->next;
}
if(count == 0)
cout<<"沒有找到該圖書信息!"<<endl;
else
cout<<"圖書信息已經刪除!"<<endl;
}
cin.get();
}
//鏈表長度
int Len(const Node *head)
{
Node *ps;
int count = 0;
ps = head->next;
while(ps)
{
count++;
ps = ps->next;
}
return count;
}
//6.保存文件
void save(Node *head)
{
Node *p;
char st[20];
p = head->next;
if((fp=fopen("book.dat", "w"))==NULL)
{
cout<<"打開文件失敗"<<endl;
return;
}
char t[255];
//將 L1.listlen() 賦予字元串中的數字
int lenth = Len(head);
fwrite(&lenth,sizeof(int),1,fp);
while(p)
{
fwrite(p,sizeof(struct Node),1,fp);
p = p->next;
}
fclose(fp);
cout<<"文件已經保存,請按ENTER鍵退出!"<<endl;
cin.get();
}
//8.刪除所有圖書信息
void Free_List(Node *head)
{
Node *p;
p = head->next;
while(p)
{
delete p;
p = head->next;
}
}
/*
void readstr(FILE *f,char *string)
{
do
{
//①: 先讀入一行文本
fgets(string, 255, f); //fgets(): 從文件 f 讀入長度為 255-1 的字元串
// 並存入到 string 中
} while ((string[0] == '/') || (string[0] == '\n'));
return;
}
*/
//讀取文件
Node *Load()
{
char c[255];
int lenth;
Node *p,*q;
head = new Node;
p = head;
if((fp=fopen("book.dat", "r"))==NULL)
{
cout<<"打開文件失敗"<<endl;
return head;
}
else
fread(&lenth,sizeof(int),1,fp);
for(int i = 0;i < lenth;i++)
{
q = new Node;
fread(q,sizeof(struct Node),1,fp);
p->next = q;
p = q;
}
p->next = NULL;
fclose(fp);
return head;
}
//9.菜單選項
void select()
{
int m,n;
cout<<"請輸入以下數字來選擇所需要的操作(1~8):";
cin.clear();
cin.sync();
cin>>n;
cin.clear();
cin.sync();
switch(n)
{
case 1:
system("cls");
Insert(head);
break;
case 2:
system("cls");
Show(head);
break;
case 3:
system("cls");
Find(head);
break;
case 4:
system("cls");
Modify(head);
break;
case 5:
system("cls");
Deletebook(head);
break;
case 6:
save(head);
break;
case 7:
exit(0);
break;
case 8:
Free_List(head);
break;
default:
break;
}
}
//10.菜單界面
void option()
{
cout<<" 圖書信息管理系統"<<endl;
cout<<"*****************************************************"<<endl;
cout<<" 1.輸入圖書信息 2.輸出圖書信息 "<<endl;
cout<<" 3.查找圖書信息 4.修改圖書信息 "<<endl;
cout<<" 5.刪除圖書信息 6.保存圖書信息 "<<endl;
cout<<" 7.退出系統 "<<endl;
cout<<"*****************************************************"<<endl;
}
int main()
{
//head = Initial();
head = Load();
while(1)
{
system("cls");
option();
select();
}
return 0;
}
㈡ 跪求一個C語言圖書信息管理系統的課程設計
通過測試!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 100
struct Student
{
int num;/*登錄號*/
char name[100];/*書名*/
char author[100];/*作者名*/
char fn[100];/*分類號*/
char place[100];/*出版單位*/
char day[100];/*出版時間*/
float money;/*價格*/
}stu[MAX];
main()/*主函數*/
{
void Input();/*輸入*/
void Display();/*輸出*/
void Find();/*查找*/
void Modify();/*刪除*/
void Change();/*修改*/
int n;
for(;;)
{
printf("\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("※ ※\n");
printf("※ 歡迎來到圖書信息管理系統 ※\n");
printf("※ ※\n");
printf("※ ※\n");
printf("※ 主菜單 ※\n");
printf("※ ※\n");
printf("※ ▲1.圖書信息錄入 ▲ ※\n");
printf("※ ※\n");
printf("※ ▲2.圖書信息瀏覽 ▲ ※\n");
printf("※ ※\n");
printf("※ ▲3.圖書信息查詢 ▲ ※\n");
printf("※ ※\n");
printf("※ ▲4.圖書信息刪除 ▲ ※\n");
printf("※ ※\n");
printf("※ ▲5.圖書信息修改 ▲ ※\n");
printf("※ ※\n");
printf("※ ▲6.退出系統 ▲ ※\n");
printf("※ ※\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n\n");
printf("請輸入選擇項(1-6):");
scanf("%d",&n);
printf("\n\n\n\n");
if(n>0&&n<7)
{
switch(n)
{
case 1:Input();break;
case 2:Display();break;
case 3:Find();break;
case 4:Modify();break;
case 5:Change();break;
case 6:printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("※ ※\n");
printf("※ 謝謝使用! ※\n");
printf("※ 再見! ※\n");
printf("※ ※\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
exit(0);
}
}
else
{
printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("※ ※\n");
printf("※ 輸入錯誤! ※\n");
printf("※ 請退出! ※\n");
printf("※ ※\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
break;
}
}
}
void Find()/*查找*/
{
FILE *fp;
int i;
int choose,t;
char ans[100];
do
{
printf("1.按書名查找\n");
printf("2.按作者名查找\n");
printf("返回主菜單(其他數字)\n");
scanf("%d",&choose);
if(choose==1)
{
printf("輸入所查書名:\n");
scanf("%s",ans);
t=-1;
if(choose==1)
{
for(i=0;i<MAX;i++) if(strcmp(ans,stu[i].name)==0)
{
t=i;
fp=fopen("student","rb");
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)==1;i++)
printf("%d %s %s %s %s %s %f\n",stu[t].num,stu[t].name,stu[t].author,stu[t].fn,stu[t].place,stu[t].day,stu[t].money);
}
}
if(t==-1) printf("不存在該信息\n");
}
else if(choose==2)
{
printf("輸入所查作者名:\n");
scanf("%s",ans);
t=-1;
if(choose==2)
{
for(i=0;i<MAX;i++)
if(strcmp(ans,stu[i].author)==0)
{ t=i;
fp=fopen("student","rb");
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)==1;i++)
printf("%d %s %s %s %s %s %f\n",stu[t].num,stu[t].name,stu[t].author,stu[t].fn,stu[t].place,stu[t].day,stu[t].money);
}
}
if(t==-1) printf("不存在該信息\n");
}
else return;
}while(1);
}
void Display()/*輸出*/
{
FILE *fp;
int i;
fp=fopen("student","rb");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf(" 圖書列表\n");
printf("-----------------------------------------------------------\n");
printf("登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
printf("-----------------------------------------------------------\n");
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)==1;i++)
{
printf("%6d %8s %8s %8s %8s %8s %8.2f\n",stu[i].num,stu[i].name,stu[i].author,stu[i].fn,stu[i].place,stu[i].day,stu[i].money);
}
fclose(fp);
}
void Input()/*輸入*/
{
FILE *fp;
int n;
fp=fopen("student","wb");
for(n=0;n<MAX;n++)
{
printf("n=%d 輸入序號n(當輸入n=-1時,返回),n=",n++);
scanf("%d",&n);
if(n==-1)
{
fclose(fp);
return;
}
else
{
printf("請輸入登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
scanf("%d%s%s%s%s%s%f",&stu[n].num,stu[n].name,stu[n].author,stu[n].fn,stu[n].place,stu[n].day,&stu[n].money);
fwrite(&stu[n],sizeof(struct Student),1,fp);
}
}
fclose(fp);
}
void Modify()/*刪除*/
{
FILE *fp;
int i,flag,n,s,j;
fp=fopen("student","rb+");
rewind(fp);
printf(" 圖書列表\n");
printf("-----------------------------------------------------------\n");
printf("登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
printf("-----------------------------------------------------------\n");
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)==1;i++)
{
printf("%6d %8s %8s %8s %8s %8s %8.2f\n",stu[i].num,stu[i].name,stu[i].author,stu[i].fn,stu[i].place,stu[i].day,stu[i].money);
printf("\n");
}
n=i;
printf("輸入待刪除圖書號:\n");
scanf("%d",&s);
for(i=0,flag=1;flag&&i<n;i++)
{
if(s==stu[i].num)
{
for(j=i;j<n-1;j++)
{
stu[j].num=stu[j+1].num;
strcpy(stu[j].name,stu[j+1].name);
strcpy(stu[j].author,stu[j+1].author);
strcpy(stu[j].fn,stu[j+1].fn);
strcpy(stu[j].place,stu[j+1].place);
strcpy(stu[j].day,stu[j+1].day);
stu[j].money=stu[j+1].money;
}
flag=0;
}
}
if(!flag)
n=n-1;
else
printf("沒有此號\n");
fp=fopen("student","wb");
for(i=0;i<n;i++)
fwrite(&stu[i],sizeof(struct Student),1,fp);
fclose(fp);
fp=fopen("student","r");
printf(" 圖書列表\n");
printf("-----------------------------------------------------------\n");
printf("登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
printf("-----------------------------------------------------------\n");
for(i=0;i<n;i++)
{
fread(&stu[i],sizeof(struct Student),1,fp);
printf("%6d %8s %8s %8s %8s %8s %8.2f\n",stu[i].num,stu[i].name,stu[i].author,stu[i].fn,stu[i].place,stu[i].day,stu[i].money);
printf("\n");
}
fclose(fp);
}
void Change()/*修改*/
{
FILE *fp;
int i,num,n;
int flag=0;
printf("請輸入要修改的圖書號:");
scanf("%d",&num);
for(i=0;i<=MAX;i++)
if(stu[i].num==num)
{
printf(" 圖書列表\n");
printf("-----------------------------------------------------------\n");
printf("登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
printf("-----------------------------------------------------------\n");
printf("%6d %8s %8s %8s %8s %8s %8.2f\n",stu[i].num,stu[i].name,stu[i].author,stu[i].fn,stu[i].place,stu[i].day,stu[i].money);
printf("-----------------------------------------------------------\n\n");
n=i;
flag=1;
break;
}
if(flag==0)
{
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf(" 輸入錯誤!\n");
printf(" 請返回!\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
return;
}
printf("\n\n\n");
fp=fopen("student","rb+");
fseek(fp,n*sizeof(struct Student),0);
printf("登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
scanf("%d%s%s%s%s%s%f",&stu[n].num,stu[n].name,stu[n].author,stu[n].fn,stu[n].place,stu[n].day,&stu[n].money);
fwrite(&stu[i],sizeof(struct Student),1,fp);
fclose(fp);
fp=fopen("student","rb");
printf(" 圖書列表\n");
printf("-----------------------------------------------------------\n");
printf("登錄號 書名 作者名 分類號 出版單位 出版時間 價格\n");
printf("-----------------------------------------------------------\n");
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)==1;i++)
{
printf("%6d %8s %8s %8s %8s %8s %8.2f\n",stu[i].num,stu[i].name,stu[i].author,stu[i].fn,stu[i].place,stu[i].day,stu[i].money);;
}
printf("-----------------------------------------------------------\n\n");
fclose(fp);
}
㈢ c語言課程設計 小型圖書管理系統設計
沒有按價格排序,自己加,可以運行
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct BOOK
{
int id,usr[10],total,store,days[10];
char name[31],author[21];
}books[100];
/*上面是結構體的定義,用於存放書籍及借書的信息。*/
void page_title(char *menu_item)
{
printf(">>> 圖 書 管 理 系 統 <<<\n\n- %s -\n\n",menu_item);
}
/*上面是列印頁眉的函數,同時通過參數menu_item,可以顯示當前的狀態。*/
void return_confirm(void)
{
printf("\n按任意鍵返回……\n");
getch();
}
/*上面是返回前請求確認的函數,以便在返回前觀察結果*/
int search_book(void)
{
int n,i;
printf("請輸入圖書序號:");
scanf("%d",&i);
for(n=0;n<100;n++)
{
if(books[n].id==i)
{
printf("書名:%s\n",books[n].name);
printf("作者:%s\n",books[n].author);
printf("存數:%d of ",books[n].store);
printf("%d\n",books[n].total);
return n;
}
}
printf("\n輸入錯誤或無效圖書序號.\n");
return -1;
}
/*上面的函數是在數組中找到圖書號匹配的記錄,顯示其信息並返
回數組下標,如果找不到相應記錄則提示錯誤並返回-1。*/
void book_out(void)
{
int n,s,l,d;
page_title("借閱圖書");
if((n=search_book())!=-1&&books[n].store>0)
{
printf("請輸入借書證序號:");
scanf("%d",&s);
printf("請輸入可借天數:");
scanf("%d",&d);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==0)
{
books[n].usr[l]=s;
books[n].days[l]=d;
break;
}
}
books[n].store--;
}
if(n!=-1&&books[n].store==0) printf("此書已經全部借出.\n");
return_confirm();
}
/*上面是借書的函數,首先調用找書函數*/
void book_in(void)
{
int n,s,l;
page_title("歸還圖書");
if((n=search_book())!=-1&&books[n].store<books[n].total)
{
printf("借閱者圖書證列表:\n");
for(l=0;l<10;l++)
if (books[n].usr[l]!=0)
printf("[%d] - %d天\n",books[n].usr[l],books[n].days[l]);
printf("請輸入借書證序號:");
scanf("%d",&s);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==s)
{
books[n].usr[l]=0;
books[n].days[l]=0;
break;
}
}
books[n].store++;
}
if(n!=-1&&books[n].store==books[n].total)
printf("全部入藏.\n");
return_confirm();
}
void book_add(void)
{
int n;
page_title("注冊新書");
for(n=0;n<100;n++)
if(books[n].id==0) break;
printf("序號:");
scanf("%d",&books[n].id);
printf("書名:");
scanf("%s",&books[n].name);
printf("作者:");
scanf("%s",&books[n].author);
printf("數量:");
scanf("%d",&books[n].total);
books[n].store=books[n].total;
return_confirm();
}
void book_del(void)
{
int n;
page_title("注銷舊書");
if((n=search_book())!=-1) books[n].id=0;
printf("該書已注銷.\n");
return_confirm();
}
void main(void)
{
menu: page_title("操作選單");
printf("請用數字鍵選擇操作\n\n");
printf("\t\t1 借閱圖書\t2 歸還圖書\n");
printf("\t\t3 注冊新書\t4 注銷舊書\n");
printf("\t\t0 退出\n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_in();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '0' : exit(0);
}
goto menu;
}
㈣ c語言課程設計:圖書管理系統設計的基本思路是什麼
圖書管理系統主要要求可以錄入書籍,添加書目,查找書本信息,刪除或修改信息,有的還要求顯示是否被借閱等。
一般採用結構體數組,鏈表,文件操作和自定義函數。主要是需要對基礎知識掌握牢固。
先定義結構體,然後對結構體的成員進行定義,選擇數組存儲書本各種信息。錄入信息可以用for和do while循環等來做。
存放信息需要文件操作函數,比如fopen,fwrite等。
刪除和添加可以刪除節點或者增加節點。
查找之類的可以用字元串操作的各種函數實現。
附上參考源代碼
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#definebooks"f:\books.txt"
#definebooksbak"f:\booksbak.txt"
structbookinfo
{
charisbn[20];
chartitle[30];
charauthor[20];
intcount;
};
structbook
{
structbookinfoonebook;
structbook*next;
};
structbook*searchBook(structbook*listptr,charisbn[])
{
while(listptr!=(structbook*)0)
if(strcmp(listptr->onebook.isbn,isbn)==0)
returnlistptr;
else
listptr=listptr->next;
return(structbook*)0;
}
voidMainSearchbook(structbook*firstptr)
{
structbook*ptr;
charisbnno[20];
printf("請輸入ISBN:");
scanf("%s",&isbnno);
ptr=searchBook(firstptr,isbnno);
if(ptr!=(structbook*)0)
{
printf("找到了!!! ");
printf("ISBN:%s ",ptr->onebook.isbn);
printf("Title:%s ",ptr->onebook.title);
printf("Author:%s ",ptr->onebook.author);
}
else
printf("sorry,notfound!!! ");
}intaddBook(structbook*listptr,structbookinfonote)
{
while(listptr->next!=0)
listptr=listptr->next;
listptr->next=(structbook*)malloc(sizeof(structbook));
listptr->next->onebook=note;
listptr->next->next=0;
return0;
}
voidMainAdd(structbook*listptr,FILE*fp)
{
intok;
structbookinfonote;
printf("請輸入ISBN:");
scanf("%s",¬e.isbn);
printf("請輸入Title:");
scanf("%s",¬e.title);
printf("請輸入Author:");
scanf("%s",¬e.author);
ok=addBook(listptr,note);
if(ok==0)
{
//將加入的圖書寫到文件中保存
fprintf(fp," %s%s%s%d",note.isbn,note.title,note.author,0);
printf("添加圖書成功!!! ");
}
else
printf("添加圖書失敗!!! ");
}
intremoveBook(structbook*listptr,charisbn[])
{
while(listptr->next!=(structbook*)0)
{
if(strcmp(listptr->next->onebook.isbn,isbn)==0)
{
listptr->next=listptr->next->next;
return0;
}
else
listptr=listptr->next;
}
return-1;
}
voidMainRemove(structbook*listptr,FILE*fp)
/************************************************************************/
/*刪除書籍函數,通過ISBN刪除鏈表節點,同時刪除文件中對應信息*/
/*刪除文件中一行,用的是笨方法,把需要的信息寫到新文件,刪除舊文件,重命名..*/
/************************************************************************/
{
charisbnno[20];
intok;
structbookinfoonebook;
printf("請輸入ISBN:");
scanf("%s",&isbnno);
ok=removeBook(listptr,isbnno);
if(!ok)
{
FILE*fpbak;
if((fpbak=fopen(booksbak,"a+"))==NULL)
printf("文件打開失敗!!! ");
fseek(fp,0,SEEK_SET);//移到文件開始
while((fscanf(fp,"%s%s%s%d ",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)
{
if(strcmp(onebook.isbn,isbnno)!=0)
{
fprintf(fpbak,"%s%s%s%d ",onebook.isbn,onebook.title,onebook.author,onebook.count);
}
}
fclose(fp);
fclose(fpbak);
if(remove(books))//刪除失敗返回非0
{
printf("刪除文件失敗!!! ");
return;
}
else
if(rename(booksbak,books))//重命名失敗返回非0值
{
printf("重命名失敗!!! ");
return;
}
printf("刪除成功!!! ");
}
else
printf("查無此書!!!");
}
intchoice(void)
{
intc;
printf("1.查看圖書 ");
printf("2.添加圖書 ");
printf("3.刪除圖書 ");
printf("4.退出程序 ");
printf("請選擇序號:");
returnc=getchar();
//returnc=getche();
printf(" ");
}
intaddEntry(FILE*fp,structbook*firstptr)
/************************************************************************/
/*主要用來載入文件中存放的圖書信息*/
/************************************************************************/
{
structbookinfoonebook;
while((fscanf(fp,"%s%s%s%d ",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)
{
while(firstptr->next!=0)
firstptr=firstptr->next;
firstptr->next=(structbook*)malloc(sizeof(structbook));
firstptr->next->onebook=onebook;
firstptr->next->next=0;
}
return0;
}
intmain(intargc,char*argv[])
{
intch;
structbookfirst;
strcpy(first.onebook.isbn,"123456");
strcpy(first.onebook.title,"ProgrammingC");
strcpy(first.onebook.author,"yhb");
first.next=0;
structbook*firstptr=&first;//鏈表頭指針
FILE*fp;
if((fp=fopen(books,"a+"))==NULL)
printf("文件打開失敗!!!");
addEntry(fp,firstptr);
while(1)
{
system("CLS");//清屏
/************************************************************************/
/*想想這里為什麼要清空緩沖區?*/
/*由於上一次(choice函數)的getchar(),還有一個' '留在緩沖區....*/
/*可以把這句話注釋掉看看,沒有這句話會遇到麻煩*/
/*如果不用fflush,可以將上面的getchar()換成getche()*/
/*比較getchar(),getch(),getche()......*/
/************************************************************************/
fflush(stdin);
ch=choice()-48;
switch(ch)
{
case1:
MainSearchbook(firstptr);
break;
case2:
MainAdd(firstptr,fp);
break;
case3:
MainRemove(firstptr,fp);
break;
case4:
printf("謝謝使用... ");
exit(0);
default:
printf("請輸入正確序號!");
}
system("PAUSE");
}
return0;
}
㈤ C語言課程設計圖書館管理系統加講解
圖書管理系統代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct BOOK
{
int id,usr[10],total,store,days[10];
char name[31],author[21];
}books[100];
/*上面是結構體的定義,用於存放書籍及借書的信息。*/
void page_title(char *menu_item)
{
clrscr();
printf(">>> 圖 書 管 理 系 統 <<<\n\n- %s -\n\n",menu_item);
}
/*上面是列印頁眉的函數,同時通過參數menu_item,可以顯示當前的狀態。*/
void return_confirm(void)
{
printf("\n按任意鍵返回……\n");
getch();
}
/*上面是返回前請求確認的函數,以便在返回前觀察結果*/
int search_book(void)
{
int n,i;
printf("請輸入圖書序號:");
scanf("%d",&i);
for(n=0;n<100;n++)
{
if(books[n].id==i)
{
printf("書名:%s\n",books[n].name);
printf("作者:%s\n",books[n].author);
printf("存數:%d of ",books[n].store);
printf("%d\n",books[n].total);
return n;
}
}
printf("\n輸入錯誤或無效圖書序號.\n");
return -1;
}
/*上面的函數是在數組中找到圖書號匹配的記錄,顯示其信息並返
回數組下標,如果找不到相應記錄則提示錯誤並返回-1。*/
void book_out(void)
{
int n,s,l,d;
page_title("借閱圖書");
if((n=search_book())!=-1&&books[n].store>0)
{
printf("請輸入借書證序號:");
scanf("%d",&s);
printf("請輸入可借天數:");
scanf("%d",&d);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==0)
{
books[n].usr[l]=s;
books[n].days[l]=d;
break;
}
}
books[n].store--;
}
if(n!=-1&&books[n].store==0) printf("此書已經全部借出.\n");
return_confirm();
}
/*上面是借書的函數,首先調用找書函數*/
void book_in(void)
{
int n,s,l;
page_title("歸還圖書");
if((n=search_book())!=-1&&books[n].store<books[n].total)
{
printf("借閱者圖書證列表:\n");
for(l=0;l<10;l++)
if (books[n].usr[l]!=0)
printf("[%d] - %d天\n",books[n].usr[l],books[n].days[l]);
printf("請輸入借書證序號:");
scanf("%d",&s);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==s)
{
books[n].usr[l]=0;
books[n].days[l]=0;
break;
}
}
books[n].store++;
}
if(n!=-1&&books[n].store==books[n].total)
printf("全部入藏.\n");
return_confirm();
}
void book_add(void)
{
int n;
page_title("注冊新書");
for(n=0;n<100;n++)
if(books[n].id==0) break;
printf("序號:");
scanf("%d",&books[n].id);
printf("書名:");
scanf("%s",&books[n].name);
printf("作者:");
scanf("%s",&books[n].author);
printf("數量:");
scanf("%d",&books[n].total);
books[n].store=books[n].total;
return_confirm();
}
void book_del(void)
{
int n;
page_title("注銷舊書");
if((n=search_book())!=-1) books[n].id=0;
printf("該書已注銷.\n");
return_confirm();
}
void main(void)
{
menu: page_title("操作選單");
printf("請用數字鍵選擇操作\n\n");
printf("1 借閱圖書\n2 歸還圖書\n\n");
printf("3 注冊新書\n4 注銷舊書\n\n");
printf("\n0 退出\n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_in();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '0' : exit(0);
}
goto menu;
}
{
int n;
page_title("廣?症慕");
if((n=search_book())!=-1) books[n].id=0;
printf("乎慕廝廣?.\n");
return_confirm();
}
void main(void)
{
menu: page_title("荷恬僉汽");
printf("萩喘方忖囚僉夲荷恬\n\n");
printf("1 処堋夕慕\n2 拷珊夕慕\n\n");
printf("3 廣過仟慕\n4 廣?症慕\n\n");
printf("\n0 曜竃\n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_in();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '0' : exit(0);
}
goto menu;
}
㈥ c語言課程設計:圖書館系統
^^#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define N sizeof(struct book)
#define PT "%-5d %10s %6s %6s %8s %3d \n",p->num,p->name,p->where,p->author,p->pub,p->count
struct book /*圖書信息*/
{
int num; /*書號*/
char name[10]; /*書名*/
char where[10]; /*所在書庫*/
char author[15]; /*作者*/
char pub[20]; /*出版社*/
int count; /*數量*/
struct book *next;
};
/*輸出模塊*/
void print(struct book *p0)
{
struct book *p;
p=p0->next;
printf("\n\n\t\t^^^^^^^^^^^^^^圖書信息表^^^^^^^^^^^^^^");
printf("\n\n圖書編號---圖書名稱---所在書庫----作者----出版社---數量\n");
while(p!=NULL)
{
printf(PT);
p=p->next;
}
getch();
}
/*輸入模塊*/
struct book *creat()
{
struct book *head,*p1,*p2;
int i=0;
head=p2=(struct book *)malloc(N);
head->next=NULL;
printf("\n\n\t\t錄入圖書信息");
printf("\n\t---------------------------------------");
while(1)
{ p1=(struct book *)malloc(N);
printf("\n 請輸入圖書編號(書號為0結束): ");
scanf("%d",&p1->num);
if(p1->num!=0)
{
printf("\n\n書名 所在書庫 作者 出版社 圖書數量\n");
scanf("%s%s%s%s%d",p1->name,p1->where,p1->author,p1->pub,&p1->count);
p2->next=p1;
p2=p1;
i++;
}
else
break;
}
p2->next=NULL;
free(p1);
printf("\n\t\t----------------------------------------");
printf("\n\t\t %d 種書錄入完畢",i);
getch();
return head;
}
/*查找模塊*/
void find(struct book *p0)
{
char name[10];
int flag=1;
struct book *p;
p=p0->next;
printf("請輸入要查找的書名:\n");
scanf("%s",name);
for(p=p0;p;p=p->next)
if(strcmp(p->name,name)==0)
{
printf("\n\n圖書編號---圖書名稱---所在書庫----作者----出版社---數量\n");
printf(PT);
flag=0;
break;
}
if(flag) printf("\n 暫無此圖書信息\n");
getch();
}
/*刪除模塊*/
void del(struct book *p0)
{
char name[10];
int flag=1;
struct book *p;
p=p0;
printf("請輸入要刪除的書名:\n");
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
p0->next=p->next; /*後續節點連接到前驅節點之後*/
free(p);
printf("\t該書資料已刪除.");
flag=0;
break;
}
p0=p;
p=p->next;
}
if(flag) printf("\n\t無此圖書信息。");
getch();
}
/*增加模塊*/
void insert(struct book *p0)
{
struct book *p;
p=(struct book *)malloc(N);
while(1)
{
printf("\n 請輸入要增加的圖書編號(書號為0 退出): ");
scanf("%d",&p->num);
if(p->num!=0)
{
if(p0->next!=NULL&&p0->next->num==p->num) /*找到重號*/
{
p=p->next;
free(p);
printf("\t該書已存在");
}
else
{printf("\n\n書名 所在書庫 作者 出版社 圖書數量\n");
scanf("%s%s%s%s%d",p->name,p->where,p->author,p->pub,&p->count);
p->next=p0->next;
p0->next=p;
printf("\t已成功插入.");
}
}
else
break;
}
getch();
}
/*修改模塊*/
void modify(struct book *p0)
{
char name[10];
int flag=1;
int choice;
struct book *p;
p=p0->next;
printf("請輸入要修改的書名:\n");
scanf("%s",name);
while(p!=NULL&&flag==1)
{
if(strcmp(p->name,name)==0)
{
printf("\n\t請選擇要修改的項:");
printf("\n\t 1.修改圖書編號\n");
printf("\n\t 2.修改圖書所在書庫\n");
printf("\n\t 3.修改圖書作者\n");
printf("\n\t 4.修改圖書出版社\n");
printf("\n\t 5.修改圖書庫存量\n");
scanf("%d",&choice);
switch(choice)
{
case 1: { printf("\n 請輸入新的圖書編號:");
scanf("%d",p->num); break;
}
case 2: { printf("\n 請輸入新的圖書書庫:");
scanf("%s",p->where); break;
}
case 3: { printf("\n 請輸入新的圖書作者:");
scanf("%s",p->author); break;
}
case 4: {printf("\n 請輸入新的圖書出版社:");
scanf("%s",p->pub); break;
}
case 5: {printf("\n 請輸入新的圖書庫存量:");
scanf("%d",p->count); break;
}
}
printf("\n\t該項已成功修改。\n\t 新的圖書信息:");
printf("\n\n圖書編號---圖書名稱---所在書庫----作者----出版社---數量\n");
printf(PT);
flag=0;
}
p0=p;
p=p0->next;
}
if(flag) printf("\n\t暫無此圖書信息。");
getch();
}
/*讀文件*/
struct book *read_file()
{
int i=0;
struct book *p,*p1,*head=NULL;
FILE *fp;
if((fp=fopen("library.txt","rb"))==NULL)
{printf("\n\n\n\n\n \t********庫文件不存在,請創建!**********");
getch();
return NULL;
}
head=(struct book *)malloc(N);
p1=head;
head->next=NULL;
printf("\n 已有圖書信息:");
printf("\n\n圖書編號---圖書名稱---所在書庫----作者----出版社---數量\n");
while(!feof(fp))
{
p=(struct book *)malloc(N); /*開辟空間以存放的取得信息*/
while(fscanf(fp,"%d%s%s%s%s%d",&p->num,p->name,p->where,p->author,p->pub,&p->count)!=EOF)
{
printf(PT);
i++;
}
p1->next=p;
p1=p;
}
p1->next=NULL;
fclose(fp);
printf("\n 共種%d 圖書信息",i);
printf("\n\n\n 文件中的信息以正確讀出。按任意鍵返回。");
getch();
return (head);
}
/*保存文件*/
void save(struct book *head)
{
FILE *fp;
struct book *p;
fp=fopen("library.txt","wb"); /*以只寫方式打開二進制文件*/
if(fp==NULL) /*打開文件失敗*/
{
printf("\n=====>打開文件失敗!\n");
getch();
return ;
}
else
for(p=head->next;p!=NULL;p=p->next)
fprintf(fp,"%d %s %s %s %s %d\n",p->num,p->name,p->where,p->author,p->pub,p->count);
fclose(fp);
printf("\n\t保存文件成功!\n");
}
void main()
{
struct book *head=NULL;
int choice=1;
head=read_file();
if(head==NULL)
{
printf("\n\t\t**********");
getch();
head=creat();
}
do
{
system("cls");
printf("\t\t----------Welcome---------\n");
printf("\n\n\t歡迎您,圖書管理員.\n");
printf("\n\n\n\n\n");
printf("\n\t 請選擇:");
printf("\n\t 1.查詢圖書信息\n");
printf("\n\t 2.修改圖書信息\n");
printf("\n\t 3.增加圖書信息\n");
printf("\n\t 4.刪除圖書信息\n");
printf("\n\t 5.顯示所有圖書信息\n");
printf("\n\t 0.退出系統\n");
scanf("%d",&choice);
switch(choice)
{
case 1: find(head); break;
case 2: modify(head); break;
case 3: insert(head); break;
case 4: del(head); break;
case 5: print(head); break;
case 0: system("cls");
printf("\n\n\n\n\n\t^^^^^^^^^^謝謝使用,再見^^^^^^^^^^!\n\n");
break;
}
}while(choice!=0);
save(head);
}
㈦ c語言課設小型圖書管理系統設計(c++)
試著做了下這個題,結果和你一樣前7項全解決了,就剩下第八個了,明天再試試,能做出來給你發代碼。要是我做不出來,還請你給我指點一二。
/*終於完成了,第八項也OK了。
你自己多運行幾組數據測試下,應該沒問題了
*/
// 20130718.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
#define N 5
void swap(string & a,string & b)
{
string c= a;
a= b;
b= c;
}
void swap1(int & a,int & b)
{
int c=a;
a=b;
b=c;
}
class A
{
private:
string name[N];
string writer[N];
int num1[N];//書號
int price[N];
int num2[N];//數量
public:
void input();
void add();
void modify();
void cut();
void look();
void seek();
void display();
};
void A::input()
{
for(int i=0;i<N;i++)
{
int j;
cout<<"請依次輸入書名,作者,編號,單價及數量,用空格鍵分開"<<endl;
cin>>name[i];
cout<<"書名錄入成功"<<endl;
cin>>writer[i];
cout<<"作者錄入成功"<<endl;
cin>>num1[i];
cout<<"編號錄入成功"<<endl;
cin>>price[i];
cout<<"單價錄入成功"<<endl;
cin>>num2[i];
cout<<"數量錄入成功"<<endl;
cout<<"是否繼續輸入"<<endl;
cout<<"1:繼續"<<endl;
cout<<"2:退出"<<endl;
cin>>j;
if(j==1) ;
else break;
}
}
void A::add()
{
for(int i=0;i<N;i++)
{
if(name[i]==writer[i])
{
int j;
cout<<"請依次輸入書名,作者,編號,單價及數量,用空格鍵分開"<<endl;
cin>>name[i];
cout<<"書名錄入成功"<<endl;
cin>>writer[i];
cout<<"作者錄入成功"<<endl;
cin>>num1[i];
cout<<"編號錄入成功"<<endl;
cin>>price[i];
cout<<"單價錄入成功"<<endl;
cin>>num2[i];
cout<<"數量錄入成功"<<endl;
cout<<"添加成功,是否繼續添加"<<endl;
cout<<"1:是"<<endl;
cout<<"2:否"<<endl;
cin>>j;
if(j==1);
else break;
}
}
}
void A::modify()
{
int i,j;
cout<<"請輸入要修改書的書號"<<endl;
cin>>j;
for(i=0;i<N;i++)
{
if(num1[i]!=j);
else
cout<<"書籍已找到"<<endl;
cout<<"請重新輸入書名,作者,編號,單價及數量,用空格鍵分開"<<endl;
cin>>name[i];
cout<<"書名錄入成功"<<endl;
cin>>writer[i];
cout<<"作者錄入成功"<<endl;
cin>>price[i];
cout<<"單價錄入成功"<<endl;
cin>>num2[i];
cout<<"數量錄入成功"<<endl;
break;
}
}
void A::cut()
{
int i,j;
cout<<"請輸入要刪除的書的書號"<<endl;
cin>>j;
for(i=0;i<N;i++)
{
if(num1[i]==j)
{ name[i]=writer[i]=" ";
num1[i]=price[i]=num2[i]=0;
cout<<"初始化完成"<<endl;
break;
}
}
}
void A::look()
{
int i;
for(i=0;i<N;i++)
{
cout<<name[i]<<writer[i]<<num1[i]<<price[i]<<num2[i]<<endl;
}
}
void A::seek()
{
int i,j;
cout<<"請輸入要查找的書的書號"<<endl;
cin>>j;
for(i=0;i<N;i++)
{
if(j==num1[i])
cout<<num1[i]<<name[i]<<writer[i]<<price[i]<<num2[i]<<endl;
else
break;
}
}
void A::display()//排序未解決,難。。。。。
{
int i,j,k/*,P,N1,N2*/;
for(i=1;i<N;i++)
{
for(j=0;j<i;j++)
{
if(price[i]>price[j])
{
swap1(price[i],price[j]);
swap(name[i],name[j]);
swap(writer[i],writer[j]);
swap1(num1[i],num1[j]);
swap1(num2[i],num2[j]);
}
}
}
for(k=0;k<N;k++)
{
cout<<setw(5)<<price[k]<<setw(10)<<name[k]<<setw(5)<<writer[k]<<setw(3)<<num1[k]<<setw(3)<<num2[k]<<endl;
}
cout<<"排序完成"<<endl;
}
int main()
{
A a;
int i;
do
{
cout<<"1:信息錄入"<<endl;
cout<<"2:添加記錄"<<endl;
cout<<"3:信息修改"<<endl;
cout<<"4:信息刪除"<<endl;
cout<<"5:信息瀏覽"<<endl;
cout<<"6:信息查詢"<<endl;
cout<<"7:信息排序"<<endl;
cout<<"8:退出系統"<<endl;
cin>>i;
switch(i)
{
case 1:a.input();break;
case 2:a.add();break;
case 3:a.modify();break;
case 4:a.cut();break;
case 5:a.look();break;
case 6:a.seek();break;
case 7:a.display();break;
case 8:cout<<"成功退出"<<endl;break;
default:cout<<"輸入錯誤"<<endl;
}
}while(i!=8);
return 0;
}
/*終於完成了,第八項也OK了。
你自己多運行幾組數據測試下,應該沒問題了
*/
㈧ C語言課程設計-圖書管理系統
您好!
還是你自己編吧!一班人都沒有那個心思,你這個代碼花的精力太大了,至少要半天才能出來!
非人力所能及!
加油,我相信你自己能寫出更完善的代碼來的!
㈨ C語言課程設計題目:圖書信息管理系統設計
你好,具體的圖書信息管理系統設計要求你來與我細談吧