一.实验结论:
1.实验任务5:
Info.hpp:
#include <iostream> #include <string> #include <iomanip> using namespace std; class Info { public: Info() {} Info(string x, string y, string z, int num) : nickname{x}, contact{y}, city{z}, n{num} {} ~Info() {} void set_nickname(string m) { nickname = m; } void set_contact(string r) { contact = r; } void set_city(string p) { city = p; } void set_n(int e) { n = e; } void print() { cout << left << setw(8) << "昵称:" << nickname << endl; cout << left << setw(8) << "联系方式:" << contact << endl; cout << left << setw(8) << "所在城市:" << city << endl; cout << left << setw(8) << "预定人数:" << n << endl; } private: string nickname, contact, city; int n; };
task5.cpp:
#include"Info.hpp" #include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; int main() { int i, d, sum = 0, number, t = 1; string a, b, c, my_choice; const int capacity = 100; vector<Info> audience_info_list(100); cout << left << setw(20) << "昵称" << left << setw(50) << "联系方式(邮箱/手机号)" << left << setw(20) << "所在城市" << left << setw(20) << "预定参加人数" << endl; i = 0; while (cin >> a >> b >> c >> d) { if (d <= capacity - sum) { audience_info_list[i].set_nickname(a); audience_info_list[i].set_contact(b); audience_info_list[i].set_city(c); audience_info_list[i].set_n(d); sum += d; i++; } else { cout << "对不起,只剩" << capacity - sum << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; while (cin >> my_choice) { if (my_choice == "q") { i = i - 1; t = 0; break; } else if (my_choice == "u") { cin >> a >> b >> c >> d; if (d > capacity - sum) { cout << "对不起,只剩" << capacity - sum << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; continue; } audience_info_list[i].set_nickname(a); audience_info_list[i].set_contact(b); audience_info_list[i].set_city(c); audience_info_list[i].set_n(d); sum += d; i++; break; } } } if (t == 0) break; } number = i; cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl; for (i = 0; i <= number; i++) audience_info_list[i].print(); }
运行结果:
2.实验任务6:
textcoder.hpp:
#include <iostream> #include <string> using namespace std; class TextCoder { public: TextCoder() {} TextCoder(string x) : text{x} {} string get_ciphertext() { encoder(); return text; } string get_deciphertext() { decoder(); return text; } private: string text; void encoder() { for (int i = 0; i < text.length(); i++) { if (text.at(i) >= 'a' && text.at(i) <= 'z') { if (text.at(i) >= 'a' && text.at(i) <= 'u') text.at(i) += 5; else if (text.at(i) > 'u' && text.at(i) <= 'z') text.at(i) -= 21; } if (text.at(i) >= 'A' && text.at(i) <= 'Z') { if (text.at(i) >= 'A' && text.at(i) <= 'U') text.at(i) += 5; else if (text.at(i) > 'U' && text.at(i) <= 'Z') text.at(i) -= 21; }
} } void decoder() { for (int j = 0; j < text.length(); j++) { if (text.at(j) >= 'a' && text.at(j) <= 'z') { if (text.at(j) >= 'f' && text.at(j) <= 'z') text.at(j) -= 5; else if (text.at(j) >= 'a' && text.at(j) < 'f') text.at(j) += 21; } if (text.at(j) >= 'A' && text.at(j) <= 'Z') { if (text.at(j) >= 'F' && text.at(j) <= 'Z') text.at(j) -= 5; else if (text.at(j) > 'A' && text.at(j) < 'F') text.at(j) += 21; } } } };
task6.cpp:
#include "task6textcoder.hpp" #include <iostream> 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(); }
运行结果:
二.实验总结:
1.在书写Info类时,最重要的是判断多组输入以及退出的条件。一开始,当剩余容量小于预定量时,我只进行了一次判断,导致在再次更新时如果剩余容量仍然不够时便不能跳出“对不起,只剩...你的选择..”,之后,我又在循环体内嵌套一次判断,并采用continue进行退出,问题解决。
2.实验任务6,主要是注意题中所指出的是私有还是公有成员函数。
标签:&&,Info,string,text,void,C++,数组,include,指针 From: https://www.cnblogs.com/jyksblog/p/16814762.html