• 欢迎光临~

实验三

开发技术 开发技术 2022-10-26 次浏览

task5:

info.hpp:

#include<iostream>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;

class info{
    private:
        string nickname;
        string contact;
        string city;
        int n;
    public:
        info(){}
        info(string name,string cont,string city,int n):
        nickname(name),contact(cont),city(city),n(n){}
        ~info(){}
            
        void print() const;    
};
void info::print() const
{
    cout << "昵称:    tt"  << nickname << endl;
    cout << "联系方式:tt"  << contact << endl;
    cout << "所在城市:tt"  << city << endl;
    cout << "预定人数:tt"  << n << endl; 
    cout << endl;
}

 

task5.cpp:

#include <iostream>
#include<vector>
#include"info.hpp"
#include<iomanip>
 using namespace std;
 int main()
 {
     const int capacity = 100;
     string name,cont,city;
     int n,sum;
     int count = 0;
     vector<info> audience_info_list;
     cout << "录入信息: " << endl;
     cout << endl;
    cout << "昵称tt" << "联系方式(邮箱/手机号)tt" << "所在城市tt" << "预定参加人数    " << endl;  
     
     while(cin >> name){
         cin >> cont >> city >> n;
         count = count + n;
         sum = capacity - count;
         if(sum > 0||sum == 0){
             info io = info(name,cont,city,n);
             audience_info_list.push_back(io);
         }
         else{
             string wise;
             count = count -n;
             sum = capacity - count;
             cout << "对不起,只剩" << sum << "个位置." << endl; 
             cout << "1.输入u,更新(update)预定信息" << endl;
             cout << "2.输入q,退出预定" << endl;
             cout << "你的选择:";
             cout << endl;
             cin >> wise;
             if(wise == "q")
                break;
             else if(wise == "u"){
                 cout << "录入信息:" << endl;
                 continue;
             }
         }
         if (sum == capacity)
             break;
          
     }
     
     cout << "截至目前,一共有" << count << "位听众预定参加。预定听众信息如下:" << endl;
     for(int i = 0;i < audience_info_list.size();i++) 
         audience_info_list.at(i).print();
     
     
 }

实验三

实验三

 

 

task6:

textcoder.hpp:

 

#pragma once
#include<iostream>
#include<string>
using namespace std;
class TextCoder{
    public:
        TextCoder(){}
        TextCoder(string te) :text{te}{}
        ~TextCoder(){}
        string get_ciphertext(){encoder(); return text;}
        string get_deciphertext(){decoder();return text;}
    private:
        string text;
        void encoder();
        void decoder();
};
void TextCoder::encoder(){
    for (int i = 0; i < text.length(); i++) {
        if (text.at(i)>= 'a' && text.at(i)<= 'z')
            text.at(i)= (text.at(i) - 'a' + 5) % 26 + 'a';
        if (text.at(i)>= 'A' && text.at(i) <= 'Z')
            text.at(i)= (text.at(i) - 'A' + 5) % 26 + 'A';
        }
    }
void TextCoder::decoder() {
    for (int i = 0; i < text.length(); i++) {
        if (text.at(i)>= 'a' && text.at(i)<= 'z')
            text.at(i)= (text.at(i) - 'a' +21) % 26 + 'a';
        if (text.at(i)>= 'A' && text.at(i)<= 'Z')
            text.at(i)= (text.at(i) - 'A' +21) % 26 + 'A';
    }
}
 

 

 

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(); 
}

 实验三

 

程序员灯塔
转载请注明原文链接:实验三
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com