实验2 数组、指针与C++标准库
2021/11/2 20:42:43
本文主要是介绍实验2 数组、指针与C++标准库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
实验任务5:
Info.hpp
#ifndef INFO_HPP #define INFO_HPP #include<iostream> #include<iomanip> #include<string> using namespace std; class Info{ public: Info(string nickname_, string contact_, string city_, int n_): nickname(nickname_), contact(contact_), city(city_), n(n_){} void print() const; private: string nickname; string contact; string city; int n; }; void Info::print() const{ cout << "称呼:\t\t" << nickname <<endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预定人数:\t" << n << endl; } #endif
task5.cpp
#include "Info.hpp" #include<iostream> #include<vector> int main(){ using namespace std; vector<Info> audience_info_list; string nickname; string contact; string city; int n; int total = 0; cout << "录入信息:" << endl; cout << endl; cout << "称呼/昵称, 联系方式(邮箱/手机号), 所在城市, 预定参加人数" << endl; const int capacity = 100; while(cin>>nickname>>contact>>city>>n){ total += n; if(total>capacity){ cout << "对不起, 只剩" << capacity-(total-n) << "个位置." << endl; cout << "1. 输入u, 更新(update)预定信息" << endl; cout << "2. 输入q, 退出预定" << endl; cout << "你的选择: "; total -= n; char flag; cin >> flag; if(flag == 'u'){ continue; } else if(flag == 'q'){ break; } } Info info(nickname, contact, city, n); audience_info_list.push_back(info); if(total == 100){ break; } } cout<<endl; cout << "截至目前,一共有" << total << "位听众预定参加.预定听众信息如下:" << endl; for(vector<Info>::iterator it = audience_info_list.begin(); it != audience_info_list.end(); it++) it->print(); }
运行测试结果:
实验任务6:
textcoder.hpp
#ifndef TEXTCODER #define TEXTCODER #include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string text_):text(text_){} string encoder(); string decoder(); private: string text; }; string TextCoder::encoder(){ for(string::iterator it = text.begin(); it!=text.end(); it++){ if (*it >= 118&&*it<=122) *it -= 21; else if (*it >= 86 && *it <= 90) *it -= 21; else if((*it>=97&&*it<=117)||(*it>=65&&*it<=85)) *it += 5; } return text; } string TextCoder::decoder() { string::iterator it = text.begin(); for (;it != text.end();it++) { if (*it <= 69&&*it>=65) *it += 21; else if (*it >= 97 && *it <= 101) *it += 21; else if((*it>=70&&*it<=90)||(*it>=102&&*it<=122)) *it -= 5; } 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输入英文文本: "; } }
运行测试结果:
这篇关于实验2 数组、指针与C++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享
- 2024-06-06基于 casdoor 的 ELK 开源登录认证解决方案: elk-auth-casdoor-icode9专业技术文章分享
- 2024-05-29Elasticsearch慢查询日志配置
- 2024-05-29揭秘华为如此多成功项目的产品关键——Charter模板
- 2024-05-29海外IDC业务拓展的7大挑战
- 2024-05-29InLine Chat功能优化对标Github Copilot,CodeGeeX带来更高效、更直观的编程体验!
- 2024-05-29CodeGeeX 智能编程助手 6 项功能升级,在Visual Studio插件市场霸榜2周!
- 2024-05-29AutoMQ 生态集成 Apache Doris
- 2024-05-292024年IDC行业的深度挖掘:机遇、挑战与未来展望
- 2024-05-29五款扩展组件齐发 —— Volcano、Keda、Crane-scheduler 等,邀你体验