实验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++标准库的更多相关文章
- C++标准库(体系结构与内核分析)(侯捷第二讲)
一.OOP和GP的区别(video7) OOP:面向对象编程(Object-Oriented programming) GP:泛化编程(Generic programming) 对于OOP来说,我们要 ...
- C++标准库分析总结(四)——<Vector、Array、Forward_list设计原则>
本节主要总结标准库Vector和Array的设计方法和特性以及相关迭代器内部特征 1.Vector 1.1 Vector 内部实现 Vector是自增长的数组,其实在标准库中没有任何一种容器能原地扩充 ...
- c/c++ 标准库 智能指针( smart pointer ) 是啥玩意儿
标准库 智能指针( smart pointer ) 是啥玩意儿 一,为什么有智能指针??? c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露. 智能指针可以帮助程序员"自 ...
- 怎么使用C++标准库来实现二维数组
在编程里,像界面布局是二维的,那么常常使用二维数组来表示界面的元素,那么就需要使用二维的数组,在现在C++肯定是以标准库为基础了,不再使用C的二维数组,那么怎么样做呢?下面就使用vector来实现二维 ...
- C++ 标准库智能指针
整理一下c++中shared_ptr,weak_ptr,unique_ptr三种指针的使用案例和注意事项,让程序资源更加案例,在标准库中,需要包含<memory>,在boost库中, 一. ...
- 标准库中的智能指针shared_ptr
智能指针的出现是为了能够更加方便的解决动态内存的管理问题.注:曾经记得有本书上说可以通过vector来实现动态分配的内存的自动管理,但是经过试验,在gcc4.8.5下是不行的.这个是容易理解的,vec ...
- KEIL-C下数组指针与指针数组实验
http://blog.csdn.net/men_wen/article/details/52694069 第一个: 数组指针的小实验 用指针传递参数 结果: 第二个: 数组指针实验 定义一个指针 ...
- C语言的本质(24)——C标准库之输入与输出(下)
4.读写二进制文件 C语言还提供了用于整块数据的读写函数.可用来读写一组数据,如一个数组元素,一个结构变量的值等. 读数据块函数调用的一般形式为: fread(buffer,size,count,fp ...
- C++著名类库和C++标准库介绍
C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...
- PHP SPL(PHP 标准库)
一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...
随机推荐
- 2019-2020-1 20199318《Linux内核原理与分析》第八周作业
第7章 可执行程序工作原理 一.学习笔记 1.ELF 2.程序编译 3.连接与库 二.试验记录 1.开始先更新内核,再用test_exec.c将test.c覆盖掉 2.test.c文件中增加了exec ...
- OSP6部署流程
准备4台虚拟机,完成初始化 一.架构如下: Controller 控制节点 也可以复用为计算节点 192.168.6.11 Compute01 192.168.6.21 Compute02 ...
- 替代学习物联网-云服务-03腾讯云MQTT
1.登录(利用微信) https://console.cloud.tencent.com/iothub 2.新建产品 3.添加设备 4.设备详细参数 域名IP固定: iotcloud-mqtt.gz. ...
- java8中CompletableFuture异步处理超时
java8中CompletableFuture异步处理超时的方法 Java 8 的 CompletableFuture 并没有 timeout 机制,虽然可以在 get 的时候指定 timeout,但 ...
- mysql 设置外键约束SET FOREIGN_KEY_CHECKS=1
问题描述:Mysql中如果表和表之间建立的外键约束,则无法删除表及修改表结构 解决方法: 在Mysql中取消外键约束: SET FOREIGN_KEY_CHECKS=0; 然后将原来表的数据导出到sq ...
- vscode python配置pip源
更换到国内清华园 C盘-User-用户名-新建"pip"文件夹-新建"pip.ini"文件 pip.ini 文件中内容: [global]index-url = ...
- C# 使用Enumerable.Range 打印数字
static void Main(string[] args) { var list1 = Enumerable.Range(0, (int)Math.Pow(2, 22)).ToList(); va ...
- centos7中通过源码安装postgresql13.6
下载地址:https://www.postgresql.org/ftp/source/ 0.安装相关依赖库 centos依赖包下载地址:https://developer.aliyun.com/pac ...
- pwm 理解
PWM: 假设PWM的时钟主频是 PWM_CLK_FREQ Hz,则如果需要输出频率为 xHz,占空比为 y% 的波形时, 则只需要在定时器的周期寄存器中写入(PWM_CLK_FREQ / x),在 ...
- C语言II—作业03
1.作业头 这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-3 这个作业要求在哪里 https://edu.cnblogs.com/cam ...