实验五代码:
Info.hpp
1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 #include<iomanip> 6 7 using std::string; 8 using std::cout; 9 using std::endl; 10 11 class Info 12 { 13 public: 14 Info(string nickname0,string contact0,string city0,int n0); 15 void print() const; 16 ~Info() = default; 17 private: 18 string nickname; 19 string contact; 20 string city; 21 int n; 22 }; 23 24 Info::Info(string nickname0, string contact0, string city0, int n0) 25 { 26 nickname = nickname0; 27 contact = contact0; 28 city = city0; 29 n = n0; 30 } 31 32 void Info::print() const//打印信息 33 { 34 cout << "昵称: " << nickname << endl; 35 cout << "联系方式: " << contact << endl; 36 cout << "所在城市: " << city << endl; 37 cout << "预定人数: " << n << endl << endl; 38 }
task5.cpp
1 #include"Info.hpp" 2 #include<iostream> 3 #include<string> 4 #include<vector> 5 #include<iomanip> 6 7 using namespace std; 8 9 int main() 10 { 11 const int capacity = 100; 12 vector<Info> audience_info_list; 13 cout << "录入信息:" << endl << endl; 14 cout << "昵称 " << "联系方式(邮箱/手机号) " << "所在城市 " << "预定参加人数 " << endl; 15 string yname, ycontact, ycity; 16 int yn = 0; 17 char choice = 'u';//choice记录用户的选择 18 int sum = 0;//记录已预定的总人数 19 while (choice == 'u' && sum < capacity && cin >> yname) 20 { 21 cin >> ycontact >> ycity >> yn; 22 Info a(yname, ycontact, ycity, yn); 23 audience_info_list.push_back(a); 24 sum += yn; 25 if (sum > capacity) 26 { 27 sum -= yn; 28 audience_info_list.pop_back(); 29 cout << "对不起,只剩" << capacity - sum << "个位置。" << endl; 30 cout << "1.输入u,更新(update)预定信息" << endl 31 << "2.输入q,退出预定" << endl 32 << "你的选择:"; 33 cin >> choice; 34 } 35 } 36 cout << endl << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl; 37 for (auto &Info:audience_info_list) 38 Info.print(); 39 }
运行截图:
实验六代码:
textcoder.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using std::cout; 6 using std::string; 7 using std::endl; 8 9 class TextCoder 10 { 11 public: 12 TextCoder(string text0) :text{ text0 } {}; 13 TextCoder(TextCoder& T) :text{ T.text } {};//构造函数 14 string get_ciphertext();//调用encoder加密文本,并返回加密后的 15 string get_deciphertext();//调用decoder解密文本,并返回解密后的 16 private: 17 string text; 18 void encoder();//加密文本 19 void decoder();//解密文本 20 }; 21 22 string TextCoder::get_ciphertext()//加密并返回 23 { 24 encoder(); 25 return text; 26 } 27 28 string TextCoder::get_deciphertext()//解密并返回 29 { 30 decoder(); 31 return text; 32 } 33 34 void TextCoder::encoder()//加密 35 { 36 for (int i = 0; i < text.size(); i++) 37 { 38 if (isupper(text.at(i))) text.at(i) = (text.at(i) - 60) % 26 + 65; 39 else if (islower(text.at(i))) text.at(i) = (text.at(i) - 92) % 26 + 97; 40 } 41 } 42 43 void TextCoder::decoder()//解密 44 { 45 for (int i = 0; i < text.size(); i++) 46 { 47 if (isupper(text.at(i))) text.at(i) = (text.at(i) - 44) % 26 + 65; 48 else if (islower(text.at(i))) text.at(i) = (text.at(i) - 76) % 26 + 97; 49 } 50 }
task6.cpp
1 #include "textcoder.hpp" 2 #include <iostream> 3 #include <string> 4 5 void test() 6 { 7 using namespace std; 8 9 string text, encoded_text, decoded_text; 10 11 cout << "输入英文文本: "; 12 while (getline(cin, text)) 13 { 14 encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象 15 cout << "加密后英文文本:\t" << encoded_text << endl; 16 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 17 cout << "解密后英文文本:\t" << decoded_text << endl; 18 cout << "\n输入英文文本: "; 19 } 20 } 21 int main() 22 { 23 test(); 24 }
运行截图:
标签:Info,string,text,C++,数组,using,include,TextCoder,指针 From: https://www.cnblogs.com/jane-d/p/16818681.html