首页 > 其他分享 >实验3

实验3

时间:2022-10-25 03:11:05浏览次数:37  
标签:info string int text void 实验 include

实验任务五cpp

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

using namespace std;

int main() {
    const int capacity = 100;
    vector<info> audience_info_list;

    string name, con, city;
    int reserve_n;

    int reserved_n = 0, left_n = capacity;

    cout << "录入信息:" << endl << endl;
    cout << left << setw(15) << "昵称"
        << setw(15) << "联系方式(邮箱/手机号)"
        << setw(15) << "所在城市"
        << setw(15) << "预定参加人数" << endl;

    while (cin >> name) {
        cin >> con >> city >> reserve_n;
        reserved_n += reserve_n;
        left_n -= reserve_n;
        if (left_n < 0) {
            char choice;
            cout << endl;
            cout << "对不起,只剩" << left_n + reserve_n << "个位置." << endl;
            cout << "1.输入u,更新(update)预定信息" << endl;
            cout << "2.输入q,退出预定" << endl;
            cout << "你的选择:";
            reserved_n -= reserve_n;
            left_n += reserved_n;
            cin >> choice;
            if (choice == 'u')
                continue;
            if (choice == 'q')
                break;
        }
        else {
            info audience(name, con, city, reserve_n);
            audience_info_list.push_back(audience);
        }
    }

    cout << endl;
    cout << "截至目前,一共有" << reserved_n << "位听众预定参加。";
    cout << "预定听众信息如下:" << endl;

    for (auto audience_info : audience_info_list) {
        audience_info.print();
    }
}

hpp

#pragma once
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class info {
public:
    info(string nickname0 = " ", string contact0 = " ", string city0 = " ", int n0 = 0);
    ~info() = default;
    void print() const;

private:
    string nickname;
    string contact;
    string 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 << setw(15) << "昵称:"
        << setw(15) << nickname << endl;
    cout << setw(15) << "联系方式:"
        << setw(15) << contact << endl;
    cout << setw(15) << "所在城市:"
        << setw(15) << city << endl;
    cout << setw(15) << "预订人数:"
        << setw(15) << n << endl;
    cout << endl;
}

实验6cpp

#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();
}

hpp

#pragma once
#include<iostream>
#include<string>
using namespace std;
class TextCoder
{
public:
	TextCoder(string c1) :text(c1) {}
	string get_ciphertext();
	string get_deciphertext();
private:
	string text;
	void encoder();
	void decoder();

};
void TextCoder::encoder() {
	int n = text.length();
	for (int i = 0; i < n; i++)
	{
		if (text[i] >= 'a' && text[i] <= 'z')
		{
			if (text[i] + 5 <= 'z')
			{
				text[i] = text[i] + 5;
			}
			else {
				text[i] = text[i] - 21;
			}

		}
		else if (text[i] >= 'A' && text[i] <= 'Z')
		{
			if (text[i] + 5 <= 'Z')
			{
				text[i] = text[i] + 5;
			}
			else {
				text[i] = text[i] - 21;
			}
		}
	}
}
void TextCoder::decoder() {
	int n = text.length();
	for (int i = 0; i < n; i++)
	{
		if (text[i] >= 'a' && text[i] <= 'z')
		{
			if (text[i] - 5 >= 'a')
			{
				text[i] = text[i] - 5;
			}
			else {
				text[i] = text[i] + 21;
			}
		}
		else if (text[i] >= 'A' && text[i] <= 'Z')
		{
			if (text[i] - 5 >= 'A')
			{
				text[i] = text[i] - 5;
			}
			else {
				text[i] = text[i] + 21;
			}
		}
	}
}
string TextCoder::get_ciphertext() {
	encoder();
	return text;
}
string TextCoder::get_deciphertext() {
	decoder();
	return text;
}

标签:info,string,int,text,void,实验,include
From: https://www.cnblogs.com/2003dingjiajie/p/16823652.html

相关文章

  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求1.编写Python程序,调用OpenDaylight的北向接口实现以下功能。    (1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight    (2)下发指令删除s1上......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践(一)基本要求1、编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;......
  • 实验7:基于REST API的SDN北向应用实践
    基础要求1.编写Python程序,调用OpenDaylight的北向接口实现以下功能利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;下发指令删除s1上的流表数据#delet......
  • 实验3
    task5Info.h1#pragmaonce2#include<iostream>3#include<vector>4#include<string>5usingnamespacestd;67classinfo{8public:9in......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求1.编写Python程序,调用OpenDaylight的北向接口实现以下功能调用OpenDaylight的北向接口获取拓扑信息importrequestsasrqfromrequests.authimportHTTP......
  • 实验3 数组、指针与现代C++标准库
    实验任务5:info.hpp:#include<iostream>#include<string>usingnamespacestd;classinfo{public:info(stringni,stringco,stringci,intn);voidpri......
  • 第三次实验
    实验五hpp1#pragmaonce23#include<iostream>4#include<iomanip>5usingnamespacestd;6classinfo7{89public:10info(stringnickna......
  • 实验三
    #pragmaonce#include<iostream>#include<vector>#include<string>#include<iomanip>usingnamespacestd;classInfo{public:Info(){}Info(string......
  • 实验2
    实验1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能。(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight。(2)下发指令删除s1上的流表数据#!/us......