Java教程

实验二

本文主要是介绍实验二,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

//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(类型名());

这篇关于实验二的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!