四、实验结论
1.实验五
#include <string> #include <iostream> #include <vector> using namespace std; class Info{ public: Info(string nname,string co,string ct,int nn){ nickname = nname; contact = co; city = ct; n = nn; } void print(){ cout << "称呼: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预定人数: " << n << endl; } private: string nickname; string contact; string city; int n; }; int main(){ vector<Info> audience_info_list; static int capacity = 100; cout << "录入信息:\n\n" ; string nickname,contact,city; int n; cout << "请依次输入预约信息(在称呼中输入stop以中止录入)\n"; cout << "称呼:"; cin >> nickname; while (nickname!="stop"){ cout << "联系方式:"; cin >> contact; cout << "所在城市:"; cin >> city; cout << "预定人数:"; cin >> n; if (n>capacity){ char a; cout << "对不起,您的预定参加人数超过livehouse场地剩余容量,请输入q中止预定,或输入u更新预定信息\n"; cout << "您的选择是:"; cin >> a; if (a=='q') break; else{ cout << "\n称呼:"; cin >> nickname; continue; } } Info audience (nickname,contact,city,n); capacity = capacity-n; audience_info_list.push_back(audience); cout << "\n称呼:"; cin >> nickname; } cout << "\n预定信息:\n"; cout << "截止目前,一共有" << 100-capacity << "位听众预定\n"; for (auto a : audience_info_list){ a.print(); cout << "\n"; } return 0; }
2.实验六
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include <string> #include <vector> using namespace std; class TextCoder{ public: TextCoder(string tt){ text = tt; } string encoder(); string decoder(); private: string text; }; string TextCoder :: encoder(){ for(auto &ch : text){ if(ch>='v' && ch<='z' || ch>='V' && ch<='Z'){ ch = (char)(ch-21); }else if(ch>='a' && ch<='u' || ch>='A' && ch<='U'){ ch = (char)(ch+5); } } return text; } string TextCoder :: decoder(){ for(auto &ch : text){ if(ch>='a' && ch<='e' || ch>='A' && ch<='E'){ ch = (char)(ch+21); }else if(ch>='f' && ch<='z' || ch>='F' && ch<='Z'){ ch = (char)(ch-5); } } return text; } #endif
#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输入英文文本: "; } }
五、实验总结
1.使用string类时,似乎必须在全局配置中添加 using namespace std??
2.范围for和自动类型推导真好使