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

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

时间:2023-11-27 16:12:27浏览次数:46  
标签:const string int text C++ 实验 include TextCoder 模板

实验任务5

#pragma once 

#include<iostream>
#include<string>
using namespace std;

class TextCoder {
public:
    TextCoder() = default; 
    TextCoder(string str);
    string get_ciphertext();
    string get_deciphertext();
    ~TextCoder() = default;

private:
    string text;
    void encoder();
    void decoder();
};

TextCoder::TextCoder(string str) {
    text = str;
}

string TextCoder::get_ciphertext() {
    encoder();
    return text;
}

string TextCoder::get_deciphertext() {
    decoder();
    return text;
}

void TextCoder::encoder() {
    for (int i = 0; i < text.length(); i++) {
        if (islower(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'a' + 7) % 26 + 'a');
        } else if (isupper(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'A' + 7) % 26 + 'A');
        }
    }
}

void TextCoder::decoder() {
    for (int i = 0; i < text.length(); i++) {
        if (islower(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'a' + 26 - 7) % 26 + 'a');
        } else if (isupper(text[i])) {
            text.replace(i, 1, 1, (text[i] - 'A' + 26 - 7) % 26 + 'A');
        }
    }
}
TextCoder.hpp
#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(); 
}
task5.cpp

 

实验任务6

#pragma once

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class Info {
public:
    Info(const string nickname_, const string contact_, const string city_, int n_);
    ~Info() = default;
    void print() const;
      
private:
    string nickname;
    string contact;
    string city;
    int n;
};

Info::Info(const string nickname_, const string contact_, const string city_, int n_)
        : nickname{nickname_}, contact{contact_}, city{city_}, n{n_} {}

void Info::print() const {
        cout << left << endl
             << "昵称:" << setw(15) << nickname << endl
             << "联系方式:" << setw(15) << contact << endl
             << "所在城市:" << setw(15) << city << endl
             << "预定人数:" << setw(15) << n << endl;
}
Info.hpp
#include "Info.hpp"
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int capacity = 100;
static int total = 0;

int main() {
    vector<Info> audience_info_list;
    string nickname_, contact_, city_;
    int n_;
    cout << "录入信息:" << endl << "\n";
    cout << "昵称   联系方式(邮箱/手机号)   所在城市   预定参加人数" << endl;
    while (cin >> nickname_)
    {
        cin >> contact_ >> city_ >> n_;
        if (total + n_ <= capacity) {
            Info tmp(nickname_, contact_, city_, n_);
            audience_info_list.push_back(tmp);//将新的听众信息存入list 
            total += n_;
            if (total == capacity)
                break;
        }
        else
        {
            char option;
            cout << "对不起,只剩" << capacity - total << "个位置."
                 << "\n1. 输入u,更新(update)预定信息"
                 << "\n2. 输入q,退出预定"
                 << "\n你的选择:";
            cin >> option;
            if (option == 'u')
            {
                cout << "请重新输入:" << endl;
            }
            if (option == 'q')
            {
                cout << endl;
                break;
            }
        }
    }
    cout << "截至目前,一共有" << total << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto& list : audience_info_list)
        list.print();
    return 0;
}
task6.cpp

 

 

标签:const,string,int,text,C++,实验,include,TextCoder,模板
From: https://www.cnblogs.com/Lucky-19/p/17854820.html

相关文章

  • C++ Windows版本线程池
    使用:ThreadPoolthreadPool(12);//设定数量threadPool.queue(myFunction,args1,args2,...);//创建任务实现:#include<windows.h>#include<iostream>#include<functional>#include<vector>#include<queue>#include<mutex>#include......
  • C++标准库函数std::async
    1、std::asyncstd::async是C++11的标准库函数,用于创建执行异步任务并返回std::future对象来获取异步执行的结果状态。该函数最简单的用法如下所示:#include<iostream>#include<thread>#include<future>std::stringpromise_string(){for(inti=0;i<200;......
  • C++ Socket网络编程(TCP)
    基于Windows平台的Socket网络编程,用的QT,这个例子里一个服务端和一个客户端,两个应用程序之间进行通信,所以创建两个项目,目录结构如下:直接贴代码:服务端:1//main.cpp2#include<QCoreApplication>3#include<iostream>4#include<QDebug>5#include<cstring>6#i......
  • std::future与std::promise在C++多线程同步与数据共享中的应用
    1、std::promise与std::futurestd::promise与std::future通过配合使用完成数据的同步与共享,两者均是模板类;std::promise存储异步执行的值或异常;std::future提供可供访问的异步执行结果。二者配合使用伪码如下:std::promise<Type>pr;std::future<Type>fu(pr.get_fu......
  • C++聊天集群服务器2
    ​ 总体项目结构如下:一、数据库的封装​ db.h如图代码量不多​ db.cpp:#include"db.h"#include<muduo/base/Logging.h>//数据库配置信息staticstringserver="127.0.0.1";staticstringuser="root";staticstringpassword="123456";st......
  • C++11 后的单例写法
    template<typenameT>classSingleton{public:staticT&getInstance(){staticTt;returnt;}Singleton(constSingleton&)=delete;Singleton&operator=(constSingleton&)=delete;protected:......
  • PIP换源_Pycharm快捷键_自定义文件头模板
    【一】PIP更换国内源永久换源打开控制台或终端,并输入以下命令:pipconfigsetglobal.index-urlhttps://mirrors.aliyun.com/pypi/simple/更改pip源后,可以通过以下命令验证:pipconfiggetglobal.index-url如果返回值为https://mirrors.aliyun.com/pypi/simple/,则表......
  • 实验4 现代C++标准库与类模板
    实验任务5TextCoder.hpp源码1#include<iostream>2#include<string>34usingstd::string;56classTextCoder{7private:8stringtext;9voidencoder();10voiddecoder();11public:12TextCod......
  • 设计模式实验
    软件设计                 石家庄铁道大学信息学院 实验21:观察者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解观察者模式的动机,掌握该模式的结构;2、能够利用观察者模式解决实际问题。    [实验任务一]:股票提醒......
  • C++ 服务端与 Java 客户端的简单连接
    记录一下如何用两种语言简单通信,(其实也大差不差的,应该把。。。)//C++服务端#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<iostream>#include<cstring>usingstd::cout;usingstd::endl;usingst......