好了,希望能够帮到您。
如果你真的要纯c的话,只需要改一下输入输出就可。
即:cin,cout改为scanf,printf。
//Memory Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std;
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<stdio.h>
using namespace std;
const int Maxb=10000; //最多的图书
class Book//图书类
{
int tag; //删除标记1:已删0:未删
int number; //ISBN书号
char name[20]; //书名
char author[10]; //主编
char number2[10];//版次
char position[20];//出版社
char time[20];//出版年
int price;//定价
int onshelf; //是否在架1:在架0:已借
public:
Book() {}
char *getname() { return name; } //获取姓名
int getnumber() { return number; } //获取ISBN书号
int gettag() { return tag; } //获取删除标记
char *getauthor() {return author;} //获取主编
char *getnumber2() {return number2;} //获取版次
char *getposition() {return position;} //获取出版社
char *gettime() {return time;} //获取出版年
char getprice() {return price;} //获取图书定价
void delbook() { tag=1; } //删除图书
void addbook(int n,char *na,char *au,char *n2,char *da,char *ti,int pr) //增加图书
{
tag=0;
number=n;
price=pr;
strcpy(name,na);
strcpy(author,au);
strcpy(number2,n2);
strcpy(position,da);
strcpy(time,ti);
onshelf=1;
}
void disp() //输出图书
{
cout << setw(10) << number << setw(10) << name << setw(10)
<< setw(10)<<author<<setw(10)<<number2<<setw(10)<<position<<setw(10)<<time<<setw(10)<<price<<endl;
}
};
class BDatabase //图书库类
{
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase() //构造函数,将book.txt读到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear() //全删
{
top=-1;
}
int addbook(int n,char *na,char *au, char *n2, char *da,char *ti,int pr) //增加图书
{
Book *p=search1(n);
if (p==NULL)
{
top++;
book[top].addbook(n,na,au,n2,da,ti,pr);
return 1;
}
return 0;
}
Book *search1(int bookid) //查找图书
{
for (int i=0;i<=top;i++)
if (book[i].getnumber()==bookid &&
book[i].gettag()==0)
return &book[i];
return NULL;
}
Book *search2(int bookid,char *name) //按书名查找图书
{
for(int i=0;i<=top;i++)
if(strcmp(book[i].getname(),name)==0)
{bookid=book[i].getnumber();
return &book[i];
}
return NULL;
}
Book *search3(int bookid,char *author) //按主编查找图书
{
for(int i=0;i<=top;i++)
if(strcmp(book[i].getauthor(),author)==0)
{bookid=book[i].getnumber();
return &book[i];
}
return NULL;
}
void bookdata(); //图书库维护
void disp()
{
cout<<setw(10)<<"图书书号"<<setw(10)<<"图书名字"<<setw(10)<<"图书主编"<<setw(10)<<"版次"<<setw(10)<<"出版社"<<setw(10)<<"出版年"<<setw(10)<<"价格"<<endl<<endl<<endl<<endl;
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase() //析构函数,将book[]写入book.txt文件中
{
fstream file("book.txt",ios::out);
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
file.write((char *)&book[i],sizeof(book[i]));
file.close();
}
};
void BDatabase::bookdata()
{
int choice=1;
int choice2=1;
int choice3=1;
int choice4;
char bname[40];
char editor[40];
char banci[40];
char position[40];
char year[40];
int value;
int bookid;
Book *b;
while (choice!=0)
{
cout<<endl<<endl;
cout<<" **************************** "<<endl;
cout<<" **** 1添加图书 **** "<<endl;
cout<<" **** 3 删除图书 **** "<<endl;
cout<<" **** 4 图书查询 **** "<<endl;
cout<<" **** 5 显示图书 **** "<<endl;
cout<<" **** 6 全部删除 **** "<<endl;
cout<<" **** 7 借书 **** "<<endl;
cout<<" **** 8 还书 **** "<<endl;
cout<<" **** 0 退出 **** "<<endl;
cout<<" ****************************"<<endl<<endl;
cout<<endl<<"请按键选择您需要的操作:";
cin>>choice;
while(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6&&choice!=0){
cout<<endl<<" ** 您输入的编号在菜单里不存在,请重新输入 **"<<'\a'<<endl<<endl;
cout<<" 请选择您需要的操作:";
cin>>choice;
}
switch (choice)
{
case 1:
cout <<"输入ISBN书号(一定为数字否则会异常):";
cin >> bookid;
cout <<"输入书名:";
cin >> bname;
cout <<"输入主编:";
cin >>editor;
cout <<"输入版次(一定为数字否则会异常):";
cin>>banci;
cout<<"输入出版社:";
cin>>position;
cout<<"输入出版年(一定为数字否则会异常):";
cin>>year;
cout<<"输入价格(一定为数字否则会异常):";
cin>>value;
addbook(bookid,bname,editor,banci,position,year,value);
cout<<"ISBN书号"<<bookid<<"添加成功,如需返回主菜单请按1,退出系统请按0(一定要输入数字)";
cin>>choice4;
while (choice4!=0&&choice4!=1)
{
cout<<"输入错误请重新输入"<<endl;
cin>>choice4;}
switch (choice4)
{
case 1:
choice=1;
break;
case 0:
choice=0;
break;}
break;
case 3:
cout << " 输入ISBN书号:";
cin >> bookid;
b=search1(bookid);
if (b==NULL)
{
cout << " 该图书不存在" << endl;
break;
}
b->delbook();
break;
case 4:
cout<<"查找方式:"<<endl<<"1按ISBN书号查询 2按书名查询 3按主编查询 0退出:";
cin>>choice3;
switch(choice3)
{
case 1:
{cout << " 输入ISBN书号:"; //按ISBN书号查询
cin >> bookid;
b=search1(bookid);
if (b==NULL)
{
cout << " 该图书不存在" << endl;
break;
}
b->disp();
}
break;
case 2:
{
cout<<"请输入书名:";
cin>>bname;
b=search2(bookid,bname);
if(b==NULL)
{
cout<<"该图书不存在啊!"<<endl;
break;
}
b->disp();
}
break;
case 3:
{
cout<<"请输入主编:";
cin>>editor;
b=search3(bookid,editor);
if(b==NULL)
{
cout<<"该主编不存在!"<<endl;
break;
}
b->disp();
}
break;
}
break;
case 5:
disp();
break;
case 6:
clear();
break;
}
}
cout<<endl<<" ****** 慢走 ******"<<endl<<endl<<endl;
};
int main()
{
BDatabase BookDB;
cout<<endl<<endl<<endl;
cout<<" Welcome to the library of SCU "<<endl;
cout<<" 欢 迎 来 到 四 川 大 学 图 书 馆 "<<endl;
cout<<endl<<endl<<"请输入0进入图书馆"<<endl;
int w;
cin>>w;
if(w==0)
BookDB.bookdata();
system("pause");
return 0;
}
追问不能运行 有错误
追答你用的是什么编译器,别告诉我你用的是visual c++ 6.0,这种古董早就没人用了,我用的是codeblocks,我的编译过了,完美运行,没任何错误,建议你也换换 编译器吧,俗话书,工欲善其事,必先利其器嘛