第一次软件开创作业

本博客内源码来源于室友大一的C++大作业,本人在此基础上进行一定的优化更改。

首先,这是一个购书系统,源码如下:

#include<string> 
#include<iostream>
using namespace std;
class buyer;
class book;
class buybook;
class System{//系统类,显示菜单 
    public:
        void menu();//打开菜单 
        void exitsystem();//退出菜单                   
};
void System::menu()//菜单 
{
    cout<<"**************************************"<<endl;
    cout<<"          欢迎使用本系统              "<<endl; 
    cout<<"           1 书籍菜单                 "<<endl;
    cout<<"           2 书籍信息                 "<<endl;
    cout<<"           3 加入购物车               "<<endl;
    cout<<"           4 付款                     "<<endl; 
    cout<<"           0 退出                     "<<endl; 
    cout<<"**************************************"<<endl;
}
void System::exitsystem()//退出菜单 
{
    cout<<"欢迎下次使用"<<endl;
    system("pause");
    exit(0); //退出程序 
} 
class book{//书籍类,显示书籍信息 
    public:
        string book_ID;
        string book_name;
        string author;
        string publishing;
        double price;
        friend System;
        book(string id="0",string name="0",string au="0",string pu="0",double pr=0);//构造函数 
        void allshow();//显示所有书籍的信息 
        void show();//显示用户指定的书籍 
        string getid();//返回书籍编号 
        string getname();//返回书籍名称 
};
book::book(string id,string name,string au,string pu,double pr)//构造函数 
{
    book_ID=id;
    book_name=name;
    author=au;
    publishing=pu;
    price=pr;     
}
    book Book[20]={//编入所有书籍的信息 
        book("001","《如何变强》","琦玉","一拳出版社",38),
        book("002","《高等数学》","亚里士多德","海洋出版社",38),
        book("003","《大学英语》","aristoping","ttk出版社",21),
        book("004","《璃月故事集》","呵呵哒","璃月出版社",28),
        book("005","《蒙德乐曲大全》","巴巴托斯","蒙德出版社",99),
        book("006","《生活小技巧》","哎嘿嘿","木木出版社",18),
        book("007","《魔法入门到入土》","嚯哈哈","万灵出版社",135),
        book("008","《魔导绪论》","嚯哈哈","万灵出版社",198),
        book("009","《炼金指南》","阿贝多","蒙德出版社",66),
        book("010","《犯罪心理学》","张三","厚大出版社",58),
        book("011","《法学入门》","张三","厚大出版社",48),
        book("012","《你不能不知道的法律知识》","张三","厚大出版社",138),
        book("013","《霍格沃茨旅游指南》","老邓头","霍格沃茨出版社",18),
        book("014","《峡谷相对论》","诶嘿","峡谷出版社",28),
        book("015","《中路教学:从黑铁到王者》","Faker","skt出版社",99),
        book("016","《大学小技巧》","大卫飞","海洋出版社",15),
        book("017","《让风告诉你》","巴巴托斯","蒙德出版社",59),
        book("018","《时间旅行的可行性》","dio","JOJO出版社",68),
        book("019","《时间暂停的小技巧》","dio","JOJO出版社",48),
        book("020","《如何抱紧大腿》","","峡谷出版社",38)
    };
