代码分析 读程 购书系统

功能分析

     购书管理系统的主要功能包括对图书信息的管理,用户信息的管理,以及购书功能的提供。该系统为购书用户的购书(包括图书信息的查询,图书的购买)活动提供帮助,同时也为图书系统的管理者管理图书信息及用户信息提供了便利。

主程序部分

#include<iostream>

using namespace std;

#include<fstream>

#include<stdlib.h>

#include<string>

#include<conio.h>               

#include <sstream>

#include <iomanip>

#include <windows.h>               //系统已有的头文件;

 

#define WIDTH 20

#define space setw(WIDTH)          //读取相邻数据的间隔;

#define NEWLINE "\r\n"

        

#include"buy.h"

#include"book.h"

#include"order.h"     //自己编写的头文件 ;

 

 

void logo1();

void logo2();

void function1();

void function2();

void function3();      //函数的提前声明部分;

 

 

book booker;

buyer buyerr;

order orderr;   //对象;

 

//---------------------------------------------------界面流程模块----------------------------------------------------------

 

void  interface1()         //程序的第一个界面

{

      cout<<"------------------------------------------------------"<<endl;

      cout<<"           欢迎来到网上购书系统                       "<<endl;

      cout<<"------------------------------------------------------"<<endl;

      cout<<"               1.管理员                               "<<endl;

      cout<<"               2.用户                                 "<<endl;

      cout<<"请选择您的身份:"<<endl;

}

 

void interface2()        //登录与注册界面

{

      cout<<"------------------------------------------------------"<<endl;

      cout<<"           欢迎来到网上购书系统                       "<<endl;

      cout<<"------------------------------------------------------"<<endl;

      cout<<"                1.登录                                "<<endl;

      cout<<"                2.返回上一界面                        "<<endl;

      cout<<"请选择您要进行的操作:"<<endl;

}

 

void interface3()        //管理员登录后界面

{

      cout<<"------------------------------------------------------"<<endl;

      cout<<"           欢迎来到网上购书系统                       "<<endl;

      cout<<"------------------------------------------------------"<<endl;

      cout<<"               1.操作图书信息                         "<<endl;

      cout<<"               2.操作用户信息                         "<<endl;

      cout<<"请选择您要进行的操作:"<<endl;

}

 

 

//------------------------------------------------登录模块-----------------------------------------------------------

 

struct user

       {

         string password;

         string name;

       };

 

//管理员登录

 

void logo1()

{

       char *password = new char [20];

     string name;

     cout << "用户名:"<<endl;

     cin>>name;

     cout <<" 密码:"<<endl;

     char test;int i=0;

    while((test=getch())!='\r')

     {

         if(test==8)           //ASC2码8为退格

         {

         if(i>0)

         {

             cout << test << " " << test;

             password[i--]='\0';

         }

         }

         else

         {

             if(i<20)

             cout << "*";

             password[i]=test;

             i++;

         }

     }                      //将输入的密码转化为“* ”;

     password[i]='\0';

     ifstream fin;

     fin.open("admis.txt",ios_base::in);

     if(fin.fail())

     {

         cout << "文件打开失败";

     }

     user c;int f1;

   while(fin>>c.name>>c.password)

     {

         if(c.name==name&&c.password==password)

         {

             cout << "登陆成功";

             f1=1;//登录成功标志

             interface3();

                  int m;

                  cin>>m;

                  switch(m)

                  {

                  case 1:function1();break;

                  case 2:function2();break;

                  }

         }

     }

     if(f1==0)//f1==0登录失败

     {

         cout << "用户名或密码错误";

         logo1();

     }

}

 

//用户登录

 

void logo2()

{

       char *password = new char [20];

     string name;

     cout << "用户名:"<<endl;

     cin>>name;

     cout <<" 密码:"<<endl;

     char test;int i=0;

    while((test=getch())!='\r')

     {

         if(test==8)           //ASC2码8为退格

         {

         if(i>0)

         {

             cout << test << " " << test;

             password[i--]='\0';

         }

         }

         else

         {

             if(i<20)

             cout << "*";

             password[i]=test;

             i++;   

         }         //将输入的密码转化为“* ”;

     }

     password[i]='\0';

     ifstream fin;

     fin.open("user.txt",ios_base::in);   //打开文件user.txt

     if(fin.fail())

     {

         cout << "文件打开失败";

     }

     user c;int f1;

   while(fin>>c.name>>c.password)

     {

         if(c.name==name&&c.password==password)

         {

             cout << "登陆成功";

             f1=1;               //登录成功标志;

             function3();

         }

     }

     if(f1==0)            //若f1=0,则登录失败;并输出提示语句 "用户名或密码错误";

     {

         cout << "用户名或密码错误";

         logo2();

     }

}

 

 

