实验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, ...
随机推荐
- Android studio学习笔记2
Android studio学习笔记2 20201303张奕博 2023.1.14 android studio动态调试apk 1.配置环境 android studio需要安装插件:1,Smalid ...
- 【剑指Offer】【链表】链表中环的入口结点
题目:给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. A:创建两个指针,一个pFast一个pSlow指向链头,pFast一次走2步,pSlow一次走1步,如果两个指针必相遇 ...
- JS数组的交集与差集
有两个数组arr1,arr2 实现arr2中去除arr1相同的元素 e.g arr1=[1,2,3] arr2=[2,3,4] ===> result = [4] 实现 获取两个数组(arr1, ...
- 几款Android 应用自动化测试工具
本文转自:https://blog.csdn.net/hebbely/article/details/78901466 简述: 本文介绍几款流行的 Android应用自动化测试工具. Monkey测试 ...
- js常用遍历理解
1.for循环用于数组的遍历循环. 2.for in 循环主要用于遍历普通对象,i 代表对象的 key 值,a[i] 代表对应的 value. 3.forEach循环 遍历数组中的每一项,没有返回值, ...
- 实验 四 [bx]和loop的使用
1. 综合使用 loop,[bx],编写完整汇编程序,实现向内存 b800:07b8 开始的连续 16 个 字单元重复填充字数据0403H. 代码 assume cs:code code segme ...
- vscode python可以运行,无法debug
参考:https://blog.csdn.net/weixin_44646187/article/details/125810974 提示'cmd' 不是内部或外部命令,也不是可运行的程序 或批处理文 ...
- c++ 保存txt文件
#include <iostream> #include <stdio.h> #include <fstream> #include <queue> # ...
- uniapp调起微信支付查询订单状态逻辑处理
首先看页面效果: <template> <view class="page"> <view class="page-bd"> ...
- 一,创建一个electron应用程序
之前我们已经用html+css+js创建了一个项目,现在将这个项目用electron以应用程序呈现. 1,首先新建一个文件夹,从终端进入该文件夹: 2,在该文件夹下执行npm init,初始化该项目. ...