首页 > 编程语言 >实验三 数组、指针与现代C++标准库

实验三 数组、指针与现代C++标准库

时间:2022-10-23 15:46:36浏览次数:43  
标签:Info string text C++ 数组 using include TextCoder 指针

实验五代码:

  Info.hpp

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 #include<iomanip>
 6 
 7 using std::string;
 8 using std::cout;
 9 using std::endl;
10 
11 class Info
12 {
13 public:
14     Info(string nickname0,string contact0,string city0,int n0);
15     void print() const;
16     ~Info() = default;
17 private:
18     string nickname;
19     string contact;
20     string city;
21     int n;
22 };
23 
24 Info::Info(string nickname0, string contact0, string city0, int n0)
25 {
26     nickname = nickname0;
27     contact = contact0;
28     city = city0;
29     n = n0;
30 }
31 
32 void Info::print() const//打印信息
33 {
34     cout << "昵称:            " << nickname << endl;
35     cout << "联系方式:        " << contact << endl;
36     cout << "所在城市:        " << city << endl;
37     cout << "预定人数:        " << n << endl << endl;
38 }

  task5.cpp

 1 #include"Info.hpp"
 2 #include<iostream>
 3 #include<string>
 4 #include<vector>
 5 #include<iomanip>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     const int capacity = 100;
12     vector<Info> audience_info_list;
13     cout << "录入信息:" << endl << endl;
14     cout << "昵称        " << "联系方式(邮箱/手机号)        " << "所在城市            " << "预定参加人数        " << endl;
15     string yname, ycontact, ycity;
16     int yn = 0;
17     char choice = 'u';//choice记录用户的选择
18     int sum = 0;//记录已预定的总人数
19     while (choice == 'u' && sum < capacity && cin >> yname)
20     {
21         cin >> ycontact >> ycity >> yn;
22         Info a(yname, ycontact, ycity, yn);
23         audience_info_list.push_back(a);
24         sum += yn;
25         if (sum > capacity)
26         {
27             sum -= yn;
28             audience_info_list.pop_back();
29             cout << "对不起,只剩" << capacity - sum << "个位置。" << endl;
30             cout << "1.输入u,更新(update)预定信息" << endl
31                 << "2.输入q,退出预定" << endl
32                 << "你的选择:";
33             cin >> choice;
34         }
35     }
36     cout << endl << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
37     for (auto &Info:audience_info_list)
38         Info.print();
39 }

运行截图:

 

 

 

 

实验六代码:

  textcoder.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 
 5 using std::cout;
 6 using std::string;
 7 using std::endl;
 8 
 9 class TextCoder
10 {
11 public:
12     TextCoder(string text0) :text{ text0 } {};
13     TextCoder(TextCoder& T) :text{ T.text } {};//构造函数
14     string get_ciphertext();//调用encoder加密文本,并返回加密后的
15     string get_deciphertext();//调用decoder解密文本,并返回解密后的
16 private:
17     string text;
18     void encoder();//加密文本
19     void decoder();//解密文本
20 };
21 
22 string TextCoder::get_ciphertext()//加密并返回
23 {
24     encoder();
25     return text;
26 }
27 
28 string TextCoder::get_deciphertext()//解密并返回
29 {
30     decoder();
31     return text;
32 }
33 
34 void TextCoder::encoder()//加密
35 {
36     for (int i = 0; i < text.size(); i++)
37     {
38         if (isupper(text.at(i)))    text.at(i) = (text.at(i) - 60) % 26 + 65;
39         else if (islower(text.at(i)))    text.at(i) = (text.at(i) - 92) % 26 + 97;
40     }
41 }
42 
43 void TextCoder::decoder()//解密
44 {
45     for (int i = 0; i < text.size(); i++)
46     {
47         if (isupper(text.at(i)))    text.at(i) = (text.at(i) - 44) % 26 + 65;
48         else if (islower(text.at(i)))    text.at(i) = (text.at(i) - 76) % 26 + 97;
49     }
50 }

  task6.cpp

 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 
 5 void test()
 6 {
 7     using namespace std;
 8 
 9     string text, encoded_text, decoded_text;
10 
11     cout << "输入英文文本: ";
12     while (getline(cin, text))
13     {
14         encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象
15             cout << "加密后英文文本:\t" << encoded_text << endl;
16         decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
17             cout << "解密后英文文本:\t" << decoded_text << endl;
18         cout << "\n输入英文文本: ";
19     }
20 }
21 int main()
22 {
23     test();
24 }

运行截图:

 

 

   

标签:Info,string,text,C++,数组,using,include,TextCoder,指针
From: https://www.cnblogs.com/jane-d/p/16818681.html

相关文章

  • C/C++ 一维数组和二维数组参数传递的几种方式
    一维数组:#include<iostream>#include<windows.h>#include<string>usingnamespacestd;//在以下几种方法中,ages都不是真正的数组,实际上是一个指针int*agesint......
  • c++dump
    //Minidump.h#pragmaonceclassCMinidump{public: CMinidump(); ~CMinidump(); staticvoidCreateDumpFile(LPCSTRlpstrDumpFilePathName,EXCEPTION_POINTERS......
  • MD5 哈希加密算法 - C++ 实现
    MD5加密算法-C++实现写在前头:还在学习中!整个文档写的很匆忙,肯定还有很多不周到的地方.欢迎在评论中提出你的宝贵意见!!算法背景BackgroundMD5消息摘要算法......
  • Java 实例 - 数组差集
    https://www.runoob.com/java/arrays-removeall.htmlhttps://blog.csdn.net/weixin_43552143/article/details/124878630......
  • c++对象的拷贝、构造、虚构
    抽象基类​ 现有如下代码:classAbstract_base{public:virtual~Abstract_base()=0;virtualvoidinterface()const=0;virtualconstchar*mumble......
  • 大规模C++软件开发 卷1 过程与架构 电子书 pdf
    链接:大规模C++软件开发卷1过程与架构  本书通过具体示例演示了基本的设计概念,为各种规模的项目奠定了基础,并演示了成功进行大规模实际开发所需的过程、方法、技......
  • 实验3 数组、指针与现代C++标准库
    5.实验任务5info.hpp#pragmaonce#include<iostream>#include<string>#include<iomanip>usingstd::string;usingstd::cout;usingstd::endl;classinfo{......
  • 数据结构 玩转数据结构 3-5 数组队列
    0课程地址https://coding.imooc.com/lesson/207.html#mid=13422 1重点关注1.1队列的特性FIFO,先进先出,水管 1.2队列的实现参考......
  • C++基础
    C++基础VS快捷键Ctrl+或-跳转到上次鼠标焦点位置CtrlKF按住Ctrl然后按K然后按FCtrlJ代码提示变量声明方式:数据类型变量名=变量初始值#include<iostre......
  • C/C++ 中的几种调试技巧(待续)
    1、利用#define宏定义作为调试开关include<stdio.h>defineDEBUG//可以注释或打开输出intmain(void){inti,sum;for(i=1,sum=0;i<=5;i++){......