实验3 数组,指针与现代c++标准库
实验五:
Info.hpp
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 using namespace std; 5 class Info 6 { 7 private: 8 string nickname; 9 string contact; 10 string city; 11 int n; 12 public: 13 Info(string nick=" ",string cont=" ",string cit=" ",int count=0): nickname{nick},contact{cont},city{cit},n{count} 14 { 15 number+=n; 16 } 17 void back() 18 { 19 number -= n; 20 } 21 void print() const 22 { 23 cout<<left<<setw(20)<<"昵称: "<<nickname<<endl; 24 cout<<left<<setw(20)<<"联系方式: "<<contact<<endl; 25 cout<<left<<setw(20)<<"所在城市: "<<city<<endl; 26 cout<<left<<setw(20)<<"预定参加人数: "<<n<<endl; 27 } 28 static int number; 29 }; 30 31 int Info::number =0;
livehouse.cpp:
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include"Info.hpp" 5 #include<iomanip> 6 using namespace std; 7 void input(string &nickname,string &contact,string &city,int &n) 8 { 9 cin>>nickname; 10 cin>>contact; 11 cin>>city; 12 cin>>n; 13 } 14 int main() 15 { 16 const int capacity=100; 17 vector<Info> audience_info_list{0}; 18 string a; 19 cout<<"录入信息: "<<endl; 20 cout<<" 姓名"<<setw(20)<<"联系方式"<<setw(20)<<" 城市"<<setw(20)<<"预定人数"<<endl; 21 while(cin>>a) //作为每一次最前面的输入,不等于ctrl+z的时候接着输入类成员 22 { 23 string nickname, contact, city; 24 int n; 25 input(nickname,contact,city,n); 26 Info t(nickname,contact,city,n); 27 audience_info_list.push_back(t); 28 if(Info::number>capacity) 29 { 30 audience_info_list.back(); 31 t.back(); 32 cout<<"对不起,只剩"<<capacity-Info::number<<"个位置"<<endl; 33 char select; 34 cout<<"1. 输入u,更新预定信息"<<endl; 35 cout<<"2. 输入q,退出预定"<<endl; 36 cout<<"你的选择是: "; 37 cin>>select; 38 if(select=='u') 39 continue; 40 else if(select=='q') 41 break; 42 } 43 44 } 45 cout<<"截至目前,一共有"<<Info::number<<"名观众参加。 "<<"预定听众信息如下: "<<endl; 46 cout<<endl; 47 for (const auto &u:audience_info_list ) 48 { 49 50 u.print(); 51 cout<<endl; 52 cout<<endl; 53 } 54 return 0; 55 56 } 57


实验六:
jiemi.cpp
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 class TextCoder 6 { 7 private: 8 string text; 9 public: 10 TextCoder(string t=" "): text{t} 11 { 12 } 13 string v; 14 string v1; 15 private: 16 void encoder() 17 { 18 v=text; 19 int i=0; 20 for(i=0;i<v.length();i++) 21 { 22 if(v[i]>='a'&&v[i]<='z') 23 { 24 if(v[i]+5<='z') 25 v[i]=v[i]+5; 26 else 27 v[i]='a'+v[i]+5-'z'; 28 } 29 else if(v[i]>='A'&&v[i]<='Z') 30 { 31 if(v[i]+5<='Z') 32 v[i]=v[i]+5; 33 else 34 v[i]='A'+v[i]+5-'Z'; 35 } 36 } 37 text=v; 38 39 } 40 private: 41 void coder() 42 { 43 v1=text; 44 int j=0; 45 for(j=0;j<v1.length();j++) 46 { 47 if(v1[j]>='a'&&v1[j]<='z') 48 { 49 if(v1[j]-5>='a') 50 v1[j]=v1[j]-5; 51 else 52 v1[j]='z'-('a'-v1[j]+5); 53 } 54 else if(v1[j]>='A'&&v1[j]<='Z') 55 { 56 if(v1[j]-5>='A') 57 v1[j]=v1[j]-5; 58 else 59 v1[j]='Z'-('A'-v1[j]+5); 60 } 61 } 62 text=v1; 63 64 } 65 public: 66 string get_deciphertext() 67 { 68 coder(); 69 return text; 70 } 71 string get_ciphertext() 72 { 73 encoder(); 74 return text; 75 } 76 }; 77 78 79 80 void test() { 81 using namespace std; 82 string text, encoded_text, decoded_text; 83 cout << "输入英文文本: "; 84 while (getline(cin, text)) { 85 encoded_text = TextCoder(text).get_ciphertext();; 86 cout << "加密后英文文本:\t" << encoded_text << endl; 87 decoded_text = TextCoder(encoded_text).get_deciphertext(); 88 cout << "解密后英文文本:\t" << decoded_text << endl; 89 cout << "\n输入英文文本: "; 90 } 91 } 92 int main() { 93 test(); 94 }


浙公网安备 33010602011771号