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 nickname0,string contact0,string city0,int n0); void print()const; }; info::info(string nickname0,string contact0,string city0,int n0):nickname(nickname0),contact(contact0),city(city0),n(n0){ } void info::print()const { cout<<"称呼:"<<nickname<<endl; cout<<"联系方式:"<<contact<<endl; cout<<"所在城市:"<<city<<endl; cout<<"预定人数:"<<n<<endl; } #endif
task5.cpp
#include"info.hpp" #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<info> audience_info_list; const int capacity=100; string a,b,c,choose; int d,s; cout<<"称呼/昵称,联系方式(邮箱/手机号码),所在城市,预定参加人数"<<endl; while(cin>>a>>b>>c>>d) { audience_info_list.push_back(info(a,b,c,d)); s+=d; if(s>capacity) { s-=d; audience_info_list.pop_back(); cout<<"对不起,只剩"<<capacity-s<<"个位置." <<endl; cout<<"1.输入u,更新预定信息"<<endl; cout<<"2.输入q,退出预定"<<endl; cout<<"你的选择:"; cin>>choose; if(choose=="u") continue; if(choose=="q") break; } } cout<<"截至目前,一共有"<<s<<"位听众预定参加。预定观众信息如下:" <<endl; for(auto &i:audience_info_list) { i.print(); } }
运行截图
textcoder.hpp源代码
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include <iostream> #include <string> using namespace std; class TextCoder{ private: string text; public: string encoder(); string decoder(); TextCoder(string text0):text(text0){}; }; 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]+=5; } else if(text[i]>='v'&&text[i]<='z'||text[i] >='V'&&text[i] <= 'Z') text[i]=text[i]-26+5; } 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]-=5; else if(text[i]>='a'&&text[i]<='e'||text[i] >= 'A'&&text[i] <= 'E') text[i]=text[i]+26-5; } return text; } #endif
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输入英文文本: "; } }
运行截图
实验小结
1,for循环用(auto &i:v)非常方便
2,''单引号中的字符相当于一个数字
3,对string直接初始化用双引号