实验二

info.cpp

1 #include<iostream>
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 class Info
 7 {
 8      private:
 9         string nickname; 
10         string contact;
11         string city; 
12         int n; 
13     public:
14         Info(string nickname0,string contact0,string city0,int n0):nickname(nickname0),contact(contact0),city(city0),n(n0){}
15         void print() const;
16         ~Info()=default; 
17    };
19 void Info::print() const
20 {
21            std:: cout<<"称呼:\t\t\t"<<nickname<<endl;
22            std:: cout<<"联系方式:\t\t"<<contact<<endl;
23            std:: cout<<"所在城市:\t\t"<<city<<endl;
24            std:: cout<<"预定人数:\t\t"<<n<<endl;
25         }
26         

task5.cpp

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

运行结果

 

 TextCoder.hpp

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

task6

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

运行结果

 

 

 

posted @ 2021-11-03 14:45  折木奉太郎A  阅读(41)  评论(2)    收藏  举报