Info.cpp文件源码
#include<iostream> #include<string> #include<vector> using namespace std; class Info { public: Info(); Info(string nickname,string contact,string city,int n):a(nickname),b(contact),c(city),d(n){} void scan(); void print(); private: string a,b,c; int d; }; void Info::scan() { cin >> a >> b >> c >> d ; } void Info::print() { cout << "称呼:" << a <<endl; cout << "联系方式:" << b <<endl; cout << "所在城市:" << c <<endl; cout << "预定人数:" << d <<endl; }
task5.cpp源码
#include<iostream> #include"Info.hpp" #include<string> #include<vector> using namespace std; template <typename T> void output(T x) { for(const auto &i : x) std::cout <<i <<","; std::cout<<"\b\b \n"; } int main() { vector<Info> audience_info_lidt; const int capacity(100); cout << "录入信息:" <<endl; cout << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" <<endl; string ni,co,ci; int n,flag=0; int n2=0; //n2记录预约人数 while(cin >> ni >> co >> ci >> n) { n2+=n; if(n2>capacity) { n2-=n; cout << "对不起,只剩" << capacity-n2 << "个位置" <<endl; cout << "1.输入u,更新(update)预定信息" <<endl; cout << "2.输入q,退出预定" <<endl; char p; cout << "你的选择:"; cin >>p; if(p=='q') break; } audience_info_lidt.push_back(Info(ni,co,ci,n)); flag++; } cout <<endl; cout <<"截至目前,一共有" << n2 << "位听众预定参加。预定信息如下:" <<endl; int i=0; while(flag--){ audience_info_lidt[i++].print(); } return 0; }
程序测试截图
Text Coder.hpp文件源码
#include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string text):t(text){} string encoder(); string decoder(); private: string t; }; string TextCoder::encoder() { for(int i=0;i<t.length();i++) { if(t[i]>='a'&&t[i]<='z'||t[i]>='A'&&t[i]<='Z'){ if(t[i]>='v'&&t[i]<='z'||t[i]>='V'&&t[i]<='Z') t[i]=t[i]-21; else t[i]=t[i]+5; } } return t; } string TextCoder::decoder() { for(int i=0;i<t.length();i++) { if(t[i]>='a'&&t[i]<='z'||t[i]>='A'&&t[i]<='Z'){ if(t[i]>='a'&&t[i]<='e'||t[i]>='A'&&t[i]<='E') t[i]=t[i]+21; else t[i]=t[i]-5; } } return t; }
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输入英文文本: "; } }
代码运行测试截图