实验二 数组,指针与C++标准库
2021/10/30 20:41:53
本文主要是介绍实验二 数组,指针与C++标准库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
info.hpp文件源码
#include <iostream> #include <string> using namespace std; class info { public: info(){}; info(string nick, string con, string c, int n1); void print() { cout << "昵称: " << nickname << endl; cout << "联系方式: " << contact << endl; cout << "所在城市: " << city << endl; cout << "预定参加人数:" << n << endl; } private: string nickname; string contact; string city; int n; }; info::info(string nick, string con, string c, int n1) { nickname = nick; contact = con; city = c; n = n1; }
task5.cpp文件源码
#include <iostream> #include <string> #include <vector> #include "info.hpp" using namespace std; int main() { vector<info> audience_info_list; const int capacity = 100; string nickname, contact, city; //昵称,联系方式,住址 int n; //预约人数 int count = 0; //记录目前有多少人 cout << "录入信息:" << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; while (cin >> nickname) { cin >> contact >> city >> n; int k = capacity - count; count += n; if (count >= capacity) { cout << "对不起,只剩" << k << "个位置" << endl; cout << "1.输入u,更新预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; char choice; cin >> choice; if (choice == 'u') { count -= n; continue; } else if (choice == 'q') { count -= n; break; } } info audience(nickname, contact, city, n); audience_info_list.push_back(audience); } cout << "截至目前,一共有" << count << "位听众预定参加,预定听众信息如下:" << endl; for (int i = 0; i < audience_info_list.size(); i++) audience_info_list[i].print(); return 0; }
结果:
textcoder.hpp文件源码
#include <iostream> #include <string> using namespace std; class textcoder { public: textcoder(string n_text); ~textcoder() = default; string encoder() //加密 { for (int i = 0; i < text.size(); i++) { // cout << text[i] << endl; if ((text[i] >= 'a' && text[i] <= 'u') || (text[i] >= 'A' && text[i] <= 'U')) text[i] = text[i] + 5; else if ((text[i] >= 'v' && text[i] <= 'z') || (text[i] >= 'V' && text[i] <= 'Z')) text[i] = text[i] - 21; } return text; } string decoder() //解密 { for (int i = 0; i < text.size(); i++) { if ((text[i] >= 'f' && text[i] <= 'z') || (text[i] >= 'F' && text[i] <= 'Z')) text[i] = text[i] - 5; else if ((text[i] >= 'a' && text[i] <= 'e') || (text[i] >= 'A' && text[i] <= 'E')) text[i] = text[i] + 21; } return text; } private: string text; }; textcoder::textcoder(string n_text) { text = n_text; }
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输入英文文本: "; } }
结果:
这篇关于实验二 数组,指针与C++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-07fastcgi 是什么-icode9专业技术文章分享
- 2024-10-07fastcgi 的详细使用教程介绍-icode9专业技术文章分享
- 2024-10-07git如何更新单个文件到本地-icode9专业技术文章分享
- 2024-10-07如何使用ASM(Abstract Syntax Tree Manipulation)技术来修改第三方AAR依赖中的函数-icode9专业技术文章分享
- 2024-10-07Activity 跳转时间耗时很长怎么优化解决-icode9专业技术文章分享
- 2024-10-07Androud Toast 有哪些常用的第三方组件-icode9专业技术文章分享
- 2024-10-07在viewmodel中怎么使用 mmkv?-icode9专业技术文章分享
- 2024-10-07MMKV.defaultMMKV() 是单例模式吗?-icode9专业技术文章分享
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享