C/C++教程

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

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

TextCoder.hpp

#include<string>

using namespace std;
class TextCoder{
    public:
        TextCoder();
        TextCoder(string text);
        ~TextCoder();
        string encoder();
        string decoder();
    private:
        string text;
};
View Code

TextCoder.cpp

#include "textcoder.hpp"
TextCoder::TextCoder(){}
TextCoder::TextCoder(string text):text(text){}
TextCoder::~TextCoder(){}
string TextCoder::encoder(){
    for(auto& ch:text){
        if(ch>='a' &&ch<='z')ch=(ch-'a'+5)%26+'a';
        if(ch>='A' &&ch<='Z')ch=(ch-'A'+5)%26+'A';
    }
    return text;
}
string TextCoder::decoder(){
    for(auto& ch:text){
        if(ch>='a' &&ch<='z')ch=(ch-'a'+21)%26+'a';
        if(ch>='A' &&ch<='Z')ch=(ch-'A'+21)%26+'A';
    }
    return text;
}
View Code

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输入英文文本: ";
    }
}
View Code

Info.hpp

#include<string>
using namespace std;
class Info{
    public:
        Info();
        ~Info();
        Info(string n,string con,string ci,int a);
        static int getTol();
        void print();
    private:
        string nickName;
        string contact;
        string city;
        int n;
        static int tol;
};
View Code

Info.cpp

#include<bits/stdc++.h>
#include "Info.hpp"
using namespace std;
int Info::tol=100;
Info::Info(string n,string con,string ci,int a):nickName{n},contact{con},city{ci},n(a){
    Info::tol-=a;
}
void Info::print(){
    cout<<nickName<<" "<<contact<<" "<<city<<" "<<n<<endl;
}
Info::Info(){
};
Info::~Info(){
};
int Info::getTol(){
    return tol;
}
View Code

main.cpp

#include<bits/stdc++.h>
#include"Info.hpp"
using namespace std;
void min(){
    vector<Info> audience_info_list;
    for (int i = 1;Info::getTol()>=0; i++){
        string a,b,c;
        int d;
        cin >> a;
        if (a == "end"){
            break;
        }
        cin>> b >> c >> d;
        if(Info::Info::getTol()-d>=0){
            audience_info_list.push_back(Info(a, b, c, d));
        }else{
            cout << "超出人数"<<endl;
            string s;
            cin >> s;
            if (s == "q")break;
            if (s == "u"){
            }
        }

    }
    cout << "共有" << 100 - Info::Info::getTol() << endl;
    for (auto i = audience_info_list.begin(); i != audience_info_list.end(); i++){
        (*i).print();
        cout << endl;
    }
}
View Code

 

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