首页 > 编程语言 >实验3 数组、指针与现代C++标准库

实验3 数组、指针与现代C++标准库

时间:2022-10-23 23:02:28浏览次数:51  
标签:&& string text cout C++ num 数组 include 指针

实验任务5

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;
class Info
{
private:
    string nickname, contact, city;
    int n;
public:
    Info(string nc, string co, string ci, int n1) :nickname(nc), contact(co), city(ci), n(n1) {
    }
    void print()const {
        cout << std::left << setw(16) << "昵称:   " << setw(16) << nickname << endl;
        cout << std::left << setw(16) << "联系方式:   " << setw(16) << contact << endl;
        cout << std::left << setw(16) << "所在城市:   " << setw(16) << city << endl;
        cout << std::left << setw(16) << "预定人数:   " << setw(16) << n << endl;
    }

};
#include"Info.hpp"
#include<string>
#include<vector>
#include<iostream>
using namespace std;
vector<Info> audience_info_list;
int main() {
    cout << "录入信息:" << endl;
    cout << "昵称         联系方式(邮箱/手机号)     所在城市      预定参加人数" << endl;
    const int capacity = 100;
    int sum = 0;
    string s1, s2, s3;
    int num;
    int count = 0;
    while (cin >> s1 >> s2 >> s3 >> num) {
        sum += num;
        if (sum > capacity) {
            sum -= num;
            cout << "对不起,只剩" << capacity - sum << "个位置." << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                << "2.输入q, 退出预定"<<endl<<"你的选择: ";
            string f;
            cin >> f;
            if (f == "u")
            {
                cin >> s1 >> s2 >> s3 >> num;
                Info f(s1, s2, s3, num);
                audience_info_list.push_back(f);
                sum+=num;
            }
            else
                break;
        }

        else audience_info_list.push_back(Info(s1, s2, s3, num));
    }
    cout << endl << "截至目前,一共有" << sum << "位听众预定参加。信息如下:" << endl;;
    for (int i = 0; i < audience_info_list.size(); i++)
    {
        audience_info_list.at(i).print();
        cout << endl;
    }
    return 0;
}

 

 

 

 实验任务6

#pragma once

#include <iostream>
#include <string>

using namespace std;
 
class TextCoder {
    public:
        TextCoder(string t): text{t} {};
        string get_ciphertext() { 
        encoder();
        return text; };
        string get_deciphertext() { 
        decoder();
        return text; };
        
    private:
        string text;
        void encoder();
        void decoder();
};

void TextCoder::encoder(){
     for(auto i = 0; i < text.size(); ++i)
     {
             if(text[i] >= 'v' && text[i] <= 'z' || text[i] >= 'V' && text[i] <= 'Z')
                text[i] = char(text[i] - 21);
             else if(text[i] >='A' && text[i] <= 'z')
                text[i] = char(text[i] + 5);  
     }
}

void TextCoder::decoder(){
     for(auto i = 0; i < text.size(); ++i)
     {
             if(text[i] >= 'a' && text[i] <= 'e' || text[i] >= 'A' && text[i] <= 'Z')
                text[i] = char(text[i] + 21);
             else if(text[i] >='A' && text[i] <= 'z')
                text[i] = char(text[i] - 5 );  
     }
}
#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 << "加密后英文文本:" << encoded_text << endl;
        
        decoded_text = TextCoder(encoded_text).get_deciphertext();
        cout << "解密后英文文本:" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    } 
}

int main() {
    test();
}

 

标签:&&,string,text,cout,C++,num,数组,include,指针
From: https://www.cnblogs.com/DeviIMayCry/p/16819936.html

相关文章