软件工程综合实践第一周作业

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++)

首先,他们小组先编写了书籍结构体,用于存放书籍基本信息的一串代码用来给卖书的一方管理图书信息,接着则是使用

菜单结构体,实现界面引导,然后是创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象,以实现删除图书信息的目的,接着是实现查询某本指定书籍的信息,在搜索图书信息中也考虑到了会出现bug的存在,当图书不存在的时候,也会显示图书不存在,

感觉在图书信息的管理中,建议能够新增一栏叫做图书编号的一栏,查找添加各种书目时更加方便快捷。之后则是设计了一个专门用于用户的界面,与管理书籍的代码类似,同时也被分成了搜索,创建,添加等条目。但此部分也依旧存在问题,例如用户的注册,以及当用户数目达到一个非常大的数值时,该如何应对,也并没有能体现出来,个人感觉是可以通过数据库来解决这种问题。之后也是专门为用户设计了一个订单的界面,包括了查询书名,价格等,还加入了可以运算总价的代码。

但在用户买书的过程中,一些比如书会缺货啊,也会导致实际运用中的一些问题,以及bug。

书籍信息的修改:

输入想要修改的书籍编号,按照查找的方法,找到i,然后进行覆盖。完成修改。

保存信息到txt文件:

同分为两部分,先存入表头,然后带参循环存入书籍信息。带参的意义就是,可以无限调用该语句,只要知道书的序号

以下是添加的代码,

class BookManage

{

 

private:

Book books[100];      //定义Book书籍结构体数组对象,最大容量100

 

public:

int length;

 

    BookManage()            //构造函数,读取磁盘文件

    {

length=0;      //从头读起,计数,最大100  

 

}

    ~BookManage()     //析构函数,保存磁盘文件

    {

 

}

 

    int add(Book book)    //添加图书资料

    {

if(length>=100)

{

            cout<<"对不起,储存已满,您无法继续添加!\n";

            return -1;

}

        else

{

            books[length].copy(book);  //调用书籍结构体copy函数

            length++;

            return (length-1);

}

}

 

int bna_search(char *bna)   //按书名查找书籍 

    {

        for(int i=0;i<length;i++)

        if(strcmp(books[i].get_bname(),bna)==0) //判断

        return i;

        return -1;

    }

 

int  bau_search(char *bau)  //按作者查找书籍   

    {   

        for(int i=0;i<length;i++)

        if(strcmp(books[i].get_bauthor(),bau)==0)//判断

 

 return i;

 

         return -1;

 

    }

 

 

 

void revise(Book book,int i) //修改书籍函数 

 

    {

 

books[i].copy(book);

 

}       //调用Book结构体copy函数

 

 

 

void  deleteBook(int index)  //删除书籍资料函数

 

    {

 

        for(int i=index;i<length-1;i++)

 

books[i].copy(books[i+1]);//用后面的地址将当前的地址覆盖   

 

        length--;    

 

    }

 

 

 

void show_bookhead() //显示输出列表,表头。

 

}

简单增加了按作者,书名查找书籍的功能,简单优化了一下修改,删除书籍信息的功能。

补充:代码没多久看懂了。

 

posted @ 2019-03-03 18:14  KaridaRaiser  阅读(152)  评论(0编辑  收藏  举报