实验二 数组、指针与C++标准库
- 实验任务5
source code in Info.hpp :
1 #include<iostream> 2 #include<string> 3 #include <iomanip> 4 using namespace std; 5 6 7 class Info{ 8 private: 9 string nickname; 10 string contact; 11 string city; 12 public: 13 int n; 14 Info(string name,string con,string cit,int m):nickname(name),contact(con),city(cit),n(m) {} 15 void print(){ 16 cout<<setiosflags(ios::left); 17 cout<<setw(10)<<"称呼:"<<nickname<<endl; 18 cout<<setw(10)<<"联系方式:"<<contact<<endl; 19 cout<<setw(10)<<"所在城市:"<<city<<endl; 20 cout<<setw(10)<<"预定人数:"<<n<<endl; 21 } 22 23 24 };
source code in task5.cpp :
1 #include"Info.hpp" 2 #include<vector> 3 4 int main(){ 5 using namespace std; 6 const int capacity=100; 7 vector<Info> audience_info_list; 8 char choice; 9 string nickname,contact,city; 10 int n,sum=0; 11 12 cout<<"录入信息"<<endl<<endl; 13 cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl; 14 15 while(cin>>nickname>>contact>>city>>n){ 16 if(sum+n>capacity){ 17 cout<<"对不起,只剩"<<capacity-sum<<"个位置"<<endl; 18 cout<<"1、输入u,更新(update)预定信息"<<endl; 19 cout<<"2、输入q,退出预定"<<endl; 20 cout<<"你的选择:"; 21 22 cin>>choice; 23 if(choice=='u') 24 continue; 25 else 26 break; 27 } 28 Info p(nickname,contact,city,n); 29 audience_info_list.push_back(p); 30 sum+=n; 31 } 32 cout<<"截至目前,一共有"<<sum<<"位听众预定参加。预定听众信息如下:"<<endl; 33 for(auto ele:audience_info_list) 34 ele.print(); 35 }
The Running results :
- 实验任务6
source code in textcoder.hpp :
1 #include<string> 2 using namespace std; 3 4 class TextCoder{ 5 private: 6 string text; 7 8 public: 9 TextCoder(string t):text(t){} 10 11 string encoder(){ 12 for(char &ch:text){ 13 if(ch>='A' && ch<='Z') 14 ch='A'+(ch-'A'+5)%26; 15 else if(c>='a' && c<='z') 16 ch='a'+(ch-'a'+5)%26; 17 } 18 return text; 19 } 20 21 string decoder(){ 22 for(char &ch:text){ 23 if(ch>='A' && ch<='Z') 24 ch='A'+(ch-'A'+21)%26; 25 else if(ch>='a' && ch<='z') 26 ch='a'+(ch-'a'+21)%26; 27 } 28 return text; 29 } 30 31 };
The Running results :