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

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

时间:2023-11-27 23:00:38浏览次数:30  
标签:string int text C++ 实验 && include 模板 cout

实验任务5:
1.代码:
textcoder.hpp:

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <vector>
 5 #include <array>
 6 #include <string>
 7 using namespace std;
 8 
 9 class TextCoder
10 {
11     private:
12         string text;
13         void encoder()
14         {
15             for(auto i=0; i<text.size(); i++) {  
16             if(text.at(i) >= 'a' && text.at(i) <= 'z') {  
17              text.at(i) = 'a' + (text.at(i) - 'a' + 7) % 26;   
18         } 
19             else if(text.at(i) >= 'A' && text.at(i) <= 'Z') {  
20             text.at(i) = 'A' + (text.at(i) - 'A' + 7) % 26; 
21         }  
22     }  
23         }
24         void decoder()
25         { 
26             for(auto i=0; i<text.size(); i++) {  
27              if(text.at(i) >= 'a' && text.at(i) <= 'z') {  
28             text.at(i) = 'a' + (text.at(i)-'a' +26- 7) % 26; 
29         } 
30             else if(text.at(i) >= 'A' && text.at(i) <= 'Z') {  
31             text.at(i) = 'A' + (text.at(i) -'A'+26 - 7) % 26; 
32         }  
33     }  
34         }
35     public:
36         string get_ciphertext()
37         {
38             encoder();
39             return text;
40         }
41         string get_deciphertext()
42         {
43             decoder();
44             return text;
45         }
46         TextCoder(string a):text{a}{}
47         TextCoder(const TextCoder& b):text{b.text}{}
48         
49         
50     
51     
52 };
View Code

textcoder.cpp:

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

2.图片:

 

实验任务6:
1.代码:
Info.hpp:

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

task6.cpp:

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

 

 

2.图片:

 

标签:string,int,text,C++,实验,&&,include,模板,cout
From: https://www.cnblogs.com/yili123/p/17854634.html

相关文章

  • 支持修改键值的优先队列(以C++,Java为例)
    #include<queue>#include<functional>template<typenameT1,typenameT2>classmutable_priority_queue;template<typenameT1,typenameT2>classmutable_priority_queue{private:std::function<bool(conststd::pair<T1,T......
  • C++ vs Python
    WhyC++isfasterthanPythonhttps://www.freecodecamp.org/news/python-vs-c-plus-plus-time-complexity-analysis/SummaryTable编程语言stronglytyped?跨平台语言类型C++YesYes编译型PythonNoYes解释型参考资料stronglytypedprogrammingla......
  • 实验5
    一、1-1、①使最大值、最小值赋值到对应指针变量        ②全部指向X首元素地址    1-2、①最大值地址        ②可以二、2-1、①24   数组s1占用字节数  数组s1长度        ②不能 数组s1没有初值和大......
  • 模板的导入和继承
    1模板的导入 -第一步:新建一个xx.html,把好看的模板写入<divclass="panelpanel-danger"><divclass="panel-heading"><h3class="panel-title">重金求子</h3></div><......
  • P3375 【模板】KMP( 普及/提高− ) 题解
    题目传送门思路:首先我们要学习一下\(KMP\)算法,不会的可以看这个大佬的文章那么我们就直接开始讲思路了。针对于每一位,\(kmp\)算法已经预处理出了一个对应\(kmp\)数组的单元,映射着如果此位失配,它可能的最靠后的一个重新开头是哪一个。让我们举一个例子:假如让\(aaab\)与......
  • 实验5
    任务1#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pmax);intmain(){inta[N];intmin,max;printf("录入%d个数据:\n",N);input(a......
  • C++标准库类std::shared_future
    std::shared_future是C++11的标准库类,其与std::future的不同是允许多个线程使用,多个线程可以同步共享,同时其又不会阻塞等待异步任务的完成。std::shared_future同样也提供get()方法用于获取异步执行的结果。#include<iostream>#include<thread>#include<future>void......
  • 实验五
    实验一找到最大最小的数;都指向x(a)数组首元素;找出数组中最大值;不可以,作用域结束,函数内的数组所占内存空间释放,指针变为野指针;实验二:s1的大小是24;sizeof是数组所占字节数;strlen统计字符个数不算‘\0’;不可以,s1的地址是const类型,而后面的字符串返回的是他的地址,s1不可改变无法......
  • 实验4 现代C++标准库与类模板
    实验任务5TextCoder.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classTextCoder{public:TextCoder(stringtext0):text{text0}{};stringget_ciphertext();stringget_deciphertext();private:s......
  • IT 运维服务规范(模板)
    一、总则本部分规定了IT运维服务支撑系统的应用需求,包括IT运维服务模型与模式、IT运维服务管理体系、以及IT运维服务和管理能力评估与提升途径。二、参考标准下列文件中的条款通过本部分的引用而成为本部分的条款。凡是注日期的引用文件,其随后所有的修改单(不包括勘误的内......