问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:test.cpp
*作 者:尚 月
*完成日期:2015年03月18日
*版 本 号:v1.0
*
*问题描述: 编写一个简单的图书管理系统要求如下:包括name(书名)、writer(著者)、public_name(出版社)、price(价格)、number(数量)、NO(书号)等等
*/
#include <iostream>
#include <cstring>
using namespace std;
class Book
{
char name[50];
char writer[50];
char public_name[50];
float price;
int number;
int NO;
public:
void setBook (char*na,char*wr,char*pu,float pr,int nu,int no);
void set_NO(int n);
int get_NO();
void borrow();
void restore();
void print();
}book;
void Book::setBook(char*na,char*wr,char*pu,float pr,int nu,int no)
{
strcpy(name,na);
strcpy(writer,wr);
strcpy(public_name,pu);
price=pr;
number=nu;
NO=no;
}
void Book::set_NO(int n)
{
NO=n;
}
int Book::get_NO()
{
return NO;
}
void Book::borrow()
{
number--;
}
void Book::restore()
{
number++;
}
void Book::print()
{
cout<<"name: "<<name<<endl;
cout<<"writer: "<<writer<<endl;
cout<<"public_name: "<<public_name<<endl;
cout<<"price: "<<price<<endl;
cout<<"number: "<<number<<endl;
cout<<"NO: "<<NO<<endl;
cout<<endl;
}
int main()
{
char m;
book.setBook("Les Miserables", "Victor Hugo","Chinese Literature Press",45,13,1103);
book.print();
cout<<"请问您是借书(B)还是还书(R),请选择: ";
cin>>m;
if(m=='B')
{
book.borrow();
cout<<"您借的该书信息为: "<<endl;
book.print();
}
else if(m=='R')
{
book.restore();
cout<<"您还的该书信息为: "<<endl;
book.print();
}
else
cout<<"对不起,您的输入有误,请重新选择!"<<endl;
return 0;
}
运行结果:
总结:注意借书以后会少,还书增加,我就犯了这么低级的错误~~~