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

task5:

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<iomanip>
 5 using namespace std;
 6 
 7 class Info
 8 {
 9     public:
10         Info(string NICKMANE,string CONTACT,string CITY,int N):nickname{NICKMANE},contact{CONTACT},city{CITY},n{N}{}
11         Info(const Info &obj):nickname{obj.nickname},contact{obj.contact},city{obj.city},n{obj.n}{}
12         ~Info()=default;
13         void print_Info();
14     
15         
16     private:
17         string nickname;
18         string contact;
19         string city;
20         int n;
21         
22         
23         
24         
25         
26 };
27 void Info::print_Info()
28 {
29     cout<<fixed<<setprecision(2);
30     cout<<"称呼:"<<nickname<<endl;
31     cout<<"联系方式:"<<contact<<endl;
32     cout<<"所在城市:"<<city<<endl;
33     cout<<"预订人数:"<<n<<endl; 
34 }
#include"Info.hpp"
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#define capacity 100
using namespace std;
int fun(){
    vector <Info> audience_list;
    int i=0;
    string nickname,contact,city;
    int n,t=0;
    while(cin>>nickname>>contact>>city>>n)
    {
        t+=n;
        if(t<=capacity)
        {
                Info info(nickname,contact,city,n);
                i++;
                audience_list.push_back(info); 
            
        }
        else 
        {
            cout<<"对不起,只剩下"<<capacity-t+n<<"位置"<<endl; 
            char b;    
            
            cout<<"输入"; 
            cin>>b;
            if(b=='p')
            {
                cout<<"退出"<<endl; 
                t=t-n;
                break;
            }
            if(b=='u')
            {
                cout<<"更新"<<endl; 
                t=t-n;
            }
            
            
        }
    
    }        
    cout<<"截至目前位置,一共有"<<t<<"人预定!,他们的信息如下:"<<endl;
    for(int k=0;k<i;k++)
                {
                     audience_list[k].print_Info();
                 }
    
}
int main()
{
   
    vector <Info> audience_list;
    cout<<"录入信息:"<<endl;
    cout<<"昵称/称呼,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl;
    fun();
    
    return 0;
            

}

 

 task6:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 class Textcode{
 6     
 7     public:
 8         Textcode(string Text):text{Text}{}
 9         Textcode(const Textcode &obj):text{obj.text}{}
10         ~Textcode()=default;
11         string encode();
12         string decode();
13     private:
14         string text;    
15         
16         
17         
18          
19 };
20 string Textcode::encode()
21 {
22     for(int i=0;i<text.length();i++)
23     {
24         if((text[i]>='a'&&text[i]<='u')||(text[i]>='A'&&text[i]<='U'))
25         {
26             text[i]=text[i]+5;
27         }
28         else if((text[i]>='v'&&text[i]<='z')||text[i]>='V'&&text[i]<='Z')
29         {
30             text[i]=text[i]-21;
31         }
32     }
33     return text;
34 }
35 string Textcode::decode()
36 {
37     for(int i=0;i<text.length();i++)
38     {
39         if((text[i]>='f'&&text[i]<='z')||(text[i]>='F'&&text[i]<='Z'))
40         {
41             text[i]=text[i]-5;
42         }
43         else if((text[i]>='a'&&text[i]<='e')||text[i]>='A'&&text[i]<='E')
44         {
45             text[i]=text[i]+21;
46         }
47     }
48     return text;
49 }
#include "textcoder.hpp"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text))
    {
        encoded_text = Textcode(text).encode();  // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = Textcode(encoded_text).decode(); // 这里使用的是临时无名对象
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

 

posted on 2021-10-29 13:56  Ancientwords  阅读(53)  评论(2)    收藏  举报