实验二

task 5.1 info.hpp

 1 #include<string>
 2 #include<iostream>
 3 using namespace std;
 4 class info{
 5 
 6 private:
 7     string nickname,city,contact;
 8     int n;
 9 public:
10     info(string nickname1="x",string city1="y",string contact1="z",int n1=0):nickname(nickname1),contact(contact1),city(city1),n(n1){}
11     void print();
12 };
13 void info::print(){
14     cout<<"称呼:"<<nickname<<endl;
15     cout<<"联系方式:"<<contact<<endl;
16     cout<<"所在城市: "<<city<<endl;
17     cout<<"预定人数:"<<n<<endl;
18 }

task 5.2 主程序

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include"info.hpp"
 5 using namespace std;
 6 
 7 int main(){
 8     vector<info>audience_info_list;
 9     cout<<"录入信息:"<<endl;
10     cout<<"\n";
11     cout<<"昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl;
12     const int capacity=100;
13     int sum=0,n;
14     string nickname1,contact1,city1; 
15     char a;
16     while(cin>>nickname1)
17     {
18         cin>>contact1;
19         cin>>city1;
20         cin>>n;
21         
22         if(sum+n>capacity)
23         {
24             cout<<"对不起,只剩"<<capacity-sum<<"个位置"<<endl;
25             cout<<"输入u,更新(update)预定信息"<<endl;
26             cout<<"输入q,退出预定"<<endl;
27             cout<<"您的选择;";
28             cin>>a;
29             if(a=='u')  cout<<"请重新输入:"<<endl;
30             else
31             {
32                 cout<<endl;
33                 break;
34             }
35         }
36         else
37         {
38             info v(nickname1,contact1,city1,n);
39             audience_info_list.push_back(v) ;
40             sum+=n;
41             
42             if(sum==capacity)    
43             break;
44           } 
45     }
46     
47     cout<<"截止目前,一共有"<<sum<<"位观众参加预定。预定观众信息如下:"<<endl; 
48     for(int i=0;i<audience_info_list.size();i++)
49         audience_info_list[i].print();
50 getchar();
51 getchar();
52 return 0;
53 }

运行结果如下图

 

 

 task 6 textcoder.hpp

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 class TextCoder{
 5     public:
 6         TextCoder(string str):text(str){};
 7         string encoder();
 8         string decoder();
 9         void show();
10     private:
11         string text;
12 };
13 string TextCoder::encoder(){
14 
15     for(int i=0;i<text.length();i++)
16     {
17         if((text[i]>='A'&&text[i]<='U')||(text[i]>='a'&&text[i]<='u'))
18         {
19             text[i]+=5;
20         }else if((text[i]>='V'&&text[i]<='Z')||(text[i]>='V'&&text[i]<='Z'))
21         {
22             text[i]-=21;
23         }    
24     }
25     return text;
26 }
27 
28 string TextCoder::decoder(){
29     for(int i=0;i<text.length();i++)
30     {
31         if((text[i]>='F'&&text[i]<='Z')||(text[i]>='f'&&text[i]<='z'))
32         {
33             text[i]-=5;
34         }else if((text[i]>='A'&&text[i]<='E')||(text[i]>='a'&&text[i]<='e'))
35         {
36             text[i]+=21;
37         }
38     }
39     return text;
40 }

task 6 主程序

 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 int main()
 5 {
 6 using namespace std;
 7 string text, encoded_text, decoded_text;
 8 cout << "输入英文文本: ";
 9 while (getline(cin, text))
10 {
11 encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象
12 cout << "加密后英文文本:\t" << encoded_text << endl;
13 decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
14 cout << "解密后英文文本:\t" << decoded_text << endl;
15 cout << "\n输入英文文本: ";
16 }
17 }

运行结果如下

 

posted @ 2021-11-02 20:00  zoracoding  阅读(18)  评论(2)    收藏  举报