实验3 数组、指针与现代C++标准库
实验任务5
task5.cpp
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include"Info.hpp" 5 using namespace std; 6 int main() 7 { 8 const int capacity=100; 9 vector<Info> audience_info_list; 10 cout<<"录入信息:"<<endl; 11 cout<<endl; 12 cout<<"昵称\t"; 13 cout<<"联系方式(邮箱/手机号)\t"; 14 cout<<"所在城市\t"; 15 cout<<"预定参加人数\t"; 16 cout<<endl; 17 string nickname,contact,city; 18 int n,num=0; 19 while(cin>>nickname) 20 { 21 cin>>contact>>city>>n; 22 num+=n; 23 if(capacity-num>=0) 24 { 25 Info p=Info(nickname,contact,city,n); 26 audience_info_list.push_back(p); 27 } 28 else 29 { 30 num-=n; 31 char c; 32 cout<<"对不起,只剩"<<capacity-num<<"个位置."<<endl; 33 cout<<"1. 输入u,更新(update)预定信息"<<endl; 34 cout<<"2. 输入q,退出预定"<<endl; 35 cout<<"你的选择:"; 36 cin>>c; 37 cout<<endl; 38 if(c =='q') 39 break; 40 else if(c =='u') 41 { 42 cout<<"请更新您的预定信息"<<endl; 43 continue; 44 } 45 } 46 } 47 cout<<"截至目前,一共有"<<num<<"名听众预定参加。预定听众信息如下:"<<endl; 48 for(int i=0;i<audience_info_list.size();++i) 49 { 50 audience_info_list.at(i).print(); 51 cout<<endl; 52 } 53 }
Info.hpp
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 #include<string> 5 using namespace std; 6 class Info{ 7 public: 8 Info(string name0,string contact0,string city0,int n):nickname{name0},contact{contact0},city{city0},n{n}{} 9 void print(); 10 private: 11 string nickname,contact,city; 12 int n; 13 }; 14 void Info::print() 15 { 16 cout<<"昵称:\t"<<nickname<<endl; 17 cout<<"联系方式:\t "<<contact<<endl; 18 cout<<"所在城市:\t "<<city<<endl; 19 cout<<"预定人数:\t "<<n<<endl; 20 }
实验任务6
textcoder.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class TextCoder{ 5 public: 6 TextCoder(string text0):text{text0}{} 7 string get_ciphertext(); 8 string get_deciphertext(); 9 private: 10 string text; 11 void encoder(); 12 void decoder(); 13 }; 14 void TextCoder::encoder() 15 { 16 int l=text.length(); 17 int i=0; 18 for(i=0;i<l;i++) 19 { 20 if(text.at(i)>='a'&&text.at(i)<='u') 21 text.at(i)+=5; 22 else if(text.at(i)>='v'&&text.at(i)<='z') 23 text.at(i)-=21; 24 else if(text.at(i)>='A'&&text.at(i)<='U') 25 text.at(i)+=5; 26 else if(text.at(i)>='V'&&text.at(i)<='Z') 27 text.at(i)-=21; 28 } 29 } 30 void TextCoder::decoder() 31 { 32 int l=text.length(); 33 int i=0; 34 for(i=0;i<l;i++) 35 { 36 if(text.at(i)>='f'&&text.at(i)<='z') 37 text.at(i)-=5; 38 else if(text.at(i)>='F'&&text.at(i)<='Z') 39 text.at(i)-=5; 40 else if(text.at(i)>='a'&&text.at(i)<'f') 41 text.at(i)+=21; 42 else if(text.at(i)>='A'&&text.at(i)<'F') 43 text.at(i)+=21; 44 } 45 } 46 string TextCoder::get_ciphertext() 47 { 48 encoder(); 49 return text; 50 } 51 string TextCoder::get_deciphertext() 52 { 53 decoder(); 54 return text; 55 }
task6.cpp
1 #include"textcoder.hpp" 2 #include<iostream> 3 #include<string> 4 void test() 5 { 6 using namespace std; 7 string text,encoded_text,decoded_text; 8 cout<<"输入英文文本: "; 9 while(getline(cin,text)){ 10 encoded_text=TextCoder(text).get_ciphertext(); 11 cout<<"加密后英文文本:\t"<<encoded_text<<endl; 12 decoded_text=TextCoder(encoded_text).get_deciphertext(); 13 cout<<"解密后英文文本:\t"<<decoded_text<<endl; 14 cout<<"\n输入英文文本:"; 15 } 16 } 17 int main() 18 { 19 test(); 20 }