C++實(shí)現(xiàn)圖書(shū)信息管理系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)圖書(shū)信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
1.題目:
類(lèi)型有:編號(hào):ISBN
書(shū)名:name
價(jià)格:price
完成如下的功能:
①錄入:從鍵盤(pán)輸入(或從文件讀入)圖書(shū)(或?qū)W生)信息表的各個(gè)數(shù)據(jù)元素;
②查找:可按不同屬性查找所有等于給定值的數(shù)據(jù)元素,找到并返回它們?cè)诒碇械奈恍颍?br />③插入:在表中第i(1=<i<=N+1)個(gè)位置插入一個(gè)新元素;
④刪除:可刪除表中第i(1=<i<=N)個(gè)位置上的元素;
⑤輸出:依次打印表中的各個(gè)元素的值;
⑥排序:可按某屬性對(duì)表中的元素進(jìn)行排序。(可選)
2.實(shí)現(xiàn)方式:單鏈表(帶頭節(jié)點(diǎn))
3.代碼實(shí)現(xiàn):
#include <iostream>
#include <string>
using namespace std;
struct Node
{
? ? int ISBN;//編號(hào)
? ? string name;//書(shū)名
? ? float price;//定價(jià)
? ? Node *next;
};
//操作類(lèi)
class Link
{
private:
? ? int number;//圖書(shū)數(shù)量
? ? Node *head;
public:
? ? Link(int a):number(a){}
? ? ~Link(){delete head;}
? ? void create_node();//創(chuàng)建
? ? void select();//功能選擇
? ? int find_node(int i);//按照編號(hào)查找
? ? int find_node(string n);//按照書(shū)名查找
? ? int find_node(float p);//按照價(jià)格查找
? ? int insert_node(int pos);//插入
? ? int delete_node(int d);//刪除
? ? int mod_node(int d);//修改
? ? void sort_node_ISBN();//按照編號(hào)排序
? ? void sort_node_price();//按照價(jià)格排序
? ? int get_node();//計(jì)數(shù)總數(shù)
? ? void print();//打印操作
};
void Link::create_node()
{
? ? Node *pnew;
? ? head = new Node;
? ? //cout<<"請(qǐng)按順序輸入圖書(shū)的ISBN,書(shū)名,定價(jià)";
? ? head->ISBN = 0;
? ? head->name = 'n';
? ? head->price = 0;
? ? head->next = NULL;
? ? Node *ptemp = head;
? ? for(int i=0;i<number;i++)
? ? {
? ? ? ? pnew = new Node;
? ? ? ? cout<<endl;
? ? ? ? cout<<"請(qǐng)按順序輸入圖書(shū)的ISBN,書(shū)名,定價(jià):";
? ? ? ? cin>>pnew->ISBN>>pnew->name>>pnew->price;
? ? ? ? pnew->next = NULL;
? ? ? ? ptemp->next = pnew;
? ? ? ? ptemp = pnew;
? ? }
}
//按編號(hào)查找
int Link::find_node(int i)
{
? ? Node *ptemp = head->next;
? ? for(int count = 0;count<number;count++)
? ? ? ? {
? ? ? ? ? ? if(ptemp->ISBN == i)//按編號(hào)查找圖書(shū)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cout<<"圖書(shū)的編號(hào)為:"<<ptemp->ISBN<<" ? ? 書(shū)名為:"<<ptemp->name<<" ? ? ? 定價(jià)為:"<<ptemp->price<<endl;
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? }
? ? ? ? ? ? ptemp = ptemp->next;
? ? ? ? }
? ? ? ? return 0;
}
//按照書(shū)名查找
int Link::find_node(string n)
{
? ? Node *ptemp = head->next;
? ? for(int count=0;count<number;count++)
? ? ? ? {
? ? ? ? ? ? if(ptemp->name == n)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cout<<"圖書(shū)的編號(hào)為:"<<ptemp->ISBN<<" ? ? 書(shū)名為:"<<ptemp->name<<" ? ? ? 定價(jià)為:"<<ptemp->price<<endl;
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? }
? ? ? ? ? ? ptemp = ptemp->next;
? ? ? ? }
? ? ? ? return 0;
}
//按照價(jià)格查找
int Link::find_node(float p)
{
? ? Node *ptemp = head->next;
? ? for(int count=0;count<number;count++)
? ? ? ? {
? ? ? ? ? ? if(ptemp->price == p)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cout<<"圖書(shū)的編號(hào)為:"<<ptemp->ISBN<<" ? ? 書(shū)名為:"<<ptemp->name<<" ? ? ? 定價(jià)為:"<<ptemp->price<<endl;
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? }
? ? ? ? ? ? ptemp = ptemp->next;
? ? ? ? }
? ? return 0;
}
//插入
int Link::insert_node(int pos)
{
? ? if((pos > number)||(pos<0))
? ? {
? ? ? ? cout<<"插入位置錯(cuò)誤!"<<endl;
? ? ? ? return 0;
? ? }
? ? else
? ? {
? ? ? ? Node *ptemp = head,*pnew;
? ? ? ? for(int i=0;i<pos-1;i++)
? ? ? ? {
? ? ? ? ? ? ptemp = ptemp->next;
? ? ? ? }
? ? ? ? pnew = new Node;
? ? ? ? cout<<"請(qǐng)按順序輸入圖書(shū)的ISBN,書(shū)名,價(jià)格:";
? ? ? ? cin>>pnew->ISBN>>pnew->name>>pnew->price;
? ? ? ? pnew->next = ptemp->next;
? ? ? ? ptemp->next = pnew;
? ? ? ? number += 1;
? ? }
? ? return 1;
}
//刪除
int Link::delete_node(int d)
{
? ? if((d > number)||(d<0))
? ? {
? ? ? ? cout<<"刪除位置錯(cuò)誤!"<<endl;
? ? ? ? return 0;
? ? }
? ? else
? ? {
? ? ? ? Node *ptemp = head,*pdelete;
? ? ? ? for(int i=0;i<d-1;i++)
? ? ? ? {
? ? ? ? ? ? ptemp = ptemp->next;
? ? ? ? }
? ? ? ? pdelete = ptemp->next;
? ? ? ? ptemp->next = pdelete->next;
? ? ? ? delete pdelete;
? ? ? ? number -= 1;
? ? }
? ? return 1;
}
//修改
int Link::mod_node(int d)
{
? ? int aa;
? ? string bb;
? ? float cc;
? ? if((d > number)||(d<0))
? ? {
? ? ? ? cout<<"要修改的位置錯(cuò)誤!"<<endl;
? ? ? ? return 0;
? ? }
? ? else
? ? {
? ? ? ? Node *ptemp = head->next;
? ? ? ? for(int i=0;i<d-1;i++)
? ? ? ? {
? ? ? ? ? ? ptemp = ptemp->next;
? ? ? ? }
? ? ? ? cout<<"要修改編號(hào)請(qǐng)輸入0,要修改書(shū)名請(qǐng)輸入1,要修改價(jià)格請(qǐng)輸入2:";
? ? ? ? int k;
? ? ? ? cin>>k;
? ? ? ? switch(k)
? ? ? ? {
? ? ? ? case 0:
? ? ? ? ? ? cout<<"請(qǐng)輸入要修改的編號(hào):";
? ? ? ? ? ? cin>>aa;
? ? ? ? ? ? ptemp->ISBN = aa;
? ? ? ? ? ? cout<<endl;
? ? ? ? ? ? break;
? ? ? ? case 1:
? ? ? ? ? ? cout<<"請(qǐng)輸入要更改的書(shū)名:";
? ? ? ? ? ? cin>>bb;
? ? ? ? ? ? ptemp->name = bb;
? ? ? ? ? ? cout<<endl;
? ? ? ? ? ? break;
? ? ? ? case 2:
? ? ? ? ? ? cout<<"請(qǐng)輸入要更改的價(jià)格:";
? ? ? ? ? ? cin>>cc;
? ? ? ? ? ? ptemp->price = cc;
? ? ? ? ? ? cout<<endl;
? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? return 1;
}
//按編號(hào)排序
void Link::sort_node_ISBN()
{
? ? Node *ptemp = head->next,*pre;
? ? Node *pr = ptemp->next;
? ? ptemp->next = NULL;
? ? ptemp = pr;
? ? while(ptemp != NULL)
? ? {
? ? ? ? pr = ptemp->next;
? ? ? ? pre = head;
? ? ? ? while(pre->next != NULL && pre->next->ISBN > ptemp->ISBN)
? ? ? ? {
? ? ? ? ? ? pre = pre->next;
? ? ? ? }
? ? ? ? ptemp->next = pre->next;
? ? ? ? pre->next = ptemp;
? ? ? ? ptemp = pr;
? ? }
? ? Link::print();
}
//按照價(jià)格排序
void Link::sort_node_price()
{
? ? Node *ptemp = head->next,*pre;
? ? Node *pr = ptemp->next;
? ? ptemp->next = NULL;
? ? ptemp = pr;
? ? while(ptemp != NULL)
? ? {
? ? ? ? pr = ptemp->next;
? ? ? ? pre = head;
? ? ? ? while(pre->next != NULL && pre->next->price > ptemp->price)
? ? ? ? {
? ? ? ? ? ? pre = pre->next;
? ? ? ? }
? ? ? ? ptemp->next = pre->next;
? ? ? ? pre->next = ptemp;
? ? ? ? ptemp = pr;
? ? }
? ? ?Link::print();
}
//獲取長(zhǎng)度
int Link::get_node()
{
? ? return number;
}
//打印輸出
void Link::print()
{
? ? Node *ptemp = head->next;
? ? for(int k=0;k<number;k++)
? ? {
? ? ? ? cout<<"圖書(shū)編號(hào):"<<ptemp->ISBN<<" ? ? ? 書(shū)名為:"<<ptemp->name<<" ? ? ? 價(jià)格為:"<<ptemp->price<<endl;
? ? ? ? ptemp = ptemp->next;
? ? }
}
//功能函數(shù)
void Link::select()
{
? ? int a;//ISBN
? ? string b;//書(shū)名
? ? float c;//定價(jià)
? ? int d;//位置
? ? int p;//選擇功能
? ? cin>>p;
? ? switch(p)
? ? {
? ? case 0:
? ? ? ? cout<<"請(qǐng)輸入圖書(shū)的編號(hào)";
? ? ? ? cin>>a;
? ? ? ? if(this->find_node(a))
? ? ? ? {
? ? ? ? ? ? cout<<endl;
? ? ? ? }
? ? ? ? else
? ? ? ? ? ? cout<<"該圖書(shū)不存在!"<<endl;
? ? ? ? break;
? ? case 1:
? ? ? ? cout<<"請(qǐng)輸入圖書(shū)的名字:";
? ? ? ? cin>>b;
? ? ? ? if(this->find_node(b))
? ? ? ? {
? ? ? ? ? ? cout<<endl;
? ? ? ? }
? ? ? ? else
? ? ? ? ? ? cout<<"該圖書(shū)不存在!"<<endl;
? ? ? ? break;
? ? case 2:
? ? ? ? cout<<"請(qǐng)輸入圖書(shū)的價(jià)格:";
? ? ? ? cin>>c;
? ? ? ? if(this->find_node(c))
? ? ? ? {
? ? ? ? ? ? cout<<endl;
? ? ? ? }
? ? ? ? else
? ? ? ? ? ? cout<<"該圖書(shū)不存在!"<<endl;
? ? ? ? break;
? ? case 3:
? ? ? ? cout<<"請(qǐng)輸入要插入的位置:";
? ? ? ? cin>>d;
? ? ? ? if(this->insert_node(d))
? ? ? ? {
? ? ? ? ? ? cout<<"插入操作的結(jié)果為:"<<endl;
? ? ? ? ? ? this->print();//打印插入結(jié)果
? ? ? ? }
? ? ? ? break;
? ? case 4:
? ? ? ? cout<<"請(qǐng)輸入要?jiǎng)h除的位置:";
? ? ? ? cin>>d;
? ? ? ? if(this->delete_node(d))
? ? ? ? {
? ? ? ? ? ? cout<<"刪除操作的結(jié)果為:"<<endl;
? ? ? ? ? ? this->print();//打印插入結(jié)果
? ? ? ? }
? ? ? ? break;
? ? case 5:
? ? ? ? cout<<"請(qǐng)輸入要修改的圖書(shū)的位置:";
? ? ? ? cin>>d;
? ? ? ? if(this->mod_node(d))
? ? ? ? {
? ? ? ? ? ? cout<<"修改后的結(jié)果為:"<<endl;
? ? ? ? ? ? this->print();
? ? ? ? }
? ? ? ? break;
? ? case 6:
? ? ? ? cout<<"按照?qǐng)D書(shū)的編號(hào)進(jìn)行排序的結(jié)果為:"<<endl;
? ? ? ? this->sort_node_ISBN();
? ? ? ? break;
? ? case 7:
? ? ? ? cout<<"按照?qǐng)D書(shū)的價(jià)格進(jìn)行排序的結(jié)果為:"<<endl;
? ? ? ? this->sort_node_price();
? ? ? ? break;
? ? case 8:
? ? ? ? cout<<"當(dāng)前館內(nèi)的圖書(shū)數(shù)量為:";
? ? ? ? cout<<this->get_node();
? ? ? ? break;
? ? }
}
int main()
{
? ? int sele=1;//功能選擇
? ? int i;//最開(kāi)始的數(shù)量
? ? cout<<"請(qǐng)輸入你要輸入的圖書(shū)的數(shù)量:";
? ? cin>>i;
? ? Link l(i);
? ? l.create_node();
? ? cout<<endl;
? ? cout<<"0---------------------為查找(按編號(hào))"<<endl;
? ? cout<<"1---------------------為查找(按書(shū)名)"<<endl;
? ? cout<<"2---------------------為查找(按定價(jià))"<<endl;
? ? cout<<"3---------------------為插入"<<endl;
? ? cout<<"4---------------------為刪除"<<endl;
? ? cout<<"5---------------------為修改"<<endl;
? ? cout<<"6---------------------為按照?qǐng)D書(shū)編號(hào)排序"<<endl;
? ? cout<<"7---------------------為按照?qǐng)D書(shū)的價(jià)格排序"<<endl;
? ? cout<<"8---------------------為顯示當(dāng)前館內(nèi)的圖書(shū)總數(shù)"<<endl;
? ? cout<<"請(qǐng)輸入要選擇的功能:";
? ? while(sele == 1)
? ? {
? ? ? ? l.select();
? ? ? ? cout<<"是否要退出管理系統(tǒng)?(輸入0退出、輸入1繼續(xù))";
? ? ? ? cin>>sele;
? ? ? ? cout<<"請(qǐng)輸入要選擇的功能:";
? ? }
? ? cout<<"-----------已退出圖書(shū)管理系統(tǒng)------------";
? ? return 0;
}4.效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)最新版
- C++順序表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++利用鏈表實(shí)現(xiàn)圖書(shū)信息管理系統(tǒng)
- C/C++實(shí)現(xiàn)圖書(shū)信息管理系統(tǒng)
- C++圖書(shū)管理系統(tǒng)程序源代碼
- C++版圖書(shū)管理系統(tǒng)
- C++使用鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
相關(guān)文章
利用C++11原子量如何實(shí)現(xiàn)自旋鎖詳解
當(dāng)自旋鎖嘗試獲取鎖時(shí)以忙等待(busy waiting)的形式不斷地循環(huán)檢查鎖是否可用,下面這篇文章主要給大家介紹了關(guān)于利用C++11原子量如何實(shí)現(xiàn)自旋鎖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06
內(nèi)聯(lián)函數(shù)inline與宏定義深入解析
類(lèi)的內(nèi)斂函數(shù)是一個(gè)真正的函數(shù)。使用內(nèi)聯(lián)函數(shù)inline可以完全取代表達(dá)式形式的宏定義2013-09-09
C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理
這篇文章主要為大家詳細(xì)介紹了C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
基于QT的TCP通信服務(wù)的實(shí)現(xiàn)
在項(xiàng)目開(kāi)發(fā)過(guò)程中,很多地方都會(huì)用到TCP通信,本文主要介紹了基于QT的TCP通信服務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C語(yǔ)言編程中分配內(nèi)存空間的相關(guān)函數(shù)
這篇文章主要介紹了C語(yǔ)言編程中分配內(nèi)存空間的相關(guān)函數(shù),分別是malloc()函數(shù)和calloc()函數(shù),需要的朋友可以參考下2015-08-08

