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

2021/10/28 1:11:37

本文主要是介绍实验二数组、指针与c++标准库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include<bits/stdc++.h>
using namespace std;

class Info
{
    private:
        string nickname;
        string contact;
        string city;
        int n;
    public:
        Info(string a,string b,string c,int d):nickname(a),contact(b),city(c),n(d){
        }
        void print()
        {
            cout<<"称呼:        "<<nickname<<endl;
            cout<<"联系方式:    "<<contact<<endl;
            cout<<"所在城市:    "<<city<<endl;
            cout<<"预定人数:    "<<n<<endl;
        }
};
#include"info.hpp"
#include<iostream>
#include<string>
#include<vector>

using namespace std;
int main()
{
    vector<Info> audience_info_list;
    const int capacity=100;
    string Nickname,Contact,City;
    char Q;
    int N,sum=0,fro=0;
    cout<<"称呼/昵称,     联系方式(邮箱/手机号),    所在城市,    预定参加人数:"<<endl; 
    while(cin>>Nickname)
    {
        cin>>Contact>>City>>N;
        if(sum+N<=capacity)
        {
            Info temp(Nickname,Contact,City,N);
            audience_info_list.push_back(temp);
            fro=capacity-sum-N;
            sum+=N;
            if(sum==capacity)
            break;
        }
        else
        {
            cout<<"对不起只剩"<<fro<<"个位置"<<endl;
            cout<<"1.输入u,更新(updata)预订信息"<<endl;
            cout<<"2.输入q,退出预定"<<endl;
            cout<<"你的选择:";
            
            while(1){
            cin>>Q;    
            if(Q=='q')
            break;
            else 
            {
            cout<<"请重新输入:"<<endl;
            }
        }
        if(Q=='q')
        break; 
        }
    }    
    
    cout<<"截止目前,一共有"<<sum<<"位听众预定参加,预定听众信息如下"<<endl;
    for(vector<Info>::iterator it=audience_info_list.begin();it != audience_info_list.end();++it)
            it->print();
    return 0;
}

 

 

#include <iostream>
#include <string>
using namespace std;
class TextCoder
{
private:
    string text;

public:
    TextCoder();
    TextCoder(string a) : text(a) {}
    string encoder();
    string decoder();
};
string TextCoder::decoder()
{
    for (int i = 0; i < text.length(); i++)
    {
        if ((text[i] >= 'f' && text[i] <= 'z') || (text[i] >= 'F' && text[i] <= 'Z'))
        {
            text[i] = text[i] - 5;
        }
        else if ((text[i] >= 'a'&&text[i] <= 'e')||(text[i]>='A'&&text[i]<='E'))
        {
            text[i] = text[i] + 21;
        }
    }
    return text;
}
string TextCoder::encoder()
{
    for (int i = 0; i < text.length(); i++)
    {
        if ((text[i] >= 'a' && text[i] <= 'u') || (text[i] >= 'A' && text[i] <= 'Z'))
            text[i] = text[i] + 5;
        else if (text[i] >= 'v' && text[i] <= 'z' || text[i] >= 'V' && text[i] <= 'Z')
            text[i] = text[i] - 21;
    }
    return text;
}
#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输入英文文本: ";
    }
}

 



这篇关于实验二数组、指针与c++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程