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


info.hpp文件源码

#ifndef INFO_HPP
#define INFO_HPP
#include<iostream>
#include<iomanip>
using namespace std;
class info {
public:
	info(string &a,string &b,string &c,int &d):nickname(a),contact(b),city(c),n(d){};
	void print() {
		cout << left << setw(15) << "称呼:"<< nickname << endl;
		cout << left << setw(15) << "联系方式:" << contact << endl;
		cout << left << setw(15) << "所在城市:" <<city << endl;
		cout << left << setw(15) << "预定人数:" << n << endl;
	};
private:
	string nickname;
	string contact;
	string city;
	int n;
};
void choose_() {
	cout << "1.输入u,更新(update)预定商品" << endl;
	cout << "2.输入q,退出预定" << endl;
	cout << "你的选择:";
}
#endif

task5.cpp文件源码

#include <iostream>
#include "info.hpp"
#include <vector>
int main() {
    using namespace std;
    vector<info> audience_info_list;
    string name, contact, city;
    int n;
    char choose;
    const int volume0 = 100;
    int volume = 0;       
    cout << "录入信息:\n\n"
        "称呼/昵称       联系方式(邮箱/手机号)   所在城市       预定参加人数" << endl;
    while (cin >> name >> contact >> city >> n) {
        if (volume + n > volume0) {
            cout << "对不起,只剩" << volume0 - volume << "个位置." << endl;
            choose_();
            cin >> choose;
            if (choose == 'q')break;
        }
        else {
            volume += n;
            audience_info_list.push_back(info(name, contact, city, n));
        }
    }
    cout << "\n截至目前,一共有" << volume << "位听众预定参加。预定听众信息如下:" << endl;
    for (auto &m : audience_info_list) {
        m.print();
    }
    return 0;
}

测试截图:



TextCoder.hpp文件源码

#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
#include<iostream>
using namespace std;
class TextCoder{
public:
	TextCoder(string& s) :text(s) {};
	string encoder() ;
	string decoder() ;
private:
	string text;
};
const int n = 5;
string TextCoder::encoder() {
	for (auto& i : text) {
		if (i >= 'a' && i <= 'z') i = (i - 'a' + n) % 26 + 'a';
		if (i >= 'A' && i <= 'Z') i = (i - 'A' + n) % 26 + 'A';
	}
	return text;
}
string TextCoder::decoder() {
	for (auto& i : text) {
			if (i >= 'a' && i <= 'z') i = (i - 'a' + 26 - n) % 26 + 'a';
			if (i >= 'A' && i <= 'Z') i = (i - 'A' + 26 - n) % 26 + 'A';
		}
	return text;
}
#endif

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

测试截图:

实验总结:
1、熟悉了范围for的用法
2、熟悉了动态数组类模板vector
3、可以用while循环实现重复录入,直到按下组合键 Ctrl+Z结束循环

posted @ 2021-10-31 20:43  椿去楸来  阅读(41)  评论(3)    收藏  举报