实验五、
Info.hpp:
#include<iostream> #include<string> using namespace std; class Info{ public: Info(string nc="***",string ct="******",string cy="**",int nn=0){ nickname=nc; contact=ct; city=cy; n=nn; } void print() const; private: string nickname; string contact; string city; int n; }; void Info::print() const { cout<<"称呼: "<<nickname<<endl; cout<<"联系方式: "<<contact<<endl; cout<<"所在城市: "<<city<<endl; cout<<"预定人数: "<<n<<endl; }
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; string nc,ct,cy; int n,m=0; cout<<"录入信息:"<<endl<<endl; cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl; while(cin>>nc>>ct>>cy>>n){ if (m+n>capacity) { cout<<"对不起,只剩" << capacity - m << "个位置."<<endl; cout<<"1. 输入u,更新(update)预定信息"<<endl; cout<<"2. 输入q,退出预定"<<endl; cout<<"你的选择:"; string s; cin>>s; if(s=="u") continue; else if(s=="q") break; } m+=n; audience_info_list.push_back(Info(nc,ct,cy,n)); } cout<<endl<<endl; cout<<"截至目前,一共有"<<m<<"位听众预定参加。预定听众信息如下:"<<endl; for(auto const &i:audience_info_list) i.print(); cout<<"\b\b \n"; }
实验结果截图:
实验六、
TextCoder.hpp:
#include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string x):text(x){} string encoder(); string decoder(); private: string text; }; string TextCoder::encoder() { for(int i=0;i<text.length();++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.length();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; }
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输入英文文本: "; } }
实验结果截图: