首页 > 其他分享 >实验四

实验四

时间:2023-12-13 17:44:49浏览次数:25  
标签:info string text void 实验 include TextCoder

task5源代码:

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 
 6 using namespace std;
 7 
 8 class TextCoder {
 9     public:
10         TextCoder() = default;
11         TextCoder(string str);
12         string get_ciphertext();
13         string get_deciphertext();
14         ~TextCoder() = default;
15     
16     private:
17         string text;
18         void encoder();
19         void decoder();
20 };
21 
22 TextCoder::TextCoder(string str) {
23     text = str;
24 }
25 
26 string TextCoder::get_ciphertext() {
27     encoder();
28     return text;
29 }
30 
31 string TextCoder::get_deciphertext() {
32     decoder();
33     return text;
34 }
35 
36 void TextCoder::encoder() {
37     for(int i = 0;i<text.length();i++) {
38         if(islower(text[i])) {
39             text.replace(i, 1, 1, (text[i] - 'a' + 7) % 26 + 'a');
40         }
41         else if(isupper(text[i])) {
42             text.replace(i, 1, 1, (text[i] - 'A' + 7) % 26 + 'A');
43         }
44     }
45 }
46 
47 void TextCoder::decoder() {
48     for(int i = 0;i<text.length();i++) {
49         if(islower(text[i])) {
50             text.replace(i, 1, 1, (text[i] - 'a' + 26 - 7) % 26 + 'a');
51         }
52         else if(isupper(text[i])) {
53             text.replace(i, 1, 1, (text[i] - 'A' + 26 - 7) % 26 + 'A');
54         }
55     }
56 }
TextCoder.hpp
 1 #include<iostream>
 2 #include<string>
 3 #include"TextCoder.hpp"
 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         decoded_text = TextCoder(encoded_text).get_deciphertext(); //这里使用的是临时无名对象
15         cout << "解密后英文文本:\t" << decoded_text << endl;
16         cout << "\n输入英文文本:";
17     }
18 }
19 
20 int main() {
21     test();
22 }
task5.cpp

 

task5运行结果:

 

task6源代码:

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

 

task6运行结果:

 

 

标签:info,string,text,void,实验,include,TextCoder
From: https://www.cnblogs.com/atry/p/17899585.html

相关文章

  • TSINGSEE青犀基于EasyCVR与AI技术的高校实验室视频可视化监管方案
    一、行业背景实验室作为科研、教学过程中的一个重要场所,其管理也十分至关重要。尤其是高校实验室安全问题,教育部《高等学校实验室安全规范》中说明,需要进一步加强高校实验室的安全管理工作,实现规范化、常态化的管理体制,以最大限度地减少实验室安全事故的发生,保障校园师生的生命安......
  • 实验6 模板类、文件I/O和异常处理
    实验任务1源代码:#pragmaonce#include<iostream>#include<stdexcept>//复数模板类声明template<typenameT>classComplex{public:Complex(Tr=0,Ti=0):real{r},imag{i}{}Complex(constComplex<T>&c):real{c.real},im......
  • 实验6 C语言结构体、枚举应用编程
    1.实验任务1源代码1//P286例8.172//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块3//打印不及格学生信息和所有学生信息程分别调用45#include<stdio.h>6#include<string.h>7#defineN3//运行程序输入测试时,可......
  • 实验 2 处理机调度算法
    1.实验任务1)回顾课本第三章中介绍过的作业或进程调度算法,包括先来先服务、最短作业优先、时间片轮转、多级队列调度和多级反馈队列调度等,介绍上述调度算法的设计原理并分析各自的特点;2)采用高级编程设计语言实现任意一种处理机调度算法;3)下面提供了实现先来先服务调度算法的参考......
  • 实验6
    task1源码//P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedef......
  • 实验6
    任务1源码1#define_CRT_SECURE_NO_WARNINGS2#include<stdio.h>3#include<string.h>4#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试56typedefstructstudent{7intid;//学号8charname......
  • 实验六
    task4源代码:#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;......
  • 实验6
    实验任务4:#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voi......
  • 实验6 c语言结构体、枚举应用编程
    实验任务4程序源码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp源代码1#include<iostream>2#include<stdexcept>34template<typenameT>5classVector{6private:7intsize;8T*vec;9public:10Vector<T>()=default;11......