图书管理系统c语言

创建一个图书管理系统是一个涉及数据结构和文件操作的项目。在C语言中,你可以使用结构体来表示图书信息,使用函数来实现系统的各项功能。以下是一个简单的图书管理系统的示例,包括基本的添加、显示、查找和删除图书的功能。

1. 定义图书结构体

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BOOKS 1000

typedef struct {
    char title[100];
    char author[50];
    char isbn[20];
    int quantity;
} Book;

Book books[MAX_BOOKS];
int totalBooks = 0;

2. 添加图书

void addBook(Book *book) {
    if (totalBooks < MAX_BOOKS) {
        books[totalBooks++] = *book;
        printf("Book added successfully.\n");
    } else {
        printf("Bookshelf is full.\n");
    }
}

3. 显示所有图书

void displayBooks() {
    if (totalBooks == 0) {
        printf("No books available.\n");
        return;
    }
    printf("List of books:\n");
    for (int i = 0; i < totalBooks; i++) {
        printf("%d. %s by %s, ISBN: %s, Quantity: %d\n", i + 1, books[i].title, books[i].author, books[i].isbn, books[i].quantity);
    }
}

4. 查找图书

int findBookByISBN(const char *isbn) {
    for (int i = 0; i < totalBooks; i++) {
        if (strcmp(books[i].isbn, isbn) == 0) {
            return i;
        }
    }
    return -1;
}

5. 删除图书

void deleteBook(const char *isbn) {
    int index = findBookByISBN(isbn);
    if (index != -1) {
        for (int i = index; i < totalBooks - 1; i++) {
            books[i] = books[i + 1];
        }
        totalBooks--;
        printf("Book deleted successfully.\n");
    } else {
        printf("Book not found.\n");
    }
}

6. 主函数

int main() {
    Book newBook;

    // 示例:添加图书
    strcpy(newBook.title, "C Programming");
    strcpy(newBook.author, "Nuha Ali");
    strcpy(newBook.isbn, "123-456-789");
    newBook.quantity = 5;
    addBook(&newBook);

    // 显示图书
    displayBooks();

    // 删除图书
    deleteBook("123-456-789");

    // 再次显示图书
    displayBooks();

    return 0;
}

