实验3

一、实验目的

  1. 知道什么是类模板,会正确定义和使用简单的类模板
  2. 能够描述数组的特性,会使用C++语法正确定义和访问内置数组,知道其局限性
  3. 能够解释指针变量的特性,会使用C++语法正确定义指针变量间接访问对象
  4. 能熟练使用现代C++标准库中的数组模板类array、动态数组类模板vector、字符串类string
  5. 知道现代C++标准库中迭代器,会使用迭代器访问各种序列容器对象
  6. 针对具体问题场景,能够灵活、组合使用C++标准库与自定义类对问题进行抽象和编程求解

二、实验内容

实验任务1

程序代码

task1_1.cpp
#include <iostream>

using std::cout;
using std::endl;

// 类A的定义
class A{
public:
    A(int x0, int y0): x{ x0 }, y{ y0 } {}
    void show() const { cout << x << ", " << y << endl; }
private:
    int x, y;
};

// 类B的定义
class B{
public:
    B(double x0, double y0): x{ x0 }, y{ y0 } {}
    void show() const { cout << x << ", " << y << endl;}
private:
    double x, y;
};

int main() {
    A a(3, 4);
    a.show();

    B b(3.2, 5.6);
    b.show();
}

task1_2.cpp
#include <iostream>
#include <string>

using std::cout;
using std::endl;

// 定义类模板X
template<typename T>
class X{
public:
    X(T x0, T y0): x{x0}, y{y0} {}
    void show() const { cout << x << ", " << y << endl;}
private:
    T x, y;
};

int main() {
    X<int> x1(3, 4);
    x1.show();

    X<double> x2(3.2, 5.6);
    x2.show();

    X<std::string> x3("hello", "c plus plus");
    x3.show();
}

结果截图

task1_1.cpp

task1_2.cpp

实验任务2

程序代码

task2_1.cpp
#include <iostream>
#include <string>

int main() {
    using namespace std;

    string s1, s2;
    s1 = "nuist";                                 // 赋值
    s1[0] = 'N';                                  // 支持通过[]和索引方式访问
    s1.at(1) = 'U';                               // 支持通过xx.at()方法访问
    cout << boolalpha << (s1 == "nuist") << endl; // 字符串比较
    cout << s1.length() << endl;                  // 字符串长度
    cout << s1.size() << endl;                    // 字符串长度
    s2 = s1 + ", 2050";                           // 字符串连接
    cout << s2 << endl;

    string email{"xyz@gmail.com"};
    auto pos = email.find("@"); // 查找子串"@"第一次出现的索引位置,如果失败,返回string::npos
    if (pos == string::npos)
        cout << "illegal email address";
    else {
        auto s1 = email.substr(0, pos);  // 取子串, 从索引0 ~ pos-1
        auto s2 = email.substr(pos + 1); // 取子串,从pos+1到末尾
        cout << s1 << endl;
        cout << s2 << endl;
    }

    string phone{"15216982937"};
    cout << phone.replace(3, 5, string(5, '*')) << endl; // 把从索引位置为3开始的连续5个字符替换成*

    string s3{"cosmos"}, s4{"galaxy"};
    cout << "s3: " + s3 + " s4: " + s4 << endl;
    s3.swap(s4); // 浜ゆ崲
    cout << "s3: " + s3 + " s4: " + s4 << endl;

    string s5{"abc"};
    const char *pstr = s5.c_str(); // 方法c_str()把string类字符串组转换成C风格的字符串
    cout << pstr << endl;

    string s6{"12306"};
    int x1 = stoi(s6); // 把string转换成int
    cout << x1 << endl;

    int x2 = 12306;
    string s7 = to_string(x2);  // 把int转换成string
    cout << s7 << endl;

    double x3 = 123.06;
    string s8 = to_string(x3); // 把double转换成string
    cout << s8 << endl;
}

task2_2.cpp
#include <iostream>
#include <string>
#include <limits>

int main() {
    using namespace std;

    const int n = 10;
    string prompt = string(n, '*') + "Enter a string: " + string(n, '*') + '\n';
  
    cout << prompt;
    string s1;
    cin >> s1;  // 从输入流中提取字符串给s1,碰到空格、回车、Tab键即结束
    cout << "s1: " << s1 << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');  // 清空输入缓冲区

    cout << prompt;
    getline(cin, s1);  // 从输入流中提取一行字符串给s1,直到换行
    cout << "s1: " << s1 << endl;

    string s2, s3;
    cout << prompt;
    getline(cin, s2, ' ');  // 从输入流中提取字符串给s2,直到指定分隔符空格
    getline(cin, s3);
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;
}

