实验3
实验3.5
Info.hpp
1 #include <iostream> 2 #include <string> 3 #include <iomanip> 4 using namespace std; 5 class Info{ 6 public: 7 Info(string name,string con,string where,int number):nickname{name},contact{con},city{where},n{number}{}; 8 void print(); 9 int get_n(); 10 11 private: 12 string nickname,contact,city; 13 int n; 14 }; 15 void Info::print(){ 16 17 cout<<"昵称: "<<nickname<<endl; 18 cout<<"联系方式: "<<contact<<endl; 19 cout<<"所在城市: "<<city<<endl; 20 cout<<"预定人数: "<<n<<endl; 21 cout<<endl; 22 } 23 24 int Info::get_n(){ 25 return n; 26 }
task5.cpp
1 #include <iostream> 2 #include "Info.hpp" 3 #include <vector> 4 #include <string> 5 #include <iomanip> 6 int main(){ 7 using namespace std; 8 const int capacity=100; 9 vector<Info> audience_info_list; 10 cout<<"录入信息:\n\n"; 11 cout<<"昵称"<<setw(35)<<"联系方式(邮箱/手机号)"<<setw(20)<<"所在城市"<<setw(25)<<"预定参加人数\n"; 12 int sum=0,number,item=0,left=0; 13 string name,con,where; 14 while(sum<=capacity&&getline(cin,name,' ')){ 15 cin>>con; 16 cin>>where; 17 cin>>number; 18 getchar(); 19 Info push(name,con,where,number); 20 left=push.get_n(); 21 audience_info_list.push_back(push); 22 item++; 23 sum+=number; 24 number=0; 25 } 26 if(sum>capacity){ 27 cout<<"对不起,只剩"<<capacity-sum+left<<"个位置\n"; 28 cout<<"1.输入u,更新(update)预定信息"<<endl; 29 cout<<"2.输入q,退出预定" <<endl; 30 cout<<"你的选择:"; 31 char choose; 32 cin>>choose; 33 cout<<endl; 34 cout<<"截至目前,一共有"<<sum-left<<"位听众预定参加。预定听众信息如下:"<<endl; 35 for(int i=0;i<item-1;i++){ 36 audience_info_list.at(i).print(); 37 } 38 } 39 else{ 40 cout<<"截至目前,一共有"<<sum<<"位听众预定参加。预定听众信息如下:"<<endl; 41 for(int i=0;i<item;i++){ 42 audience_info_list.at(i).print(); 43 } 44 } 45 }


实验3.6
textcoder.hpp
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class TextCoder{ 5 public: 6 TextCoder(string tx); 7 string get_ciphertext(); 8 string get_deciphertext(); 9 void ciphertext(); 10 void deciphertext(); 11 private: 12 string text; 13 14 }; 15 TextCoder::TextCoder(string tx){ 16 text=tx; 17 } 18 void TextCoder::ciphertext(){ 19 for(int i=0;i<=text.size();i++){ 20 if(text[i]>=65&&text[i]<=85){ 21 text[i]+=5; 22 } 23 else if(text[i]>=97&&text[i]<=117){ 24 text[i]+=5; 25 } 26 else if(text[i]>=86&&text[i]<=90){ 27 text[i]-=11; 28 } 29 else if(text[i]>=118&&text[i]<=122){ 30 text[i]-=11; 31 } 32 } 33 34 } 35 void TextCoder::deciphertext(){ 36 for(int i=0;i<=text.size();i++){ 37 if(text[i]>=70&&text[i]<=90){ 38 text[i]-=5; 39 } 40 else if(text[i]>=102&&text[i]<=122){ 41 text[i]-=5; 42 } 43 else if(text[i]>=65&&text[i]<=69){ 44 text[i]-=11; 45 } 46 else if(text[i]>=97&&text[i]<=101){ 47 text[i]-=11; 48 } 49 } 50 51 } 52 string TextCoder::get_ciphertext(){ 53 this->ciphertext(); 54 return text; 55 } 56 string TextCoder::get_deciphertext(){ 57 this->deciphertext(); 58 return text; 59 }
task6.cpp
1 #include "textcoder.hpp" 2 #include <iostream> 3 #include <string> 4 void test(){ 5 using namespace std; 6 string text,encoded_text,decoded_text; 7 cout<<"输入英文文本: "; 8 while(getline(cin,text)){ 9 encoded_text=TextCoder(text).get_ciphertext(); 10 cout<<"加密后英文文本:\t"<<encoded_text<<endl; 11 decoded_text=TextCoder(encoded_text).get_deciphertext(); 12 cout<<"解密后英文文本:\t"<<decoded_text<<endl; 13 cout<<"\n输入英文文本:"; 14 } 15 } 16 int main(){ 17 test(); 18 }

浙公网安备 33010602011771号