这个示例展示了一个简单的图书管理系统的基本功能。在实际应用中,你可能需要添加更多的功能,比如编辑图书信息、按不同标准排序、保存到文件系统等。此外,错误检查和用户界面也是需要考虑的重要方面。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言源代码的图书管理系统 #include<iostream.h> #include<fstream.H> #include<stdlib.h> #include<string.h> struct bookData { int booknumber; char bookname[30]; int store; }; struct Person { char name[20]; char studynumber[10]; int count; bookData Rbook[2]; //bookData Rbook; }; class History { public: History(fstream& a){readerbook=a;} void writehistory(); void readhistory(fstream&); private: fstream readerbook; }; class Liberian; void Find(fstream&); void CreateTxt(fstream&); class Reader { friend Liberian; public: Reader(fstream&); void print(); void handlebook(fstream&); void change(); bool findreader(); void Addreader(); void deletereader(); private: fstream file; Person person; }; class Liberian { public: Liberian(char *,char *); void changeReader(Reader&); void selfprint(); void Append(fstream&); private: char workname[20]; char worknumber[20]; }; int main() { fstream bookRecord(" book.dat",ios::in| ios::out); if(!bookRecord) { cerr<<" Can't open,try again!"<<endl; exit(1); } fstream readerRecord("reader.dat",ios::in|ios::out); if(!readerRecord) { cerr<<" Can't open,try again!"<<endl; exit(1); } int choice; int key; Reader reader(readerRecord);//读者管理系统 Liberian liberian("李天","123456");//图书管理员 History rec(readerRecord);//声明显示历史记录类 while(1) { cout<<" 请输入您的选择:\n\n" <<"1--图书管理员\n" <<"2--读者系统\n" <<"0--结束程序\n"; cin>>choice; switch(choice) { case 1: { cout<<" Hello,图书管理员:\n"; liberian.selfprint();cout<<endl; cout<<"1-- 增加新图书\n" <<"2-- 改变读者的属性\n" <<"3-- 显示历史记录\n" <<"0-- 返回\n"; cin>>key; switch(key) { case 1: liberian.Append(bookRecord); break; case 2: liberian.changeReader(reader); break; case 3: rec.readhistory(readerRecord); case 0: break ; } } break; case 2: { cout<<"学生读者\n"; int iflag=1; //reader.findreader(); cout<<"1-- 借书与还书信息\n" <<"2-- 修改个人信息\n" <<"0-- 返回\n"; cin>>key; switch(key) { case 1: reader.handlebook(bookRecord); break; case 2: reader.change(); break; case 0: break; } } break; default: return 0; } } return 0; } //增加书籍 void Liberian::Append(fstream& storef) { bookData book; int choice; int key; int num; storef.seekp(0,ios::end); int posEnd=storef.tellp(); cout<<"*************书籍入库***************"<<endl; while(1) { cout<<" Please enter your choice:"<<endl; cout<<"1--添加新书\n"<<"2--已有书籍\n"<<"0--返回\n"; cin>>choice; switch(choice) { case 1: cout<<" 书号, 书名, 数量?"<<endl; cin>>book.booknumber>>book.bookname>>book.store; storef.write(reinterpret_cast<char*>(&book),sizeof(bookData)); break; case 2: storef.seekg(0,ios::beg); cout<<"Booknumber?\n"; cin>>key; do{ storef.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&storef.tellg()!=posEnd); if(book.booknumber==key) { cout<<book.booknumber<<"----"<<book.bookname<<"----"<<book.store<<endl; cout<<" 输入已有书增加的数量:"<<endl; cin>>num; if(num>0) book.store+=num; else { cout<<" Invalid input"<<endl; } storef.seekp(-long(sizeof(bookData)),ios::cur);//////////////////// storef.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<"现在书籍: "<<book.bookname<<" 余量为: "<<book.store<<endl; } else cout<<"找不到此书,请重新查阅书号是否正确!"<<endl; break; case 0: return ; } } } //书籍查询 void Find(fstream & f) { bookData book; int key; int choice; f.seekg(0,ios::end); int posEnd=f.tellp(); cout<<"*************书籍查询***************"<<endl; while(1) { cout<<" 请输入您的选择\n" <<" 1-- 检索一本书\n" <<"2-- 显示全部书籍\n" <<"0-- 返回\n"; cin>>choice; switch(choice) { case 1: f.seekg(0,ios::beg); cout<<"输入你想检索书的书号"<<endl; cin>>key; do{ f.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&f.tellg()!=posEnd); if(book.booknumber==key) cout<<book.booknumber<<"---"<<book.bookname<<"---"<<book.store<<endl; else cout<<"找不到此书,请重新确认!"<<endl; break; case 2: f.seekg(0,ios::beg); do{ f.read(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<book.booknumber<<"---"<<book.bookname<<"--"<<book.store<<endl; }while(f.tellg()!=posEnd); break; case 0: return ; } } } Reader::Reader(fstream& c) { file=c; Person person={"0","0",0,{{0,"0",0}, {0,"0",0}}}; } void Reader::print() { cout<<"Name"<<"---"<<person.name<<'\n\n'<<"studynumber---"<<person.studynumber<<'\n'; } //操作书籍?????????????????????? void Reader::handlebook(fstream &filee) { int key; int choice; bookData book; bool iflag;int num=0; filee.seekp(0,ios::end); int posEnd=filee.tellp(); while(!(iflag=findreader())) { num++; if(num>=3) return; } //cout<<person.name; while(1) { cout<<"1-- 借书\n" <<"2-- 还书\n" <<"3-- 查找一本书\n" <<"0-- 返回"<<'\n'; cin>>choice; switch(choice) { case 1: { filee.seekp(0,ios::end); int posEnd=filee.tellp(); if(person.count<=1) { cout<<" 输入你要借阅图书的书号:"<<endl; cin>>key; filee.seekg(0); do{ filee.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&filee.tellp()!=posEnd); //cout<<book.booknumber<<endl; if(book.booknumber==key) { //cout<<" 找到你想借阅的图书,成功借阅!"<<endl; if(book.store>0) { person.Rbook[person.count]=book; person.count+=1; book.store-=1; filee.seekp(-long(sizeof(bookData)),ios::cur); filee.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<" 找到你想借阅的图书,成功借阅!"<<endl; cout<<"书籍 :"<<book.bookname<<"剩余的本数为:"<<book.store<<endl; } else cout<<" 现在此图书已经被借完了,请耐心等待几天!"<<endl; } else cout<<" 找不到你想要的图书"<<endl; } else cout<<"你最多只能借2本\n\n"; } break; case 2: { bookData blankbook={0,"0",0}; //if(person.count>0) //{ int iflag=0; filee.seekg(0); cout<<" 输入你想要还的书的书号:"<<endl; cin>>key; for(int i=0;i<2;i++) { if(key==person.Rbook[i].booknumber) { person.Rbook[i]=blankbook; cout<<"成功归还此书!"; iflag=1; person.count--; do{ filee.read(reinterpret_cast<char*>(&book),sizeof(bookData)); }while(book.booknumber!=key&&filee.tellp()!=posEnd); if(book.booknumber==key) { book.store+=1; filee.seekp(-long(sizeof(bookData)),ios::cur); filee.write(reinterpret_cast<char*>(&book),sizeof(bookData)); cout<<"书籍 :"<<book.bookname<<"余本量为: "<<book.store<<endl; } } } if(!iflag) { cout<<" 你没有借阅那本书,请确认!"<<endl; } // } // else // cout<<"你没有借书,请重新确认!\n\n"; } break; case 3: Find(filee); break; case 0: return; } } } void Reader::change() { char newname[20]; char newnumber[10]; int choice; file.seekp(0,ios::cur); int Posend=file.tellp(); bool iflag;int num=0; while(!(iflag=findreader())) { num++; if(num>=3) return; } cout<<" 请输入你的选择!"<<endl; cout<<"1--改变名字!\n" <<"2--修改学号r\n" <<"0--返回\n"; cin>>choice; switch(choice) { case 1: cout<<"输入你的新名字!\n"; cin>>newname; strcpy(person.name,newname); break; case 2: cout<<"输入你的信学号?\n"; cin>>newnumber; strcpy(person.studynumber,newnumber); break; default: break;; } file.seekp(-long(sizeof(Person)),ios::cur); file.write(reinterpret_cast<char*>(&person),sizeof(Person)); cout<<"信息修改成功!"<<endl; } void Liberian::selfprint() { cout<<" 图书管理员 :\n" <<worknumber<<"---"<<workname<<" 为您服务!\n"; } Liberian::Liberian(char *a,char*b) { strcpy(workname,a); strcpy(worknumber,b); } void Liberian::changeReader(Reader& a) { int choice; char newname[20];char newnumber[10]; cout<<"修改读者的数据信息!\n"; cout<<"1-- 增加一个读者\n" <<"2-- 删除一个读者\n" <<"0-- 结束程序\n"; cin>>choice; switch(choice) { case 1: a.Addreader(); break; case 2: a.deletereader(); break; case 0: return; } } void Reader::Addreader() { Person temp;bookData book={0,"0",0}; file.seekp(0,ios::end); int Posend=file.tellp(); cout<<"输入你想要增加的读者的名字"<<endl; cin>>temp.name; cout<<"新读者的学号:"<<endl; cin>>temp.studynumber; temp.Rbook[1]=book; temp.Rbook[0]=book; temp.count=0; file.write(reinterpret_cast<char*>(&temp),sizeof(Person)); cout<<" 成功添加!"<<temp.name<<endl; } void Reader::deletereader() { file.seekp(0,ios::end); int Posend=file.tellp(); Person person; char name[20]; cout<<"输入要删除的读者的名字!"<<endl; cin>>name; file.seekg(0); do{ file.read(reinterpret_cast<char*>(&person),sizeof(Person)); }while(strcmp(name,person.name)&&file.tellp()!=Posend); if(!strcmp(name,person.name)) { bookData blankbook={0,"0",0}; Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}}; file.seekp(-long(sizeof(Person)),ios::cur); file.write(reinterpret_cast<char*>(&guest),sizeof(Person)); cout<<"成功删除!"<<person.name<<endl; } else cout<<"查无此人!"<<endl; } bool Reader::findreader() { file.seekp(0,ios::end); int Posend=file.tellp(); char name[20]; Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}}; cout<<"输入查找人的名字!"; cin>>name; file.seekg(0); do{ file.read(reinterpret_cast<char*>(&person),sizeof(Person)); }while(strcmp(name,person.name)&&file.tellp()!=Posend); if(!strcmp(name,person.name)) { cout<<"查到此人:"<<person.name<<endl; return true; } else { cout<<"找不到此人,请重新确认!"<<endl; return false; } } //借阅历史 void History::readhistory(fstream& a) { readerbook=a; char name[10]="0"; readerbook.seekp(0,ios::end); int Posend=readerbook.tellp(); Person guest; int iflag=1; cout<<" 书籍借阅信息如下:"<<endl; cout<<"姓名 学号 借书量 书籍名"<<endl; readerbook.seekg(0,ios::beg); do{ readerbook.read(reinterpret_cast<char*>(&guest),sizeof(Person)); if(strcmp(guest.Rbook[1].bookname,name) || strcmp(guest.Rbook[1].bookname,name)) { cout<<guest.name<<" "<<guest.studynumber<<" "<<guest.count<<" "; for(int i=0;i<2;i++) if(strcmp(guest.Rbook[i].bookname,name)) cout<<guest.Rbook[i].bookname; iflag=0; } }while(readerbook.tellp()!=Posend); if(iflag) cout<<"没有读者借阅图书!"<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youyouxiong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值