实验二

//info.hpp

#ifndef INFO_HPP
#define INFO_HPP
#include<iostream>
#include<string>

using namespace std;

class Info{
public:
    Info(string a,string b,string c,int d):nickname{a},contact{b},city{c},n{d}{}
    void print(){
        cout << "nickname: \t" << nickname << endl;
        cout << "contact: \t" << contact << endl;
        cout << "city: \t\t" << city << endl;
        cout << "number: \t" << n << endl;        
    }
private:
    string nickname;
    string contact;
    string city;
    int n;//预定参加人数    
};

#endif

 

//main.cpp

#include "info.hpp"
#include <iostream>
#include <vector>
using namespace std;

int main() {
    
    const int capacity = 100;
    
    vector<Info> audience_info_list;
    
    cout << "请输入信息\n";
    cout << "nickname:    " << "contact:       " << "city:       " << "numeber: \n";
    
    string nickname,contact,city;
    int number;
    int t = 0;
    while(cin>>nickname && t<capacity)
    {
        cin >> contact;
        cin >> city;
        cin >> number;
        
        if(t+number < capacity)
        {
            t += number;
            audience_info_list.push_back(Info(nickname,contact,city,number));
        }
        else 
        {
            char  a;
            cout << "预约人数达到上限,请输入q退出预定,或输入u更新预定信息\n";
            cin >> a;
            if(a == 'q')
            {
                break;
            }
            else if(a == 'u')
            {
                cin >> nickname;
                cin >> contact;
                cin >> city;
                cin >> number;
                if(t+number < capacity)
                {
                    t += number;
                    audience_info_list.push_back(Info(nickname,contact,city,number));
                }
            }
        }
    }
    
    cout << endl;
    
    cout << "共有" << t << "名观众参加\n";
    for (auto i = audience_info_list.begin(); i != audience_info_list.end(); ++i)
    {
        (*i).print();
    }
    
}

 

 

  

 

 

//textcoder.hpp

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP

#include <iostream>
#include <string>

using namespace std;

class TextCoder{
public:
    TextCoder(string t = " "):text{t}{}
    string encoder()
    {
        int i;
        for(i = 0; i < text.length(); i++)
        {
            if(text[i] >= 'a' && text[i] <= 'u' || text[i] >= 'A' && text[i] <= 'U')
                text[i] += 5;
            else if(text[i] > 'u' && text[i] <= 'z' || text[i] >'U' && text[i] <= 'Z')
                text[i] -= 21; 
        }
        return text;
    }     

    string decoder()
    {
        int i;
        for(i = 0; i < text.length(); i++)
        {
            if(text[i] >= 'f' && text[i] <= 'z' || text[i] >= 'F' && text[i] <= 'Z')
                text[i] -= 5;
            else if(text[i] < 'f' && text[i] >='a' || text[i] < 'F' && text[i] >= 'A')
                text[i] += 21; 
        }
        return text;
    }
    
private:
    string text;
}; 

#endif

 

//main.cpp

#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 = TextCoder(text).encoder();  // 这里使用的是临时无名对象
        cout << "加密后英文文本:\t" << encoded_text << endl;

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

 

实验总结:

1. 学习了string相关的用法 :  输入(cin、getine)下标访问等

2. 动态数组类模板vector :

  (1)定义:vector<类型,长度> 数组名{初始化列表};

  (2)   追加:数组名.push_back(类型名());

posted @ 2021-10-29 00:31  gujia  阅读(41)  评论(3编辑  收藏  举报