实验2 数组、指针与C++标准库
Info.hpp
#ifndef INFO_HPP #define INFO_HPP #include <iostream> #include <string> using namespace std; class info { private: string nickname, contact, city; int n; public: info(string nickname0, string contact0, string city0, int n0); void print()const; }; info::info(string nickname0, string contact0, string city0, int n0) :nickname(nickname0), contact(contact0), city(city0), n(n0) { } void info::print()const { cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预定人数: " << n << endl; } #endif
task5.cpp
#include "Info.hpp" #include <iostream> #include <string> #include <vector> int main() { using namespace std; vector<info> audience_info_list; const int capacity = 100; int num = 0, i = 0; string NICKNAME, CONTACT, CITY; int N; cout << "录入信息: " << endl; cout << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; while (cin >> NICKNAME >> CONTACT >> CITY >> N) { num += N; if (num > capacity) { num -= N; cout << "对不起,只剩" << (capacity - num) << "个位置" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择: "; char Choose; cin >> Choose; while (Choose != 'q' && Choose != 'u') { cout << "输入错误,请重新输入:"; cin >> Choose; } if (Choose == 'q') { break; } if (Choose == 'u') { continue; } } else { info audience(NICKNAME, CONTACT, CITY, N); audience_info_list.push_back(audience); } } cout << endl; cout << "截至目前,一共有" << num << "位听众预定参加。预定听众信息如下:" << endl; for (auto audience_info : audience_info_list) { audience_info.print(); } }
textcoder.hpp
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include <iostream> #include <string> using namespace std; class TextCoder{ private: string text; public: TextCoder(string text0 = nullptr) :text(text0) { } string encoder(); string decoder(); }; string TextCoder::encoder() { for (int i = 0; i < text.size(); i++) { if (text[i] >= 'a' && text[i] <= 'u' || text[i] >= 'A' && text[i] <= 'U') { text[i] = text[i] + 5; } else if(text[i] >= 'v' && text[i] <= 'z' || text[i] >= 'V' && text[i] <= 'Z') { text[i] = text[i] - 21; } } return text; } string TextCoder::decoder() { for (int i = 0; i < text.size(); i++) { if (text[i] >= 'f' && text[i] <= 'z' || text[i] >= 'F' && text[i] <= 'Z') { text[i] = text[i] - 5; } else if(text[i] >= 'a' && text[i] <= 'e' || text[i] >= 'A' && text[i] <= 'E'){ text[i] = text[i] + 21; } } return text; } #endif
task6.cpp
#include "textcoder.hpp" #include <iostream> #include <string> int main() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)) { encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象 cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } }