首页 > 编程语言 >实验4 现代C++标准库与类模板

实验4 现代C++标准库与类模板

时间:2023-11-30 19:23:36浏览次数:63  
标签:std string void C++ 实验 using include 模板 cout

实验任务1

task1

task1_1.cpp

#include <iostream>

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

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

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

void test() {
    cout << "测试类A: " << endl;
    A a(3, 4);
    a.show();

    cout << "\n测试类B: " << endl;
    B b(3.2, 5.6);
    b.show();
}

int main() {
    test();
}
View Code

运行结果

 

task1_2.cpp

#include <iostream>
#include <string>

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

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;
};

void test() {
    cout << "测试1: 模板类抽象类型T用int实例化" << endl;
    X<int> x1(3, 4);
    x1.show();

    cout << "\n测试2: 模板类抽象类型T用double实例化" << endl;
    X<double> x2(3.2, 5.6);
    x2.show();

    cout << "\n测试3: 模板类抽象类型T用标准库中的string实例化" << endl;
    X<std::string> x3("hello", "c plus plus");
    x3.show();
}

int main() {
    test();
}
View Code

运行结果

 

实验任务2

task2_1.cpp

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

using namespace std;

int main() {
    
    const int n = 10;
    string prompt = string(n, '*') + "Enter a string: " + string(n, '*') + '\n';

    cout << "测试1:";
    cout << prompt;
    string s1;
    cin >> s1;
    cout << "s1: " << s1 << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << "\n测试2:";
    cout << prompt;
    getline(cin, s1); 
    cout << "s1: " << s1 << endl;

    cout << "\n测试3:";
    string s2, s3;
    cout << prompt;
    getline(cin, s2, ' ');
    getline(cin, s3);
    cout << "s2: " << s2 << endl;
    cout << "s3: " << s3 << endl;
}
View Code

运行结果

 

 

task2_2.cpp

#include <iostream>
#include <string>

int main() {
    using namespace std;

    string s;

    cout << "Enter words: \n";

    while( getline(cin, s) ) {
        for(auto &ch: s)
            ch = toupper(ch);
        cout << s << "\n";
    }
}
View Code

运行结果

 

task2_3.cpp

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    using namespace std;

    string s1;

    cout << "Enter words: \n";
    getline(cin, s1);
    cout << "original words: \n";
    cout << s1 <<endl;
    cout << "to uppercase: \n";
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    cout << s1 << endl;
}
View Code

运行结果

 

task2_4.cpp

#include <iostream>
#include <string>

int main() {
    using namespace std;

    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{"[email protected]"};
    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{"15216982937"};
    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;
}
View Code

运行结果

 

实验任务3

task3_1.cpp

#include <iostream>
#include <vector>


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

int main() {
    using namespace std;

    vector<int> v1;
    vector<int> v2(5);
    vector<int> v3(5, 42);
    vector<int> v4{1, 9, 8, 4};
    vector<int> v5{v4};

    cout << "v2: ";
    output(v2);

    cout << "v3: ";
    output(v3);

    cout << "v4: ";
    output(v4);

    cout << "v5: ";
    output(v5);
}
View Code

 

task3_2.cpp

#include <iostream>
#include <vector>

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

void output(const vector<int> &v) {
    cout << "v.size() = " << v.size() << endl;
    cout << "v.capacity() = " << v.capacity() << endl;
    cout << endl;
}

int main() {
    vector<int> v{42};
    output(v);

    v.push_back(55);
    v.push_back(90);
    output(v);

    for(auto i = 0; i < 8; ++i)
        v.push_back(i);
    output(v);

    v.pop_back();
    output(v);
}
View Code

 

实验任务4

task4.cpp

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

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";
}

void test1() {
    string s1{"cplus"};

    output1(s1);

    reverse(s1.begin(), s1.end());
    output2(s1);

    sort(s1.begin(), s1.end()); 
    output3(s1);
}

void test2() {
    array< array<int, 4>, 3> x{1, 9, 8, 4, 2, 0, 2, 3, 2, 0, 4, 9 };

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

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>());
    output2(v1);

    reverse(v1.begin(), v1.end());
    output3(v1);
}

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

    cout << "测试2: " << endl;
    test2();

    cout << "测试3: " << endl;
    test3();
}
View Code

 

实验任务5

task5.cpp

#include "textcoder.hpp"
#include <iostream>
#include <string>

void test() {
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入英文文本: ";
    while (getline(cin, text)) {
        encoded_text = TextCoder(text).get_ciphertext();
        cout << "加密后英文文本:\t" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).get_deciphertext();
        cout << "解密后英文文本:\t" << decoded_text << endl;
        cout << "\n输入英文文本: ";
    }
}

int main() {  
    test(); 
}
View Code

textcoder.hpp

#include<iostream>
#include<string>

using std::string;

class TextCoder {
    private:
        string text;
        void encoder();
        void decoder();
    public:
        TextCoder(string &str);
        TextCoder(const TextCoder &t);
        string get_ciphertext();
        string get_deciphertext();
};
TextCoder::TextCoder(string &str):text{str} {
}
TextCoder::TextCoder(const TextCoder &t):text{t.text} {
}
void TextCoder::encoder() {
    for (auto &i : text) {
        i=i+7;
    }
}
void TextCoder::decoder() {
    for (auto &i : text) {
        i=i-7;
    }

}
string TextCoder::get_ciphertext() {
    encoder();
    return text;
}
string TextCoder::get_deciphertext() {
    decoder();
    return text;
}
View Code

运行结果

 

实验任务6

task6.cpp

#include "Info.hpp"
#include<iostream>
#include<string>
#include <vector>

using namespace std;

int main()
{
    const int capacity=100;
    vector<Info> audience_info_list;
    
    cout << "录入信息:" << endl;
    cout << endl;
    cout << "昵称     联系方式(邮箱/手机号)     所在城市     预定参加人数     " << endl;
    char choice;
    int sum=0;
    string name, contact, city;
    int n;
    while(cin >> name >> contact >> city >> n)
    {
        sum+=n;
        
        if(sum > capacity)
        {
            sum-=n;
            cout << "对不起,只剩" << capacity-sum << "个位置" << endl;
            cout << "1.输入u,更新(update)预定信息" << endl
                 << "2.输入q, 退出预定" << endl
                 <<"你的选择: ";
            cin >> choice;
            
            if(choice=='u')
            {
                cin >> name >> contact >> city >> n;
                Info a(name,contact,city,n);
                audience_info_list.push_back(a);
                sum+=n;
            }
            else 
             break;
        }
        else audience_info_list.push_back(Info(name, contact, city, n));
    }
    cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
    for(auto &Info:audience_info_list)
    {
        Info.print();
    }
}
View Code

Info.hpp

#pragma once 

#include<iostream>
#include<string>
#include <iomanip>

using namespace std;
class Info{
public:
    Info(string nickname0, string contact0, string city0, int n0);
    ~Info()=default;

    void print() const;
    
private:
    string nickname,contact,city;
    int n;
};

Info::Info(string nickname0, string contact0, string city0, int n0)
{
    nickname = nickname0;
    contact = contact0;
    city = city0;
    n=n0;
}

void Info::print() const
{
    cout << "昵称:     " << nickname << endl;
    cout << "联系方式:     " << contact << endl;
    cout << "所在城市:     " << city << endl;
    cout << "预定参加人数:     " << n << endl;
}
View Code

运行结果

 

标签:std,string,void,C++,实验,using,include,模板,cout
From: https://www.cnblogs.com/NFurioso/p/17868073.html

相关文章

  • 第三方实验室LIMS管理系统源码
    LIMS实验室信息管理系统源码LIMS系统的功能根据实验室的规模和任务而有所不同,其系统主要功能包括:系统维护、基础数据编码管理,样品管理、数据管理、报告管理、报表打印、实验材料管理、设备管理等。它可以取代传统的手工管理模式而给检测实验室带来巨大的变化,提高检测实验室的整体......
  • SSL——ASA防火墙——Cisco实验(详解)
    SSLVPN一、理论部分定义:SSLVPN,操作在OSI模型的会话层,可以使用户通过浏览器对VPN进行连接,但是没有隧道。使用https加密的范式进行数据传输,跨平台性强,只要终端设备有浏览器,能够上网,就可以进行VPN连接,并访问资源。扩展:可以通过DNS进行访问,或者使用花生壳注册免费域名进行访问ssl(......
  • C/C++ 实现FTP文件上传下载
    FTP(文件传输协议)是一种用于在网络上传输文件的标准协议。它属于因特网标准化的协议族之一,为文件的上传、下载和文件管理提供了一种标准化的方法,在Windows系统中操作FTP上传下载可以使用WinINet库,WinINet(WindowsInternet)库是Windows操作系统中的一个网络API库,用于访问Interne......
  • 欧拉函数模板
    #include<bits/stdc++.h>usingnamespacestd;//欧拉函数,质数vector<int>euler_range(intn){vector<int>phi(n+1),prime;vector<bool>is_prime(n+1,true);is_prime[1]=0,phi[1]=1;for(inti=2;i<=n;i......
  • C++基础
    文章参考:《C++面向对象程序设计》✍千处细节、万字总结(建议收藏)_白鳯的博客-CSDN博客C++运算符重载_c++重载=-CSDN博客一.C++基础1.一个简单的案例#include<iostream>//编译预处理命令usingnamespacestd;//使用命名空间intadd(inta,intb);//......
  • [转载]C/C++ 遍历窗口标题类名
    原地址:https://cloud.tencent.com/developer/article/2201930遍历每个进程,一次查找进程下的窗口,找到窗口标题为“”,窗口类名为“RunDll”的窗口。如果找到返回true,没找到返回false。#pragmaregion依赖typedefstructEnumHWndsArg{std::vector<HWND>*vecHWnds......
  • LIMS实验室信息管理系统源码,支持二次开发
    LIMS实验室信息管理系统源码,支持二次开发LIMS实验室信息管理系统是一种软件类型,旨在通过跟踪与样品、实验、实验室工作流程和仪器相关的数据,提高实验室产能和效率。覆盖实验室从合同审批、委托下单、样品管理、生产调度、检测记录、报告管理、财务开票结算等全业务的过程管理。1、......
  • C++使用OpenSSL实现AES-256-CBC加密解密实例----亲测OK
    //AesUtil.h#ifndef__AES_UTIL_H__#define__AES_UTIL_H__#ifdef__cplusplus//告诉编译器,这部分代码按C语言的格式进行编译,而不是C++的extern"C"{#endifstringUTIL_aes_cbc_encrypt(constunsignedchar*password,unsignedintpassword_byte_len,c......
  • C++使用OpenSSL实现Base64编码、解码实例----亲测OK
    摘自:https://www.dandelioncloud.cn/article/details/1498198300963708930 //Base64Util.h#ifndef__BASE64_UTIL_H__#define__BASE64_UTIL_H__#ifdef__cplusplus//告诉编译器,这部分代码按C语言的格式进行编译,而不是C++的extern"C"{#endifstringUTIL......
  • 实验四
    实验五TextCoder.hpp点击查看代码#include<string>classTextCoder{private:std::stringtext;voidencoder(){for(char&c:text){if(c>='a'&&c<='z'){c=((c......