• 机房管理系统


    c++的小项目来源地址 还有很多能修改的地方(有时间再改吧)

    缺少继承的构造函数相关知识点 有点不足

    缺少多态继承

    放个main其他打包看来要学习使用github

      1 #include<iostream>
      2 #include<memory>
      3 #include"orderFile.h"
      4 #include"Student.h"
      5 #include"teacher.h"
      6 #include"manager.h"
      7 
      8 using namespace std;
      9 /*
     10 //https://stackoom.com/question/1mfyQ/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8unique-ptr%E6%89%A7%E8%A1%8Cdynamic-cast
     11 template <typename To, typename From, typename Deleter>
     12 unique_ptr<To, Deleter> dynamic_pointer_cast(unique_ptr<From, Deleter>&& p) {
     13     if (To* cast = dynamic_cast<To*>(p.get()))
     14     {
     15         unique_ptr<To, Deleter> result(cast, std::move(p.get_deleter()));
     16         p.release();
     17         return result;
     18     }
     19     return unique_ptr<To, Deleter>(nullptr); // or throw std::bad_cast() if you prefer
     20 }
     21 */
     22 
     23 void studentMenu(shared_ptr<Identity> &student)
     24 {
     25     while (true)
     26     {
     27         student->openMenu();//call virtual function member()
     28         if (auto s_up = dynamic_pointer_cast<Student>(student))
     29         {
     30             int select = 0;
     31                 cin >> select; //接受用户选择
     32 
     33                 if (select == 1) //申请预约
     34                 {
     35                     s_up->applyOrder();
     36                 }
     37                 else if (select == 2) //查看自身预约
     38                 {
     39                     s_up->showMyOrder();
     40                 }
     41                 else if (select == 3) //查看所有人预约
     42                 {
     43                     s_up->showAllOrder();
     44                 }
     45                 else if (select == 4) //取消预约
     46                 {
     47                     s_up->cancelOrder();
     48                 }
     49                 else
     50                 {
     51                     //注销登录
     52                     cout << "注销成功" << endl;
     53                     system("pause");
     54                     system("cls");
     55                     return;
     56                 }
     57         }
     58     }
     59 }
     60 //进入教师子菜单界面
     61 void teacherMenu(shared_ptr<Identity> &teacher)
     62 {
     63     while (true)
     64     {
     65         //调用子菜单界面
     66         teacher->openMenu();
     67 
     68         if (auto t_up = dynamic_pointer_cast<Teacher>(teacher))
     69         {
     70             int select = 0; //接受用户选择
     71 
     72             cin >> select;
     73 
     74             if (select == 1) //查看所有预约
     75             {
     76                 t_up->showAllOrder();
     77             }
     78             else if (select == 2) //审核预约
     79             {
     80                 t_up->checkOrder();
     81             }
     82             else
     83             {
     84                 cout << "注销成功" << endl;
     85                 system("pause");
     86                 system("cls");
     87                 return;
     88             }
     89         }
     90     }
     91 }
     92 
     93 //进入管理员子菜单界面
     94 void managerMenu(shared_ptr<Identity> &manager)
     95 {
     96     while (true)
     97     {
     98         //调用管理员子菜单
     99         manager->openMenu();
    100 
    101         //将父类指针 转为子类指针,调用子类里其他接口
    102         if (auto m_up = dynamic_pointer_cast<Manager>(manager))
    103         {
    104             int select = 0;
    105             //接受用户选项
    106             cin >> select;
    107 
    108             if (select == 1) //添加账号
    109             {
    110                 //cout << "添加账号" << endl;
    111                 m_up->appPerson();
    112             }
    113             else if (select == 2) //查看账号
    114             {
    115                 //cout << "查看账号" << endl;
    116                 m_up->showPerson();
    117             }
    118             else if (select == 3) //查看机房
    119             {
    120                 //cout << "查看机房" << endl;
    121                 m_up->showComputer();
    122             }
    123             else if (select == 4) //清空预约
    124             {
    125                 //cout << "清空预约" << endl;
    126                 m_up->cleanFile();
    127             }
    128             else
    129             {
    130                 //注销
    131                 cout << "注销成功" << endl;
    132                 system("pause");
    133                 system("cls");
    134                 return;
    135             }
    136         }
    137         
    138     }
    139 
    140 }
    141 void Login(const string &filename,const input &shenfen)
    142 {
    143     ifstream ifs;
    144     ifs.open(filename);
    145 
    146     if (!ifs.good())
    147     {
    148         cout << "文件打开失败" << endl;
    149         ifs.close();
    150         return;
    151     }
    152 
    153     int _id = 0;
    154     string _name;
    155     string _pswd;
    156 
    157     //判断身份
    158     if (shenfen == input::stu) //学生身份
    159     {
    160         cout << "请输入你的学号:" << endl;
    161         cin >> _id;
    162     }
    163     else if (shenfen == input::tea)
    164     {
    165         cout << "请输入您的职工号: " << endl;
    166         cin >> _id;
    167     }
    168 
    169     cout << "请输入用户名:" << endl;
    170     cin >> _name;
    171 
    172     cout << "请输入密码:" << endl;
    173     cin >> _pswd;
    174     
    175     if (shenfen == input::stu)
    176     {
    177         //学生身份验证
    178         int fId; //从文件中读取的id号
    179         string fName; //从文件中获取的姓名
    180         string fPwd; //从文件中获取密码
    181         while (ifs >> fId && ifs >> fName && ifs >> fPwd)
    182         {
    183             //与用户输入的信息做对比
    184             if (fId == _id && fName == _name && fPwd == _pswd)
    185             {
    186                 cout << "学生验证登录成功!" << endl;
    187                 system("pause");
    188                 system("cls");
    189                 shared_ptr<Identity> person = make_shared<Student>(_id, _name, _pswd);
    190 //                unique_ptr<Identity> person(new Student(_id, _name, _pswd));
    191                 studentMenu(person);
    192                 return;
    193             }
    194         }
    195     }
    196     else if (shenfen == input::tea)
    197     {
    198         //教师身份验证
    199         int fId; //从文件中获取的id号
    200         string fName; //从文件中获取的姓名
    201         string fPwd; //从文件中获取的密码
    202         while (ifs >> fId && ifs >> fName && ifs >> fPwd)
    203         {
    204             if (fId == _id && fName == _name && fPwd == _pswd)
    205             {
    206                 cout << "教师验证登录成功!" << endl;
    207                 system("pause");
    208                 system("cls");
    209                 shared_ptr<Identity> person = make_shared<Teacher>(_id, _name, _pswd);
    210 //                unique_ptr<Identity> person(new Teacher(_id, _name, _pswd));
    211                 //进入教师子菜单
    212                 teacherMenu(person);
    213                 return;
    214             }
    215         }
    216     }
    217     else if (shenfen == input::admin)
    218     {
    219         //管理员身份验证
    220         string fName; //从文件中获取姓名
    221         string fPwd; //从文件中获取密码
    222 
    223         while (ifs >> fName && ifs >> fPwd)
    224         {
    225             if (_name == fName && _pswd == fPwd)
    226             {
    227                 cout << "管理员验证登录成功!" << endl;
    228                 system("pause");
    229                 system("cls");
    230                 shared_ptr<Identity> person = make_shared<Manager>(_name, _pswd);
    231 //                unique_ptr<Identity> person(new Manager(_name, _pswd));
    232                 //进入管理员子菜单界面
    233                 managerMenu(person);
    234                 return;
    235             }
    236         }
    237     }
    238     cout << "验证登录失败!" << endl;
    239     system("pause");
    240     system("cls");
    241     return;
    242 }
    243 
    244 
    245 int main()
    246 {
    247     while (true)
    248     {
    249         cout << "= ==================== = 欢迎来到张帅哥的机房预约系统  ==================== = " << endl;
    250         cout << endl << "请输入您的身份" << endl;
    251         cout << "		 -------------------------------
    ";
    252         cout << "		|                               |
    ";
    253         cout << "		|          1.学    生           |
    ";
    254         cout << "		|                               |
    ";
    255         cout << "		|          2.老    师           |
    ";
    256         cout << "		|                               |
    ";
    257         cout << "		|          3.管 理 员           |
    ";
    258         cout << "		|                               |
    ";
    259         cout << "		|          0.退    出           |
    ";
    260         cout << "		|                               |
    ";
    261         cout << "		 -------------------------------
    ";
    262         cout << "输入您的选择: ";
    263 
    264         input shenfen;
    265         cin >> shenfen;
    266         switch (shenfen)
    267         {
    268         case input::stu:
    269             Login(student_file, input::stu);
    270             break;
    271         case input::tea:
    272             Login(teacher_file, input::tea);
    273             break;
    274         case input::admin:
    275             Login(admin_file, input::admin);
    276             break;
    277         case input::exit:  //退出系统
    278             cout << "欢迎下一次使用" << endl;
    279             system("pause");
    280             return 0;
    281             break;
    282         default:
    283             cout << "输入有误,请重新选择!" << endl;
    284             system("pause");
    285             system("cls");
    286             break;
    287         }
    288     }
    289     system("pause");//可以实现冻结屏幕,便于观察程序的执行结果
    290     return 0;
    291 }

     

    static

         如果该关键字位于全局函数或者变量的声明的前面,表明该编译单元不导出这个函数/变量的符号。因此无法在别的编译单元里使用。(内部链接)。如果是static局部变量,则该变量的存储方式和全局变量一样,但是仍然不导出符号。

        默认链接属性:对于函数和变量,模认外部链接,对于const变量,默认内部链接。(可以通过添加extern和static改变链接属性)

        外部链接的利弊:外部链接的符号,可以在整个程序范围内使用(因为导出了符号)。但是同时要求其他的编译单元不能导出相同的符号(不然就是duplicated external simbols)

        内部链接的利弊:内部链接的符号,不能在别的编译单元内使用。但是不同的编译单元可以拥有同样名称的内部链接符号。

    C++什么时候需要将#include写到头文件,什么时候需要写到cpp文件?

    看你这个类怎么用,以及其他类是否作为接口出现。

    若其他类需要作为接口(用到该类相关类型的参数),则需要放到这个类的头文件。

    若这个类的实现可见,而其他类作为实现的一部分(成员或基类),则也需要放头文件。

    否则,若这个类的实现不必可见(使用 pImpl ),而其他类也不作为接口,则其他类的头文件不必放进该类的头文件,只需放进源文件。

     

    更泛化的放头文件还是源文件需要具体问题具体分析。

    有时希望降低编译依赖或保持 ABI 兼容,就把尽量多的东西放源文件,头文件里不放类定义。

    有时希望促进内联优化,就把尽量多的东西放在头文件(为此有些函数定义需要用 inline )



    作者:暮无井见铃
    链接:https://www.zhihu.com/question/270945429/answer/357624598
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
     
    枚举映射
    int getInt(std::string const& s)
    {
       static std::map<std::string, int> theMap = {{"One", 1, {"Two", 2}, {"Three", 3}};
       if ( theMap.find(s) == theMap.end() )
       {
          // Deal with exception.
       }
       return theMap[s];
    }

    枚举输入

    std::istream& operator>>(std::istream& is, input& item)
    {
        int select;
        is >> select;
        if (is)
            item = input(select);
        else
        {
            item = input(0);
            std::cerr << "读取失败" << endl;
        }
        return is;
    }

    dynamic 与 智能指针

    dynamic_unique_cast

    template <typename To, typename From, typename Deleter>
    unique_ptr<To, Deleter> dynamic_unique_cast(unique_ptr<From, Deleter>&& p) {
        if (To* cast = dynamic_cast<To*>(p.get()))
        {
            unique_ptr<To, Deleter> result(cast, std::move(p.get_deleter()));
            p.release();
            return result;
        }
        throw std::bad_cast();
    //    return unique_ptr<To, Deleter>(nullptr); // or throw std::bad_cast() if you prefer
    }

    dynamic_pointer_cast

     
  • 相关阅读:
    Visual C++ 2012/2013的内存溢出检測工具
    MATLAB新手教程
    LCD开发之汉字显示
    支持向量机通俗导论(理解SVM的三层境地)
    类与类之间的简单传值
    hibernate官方新手教程 (转载)
    秒杀多线程第四篇 一个经典的多线程同步问题
    Java的位运算符具体解释实例——与(&amp;)、非(~)、或(|)、异或(^)
    mysql基础:mysql与C结合实例
    php实现字符串的排列(交换)(递归考虑所有情况)
  • 原文地址:https://www.cnblogs.com/Z-s-c11/p/14103240.html
Copyright © 2020-2023  润新知