实验五:
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 encoder() 14 { 15 string v=text; 16 int i=0; 17 for(i=0;i<v.length();i++) 18 { 19 if(v[i]>='a'&&v[i]<='z') 20 { 21 if(v[i]+5<='z') 22 v[i]=v[i]+5; 23 else 24 v[i]='a'+v[i]+5-'z'; 25 } 26 else if(v[i]>='A'&&v[i]<='Z') 27 { 28 if(v[i]+5<='Z') 29 v[i]=v[i]+5; 30 else 31 v[i]='A'+v[i]+5-'Z'; 32 } 33 } 34 text=v; 35 return v; 36 } 37 string coder() 38 { 39 string v1=text; 40 int j=0; 41 for(j=0;j<v1.length();j++) 42 { 43 if(v1[j]>='a'&&v1[j]<='z') 44 { 45 if(v1[j]-5>='a') 46 v1[j]=v1[j]-5; 47 else 48 v1[j]='z'-('a'-v1[j]+5); 49 } 50 else if(v1[j]>='A'&&v1[j]<='Z') 51 { 52 if(v1[j]-5>='A') 53 v1[j]=v1[j]-5; 54 else 55 v1[j]='Z'-('A'-v1[j]+5); 56 } 57 } 58 text=v1; 59 return v1; 60 } 61 62 };
标签:city,string,int,c++,v1,数组,include,nickname,指针 From: https://www.cnblogs.com/lc114514/p/16807186.html