实验二 数组、指针与C++标准库

实验任务5

程序源码

  • Info.cpp

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 #include <iostream>
 4 #include <iomanip>
 5 #include <string>
 6 using namespace std;
 7 class Info
 8 {
 9     public:
10         Info(string n,string co,string ci,int number);
11         void print();
12         static int rest(int cpacity);
13     private    :/*author:201913950050ZhouRuoHong*/
14         string nickname;
15         string contact;
16         string city;
17         int n;    
18         static int total;    
19 };
20 int Info::total=0;
21 Info::Info(string n,string co,string ci,int number):nickname(n),contact(co),city(ci),n(number){
22     total+=number;
23 }
24 
25 void Info::print(){
26     cout<<setiosflags(ios::left);
27     cout<<setw(10)<<"称呼:"<<nickname<<endl;/*author:201913950050ZhouRuoHong*/
28     cout<<setw(10)<<"联系方式:"<<contact<<endl;
29     cout<<setw(10)<<"所在城市:"<<city<<endl;
30     cout<<setw(10)<<"预定人数:"<<n<<endl;
31 }
32 int Info::rest(int cpacity){
33     return (cpacity-total);
34 }
35 #endif
  • task5.cpp
#include <iostream>
#include "Info.hpp"
#include <vector>
using namespace std;
int main()
{
    vector<Info> audience_info_list;
    const int cpacity=100;
    string nickname,contact,city,n;
    cout<<"录入信息:"<<endl;
    cout<<endl;
    cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl;
    while(cin>>nickname>>contact>>city>>n)
    {
        int number=stoi(n);
        int tempmax=Info::rest(cpacity);
        if(number>tempmax)
        {
            cout<<"对不起,只剩"<<Info::rest(cpacity)<<"位置."<<endl;
            cout<<"1.输入u,更新预定信息"<<endl;
            cout<<"2.输入q,退出预定"<<endl;
            cout<<"你的选择:";/*author:201913950050ZhouRuoHong*/
            char x;
            cin>>x;
            if(x=='q')
            {
                break;
             } 
             else
             {
                 continue;
             }
        }
        else
        {
            Info temp(nickname,contact,city,number);
            audience_info_list.push_back(temp);
        }
    }
    cout<<endl;
    cout<<endl;
    cout<<"截至目前,一共有"<<cpacity-Info::rest(cpacity)<<"位听众预定参加。预定听众信息如下:"<<endl;
    for(auto i:audience_info_list) /*author:201913950050ZhouRuoHong*/
    {
        i.print();
    }
    cout<<"\b\b\n";
}

运行测试结果截图

  • 用户预约时,预定人数没有达到上限:

 

  • 用户预约时,预定人数达到上限,并输入u:

 

  •  用户预定时,预定人数达到上限,并输入q:

 

 

 实验任务6

程序源码

  • TextCoder.hpp
 1 #ifndef TEXTCODER_HPP
 2 #define TEXTCODER_HPP
 3 #include <iostream>
 4 #include <iomanip>
 5 #include <string>
 6 using namespace std;
 7 class TextCoder
 8 {
 9     public:
10         TextCoder(string input):text(input){};
11         ~TextCoder(){};
12         string encoder();
13         string decoder();
14     private:
15         string text;
16 };
17 string TextCoder::encoder()
18 {
19     string encoded_text=text;
20     for(auto &i:encoded_text)
21     {
22         if('a'<=i&&i<='u')
23         {
24             i=i+5;
25         }
26         else if('v'<=i&&i<='z')
27         {
28             i=i-21;
29         }
30         else if('A'<=i&&i<='U')
31         {
32             i=i+5;
33         }
34         else if('V'<=i&&i<='Z')
35         {
36             i=i-21;    
37         }
38         else
39         {
40             i=i;
41         }
42     }
43     return encoded_text;
44 }//AUTHOR:ZhouRuoHong
45 string TextCoder::decoder()
46 {
47     string decoded_text=text;
48     for(auto &i:decoded_text)
49     {
50         if('f'<=i&&i<='z')
51         {
52             i=i-5;
53         }
54         else if('a'<=i&&i<='e')
55         {
56             i=i+21;
57         }
58         else if('F'<=i&&i<='Z')
59         {
60             i=i-5;
61         }
62         else if('A'<=i&&i<='E')
63         {
64             i=i+21;    
65         }
66         else
67         {
68             i=i;
69         }
70     }
71     return decoded_text;
72 }
73 #endif

 

  • task6.cpp
 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 << "加密后英文文本:\t" << encoded_text << endl;
16 
17         decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
18         cout << "解密后英文文本:\t" << decoded_text << endl;
19         cout << "\n输入英文文本: ";
20     }
21 }

运行测试结果截图:

 

posted @ 2021-11-02 14:09  敲代码的喵  阅读(44)  评论(4)    收藏  举报