C++实现图书管理系统源码
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Books
{
private:
string bookname; //书名
string writername; //作者名
string publishname; //出版社
int num; //图书数量
string sname; // 学生姓名
int number; // 学生账号
static int length; //静态数据成员
int zf; //借阅图书数量
public:
Books(string b="1",string w="2",string p="3",int n=0,int f=0); //声明构造函数
void maintain(Books *m); // 信息的增删改
void demand(Books *m); // 信息查询
void bore(Books *m); //图书的借,还
void stat(Books *m); //信息统计和显示
void jssc(); // 输出格式函数
void sc(Books *p); //输出格式函数
void rest(Books *m); //统计图书受欢迎程度
void storage(Books *m); //信息保存
void OpenFile(); //打开文件
void quit(); //退出系统
};
int Books::length=5;
Books::Books(string b,string w,string p,int n,int f)//定义构造函数
{
bookname=b;
writername=w;
publishname=p;
num=n;
zf=f;
}
void Books::maintain(Books *m)//图书的增删改
{
int i=0;
int j;
m[i].sc(m);
B:cout<<"添加图书请按1,删除图书请按2,修改图书信息请按3,退出请按0"< int a; cin>>a; if(a==1) //添加图书 { A:cout<<"请依次输入书名,作者,出版社,图书数量"< cin>>(*(m+length)).bookname>>(*(m+length)).writername>>(*(m+length)).publishname>>(*(m+length)).num; length++; cout<<"继续添加请按1,执行其他操作请按2"< int b; cin>>b; if(b==1) goto A; else goto B; } if(a==2) //删除图书 { C:cout<<"请输入要删除的书名" < string a; cin>>a; i=0; while(i { i++; } if(i==length) { cout<<"要删除的图书不存在"< } else { for(j=i;j { m[j]=m[j+1]; } length--; } cout<<"继续删除请按1,执行其他操作请按2"< int c; cin>>c; if(c==1) goto C; else goto B; } if(a==3) //修改图书信息 { D:cout<<"请输入要修改的书名" < string b; cin>>b; i=0; while(i { i++; } cout<<"请输入要修改的图书信息" < cin>>m[i].bookname>>m[i].writername>>m[i].publishname>>m[i].num; cout<<"继续修改请按1,执行其他操作请按2"< int d; cin>>d; if(d==1) goto D; else goto B; } if(a==0) { goto E; } E:; } void Books::demand(Books *m)//查找图书 { cout<<"请输入要查找的书名" < F:string b; cin>>b; int i=0; while(i { i++; } if(m[i].bookname==b)//找到开始修改 { cout< } if(i==length) { cout<<"未找到此书"< } cout<<"继续查找请按1,退出请按2"< int d; cin>>d; if(d==1) goto F; else goto G; G:; } void Books::bore(Books *m) //借书还书 { int i; A:cout<<"借书请按1,还书请按2,退出请按0:"< int t; cin>>t; if(t==1) { B:cout<<"请输入要借的书名和数量:" < string a; int s; cin>>a; cin>>s; int i=0; while(i { i++; } if(m[i].bookname==a)//找到图书 { if(m[i].num-s>=0) { m[i].num=m[i].num-s; cout<<"请输入借书人姓名及学号:" < string b; int c; cin>>b; cin>>c; m[i].sname=b; m[i].number=c; m[i].zf=s; } else { cout<<"图书数量不够"< } } if(i==length) { cout<<"未找到此书"< } cout<<"继续借书请按1,执行其他操作请按2"< int g; cin>>g; if(g==1) goto B; else goto A; } if(t==2) { C:cout<<"请输入要还的书名和数量:" < string d; int e; cin>>d; cin>>e; i=0; while(i { i++; } if(m[i].bookname==d) //找到图书 { m[i].num=m[i].num+e; m[i].zf=m[i].zf-e; } else { cout<<"未找到图书"< } cout<<"继续还书请按1,执行其他操作请按2"< int f; cin>>f; if(f==1) goto C; else goto A; } if(t==0) { goto E; } E:; } void Books::stat(Books *m) { int i=0; int n=0; int fsum=0;//被借阅数量 int sum=0;//剩余书籍总数量 for(i=0;i { sum+=m[i].num; fsum+=m[i].zf; } cout<<"被借阅数量:"< for(i=0;i { if(m[i].zf!=0) { if(n==0) { cout<<"借书人姓名"<<" "< n++; } m[i].jssc(); } } cout< n=0; cout<<"剩余书籍总量:"< cout<<"书名"< m[i].sc(m); } void Books::jssc() { cout< } void Books::sc(Books *p) { int i=0; cout< for(i=0;i { cout< } } void Books::rest(Books *m) { int i=0; float fsum=0;//被借阅数量 float v[length]; for(i=0;i { fsum=fsum+m[i].zf; } if(fsum==0) { cout<<"图书未被借阅,无法统计"< } else { for(i=0;i { v[i]=m[i].zf/fsum; if(v[i]==0) { cout< } else { if(v[i]<0.15) { cout< } else { if(v[i]<0.3) { cout< } else { cout< } } } } } } void Books::storage(Books *m) { ofstream outfile("D:课程设计.txt",ios::out); int i=0; int fsum=0;//被借阅数量 int sum=0;//剩余书籍总数量 for(i=0;i { sum+=m[i].num; fsum+=m[i].zf; } if(!outfile) { cout<<"open error!"< exit(1); } if(fsum!=0) { outfile<<"被借阅数量:"< outfile<<"------------------------------------------------------------"< outfile<<"借书人姓名"<<" "< outfile<<"------------------------------------------------------------"< for(i=0;i { if(m[i].zf!=0) { outfile< } } outfile< } outfile<<"剩余书籍总量"< outfile<<"------------------------------------------------------------"< outfile< outfile<<"------------------------------------------------------------"< for(i=0;i { outfile< } outfile.close(); } void Books::OpenFile() { system("D:课程设计.txt"); } int main() { Books book[100]={ Books("aaa","aa","abc",1), Books("bbb","bb","bcd",2), Books("ccc","cc","cde",3), Books("ddd","dd","def",4), Books("eee","ee","efg",5) }; int sber; //管理员账号 Books *m; m=book; //定义指针指向对象数组 int z; //7个功能号选择 int t; //注册或者登陆选择 int elem[100]; //存放学号账户 string s[100]; //用户密码 string s1; //密码确认 int s2; //账号登陆 char c[10],d[10]; char ch; int i,j; int k; int x,h,v; //循环计数专用 elem[0]=1997; elem[1]=1998; s[0]="1234"; s[1]="1234"; i=2; cout<<"新用户注册请按1,登录请按2:"; //注册 cin>>t; if(t==1) { AAA:cout<<"输入账号名:"; cin>>sber; elem[i]=sber; for(v=0;v
{ if(elem[v]==sber) { cout<<"用户账号重名,请重新输入."; goto AAA; } } cout<<"输入密码:"; h=0; while(h<10&&(c[h]=getch())!=' ') { d[h]=c[h]; h++; cout<<"*"; } s[i]=d; //字符数组赋值给字符串 cout< cout<<"再次输入密码确认:"; h=0; while(h<10&&(c[h]=getch())!=' ') { d[h]=c[h]; h++; cout<<"*"; } s1=d; //字符数组赋值给字符串 cout< if(s[i]==s1) { cout<<"注册成功!"< i++; } else { cout<<"注册失败!"< } cout<<"继续注册请按1,否则输入2跳转登录界面:"; cin>>t; if(t==1) { system("cls"); goto AAA; } else { system("cls"); } } cout<<"执行登录请按2,退出请按3:"; //登录 cin>>t; if(t==2) { cout<<"输入账号:"; cin>>s2; for(j=0;j<=i;j++) { if(elem[j]==s2) { k=j; //k为位序 goto BBB; } } cout<<"账号不存在,退出!"; exit(0); BBB:cout<<"输入密码:"; for(x=0;x<3;x++) { h=0; while(h<10&&(c[h]=getch())!=' ') { d[h]=c[h]; h++; cout<<"*"; } s1=d; //字符数组赋值给字符串 if(s[k]==s1) { cout<<"登陆成功"; x=x-1; break; } else { cout< } } if(x==3) { cout<<"输入次数过多,强制退出!"; exit(0); } } else { exit(0); } ch='y'; while(ch=='y'||ch=='Y') { system("cls"); //清屏函数 cout<<"|--------------------|"< cout<<"| 图书管理系统 |"< cout<<"|--------------------|"< cout<<"| 1--信息的增删改 |"< cout<<"| 2--图书查询 |"< cout<<"| 3--借书还书 |"< cout<<"| 4--信息统计和显示 |"< cout<<"| 5--图书受欢迎程度 |"< cout<<"| 6--信息保存 |"< cout<<"| 7--打开保存文件 |"< cout<<"| 8--退出系统 |"< cout<<"|--------------------|"< cout<<"输入要执行的功能1-7:"; cin>>z; switch(z) { case 1: book[0].maintain(m); break; case 2: book[0].demand(m); break; case 3: book[0].bore(m); break; case 4: book[0].stat(m); break; case 5: book[0].rest(m); break; case 6: book[0].storage(m);break; case 7: book[0].OpenFile();break; case 8: exit(1);break; } cout<<"继续请按y或Y:"; cin>>ch; } return 0; }