第一次作业

此段代码是去年小学期的代码,要求是用c++完成一个图书管理系统,完成对文件的搜索写入写出。

//图书信息
class book
{
public:
void writet(const char *filename); //添加图书信息
void readt(const char *filename); //读取图书信息
void delt(const char *filename); //删除图书信息
int search_post(const char *filename,int id); //搜索图书信息
void searcht(const char *filename); //查看图书信息
void editt(const char *filename); //修改图书信息
};


void book::writet(const char *filename) //添加图书信息
{
ofstream out;
out.open("books.txt",ios::app);
if(!out.is_open())
{
cout<<"open error"<<endl;
return ;
}
else
{
int id;
string name,author,publishing;
double price;
cout<<"请输入id、书名、作者、出版社、价格:"<<endl;
cin>>id>>name>>author>>publishing>>price;
out.fill(' ');
out.setf(ios::left);
out<<space<<id<<space<<name<<space<<author<<space<<publishing<<space<<price<<NEWLINE;
out.close();
cout<<"添加成功!!"<<endl;
}
}

void book::readt(const char *filename) //读取图书信息
{
ifstream in;
in.open("books.txt",ios::in);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
stringstream ss; //创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
char line[100];
cout.setf(ios::left);
cout.fill(' '); //用空格填充
cout<<space<<"id"<<space<<"name"<<space<<"author"<<space<<"publishing"<<space<<"price"<<endl;
while(in.peek()!=EOF)
{
in.getline(line,100);
ss<<line;
int id;
string name,author,publishing;
double price;
ss>>id>>name>>author>>publishing>>price;
cout<<setw(20)<<id<<setw(20)<<name<<setw(20)<<author<<setw(20)<<publishing<<setw(20)<<price<<endl;
ss.str(""); //用""给ss赋值
ss.clear(); //清空字符串
}
in.close();
}
}


void book::delt(const char *filename) //删除图书信息
{
int id;
cout<<"请输入您要删除图书的编号"<<endl;
cin>>id;
ifstream in;
in.open("books.txt",ios::in);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
string temp;
stringstream ss;
int curId;;
while(in.peek()!=EOF)
{
string line;
getline(in,line);
ss<<line;
ss>>curId;
if(curId!=id)
{
temp += line + NEWLINE;
}
ss.str("");
ss.clear();
}
in.close();
ofstream out;
out.open("books.txt",ios::out);
if(!out.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
out<<temp;
out.close();
cout<<"删除成功!!"<<endl;
}
}
}


int book::search_post(const char *filename,int id) //搜索图书信息
{
ifstream in;
in.open("books.txt",ios::in|ios::binary);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return -1;
}
else
{
stringstream ss;
while(in.peek()!=EOF)
{
int start = in.tellg();
string line;
getline(in,line);
ss<<line;
int curID;
ss>>curID;
if(curID == id)
{
in.close();
return start;
}
ss.str("");
}
cout<<"对不起您查找的图书信息不存在!"<<endl;
in.close();
}
return -1;
}


void book::searcht(const char *filename) //查看图书信息
{
cout<<"请输入您要查找的图书id:"<<endl;
int id;
cin>>id;
int pos = search_post("books.txt",id);
string line;
fstream in;
in.open("books.txt",ios::in|ios::binary);
in.seekg(pos,ios::beg);
getline(in,line);
cout.setf(ios::left);
cout<<setw(13)<<"id"<<setw(20)<<"name"<<setw(15)<<"author"<<setw(15)<<"publishing"<<setw(15)<<"price"<<endl;
cout<<line<<endl;
}


void book::editt(const char *filename) //修改图书信息
{
int id;
string name,author,publishing;
double price;
cout<<"请输入您要修改的图书id"<<endl;
cin>>id;
cout<<"请输入该图书新的书名、作者、出版社、价格"<<endl;
cin>>name>>author>>publishing>>price;
stringstream infoTemp;
infoTemp.fill(' ');
infoTemp.setf(ios::left);
infoTemp<<space<<id<<space<<name<<space<<author<<publishing<<price;
string newInfo;
getline(infoTemp,newInfo);
fstream file;
file.open("books.txt",ios::in|ios::out|ios::binary);
if(!file.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
int pos = search_post("books.txt",id);
file.seekg(pos,ios::beg);
file<<newInfo;
cout<<"修改后信息为:"<<endl;
cout<<newInfo<<endl;
file.close();
}
}

 

 


用户
class buyer
{
public:
void ready(const char *filename); //读取用户信息
void dely(const char *filename); //删除用户信息
int search_posy(const char *filename,int id); //搜索用户信息
void searchy(const char *filename); //查找用户信息
void readd(const char *filename); //查看订单信息
};

 

void buyer::ready(const char *filename) //读取用户信息
{
ifstream in;
in.open("users.txt",ios::in);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
stringstream ss; //创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
char line[100];
cout.setf(ios::left);
cout.fill(' '); //用空格填充
cout<<space<<"id"<<space<<"name"<<space<<"address"<<space<<"会员等级"<<endl;
while(in.peek()!=EOF)
{
in.getline(line,100);
ss<<line;
int id,meg;
string name,address;
ss>>id>>name>>address>>meg;
cout<<space<<id<<space<<name<<space<<address<<space<<meg<<endl;
ss.str(""); //用""给ss赋值
ss.clear(); //清空字符串
}
in.close();
}
}


void buyer::dely(const char *filename) //删除用户信息
{
int id;
cout<<"请输入您要删除用户的编号"<<endl;
cin>>id;
ifstream in;
in.open("users.txt",ios::in);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
string temp;
stringstream ss;
int curId;;
while(in.peek()!=EOF)
{
string line;
getline(in,line);
ss<<line;
ss>>curId;
if(curId!=id)
{
temp += line + NEWLINE;
}
ss.str("");
ss.clear();
}
in.close();
ofstream out;
out.open("users.txt",ios::out);
if(!out.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
out<<temp;
out.close();
cout<<"删除成功!!"<<endl;
}
}

}

 

