5.实验任务5
Info.hpp文件源码
#ifndef INFO_HPP_INCLUDED #define INFO_HPP_INCLUDED #include<iostream> #include<string> using namespace std; class Info{ private: string nickname; string contact; string city; int n; public: Info(string a,string b,string c,int d): nickname{a},contact{b},city{c},n(d){} print(); }; Info::print() { cout<<"称呼\t\t"<<nickname<<endl; cout<<"联系方式"<<"\t"<<contact<<endl; cout<<"所在城市"<<"\t"<<city<<endl; cout<<"预定人数"<<"\t"<<n<<endl; } #endif // INFO_HPP_INCLUDED
task5.cpp源码
#include<iostream> #include<string> #include<vector> #include"Info.hpp" using namespace std; int main() { int sum=0; const int capacity=100; vector<Info> audience_info_list; string a,b,c; int d; cout<<"录入信息"<<endl<<endl; cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl; while(cin>>a>>b>>c>>d) { sum+=d; if(sum<capacity) { audience_info_list.push_back(Info(a,b,c,d)); } else { sum-=d; cout<<"对不起,只剩"<<capacity-sum<<"个位置."<<endl; cout<<"输入u,更新(update)预定信息"<<endl; cout<<"输入q,退出预定"<<endl; string x; cin>>x; if(x=="u") continue; else break; } } cout<<endl<<"截至目前,一共有"<<sum<<"位听众预定参加,预定听众信息如下:"<<endl; for(auto t=audience_info_list.begin();t!=audience_info_list.end();++t) (*t).print(); }
运行测试结果截图
6.实验任务6
TextCoder.hpp文件源码
#ifndef TEXTCODER_HPP_INCLUDED #define TEXTCODER_HPP_INCLUDED #include <iostream> #include <string> using namespace std; class TextCoder { public: TextCoder(string a):text{a}{} string encoder(); string decoder(); private: string text; }; string TextCoder::encoder(){ for(auto i=0;i<text.length();++i) { if (text.at(i)>='a'&&text.at(i)<='u'||text.at(i)>='A'&&text.at(i)<='U') text.at(i)+= 5; else if (text.at(i)>='v'&&text.at(i)<='z'||text.at(i)>='V'&&text.at(i)<='Z') text.at(i)-=21; } return text; } string TextCoder::decoder(){ for(auto i=0;i<text.length();++i) { if (text.at(i)>='f'&&text.at(i)<='z'||text.at(i)>='F'&&text.at(i)<='Z') text.at(i)-= 5; else if (text.at(i)>='a'&&text.at(i)<='e'||text.at(i)>='A'&&text.at(i)<='E') text.at(i)+=21; } return text; } #endif // TEXTCODER_HPP_INCLUDED
task6.cpp源码
#include <iostream> #include<string> #include"../TextCoder.hpp" using namespace std; 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输入英文文本: "; } }
运行测试结果截图