结果截图

task2_1.cpp

task2_2.cpp

实验任务5

程序代码

info.hpp
#pragma once

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;
class info
{
private:
    string nickname, contact, city;
    int n;
public:
    info(string nknm, string cnt, string ct, int n): nickname{nknm}, contact{cnt}, city{ct}, n{n}{}
    void print();
    // void set_nickname(){getline(cin, nickname);}
    // void set_contact(){getline(cin, contact);}
    // void set_city(){getline(cin, city);}
    // void set_n(){cin >> n;}
    // int get_n(){return n;}
};

void info::print()
{
    cout << setfill(' ') << setw(8) << left << "昵称: " << nickname << endl
         << setfill(' ') << setw(8) << left << "联系方式: " << contact << endl
         << setfill(' ') << setw(8) << left << "所在城市: " << city << endl
         << setfill(' ') << setw(8) << left << "预定人数: " << n << endl;  
    cout << endl;
}

task5.cpp
#include "info.hpp"
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
using namespace std;

const int capacity = 100;
vector<info> audience_info_list;
void test()
{
    
    cout << "录入信息:\n" << endl;
    cout << setfill(' ') << setw(16) << left << "昵称"
         << setfill(' ') << setw(28) << left << "联系方式(邮箱/手机号)"
         << setfill(' ') << setw(16) << left << "所在城市"
         << setfill(' ') << setw(16) << left << "预定参加人数" << endl;  
    
    int count = 0;
    string s1, s2, s3;
    int n1;
    
    while(cin >> s1 >> s2 >> s3 >> n1)
    {      
        audience_info_list.push_back(info(s1, s2, s3, n1));
        count += n1;
        // audience_info_list[i].set_nickname();
        // audience_info_list[i].set_contact();
        // audience_info_list[i].set_city();
        // audience_info_list[i].set_n();
        // count += audience_info_list[i].get_n();
        if(count > capacity)
        {
            count -= n1;
            cout << "对不起, 只剩" << capacity - count << "个位置" << endl
                 << "1. 输入u, 更新预定信息" << endl
                 << "2. 输入q, 退出预定" << endl
                 << "你的选择: ";
            char s;
            cin >> s;
            if( s == 'q')
            {
                break;
            }
            else if ( s == 'u')
            {
                continue;
            }
            
        }
        else if (count == capacity)
        {
            break;
        }   
    }
    cout << "截至目前,一共有" << count << "位听众预定参加。预定听众信息如下:" << endl;
    // for(auto &item: audience_info_list)
    // {
    //    item.print();
    // }
    for (auto &item : audience_info_list)
    {
        item.print();
    }
}

int main()
{
    test();
}

结果截图

结果1

结果2

实验任务6

程序代码

TextCoder.hpp
#pragma once

#include<iostream>
#include<string>
using namespace std;
class TextCoder
{
private:
    string text;
    void encoder();
    void decoder();
public:
    TextCoder(string t);
    string get_ciphertext();
    string get_deciphertext();
    ~TextCoder() = default;
};
TextCoder::TextCoder(string t1): text{t1}{}

void TextCoder::encoder()
{
    for(int i = 0; i < text.length(); i++)
    {
        if((text[i] >= 97 && text[i] <= 122) || (text[i] >= 65 && text[i] <= 90))
        {
            if (text[i] + 5 > 122 || (text[i] + 5 > 90 && text[i] + 5 < 97))
            {
                text[i] -= 21; 
            }
            else
            {
                text[i] += 5; 
            }
            
        }
        else
        {
            continue;
        }
    }
}
void TextCoder::decoder()
{
    for(int i = 0; i < text.length(); i++)
    {
        if((text[i] >= 97 && text[i] <= 122) || (text[i] >= 65 && text[i] <= 90))
        {
            if (text[i] - 5 < 65 || (text[i] - 5 > 90 && text[i] - 5 < 97))
            {
                text[i] += 21; 
            }
            else
            {
                text[i] -= 5; 
            }
            
        }
        else
        {
            continue;
        }
    }
}

string TextCoder::get_ciphertext()
{
    encoder();
    return text;
}
string TextCoder::get_deciphertext()
{
    decoder();
    return text;
}
task6.cpp
#include "TextCoder.hpp"
#include <iostream>
#include <string>

void test() {
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text)) {
        encoded_text = TextCoder(text).get_ciphertext();  // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

int main() {  
    test(); 
}

结果截图

更改数据测试

posted @ 2022-10-25 18:53  civilwen  阅读(30)  评论(0)    收藏  举报