//-------------------------------------管理员添加,读取,删除和修改图书信息部分----------------------------------------------

 

//管理员的功能模块1

 

void function1()              

{

      book booker;

      const char *filename = "books.txt";

    while(true)

    {

        cout<<"--------------------------"<<endl;

        cout<<"0、查看全部信息"<<endl;

        cout<<"1、新增图书信息"<<endl;

        cout<<"2、删除图书信息"<<endl;

        cout<<"3、修改图书信息"<<endl;

        cout<<"4、查找图书信息"<<endl;

           cout<<"5、返回上一界面"<<endl;

        int cmd;

        cin>>cmd;

        system("cls");

        switch(cmd)

        {

            case 0:booker.readt("books.txt");break;        //读取books.txt信息 ;

            case 1:booker.writet("books.txt");break;       //编写/添加books.txt信息;

            case 2:booker.delt("books.txt");break;         //删除books.txt信息 ;

            case 3:booker.editt("books.txt");break;        //编辑books.txt信息 ;

            case 4:booker.searcht("books.txt");break;      //查询books.txt信息;

                 case 5:

                      {

                            interface3();

                         int m;

                         cin>>m;

                         switch(m)

                            {

                            case 1:function1();break;      //调用 function1(),即递归;

                            case 2:function2();break;      //调用function2();

                            }

                      }

                      break;

        }

    }

}

 

 

//----------------------------------------管理员查看和删除用户信息和订单信息模块--------------------------------------------

 

 

void function2()                    //管理员的功能模块2

{

      const char *filename = "users.txt";     //文件名users.txt;

    while(true)

    {

        cout<<"--------------------------"<<endl;

        cout<<"1、删除用户信息"<<endl;

        cout<<"2、查看用户信息"<<endl;

           cout<<"3、查找用户信息"<<endl;

           cout<<"4、返回上一界面"<<endl;

        int cmd;

        cin>>cmd;

        system("cls");

        switch(cmd)

        {

               case 1:buyerr.dely("users.txt");break;          //删除users.txt信息;

            case 2:buyerr.ready("users.txt");break;          //读取users.txt信息;

            case 3:buyerr.searchy("users.txt");break;        //查询users.txt信息;

                 case 4:

                      {

                            interface3();

                         int m;

                         cin>>m;

                         switch(m)

                            {

                            case 1:function1();break;         //调用function1();

                            case 2:function2();break;         //调用function2(),递归;

                            }

                      }

                      break;

        }

    }

}

 

//----------------------------------------用户查看图书信息,购书,查看订单和结算模块-----------------------------------------

 

//用户功能模块

 

void function3()

{

      const char *filename = "orders.txt";

    while(true)

    {

        cout<<"--------------------------"<<endl;

        cout<<"1、图书信息"<<endl;

        cout<<"2、购书"<<endl;

           cout<<"3、查看订单信息"<<endl;

           cout<<"请选择您要进行的操作:";

        int cmd;

        cin>>cmd;

        system("cls");

        switch(cmd)

        {

               case 1:booker.readt("books.txt");break;

            case 2:orderr.options1();break;         //未成功

            case 3:orderr.searcho("orders.txt");break;

        }

    }

}

 

//------------------------------------------------------主程序流程--------------------------------------------------------------

 

void maind()

{

     interface1();

         int i,j;

         cin>>i;

         switch(i)

         {

             case 1:interface2();cin>>j;          //管理员身份

              switch(j)

              {

                 case 1:logo1();break;            //登录

                 case 2:maind();break;

              }

              break;

      case 2:interface2();cin>>j;                 //用户身份

           switch(j)

           {

           case 1:logo2();break;                  //登录

           case 2:maind();break;

           }

           break;

         }

}

 

 

int main()

{

    maind();

    return 0;

}

头文件1:buy.h

//用户信息

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 ;                                    //读取用户【users.txt】信息

    }

    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();           //关闭文件users.txt;

    }

}

 

 

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();

    }

 

头文件2:book.h

//图书信息

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);                     //打开文件books.txt

    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();                                   //关闭文件books.txt

        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();

    }

}

头文件3:order.h

//订单信息

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;              //初始化购书金额为 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);       //输入所选购图书的信息

}

 

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;                                        //输入用户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;                          //输出查询结果

}

 

心得体会

       通过对图书管理系统整段代码的分析,我发现代码的编写过程并不容易,它需要一次又一次的修改和完善;所以说,通读他人的代码的过程实际上是一个学习的过程,通过它,我可以发现别人代码的长处并学为己用,对于代码中存在的缺陷,我可以避免自己之后出现这种错误。

代码来源

软工1班 学号 1759112同学提供。 

 

 

 

posted @ 2019-03-03 20:27  LRLRR  阅读(158)  评论(0编辑  收藏  举报