⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 b_6_2.cpp

📁 C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体
💻 CPP
字号:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Book
{
public:
	Book(string name, string author, int number);
	~Book();
	static void borrow(int, int);
	static void print();
	static void leave();
private:
	static Book *head, *tail; // 链表的首尾指针
	unsigned int sequence; // 序号
	string name;   //书名
	string author; //作者
	unsigned int number; //数量
	Book *next; // 指向下一个对象
};

Book* Book::head = NULL;
Book* Book::tail = NULL;

Book ::Book(string n, string a, int num)
{ if (head == NULL)
   { head = this;
     tail = this;
     sequence=0;
   }
  name = n;
  author = a;
  number = num;
  sequence = tail->sequence+1; 
  tail->next=this;
  next = NULL;
  tail = this;
}
Book::~Book()
{
  cout << "所有的内存被释放!";
}

void Book::leave()
{
  Book *p=head,*temp;
  while (p!=NULL)
  { temp=p->next;
	delete p;
	p = temp;
  }
}
void Book::borrow(int s,int n)
{ int i=0;
  Book *p=head;
  for(int i=1; i<s;i++)
	  p=p->next;
  p->number=p->number-n;
  print();
}

void Book::print()
{ Book *p;
  p=head;
  cout << "序号"<<setw(10)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"数量"<<endl;
  while (p!=NULL)
  { cout << p->sequence <<setw(12)<< p->name <<setw(12)<<p->author<<setw(9)<<p->number<<endl;
    p=p->next;
  }
}

void main()
{
    string aBookname;
	string aBookauthor;
	int aBooknumber;
	int s;
	int n;
	char ch;
	Book *aa;
  
	do
	{  cout << "q:退出  i:新增图书  b:借出图书   h:归还图书   p:显示信息"<<endl;
	   cin >>ch;
	   switch (ch)
	   {
	   case 'q':
		   Book::leave();
		   return;
	   case 'i':
		  cout<<"请输入书名: ";
		  cin>>aBookname;
		  cout<<"请输入作者: ";
		  cin>>aBookauthor;
		  cout<<"请输入图书数量: ";
		  cin>>aBooknumber;
     
		  aa =new Book(aBookname,aBookauthor,aBooknumber);
		  break;
	   case 'b':
		   cout << "请输入图书序号:";
		   cin >> s;
		   cout << "请输入欲借数量:";
		   cin >> n;
		   Book::borrow(s,n);
		   break;
	   case 'p':
		   Book::print();
		   break;


	   case 'h':
       default: break;
	   }
	} while (1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -