实验任务5:
Info.hpp
#ifndef INFO_HPP #define INFO_HPP #include<iostream> #include<iomanip> #include<string> using namespace std; class Info{ public: Info(string nickname_, string contact_, string city_, int n_): nickname(nickname_), contact(contact_), city(city_), n(n_){} void print() const; private: string nickname; string contact; string city; int n; }; void Info::print() const{ cout << "称呼:\t\t" << nickname <<endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预定人数:\t" << n << endl; } #endif
task5.cpp
#include "Info.hpp" #include<iostream> #include<vector> int main(){ using namespace std; vector<Info> audience_info_list; string nickname; string contact; string city; int n; int total = 0; cout << "录入信息:" << endl; cout << endl; cout << "称呼/昵称, 联系方式(邮箱/手机号), 所在城市, 预定参加人数" << endl; const int capacity = 100; while(cin>>nickname>>contact>>city>>n){ total += n; if(total>capacity){ cout << "对不起, 只剩" << capacity-(total-n) << "个位置." << endl; cout << "1. 输入u, 更新(update)预定信息" << endl; cout << "2. 输入q, 退出预定" << endl; cout << "你的选择: "; total -= n; char flag; cin >> flag; if(flag == 'u'){ continue; } else if(flag == 'q'){ break; } } Info info(nickname, contact, city, n); audience_info_list.push_back(info); if(total == 100){ break; } } cout<<endl; cout << "截至目前,一共有" << total << "位听众预定参加.预定听众信息如下:" << endl; for(vector<Info>::iterator it = audience_info_list.begin(); it != audience_info_list.end(); it++) it->print(); }
运行测试结果:
实验任务6:
textcoder.hpp
#ifndef TEXTCODER #define TEXTCODER #include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string text_):text(text_){} string encoder(); string decoder(); private: string text; }; string TextCoder::encoder(){ for(string::iterator it = text.begin(); it!=text.end(); it++){ if (*it >= 118&&*it<=122) *it -= 21; else if (*it >= 86 && *it <= 90) *it -= 21; else if((*it>=97&&*it<=117)||(*it>=65&&*it<=85)) *it += 5; } return text; } string TextCoder::decoder() { string::iterator it = text.begin(); for (;it != text.end();it++) { if (*it <= 69&&*it>=65) *it += 21; else if (*it >= 97 && *it <= 101) *it += 21; else if((*it>=70&&*it<=90)||(*it>=102&&*it<=122)) *it -= 5; } 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输入英文文本: "; } }
运行测试结果: