实验三

实验任务一

model 1

#include <iostream>

using std::cout;
using std::endl;

// 类A的定义
class A{
public:
    A(int x0, int y0): x{ x0 }, y{ y0 } {}
    void show() const { cout << x << ", " << y << endl; }
private:
    int x, y;
};

// 类B的定义
class B{
public:
    B(double x0, double y0): x{ x0 }, y{ y0 } {}
    void show() const { cout << x << ", " << y << endl;}
private:
    double x, y;
};

int main() {
    A a(3, 4);
    a.show();

    B b(3.2, 5.6);
    b.show();
}

model 2

#include <iostream>
#include <string>

using std::cout;
using std::endl;

// 定义类模板X
template<typename T>
class X{
public:
    X(T x0, T y0): x{x0}, y{y0} {}
    void show() const { cout << x << ", " << y << endl;}
private:
    T x, y;
};

int main() {
    X<int> x1(3, 4);
    x1.show();

    X<double> x2(3.2, 5.6);
    x2.show();

    X<std::string> x3("hello", "c plus plus");
    x3.show();
}

实验二

model 1

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1,s2;
    s1 = "nuist";
    s1[0] = 'N';
    s1.at(1) = 'U';
    cout << boolalpha << (s1 =="nuist") << endl;
    cout << s1.length() << endl;
    cout << s1.size() << endl;
    s2 = s1 + ", 2050";
    cout << s2 << endl;
    
    string email{"xyz@gmail.com"};
    auto pos = email.find("@");
    
    if(pos == string::npos)
        cout << "illegal email address";
    else {
        auto s1 = email.substr(0, pos);
        auto s2 = email.substr(pos + 1);
        cout << s1 << endl;
        cout << s2 << endl;
    }
    string phone{"13405862003"};
    cout << phone.replace(3, 5,string(5, '*')) << endl;
    
    string s3{"cosmos"}, s4{"galaxy"};
    cout << "s3: " + s3 + "s4: " + s4 << endl;
    s3.swap(s4);
    cout << "s3: " + s3 + "s4: " + s4 << endl;
    
    string s5{"abc"};
    const char *pstr = s5.c_str();
    cout << pstr << endl;
    
    string s6{"12306"};
    int x1 = stoi(s6);
    cout << x1 << endl;
    
    int x2 = 12306;
    string s7 = to_string(x2);
    cout << s7 << endl;
    
    double x3 = 123.06;
    string s8 = to_string(x3);
    cout << s8 << endl;
    return 0;
}

model 2

#include<iostream>
#include<string>
#include<limits>

int main()
{
    using namespace std;
    
    const int n = 10;
    string prompt = string(n,'*') + "Enter a string: " + string(n, '*') + '\n';
    cout << prompt;
    string s1;
    cin >> s1;
    cout << "s1: " << s1 << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    cout << prompt;
    getline(cin, s1);
    cout << "s1: " << s1 << endl;
    
    string s2, s3;
    cout << prompt;
    getline(cin, s2, ' ');
    getline(cin, s3);
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;
}

实验四

model 1

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<array>

using namespace std;

template<typename T>
void output1(const T &obj)
{
    for(auto i = 0;i < obj.size();++ i)
        cout << obj.at(i) << ", ";
    cout << "\b\b \n";
    
}

template<typename T>
void output2(const T &obj) {
    for(auto p = obj.cbegin();p != obj.cend(); ++p)
        cout << *p << ", ";
    cout << "\b\b \n";
}

template<typename T>
void output3(const T &obj){
    for(auto &item: obj)
        cout << item << ", ";
    cout << "\b\b \n";
}

// 测试string类对象
void test1() {
    string s1{"cplus"};

    output1(s1);

    reverse(s1.begin(), s1.end());  // 对对象s1中的数据项进行翻转
    output2(s1);

    sort(s1.begin(), s1.end());   // 对对象s1中的数据项排序(默认升序)
    output3(s1);
}

// 测试array<int>类对象
void test2() {
    array< array<int, 4>, 3> x{1, 9, 8, 4, 2, 0, 2, 2, 2, 0, 4, 9 };

    output1( x.at(0) );
    output2( x.at(1) );
    output3( x.at(2) );
}

// 测试vector<string>类对象
void test3() {
    vector<string> v1 {"Sheldon", "Leonard", "Howard", "Raj"};

    v1.push_back("Penny");
    v1.push_back("Amy");

    output1(v1);

    sort(v1.begin(), v1.end(), std::greater<string>());  // 对v1对象中的字符串按降序排序
    output2(v1);

    reverse(v1.begin(), v1.end());  // 对v1对象中的字符串翻转
    output3(v1);
}

int main() {
    cout << "test1: " << endl;
    test1();

    cout << "test2: " << endl;
    test2();

    cout << "test3: " << endl;
    test3();
}

实验五

//main.cpp
#include<iostream>
#include<string>
#include<vector>
#include"info.hpp"
#include<iomanip>

using namespace std;

const int capacity = 100;

int main()
{
    string nickname,contact,city;
    int num,cnt = 0;
    vector<info> audience_info_list;
    cout << "please enter informance" << endl;
    cout << "nickname\t\t" << "contact(email/phone)\t\t" << "city\t"<<"reserved number\t" << endl;
    while(cin >> nickname)
    {
        cin >> contact >> city >> num;
        cnt += num;
        if(capacity > cnt){
               info x = info(nickname,contact,city,num);
               audience_info_list.push_back(x);
        }
        else{
            cnt -= num;
            cout << "sorry,only left" << (capacity - cnt) << "sits." << endl;
           
        }
        cout << "entil now,all have" << cnt << "audiences, the information are as follow: "<< endl;
        for(int i= 0;i < audience_info_list.size();i ++)
        {
            audience_info_list.at(i).print();
            cout << endl;
        }
        cout << "1.enter'u',update information" << endl;
        cout << "2.enter'q',to quit reserve" << endl;
        cout << "your choice: ";
        string n;
        cin >> n;
        if(n == "q") break;
        else if(n =="u")
        {
            cout << "please enter information " << endl;
            cout << "nickname\t\t" << "contact(email/phone)\t\t" << "city\t"<<"reserved number\t" << endl;
            continue;
        }
        
    }
    return 0;
}

//info.hpp
#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class info
{
public :
    info(string nickname0="a",string contact0= "a",string city0="a",int num0 = 0):nickname{nickname0},contact{contact0},city{city0},num{num0} {}
    void print() const;
    
private:
    string nickname,contact,city;
    int num;
    
};

void info::print() const{
    cout << "nickname: \t\t" << nickname << endl;
    cout << "contact: \t" << contact << endl;
    cout << "city: \t\t" << city << endl;
    cout << "reserve number: \t" << num << endl;
}

实验6

// cpp

#include "textcoder.hpp"
#include <iostream>
#include <string>
void test(){
    using namespace std;
    string text,encoded_text,decoded_text;
    cout<<"please enter the text:";
    while(getline(cin,text)){
        encoded_text=TextCoder(text).get_ciphertext();
        cout<<"the text encodered:\t"<<encoded_text<<endl;
        decoded_text=TextCoder(encoded_text).get_deciphertext();
        cout<<"the encodered text discodered:\t"<<decoded_text<<endl;
        cout<<"please enter the text: ";
    }
}
int main(){
    test();
}

// hpp
#include <iostream>
#include <string>
using namespace std;
class TextCoder{
    public:
    TextCoder(string tx);
    string get_ciphertext();
    string get_deciphertext();
    void ciphertext();
    void deciphertext();
    private:
    string text;

};
TextCoder::TextCoder(string tx){
    text=tx;
}
void TextCoder::ciphertext(){
    for(int i=0;i<=text.size();i++){
        if(text[i]>='a'&&text[i]<='u'){
            text[i]+=5;
        }
        else if(text[i]>='A'&&text[i]<='U'){
            text[i]+=5;
        }
        else if(text[i] >'u'&&text[i]<='z'){
            text[i]-=21;
        }
        else if(text[i]>'U'&&text[i]<'Z'){
                text[i]-=21;
        }
    }

}
void TextCoder::deciphertext(){
    for(int i=0;i<=text.size();i++){
        if(text[i]>=70&&text[i]<=90){
            text[i]-=5;
        }
        else if(text[i]>=102&&text[i]<=122){
            text[i]-=5;
        }
        else if(text[i]>=65&&text[i]<=69){
            text[i]+=21;
        }
        else if(text[i]>=97&&text[i]<=101){
                text[i]+=21;
        }
    }

}
string TextCoder::get_ciphertext(){
    this->ciphertext();
    return text;
}
string TextCoder::get_deciphertext(){
    this->deciphertext();
    return text;
}

posted @ 2022-10-19 16:39  MRchenyuheng  阅读(34)  评论(0)    收藏  举报