实验任务5:
Info.hpp文件源码:
#ifndef INFO_HPP #define INFO_HPP #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){} void print(); int get_n() const; ~Info()=default; }; int Info::get_n() const { return n; } void Info::print() { cout<<"称呼: "<<nickname<<endl; cout<<"联系方式: "<<contact<<endl; cout<<"所在城市: "<<city<<endl; cout<<"预定人数: "<<n<<endl; } #endif
livehouse.cpp源码:
#include "Info.hpp" #include<iostream> #include<string> #include<vector> using namespace std; int main() { cout<<"录入信息:"<<endl; cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl; vector<Info>audience_info_list; const int capacity=100; int sum=0,i=0,d,flag; string a,b,c; while(sum<=capacity) { cin>>a; if(a=="#stop#") break; cin>>b>>c>>d; Info information(a,b,c,d); audience_info_list.push_back(information); sum+=information.get_n(); } while(sum>capacity) { flag=0; int x=audience_info_list.size()-1; cout<<"对不起,只剩"<<capacity-sum+audience_info_list[x].get_n()<<"个位置"<<endl; cout<<"1.输入u,更新(update)预定信息"<<endl; cout<<"2.输入q,退出预定"<<endl; cout<<"你的选择:"; char choose; cin>>choose; if(choose=='u') { sum-=audience_info_list[x].get_n(); while(sum<=capacity) { cin>>a; if(a=="#stop#") break; cin>>b>>c>>d; Info information(a,b,c,d); audience_info_list.push_back(information); sum+=information.get_n(); } } else if(choose=='q') { sum-=audience_info_list[x].get_n(); break; } } cout<<"截至目前,一共有"<<sum<<"位听众预定参加,预定观众信息如下:"<<endl; if(flag!=0) { for (int j=0;j<audience_info_list.size();j++) audience_info_list.at(j).print(); } else { for (int j=0;j<audience_info_list.size()-1;j++) audience_info_list.at(j).print(); } }
运行结果:
实验任务6:
TectCoder.hpp文件源码:
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include<iostream> #include<string> using namespace std; class TextCoder { private: string text; public: TextCoder(string a):text(a){} string encoder(); string decoder(); }; string TextCoder::encoder() { for(auto &ch:text) { if(ch>='a'&&ch<='u'||ch>='A'&&ch<='U') ch=ch+5; else if(ch>='v'&&ch<='z'||ch>='V'&&ch<='Z') { ch=ch-21; } } return text; } string TextCoder::decoder() { for(auto &ch:text) { if(ch>='f'&&ch<='z'||ch>='F'&&ch<='Z') ch=ch-5; else if(ch>='a'&&ch<='e'||ch>='A'&&ch<='E') { ch=ch+21; } } return text; } #endif
textcoder.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输入英文文本: "; } }
运行结果: