首页 > 编程语言 >实验4 现代C++标准库与类模板

实验4 现代C++标准库与类模板

时间:2023-11-27 10:11:06浏览次数:29  
标签:Info string int text C++ 实验 include TextCoder 模板

实验任务5
TextCoder.hpp源码

 1 #include<iostream>
 2 #include<string>
 3 
 4 using std::string;
 5 
 6 class TextCoder {
 7     private:
 8         string text;
 9         void encoder();
10         void decoder();
11     public:
12         TextCoder(string &str);
13         TextCoder(const TextCoder &t);
14         string get_ciphertext();
15         string get_deciphertext();
16 };
17 TextCoder::TextCoder(string &str)
18     : text{str} {
19 }
20 TextCoder::TextCoder(const TextCoder &t)
21     : text{t.text} {
22 }
23 void TextCoder::encoder() {
24     for (auto &i : text) {
25         if (i >= 'a' && i <= 'z')
26             i = 'a' + ((i - 'a') + 7) % 26;
27         else if (i >= 'A' && i <= 'Z')
28             i = 'A' + ((i - 'A') + 7) % 26;
29     }
30 }
31 void TextCoder::decoder() {
32     for (auto &i : text) {
33         if (i >= 'a' && i <= 'z')
34             i = 'a' + ((i - 'a') + 26 - 7) % 26;
35         else if (i >= 'A' && i <= 'Z')
36             i = 'A' + ((i - 'A') + 26 - 7) % 26;
37     }
38 
39 }
40 string TextCoder::get_ciphertext() {
41     encoder();
42     return text;
43 }
44 string TextCoder::get_deciphertext() {
45     decoder();
46     return text;
47 }
View Code task5.cpp源码
 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     string text, encoded_text, decoded_text;
 9 
10     cout << "输入英文文本: ";
11     while (getline(cin, text)) {
12         encoded_text = TextCoder(text).get_ciphertext();  // 这里使用的是临时无名对象
13         cout << "加密后英文文本:\t" << encoded_text << endl;
14 
15         decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象
16         cout << "解密后英文文本:\t" << decoded_text << endl;
17         cout << "\n输入英文文本: ";
18     }
19 }
20 
21 int main() {  
22     test(); 
23 }
View Code

运行测试截图


实验任务6
Info.hpp文件源码

 1 #include<iostream>
 2 
 3 using std::string;
 4 
 5 class Info {
 6     private:
 7         string nickname;
 8         string contact;
 9         string city;
10         int n;
11     public:
12         Info(string&,string&,string&,int&);
13         void print();
14 };
15 Info::Info(string &nickname,string &contact,string &city,int &n) {
16     this->nickname = nickname;
17     this->contact = contact;
18     this->city = city;
19     this->n = n;
20 }
21 void Info::print() {
22     std::cout<<"昵称:\t\t"<<nickname<<std::endl;
23     std::cout<<"联系方式:\t"<<contact<<std::endl;
24     std::cout<<"所在城市:\t"<<city<<std::endl;
25     std::cout<<"预定人数:\t"<<n<<std::endl;
26 }
View Code

task6.cpp源码

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include"Info.hpp"
 5 using namespace std;
 6 
 7 int main() {
 8     const int capacity = 100;
 9     vector<Info> audience_info_list;
10     vector<Info> &v = audience_info_list;
11     int sum = 0;
12 
13     cout << "录入信息:\n\n";
14     cout << "昵称\t\t";
15     cout << "联系方式(邮箱/手机号)\t\t";
16     cout << "所在城市\t";
17     cout << "预定人数" << endl;
18     string nickname, contact, city;
19     int n;
20     while (cin >> nickname >> contact >> city >> n) {
21         if (sum + n <= capacity) {
22             Info a(nickname, contact, city, n);
23             v.push_back(a);
24             sum += n;
25         }
26         else {
27             cout << "对不起,只剩" << capacity - sum << "个位置。\n";
28             cout << "1.输入u,更新(updata)预定信息" << endl;
29             cout << "2.输入q,退出预定" << endl;
30             cout << "你的选择:";
31 
32             char c;
33             cin >> c;
34             if (c == 'q') break;
35             else if (c == 'u') continue;
36             else break;
37         }
38         if (sum == capacity) break;
39     }
40     cout << endl;
41 
42     cout << "截至目前,一共有" << sum << "位听众预定参加。";
43     if (sum != 0) {
44         cout << "预定听众信息如下:" << endl;
45         for (int i = 0; i < v.size(); i++) {
46             v[i].print();
47             cout << endl;
48         }
49     }
50 }
View Code

运行测试截图

 

标签:Info,string,int,text,C++,实验,include,TextCoder,模板
From: https://www.cnblogs.com/ffhfAdjFH7Vr/p/17858212.html

相关文章

  • 设计模式实验
    软件设计                 石家庄铁道大学信息学院 实验21:观察者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解观察者模式的动机,掌握该模式的结构;2、能够利用观察者模式解决实际问题。    [实验任务一]:股票提醒......
  • C++ 服务端与 Java 客户端的简单连接
    记录一下如何用两种语言简单通信,(其实也大差不差的,应该把。。。)//C++服务端#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<iostream>#include<cstring>usingstd::cout;usingstd::endl;usingst......
  • C语言调用C++类成员函数讲解和实例
    原文:https://blog.csdn.net/LxXlc468hW35lZn5/article/details/1257010071、问题成因C语言与C++调用问题原因主要在于C编译器和C++编译器的不同。C是过程式语言,C编译器编译后,函数在符号库中就是函数名,没有其他任何附加信息。而C++是对象式语言,支持函数重载,C++编译器编译后,在符号......
  • 【C与C++的相互调用方法】
    原文:https://blog.csdn.net/qq_43899283/article/details/132343699C与C++的相互调用方法C与C++为什么相互调用的方式不同C++中调用CC中调用C++致谢C与C++为什么相互调用的方式不同  C和C++之间的相互调用方式存在区别,主要是由于C和C++语言本身的设计和特性不同。函数......
  • extern "C":实现C++和C的混合编程
    原文:https://c.biancheng.net/view/8064.html通过《C语言和C++到底有什么关系?》一节的学习,读者已经了解了C++和C语言之间的关系。简单的理解,C++就是在C语言的基础上增加了一些新特性,从大的方面讲,C++不仅支持面向过程编程,还支持面向对象编程和泛型编程;从小的方面讲,C++还......
  • #P1042. 静态RMQ[ST表模板]
    题意是:给定一个长度为N的数列,和M次询问,求出每一次询问的区间内数字的最大值。ST表的基本功能是对区间进行查询,其核心使用的是倍增的思想f[i][k]:意思是从第i个数开始往后2^k个数f[i][k]=max(f[i][k-1],f[i+2^k-1][k-1])求【l,r】区间max(f[i][k],f[r-2^k+1][k])#define......
  • 编译原理实验
      熟了词法分析、语法分析、语义分析进行整合,构造完整的应用程序。熟悉了软件的重构。......
  • C++U3-第2课-基础排序(二)
    上节课作业讲师视频分享链接:百度云网盘链接:https://pan.baidu.com/s/1PFBLFdX6C-9FhKXWrhDBew?pwd=l8r3提取码:l8r3本节课教学目标 插入排序概念 插入排序的代码和思路分析  插入代码详细解释【题意分析】1.从第一个元素开始,该元素可以认为已经被排序;2.取出下......
  • C++11以及17部分特性
    1//1、并发支持2//1.1、C++11内存模型:3//a.原子性(Atomicity):对于原子类型(std::atomic),其成员函数的操作是原子的,不会被其他线程中断。4//b.可见性(Visibility):对于非原子类型,通过使用互斥量或同步操作来确保共享数据的可见性,即在一个线程中对共享数据的......
  • Flask 使用Jinja2模板引擎
    Jinja2,由Flask框架的创作者开发,是一款功能丰富的模板引擎,以其完整的Unicode支持、灵活性、高效性和安全性而备受推崇。最初受Django模板引擎启发,Jinja2为Flask提供了强大的模板支持,后来也成为其他项目的首选。在本文中,我们将深入探讨Jinja2的特性、语法以及如何在Flask应用中使用......