面向对象->实验报告二(C++)

导读:本篇文章讲解 面向对象->实验报告二(C++),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

task5


为了快速体验最佳效果请下载编译后的程序:
点我下载(Mac)
点我下载(Windows)

Mac版使用说明:下载后chmod +x 2_task5 添加可执行权限后再运行


实验要求

面向对象->实验报告二(C++)


介于学习了一些新的库函数,下面用两种方法实现,一种使用了大量c++中的库函数,一种是只为实现目的而采用的精简版。可以按需求学习。


* 法一

main.cpp

/* main.cpp */
#include <iostream>
#include "info.hpp"
#include "vector"
#include "array"
#include "regex"
#include "sstream"
int main() {
    using namespace std;
    vector<Info> audience_info_list;
    array<string, 4> vec_temp{};
    int temp, flag = -1;

    char choose;
    const int capacity = 100;   // 最大容量
    int now_capacity = 0;       // 当前人数
    regex reg("\\s+");
    string buffer;

    /* 输入模块 */
    main_menu();
    while (getline(cin, buffer)) {
        buffer = regex_replace(buffer, reg, " ");    /* 去多余空格 */
        stringstream stream;
        istringstream iss(buffer);
        for(auto &i : vec_temp)
            getline(iss, i, ' ');
        stream << vec_temp.at(3);
        stream >> temp;
        if(now_capacity + temp > capacity){
            cout << "对不起,只剩" << capacity-now_capacity << "个位置." << endl;
            do{
                quit_menu();
                cin >> choose;
                switch (choose) {
                    case 'u':
                        flag = 0;   cin.ignore();   break;  // 吃掉cin后的换行,防止被下一次循环的getline识别
                    case 'q':
                        flag = 1;   break;
                    default:
                        flag = -1;  break;
                }
            }while(flag == -1);
        }else{
            now_capacity += temp;
            // 构造函数初始化
            audience_info_list.emplace_back(vec_temp.at(0), vec_temp.at(1), vec_temp.at(2), temp);
        }
        if (flag == 1)break;
    }

    /* 输出模块 */
    cout << "\n截至目前,一共有" << now_capacity << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto _t: audience_info_list)
        _t.print();
    return 0;
}

info.hpp

/* info.hpp */
#ifndef CPP_INFO_HPP
#define CPP_INFO_HPP

#include "iostream"
#include "iomanip"
using namespace std;
class Info{
    string nickname;
    string contact;
    string city;
    int n;
public:
    Info(string &a,string &b, string &c, int &d):
            nickname(a), contact(b), city(c), n(d){};
    void print();
};
void Info::print() {
    cout << left << setw(19) <<"称呼:"    << left << nickname << endl;
    cout << left << setw(21) <<"联系方式:" << left << contact << endl;
    cout << left << setw(21) <<"所在城市:" << left << city << endl;
    cout << left << setw(21) <<"预定人数:" << left << n << endl;
}
void quit_menu(){
    cout << "1. 输入u,更新(update)预定商品" << endl;
    cout << "2. 输入q,退出预定" << endl;
    cout << "你的选择:";
}
void main_menu(){
    cout << "录入信息:"<<endl<<endl;
    cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
}

#endif //CPP_INFO_HPP

* 法二

main.cpp

/* main.cpp */
#include <iostream>
#include "info.hpp"
#include "vector"
int main() {
    using namespace std;
    vector<Info> audience_info_list;
    string name, contact, city;
    int n;
    char choose;
    const int capacity = 100;   // 最大容量
    int now_capacity = 0;       // 当前人数
    /* 输入模块 */
    cout << "录入信息:\n\n"
            "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
    while (cin >> name >> contact >> city >> n) {
        if(now_capacity+n > capacity){
            cout << "对不起,只剩" << capacity-now_capacity << "个位置." << endl;
            quit_menu();
            cin >> choose;
            if(choose == 'q')break;
        }else{
            now_capacity += n;
            audience_info_list.emplace_back(name, contact, city, n);
        }
    }
    /* 输出模块 */
    cout << "\n截至目前,一共有" << now_capacity << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto _t: audience_info_list) {
        _t.print();
    }
    return 0;
}

info.hpp

/* info.hpp */
#ifndef CPP_INFO_HPP
#define CPP_INFO_HPP

#include "iostream"
#include "iomanip"
using namespace std;
class Info{
    string nickname;
    string contact;
    string city;
    int n;
public:
    Info(string &a,string &b, string &c, int &d):
        nickname(a), contact(b), city(c), n(d){};
    void print();

};
void Info::print() {
    cout <<  left << setw(19);
    cout << "称呼:"<<  nickname << endl;
    cout <<  left << setw(21);
    cout <<   "联系方式:" <<  contact << endl;
    cout <<  left << setw(21);
    cout <<  "所在城市:" << city << endl;
    cout <<  left << setw(21);
    cout <<   "预定人数:" <<  n << endl;
}

void quit_menu(){
    cout << "1. 输入u,更新(update)预定商品" << endl;
    cout << "2. 输入q,退出预定" << endl;
    cout << "你的选择:";
}

#endif

运行结果

测试环境 : MacOS。在windows下由于非等宽字体的问题可能会出现输出不对齐,将称呼前的setw(19)改为setw(21)即可。
另外值得注意的是:所有Linux和类Unix操作系统都是以^D结束操作,Windows是以^Z结束操作。

面向对象->实验报告二(C++)
面向对象->实验报告二(C++)


总结

  • 法一练习了 vectorarraygetlineregexistringstreamstringstreamemplace_back基于范围的for循环迭代循环 等技术,将其综合运用与具体的代码实现中。对于一些常见的技术手法都有了了解。更区分了cc++代码风格的不同。

  • 法二仅为了达成目标而没有练习使用上述技术,虽然代码更简洁但对于练习和提升没有太大帮助。


task6


为了快速体验最佳效果请下载编译后的程序:
点我下载(Mac)
点我下载(Windows)

Mac版使用说明:下载后chmod +x 2_task6 添加可执行权限后再运行


实验要求

面向对象->实验报告二(C++)


main.cpp

/* main.cpp */
#include "textcoder.hpp"
#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string text, encoded_text, decoded_text;
    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;
        decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

textcoder.hpp

/* textcoder.hpp */
#ifndef CPP_TEXTCODER_HPP
#define CPP_TEXTCODER_HPP

#include "iostream"
using namespace std;
class TextCoder{
    string text;
public:
    TextCoder(string &temp):text(temp){};
    string encoder();
    string decoder();
};
string TextCoder::encoder() {
    for(auto &i : text){
        if(i>='a' && i<='z') i=(i-'a'+5)%26+'a';
        if(i>='A' && i<='Z') i=(i-'A'+5)%26+'A';
    }
    return text;
}
string TextCoder::decoder() {
    for(auto &i : text){
        if(i>='a' && i<='z') i=(i-'a'+21)%26+'a';
        if(i>='A' && i<='Z') i=(i-'A'+21)%26+'A';
    }
    return text;
}
#endif //CPP_TEXTCODER_HPP

运行结果

注意:测试环境为MacOS,在所有Linux和类Unix操作系统中都是以^D结束操作,windows是以^Z结束操作。

面向对象->实验报告二(C++)


总结

  • 核心就是一个加解密算法(字符偏移)。

总结

  • 初步体会了getlinecingetscin.getline() 的使用与区别
  • 体会了迭代器循环基于范围的for循环的使用手法
  • 对于正则表达式在实际操作过程中的应用有了更深的体会

参考资料

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/17935.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!