实验任务5:

task 5.hpp:

#include"intro.hpp"
#include <iostream>
#include <string>
#include<iomanip>
#include<vector>


using namespace std;


int main() {
    string na, c, city;
    int count = 0;
    const int top = 100;
    int total = 0;
    int n;
    intro s;
    vector<intro> aud_list;
    while (getline(cin, na, ' ')) {

        getline(cin, c, ' ');
        getline(cin, city, ' ');
        cin >> n;
        total = total + n;
        
        if (total > top) {
            string ch;
            cout << "sorry, we have had enough audiences" << endl;
            cout << "enter 'u' to update or 'q' to quit" << endl;
            cout << "your choice:";
                cin >> ch;
                getchar();
            if (ch == "q") {
                total = total - n;
                break;
            }
            else if (ch == "u") {
                total = total - n;
                continue;
            }
        }

        s.set_intro(na, c, city, n);
    getchar();
    
        aud_list.push_back(s);
        count++;

    }
cout<<"we now have "<<total<<" "<<"audiences"<<endl;


    for (int i = 0; i < count; i++) {
        aud_list[i].print();
        cout << endl;

    };
}

intro.hpp:

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

using namespace std;
class intro {
public:
    intro() {};
    intro(string s1, string s2, string s3, int num) :nakename{ s1 }, contact{ s2 }, city{ s3 }, n{ num } {}
    void print() {
        cout << left << setw(15) << "昵称" << nakename << endl
            << left << setw(15) << "联系方式" << contact << endl
            << left << setw(15) << "所在城市" << city << endl
            << left << setw(15) << "预定参加人数" << n << endl;

    }
    void set_intro(string a, string b, string c, int d) {
        nakename = a;
        contact = b;
        city = c;
        n = d;

    };
private:string nakename;
       string contact;
       string city;
       int n;
}

输入:xiaoming 135677 shanghai 90

scp 231231 beijing 5

输入:xiaoming 135677 shanghai 90

scp 231231 beijing 50

再输入 u 

scp 231231 beijing 5

实验任务6:

 textcoder.hpp

#include <iostream>
#include <string>
using namespace std;
class TextCoder {
public:
    TextCoder(string s1):text{s1}{}
    void print() {
        cout << text;
    }
    string get_ciphertext(){
        encoder(text);
        return text;
    }
    string get_deciphertext() {
        decoder(text);
        return text;
    }
private:
    string text;
       void encoder(string a){
       for(int i=0;i< a.length();i++)
       {if(a[i] == 'U') { a[i] = 'Z'; }
            else if (text[i] >= 'A' && text[i] <= 'Z')  {
                a[i] = (a[i] - 64 + 5) % 26 + 64; 
           }
            else if (a[i] =='u') { a[i] = 'z'; }
            else if (text[i] >= 'a' && text[i] <= 'z') {
               a[i] = (a[i] - 96 + 5) % 26 + 96; 

           }
           else { continue; }
       }
       text = a;
       }
      void decoder(string a){
          for (int i = 0; i < a.length(); i++)
          {if (a[i] == 'Z') { a[i] = 'U'; }
          else if (a[i] == 'E') { a[i] = 'Z'; }
              else if (text[i] >= 'A' && text[i] <= 'Z') {
                  a[i] = (a[i] - 64 + 21) % 26 + 64;
              }
              else if (a[i] == 'e') { a[i] = 'z'; }
              else if (a[i] == 'z') { a[i] = 'u'; }
              else if (text[i] >= 'a' && text[i] <= 'z') {
                  a[i] = (a[i] - 96 + 21) % 26 + 96;

              }
              else { continue; }
          }
          text = 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(); 
}

 

总结:

字母加密的算法公式: 对于小写字母(a的AS码为65):a[i] = (a[i] + 26 +(向后位移个数) – 1 – 64) % 26 + 1 + 64;

原文地址:http://www.cnblogs.com/lovelexington/p/16806831.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性