void book::allshow()
{
    for(int i=0;i<20;i++)
    {
        cout<<"-----------------------------------------------"<<endl;
        cout<<"编号:"<<Book[i].book_ID<<endl;
        cout<<"书名:"<<Book[i].book_name<<endl;
        cout<<"作者:"<<Book[i].author<<endl;
        cout<<"出版社:"<<Book[i].publishing<<endl;
        cout<<"价格:"<<Book[i].price<<endl;
    }
} 
void book::show()//查找书籍 
{
    string name;
    int num=0;
    cout<<"请输入书籍的编号或名称:"; 
    cin>>name;
    for(int i=0;i<20;i++)
    {
        if(Book[i].book_ID==name||Book[i].book_name==name)
        { 
            cout<<"-----------------------------------------------"<<endl;
            cout<<"编号:"<<Book[i].book_ID<<endl;
            cout<<"书名:"<<Book[i].book_name<<endl;
            cout<<"作者:"<<Book[i].author<<endl;
            cout<<"出版社:"<<Book[i].publishing<<endl;
            cout<<"价格:"<<Book[i].price<<endl;
            num+=1;
        }
    }
    if(num==0)
    cout<<"书库没有这本书。"<<endl;
}
string book::getid()
{
    return book_ID;
}
string book::getname()
{
    return book_name;
}
class buybook:public book{//购物车 
    public:
        int b_num;//书籍数量 
        static double Pri;//总价
        void buy();//选择想购买的书 
        void openbc();
    protected:
        string book_ID;
        string book_name;
        string author;
        string publishing;     
};
double buybook::Pri=0.0;
void buybook::buy()
{
    int i=0;
    int num=0;
    int a=0;
    string name;
    while(i==0)
    {
        cout<<"请输入你想购买的书籍的书名或编号:";
        cin>>name;
        for(int j=0;j<20;j++)
        {
            if(Book[j].book_ID==name||Book[j].book_name==name)
            {
                a=j;
            }
            else
            {
                num+=1;
            }
        }
        if(num==20)
        {
            cout<<"输入错误!"<<endl;
            num-=20;
        }
        else{
            Pri+=Book[a].price;
            a=0;
        }
        cout<<"如果你想继续购买,请按0:";
        cin>>i;
        num=0;
    }
} 
class buyer:public buybook{
    public:
        string zh;//账号 
        string mm;//密码
        string name;//名字 
        string address;//地址 
        string pay;//折扣 
        string grade;//会员等级 
        buyer(string z,string MM,string na,string ad,string Gr,string p);
        void showin();//登录 
        void payall();//付钱 
        int jh;//记号 
        int ge;//会员等级记号 
}; 
buyer::buyer(string z="0",string MM="0",string na="0",string ad="0",string Gr="0",string p="0")
{
    zh=z;
    mm=MM;
    name=na;
    address=ad;
    grade=Gr;
    pay=p;
}
buyer BUYer[10]={//编入会员信息 
        buyer("10111","123456789","温迪","风起地","普通会员","100%"),
        buyer("10112","125408823","钟离","璃月","黄金会员","70%"),
        buyer("10583","277877025","刻晴","璃月","钻石会员","100%"),
        buyer("10661","234237457","凝光","群玉阁","钻石会员","90%"),
        buyer("10333","175869423","可莉","蒙德","黄金会员","80%"),
        buyer("10005","157862138","优菈","蒙德","钻石会员","80%"),
        buyer("16996","551782317","安柏","蒙德","普通会员","100%"),
        buyer("12138","184723596","甘雨","绝云间","钻石会员","70%"),
        buyer("14265","175203541","凯亚","蒙德","普通会员","100%"),
        buyer("13738","147213486","莫娜","蒙德","白银会员","90%"),
    };
void buyer::showin()//登录系统 
{
    int num=0;
    string ZZ;
    string MM;
    cout<<"请输入账号:";
    cin>>ZZ;
    cout<<endl;
    cout<<"请输入密码:"; 
    cin>>MM;
    cout<<endl;
    for(int i=0;i<10;i++)
    {
        if(BUYer[i].zh==ZZ&&BUYer[i].mm==MM)
        {    
            cout<<"-----------------------------"<<endl;
            cout<<"姓名:"<<BUYer[i].name<<endl;
            cout<<"地址:"<<BUYer[i].address<<endl;
            cout<<"会员等级:"<<BUYer[i].grade<<endl;
            if(BUYer[i].grade=="普通会员")
                cout<<"折扣:无"<<endl;
            else
                cout<<"折扣:"<<BUYer[i].pay<<endl; 
            num+=1;
            ge=i;
        }
    }
    if(num==0)
    {
        jh=0;
    }
    else
        jh=1; 
}
void buyer::payall()//付费函数 
{
    cout<<"书籍总价:"<<Pri<<endl;
    cout<<"您需要支付的金额:";
    if(BUYer[ge].grade=="普通会员")
        cout<<Pri<<endl;
    else
        if(BUYer[ge].grade=="白银会员")
            cout<<0.9*Pri<<endl;
        else
            if(BUYer[ge].grade=="黄金会员")
                cout<<0.8*Pri<<endl;
        else
            cout<<0.7*Pri<<endl;
}
int main()
{
    System s;
    book a;
    buybook b;
    buyer B;
    int select=0;//创建用户选择输入的变量
    B.showin();
    if(B.jh==0)
        cout<<"账号或密码错误!"<<endl;
    while(B.jh==1)
    {
        s.menu();
        cout<<"请输入您的选择:";
        cin>>select;
        switch(select)
        {
            case 0:
                s.exitsystem();//退出系统
                break;
            case 1:a.allshow();//书目 
                break;
              case 2:a.show();//书籍查询 
                break;
            case 3:b.buy();//想买树 
                break;
            case 4:B.payall();//查看购物车 
                break;
            dafault:
                "输入错误!";
                break;
        }            
    }
    system("pause");
    return 0;
} 

我觉得嘛,这个代码在对于书籍和顾客的信息的保存类型上可以进行一定的有话,因为室友是将这些信息作为全局变量保存在代码中,但一定程度上不便于后台管理员对数据进行修改,因此我决定将这些信息更改为文件保存。

 只需要构建一个从.txt文件中读取数据的函数

void bookal(book Book[n])
{
     ifstream in("data.txt",ios::in);//数据数据文件
     if(!in.is_open())
     {
          cout<<"打开错误!"<<endl;
          exit(0);
     }
     int i=0;
     int j=0;
     vector<double> booK(5);
     for(i;i<n;i++)
     {
          while(!in.eof()&&j<5)
          {
                in>>booK[j];
                j++;
           }
     }

另外读取顾客的数据也可以通过对这个函数进行一定修改实现。

 

 这样,如果想修改书籍或顾客信息,可以直接去指定的数据文件中修改

 

posted @ 2022-03-01 15:35  忆空凡  阅读(62)  评论(0)    收藏  举报