实验3数组、指针与现代c++标准库
一、task5
1.源代码:
(1)Info.cpp
#include<iostream> #include<string> #include<iomanip> using namespace std; class info { public: info(string nickname0, string contact0, string city0, int m) :nickname{ nickname0 }, contact{ contact0 }, city{ city0 }, n{ m } {} void print(); private: string nickname,contact,city; int n = 0; }; void info::print() { cout << "昵称:" <<"\t"<< nickname << endl; cout << "联系方式:" << "\t" << contact << endl; cout << "所在城市:" << "\t" << city << endl; cout << "预定人数:" << "\t" << n << endl; cout << endl; }
(2)task5.cpp
#include "info.cpp" #include <iostream> #include<vector> #include<iomanip> int main() { const int capacity = 100; vector<info>audience_info_list ; string nickname, contact, city; int sum_people = 0,n=0; char flag1; cout << "录入信息:" << endl; cout << "昵称" << " 联系方式(邮箱/手机号)" <<" 所在城市" <<" 预定参加人数" << endl; while (cin >> nickname) { cin >> contact >> city >> n; sum_people +=n; if (sum_people > capacity) { cout << "对不起,只剩下:" << capacity - (sum_people - n) << "个位置了." << endl;; cout << "1.输入u,更新(update)预定信息." << endl; cout << "2.输入q,退出预定." << endl; cout << "你做出的选择是:" ; cin >> flag1; if (flag1 == 'u') { sum_people -= n; continue; } else if (flag1 == 'q') break; } info x(nickname,contact,city,n); audience_info_list.push_back(x); } if(sum_people>capacity) cout << "截至目前,一共有" << sum_people-n << "位听众预定参加,预定听众信息如下:" << endl; else cout << "截至目前,一共有" << sum_people << "位听众预定参加,预定听众信息如下:" << endl; for (auto i = 0; i < audience_info_list.size(); ++i) audience_info_list[i].print(); }
2.运行结果:
二、task6
1.源代码
(1)textcoder.hpp
#pragma once #include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string text0):text{ text0}{} ~TextCoder()=default; string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; void TextCoder::encoder(){ for(auto &i:text){ if(i>='a'&&i<='z') { if(i<='u')i=i+5; else i='a'+i-'u'; } else if(i>='A'&&i<='Z') { if(i<'U')i=i+5; else i='A'+i-'u'; }}} void TextCoder::decoder(){ for(auto &i:text){ if(i>='a'&&i<='z') { if(i>='f')i=i-5; else i='u'+i-'a'; } else if(i>='A'&&i<='Z') { if(i>='F')i=i-5; else i='U'+i-'A'; }}} string TextCoder::get_ciphertext(){ encoder(); return text; } string TextCoder::get_deciphertext(){ decoder(); return text; }
(2)task6.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(); }
2.运行结果
标签:info,string,text,void,实验,include,TextCoder From: https://www.cnblogs.com/xiaoshuai66/p/16821187.html