首页 > 其他分享 >实验3

实验3

时间:2022-10-24 23:25:35浏览次数:54  
标签:info city string text 实验 && include

task5

Info.h

 1 #pragma once
 2 #include <iostream>
 3 #include <vector>
 4 #include <string>
 5 using namespace std;
 6 
 7 class info {
 8 public:
 9     info(string name, string contact, string city, int total) :nickname{ name }, contact{ contact }, city{ city }, n{ total }{}
10     void print();
11 
12 private:
13     string nickname;
14     string contact;
15     string city;
16     int n;
17 
18 };
19 
20 void info::print() {
21     cout << "昵称:\t  \t" << nickname << "\n" << "联系方式:\t" << contact << "\n" << "所在城市:\t" << city << "\n" << "预定人数:\t" << n << endl;
22 }

task5.cpp

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include "Info.h"
 5 
 6 void test() {
 7     using namespace std;
 8 
 9     const int capacity = 100;
10     string name, contast, city;
11     int n, count = 0;
12     vector<info> audience_info_list;
13 
14     cout << "录入信息:" << endl;
15     cout << "昵称\t" << "联系方式(邮箱/手机号)\t\t" << "所在城市\t" << "预定参加人数\t" << endl;
16 
17     while (cin >> name) {
18         cin >> contast >> city >> n;
19         count += n;
20 
21         if (capacity - count >= 0) {
22             info p0 = info(name, contast, city, n);
23             audience_info_list.push_back(p0);
24 
25         }
26         else {
27             string next;
28             count -= n;
29             cout << "对不起,只剩" << (capacity - count) << "个位置." << endl;
30             cout << "1.输入u,更新(update)预定信息" << endl;
31             cout << "2.输入q,退出预定" << endl;
32             cout << "你的选择:";
33             cin >> next;
34             if (next == "q")
35                 break;
36             else if (next == "u") {
37                 cout << "录入信息:" << endl;
38                 continue;
39             }
40         }
41 
42         if (count == capacity)
43             break;
44 
45     }
46 
47     cout << "截至目前,一共有" << count << "位听众参加预定。预定信息如下:" << endl;
48 
49     for (int i = 0; i < audience_info_list.size(); i++) {
50         audience_info_list.at(i).print();
51         cout << endl;
52     }
53 
54 }
55 
56 int main() {
57     test();
58 }

实验截图

 

task6

textcoder.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include <string>
 4 using namespace std;
 5  
 6 class TextCoder {
 7 private:
 8     string text;
 9     void encoder();
10     void decoder();
11 public:
12     TextCoder();
13     TextCoder(string t0) :text{t0}{}
14     string get_ciphertext();
15     string get_deciphertext();
16 };
17  
18 TextCoder::TextCoder(){}
19  
20 void TextCoder::encoder(){
21  
22     int len = text.length();
23     for (int i = 0; i < len; i++) {
24         if (text.at(i) >= 'a' && text.at(i) <= 'u') {
25             text.at(i) += 5;
26         }
27         else if (text.at(i) >= 'v' && text.at(i) <= 'z') {
28             text.at(i) -= 21;
29         }
30         else if (text.at(i) >= 'A' && text.at(i) <= 'U') {
31             text.at(i) += 5;
32         }
33         else if (text.at(i) >= 'V' && text.at(i) <= 'Z') {
34             text.at(i) -= 21;
35         }
36     }
37  
38 }
39  
40 void TextCoder::decoder() {
41     int len = text.length();
42     for (int i = 0; i < len; i++) {
43         if (text.at(i) >= 'f' && text.at(i) <= 'z') {
44             text.at(i) -= 5;
45         }
46         else if (text.at(i) >= 'a' && text.at(i) <= 'e') {
47             text.at(i) += 21;
48         }
49         else if (text.at(i) >= 'F' && text.at(i) <= 'Z') {
50             text.at(i) -= 5;
51         }
52         else if (text.at(i) >= 'A' && text.at(i) <= 'E') {
53             text.at(i) += 21;
54         }
55     }
56 }
57  
58 string TextCoder::get_ciphertext() {
59     encoder();
60     return text;
61 }  
62  
63 string TextCoder::get_deciphertext() {
64     decoder();
65     return text;
66 }

task6.cpp

 1 #include<iostream>
 2 #include "textcoder.hpp"
 3 #include <string>
 4 void test() {
 5     using namespace std;
 6     string text, encoded_text, decoded_text;
 7     cout << "输入英文文本: ";
 8     while (getline(cin, text)) {
 9         encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时名对象
10         cout << "加密后英文文本:\t" << encoded_text << endl;
11         decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
12         cout << "解密后英文文本:\t" << decoded_text << endl;
13         cout << "\n输入英文文本: ";
14     }
15 }
16 int main() {
17     test();
18 }

实验截图

 

标签:info,city,string,text,实验,&&,include
From: https://www.cnblogs.com/lee404error/p/16823421.html

相关文章

  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求1.编写Python程序,调用OpenDaylight的北向接口实现以下功能调用OpenDaylight的北向接口获取拓扑信息importrequestsasrqfromrequests.authimportHTTP......
  • 实验3 数组、指针与现代C++标准库
    实验任务5:info.hpp:#include<iostream>#include<string>usingnamespacestd;classinfo{public:info(stringni,stringco,stringci,intn);voidpri......
  • 第三次实验
    实验五hpp1#pragmaonce23#include<iostream>4#include<iomanip>5usingnamespacestd;6classinfo7{89public:10info(stringnickna......
  • 实验三
    #pragmaonce#include<iostream>#include<vector>#include<string>#include<iomanip>usingnamespacestd;classInfo{public:Info(){}Info(string......
  • 实验2
    实验1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能。(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight。(2)下发指令删除s1上的流表数据#!/us......
  • 实验2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0,i<N,i++)......
  • 实验5:开源控制器实践——POX
    一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3.能够运用POX控制器编写自定......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;(2)下发指令删除s1上的流表数据。import......
  • 实验3
    task51#include<iostream>2#include<vector>3#include<string>4#include<iomanip>5usingnamespacestd;6classInfo{7friendvoidprint(vect......