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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
內(nèi)聯(lián)函數(shù)inline與宏定義深入解析
類的內(nèi)斂函數(shù)是一個真正的函數(shù)。使用內(nèi)聯(lián)函數(shù)inline可以完全取代表達式形式的宏定義2013-09-09
C語言編程中分配內(nèi)存空間的相關(guān)函數(shù)
這篇文章主要介紹了C語言編程中分配內(nèi)存空間的相關(guān)函數(shù),分別是malloc()函數(shù)和calloc()函數(shù),需要的朋友可以參考下2015-08-08