int buyer::search_posy(const char *filename,int id) //搜索用户信息
{
ifstream in;
in.open("users.txt",ios::in|ios::binary);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return -1;
}
else
{
stringstream ss;
while(in.peek()!=EOF)
{
int start = in.tellg();
string line;
getline(in,line);
ss<<line;
int curID;
ss>>curID;
if(curID == id)
{
in.close();
return start;
}
ss.str("");
}
cout<<"对不起您查找的用户信息不存在!"<<endl;
in.close();
}
return -1;
}


void buyer::searchy(const char *filename) //查找用户信息
{
cout<<"请输入您要查找的用户id:"<<endl;
int id;
cin>>id;
int pos = search_posy("users.txt",id);
string line;
fstream in;
in.open("users.txt",ios::in|ios::binary);
in.seekg(pos,ios::beg);
getline(in,line);
cout.setf(ios::left);
cout<<setw(12)<<"id"<<setw(14)<<"name"<<setw(14)<<"address"<<setw(12)<<"会员等级"<<endl;
cout<<line<<endl;
}


void buyer::readd(const char *filename) //查看用户信息
{
ifstream in;
in.open("orders.txt",ios::in);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return ;
}
else
{
stringstream ss; //创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
char line[100];
cout.setf(ios::left);
cout.fill(' '); //用空格填充
while(in.peek()!=EOF)
{
in.getline(line,100);
ss<<line;
int id;
string name,author,publishing;
double price;
ss>>id>>name>>author>>publishing>>price;
cout<<space<<id<<space<<name<<space<<author<<space<<publishing<<space<<price<<endl;
ss.str(""); //用""给ss赋值
ss.clear(); //清空字符串
}
in.close();
}
}

 


订单
class order
{
protected:
double pay; //购书金额
public:
order();
//购书功能
int find_user(const char *filename,int id); //将用户信息输入到订单信息中
int find_book(const char *filename,int id); //将图书信息输入到订单信息中
void options1(); //选购图书
int search_poso(const char *filename,int id); //搜索订单信息
void searcho(const char *filename); //查找订单信息
};

order::order()
{
pay=0;
}

int order::find_user(const char *filename,int id)
{
ofstream outfile;
outfile.open("orders.txt", ios::app);
outfile<<endl;
ifstream in;
in.open("users.txt",ios::in|ios::binary);
if(!in.is_open()) //将购书的用户信息输入到订单中
{
cout<<"file open error"<<endl;
return -1;
}
else
{
stringstream ss;
while(in.peek()!=EOF)
{
int start = in.tellg();
string line;
getline(in,line);
outfile<<line;
outfile.close();
ss<<line;
int curID;
ss>>curID;
if(curID == id)
{
in.close();
return start;
}
ss.str("");
}
cout<<"对不起您查找的图书信息不存在!"<<endl;
}
return -1;
}


int order::find_book(const char *filename,int id)
{
ofstream outfile;
outfile.open("orders.txt", ios::app);
ifstream in;
in.open("books.txt",ios::in|ios::binary);
if(!in.is_open()) //将购买的图书信息输入到订单中
{
cout<<"file open error"<<endl;
return -1;
}
else
{
stringstream ss;
while(in.peek()!=EOF)
{
int start = in.tellg();
string line;
getline(in,line);
outfile << line;
outfile.close();
ss<<line;
int curID;
ss>>curID;
if(curID == id)
{
in.close();
return start;
}
ss.str("");
}
cout<<"对不起您查找的图书信息不存在!"<<endl;
}
return -1;
}

void order::options1() //选购图书
{
int p;
cout<<"请输入用户编号:";
cin>>p;
cout<<endl;
find_user("users.txt",p);
int q;
cout<<"请输入书编号:";
cin>>q;
cout<<endl;
find_book("books.txt",q);
// options1();
}

int order::search_poso(const char *filename,int id) //搜索订单信息
{
ifstream in;
in.open("orders.txt",ios::in|ios::binary);
if(!in.is_open())
{
cout<<"file open error"<<endl;
return -1;
}
else
{
stringstream ss;
while(in.peek()!=EOF)
{
int start = in.tellg();
string line;
getline(in,line);
ss<<line;
int curID;
ss>>curID;
if(curID == id)
{
in.close();
return start;
}
ss.str("");
}
cout<<"对不起您查找的订单信息不存在!"<<endl;
in.close();
}
return -1;
}


void order::searcho(const char *filename)
{
cout<<"请输入您要查找的用户id:"<<endl;
int id;
cin>>id;
int pos = search_poso("orders.txt",id);
string line;
fstream in;
in.open("orders.txt",ios::in|ios::binary);
in.seekg(pos,ios::beg);
getline(in,line);
cout.setf(ios::left);
cout<<line<<endl;
}

阅读完同学写的代码后,此代码是图书管理系统,用c++写的代码,在该注释的地方都注释了,没有数据库的支持,总体来说是一个雏形代码,还有很多需要完善。

不过还有很多值得借鉴的地方,与我写的代码对比的话,他在文件处理方面要比我做的好很多,可以再这方面向这段代码学习。

整体代码缺少些严谨性,有些地方应该加入判断条件防止出现输入的错误。

最后,经过阅读代码,我也从中学到了很多,知道了直接的不足,还有对于代码的理解又进了一步,学习优秀的代码,通过不严谨的代码反思自己。

 

转载于:https://www.cnblogs.com/wangyue111/p/10467525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值