首页 > 其他分享 >实验四

实验四

时间:2023-11-28 19:12:16浏览次数:25  
标签:Info city string int 实验 include cout

任务五 textcoder.hpp

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <iomanip>
 5 using namespace std;
 6 class TextCoder
 7 {
 8 private:
 9     string text;
10     void encoder(string &a)
11     {
12         for (int i = 0; i < a.length(); i++)
13         {
14             if (a[i] >= 65 && a[i] <= 90)
15             {
16                 a[i] = 65 + (a[i] - 65 + 7) % 26;
17             }
18             else if (a[i] >= 97 && a[i] <= 122)
19             {
20                 a[i] = 97 + (a[i] - 97 + 7) % 26;
21             }
22         }
23     }
24     void docedor(string &b)
25     {
26         for (int i = 0; i < b.length(); i++)
27         {
28             if (b[i] >= 65 && b[i] <= 90)
29             {
30                 b[i] = 90 - (90 - b[i] + 7) % 26;
31             }
32             else if (b[i] >= 97 && b[i] <= 122)
33             {
34                 b[i] = 122 - (122 - b[i] + 7) % 26;
35             }
36         }
37     }
38 public:
39     TextCoder(string text) :text{ text } {}
40     ~TextCoder() {}
41     string get_ciphertext();//调用私有成员encoder()加密text中的文本
42     string get_deciphertext();//调用私有成员decoder()解密text中的文本
43 };
44 string TextCoder::get_ciphertext()
45 {
46     string a=text;
47     encoder(a);
48     return a;
49 }
50 string TextCoder::get_deciphertext()
51 {
52     string b = text;
53     docedor(b);
54     return b;
55 }
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

 

任务六

task6.cpp

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include"info.hpp" 
 5 int main()
 6 {
 7     string a, b, c; int d,sum=0;
 8     int const capacitd = 100;
 9     vector<Info> x;
10     cout << "录入信息:" << endl<<endl;
11     cout << "昵称\t    " << "联系方式(邮箱/手机号)\t    " << "所在城市\t    " << "预定参加人数\t" << endl;
12     for ( sum ;;)
13     {
14         cin >> a >> b>> c >> d;
15         Info z(a, b, c, d);
16         int y = sum; 
17         if (sum+d > capacitd)
18         {
19             cout << "对不起,只剩" << capacitd - y << "个位置" << endl;
20             cout << "1.输入u,更新(update)预定信息" << endl;
21             cout << "2.输入o,退出(out)预定" << endl; char s;
22             cout << "您的选择:"; cin >> s;
23             if (s == 'u')    continue; cout << endl;
24             break;
25         }
26         else
27         {
28             sum += d;
29             x.push_back(z);
30         }
31     }
32     cout << "截至目前,一共有" << sum << "听众预定参加。预定听众信息如下。" << endl;
33     for (int i = 0; i < x.size(); i++)
34     {
35         x[i].print();
36     }
37 }
View Code

info.hpp

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

 

 

标签:Info,city,string,int,实验,include,cout
From: https://www.cnblogs.com/dmsx/p/17862724.html

相关文章

  • 实验4
    1#include<iostream>2#include<string>34classTextCoder{5private:6std::stringtext;78voidencoder(){9for(auto&c:text){10if(c>='a'&&c<='z......
  • 人工智能-产生式系统实验(动物识别)
    1.实验目的1.熟悉知识的表示方法2.掌握产生式系统的运行机制3.产生式系统推理的基本方法。 2.实验内容运用所学知识,设计并编程实现一个小型动物识别系统,能识别虎、金钱豹、斑马、长颈鹿、鸵鸟、企鹅、信天翁等七种动物的产生式系统。规则库:r1:IF该动物有毛发THEN该动物是......
  • 实验五。
    实验一找到最大最小的数;都指向x(a)数组首元素;找出数组中最大值;不可以,作用域结束,函数内的数组所占内存空间释放,指针变为野指针;实验二:s1的大小是24;sizeof是数组所占字节数;strlen统计字符个数不算‘\0’;不可以,s1的地址是const类型,而后面的字符串返回的是他的地址,s1不可改变无法......
  • 【2023CANN训练营第二季】——Ascend C自定义算子工程介绍及实验
    一、自定义算子工程介绍与创建自定义算子工程是一个包含用户编写的host侧和kerne|侧算子实现文件的,用于编译和安装自定义算子run包的工程框架。CANN软件包中提供了工程创建工具msopgen,开发者可以输入算子原型定义文件生成AscendC算子开发工程。需要编写AddCustom算子的原型定义......
  • 实验4 现代C++标准库与类模板
    实验任务5:1.代码:textcoder.hpp:1#pragmaonce23#include<iostream>4#include<vector>5#include<array>6#include<string>7usingnamespacestd;89classTextCoder10{11private:12stringtext;13......
  • 实验5
    一、1-1、①使最大值、最小值赋值到对应指针变量        ②全部指向X首元素地址    1-2、①最大值地址        ②可以二、2-1、①24   数组s1占用字节数  数组s1长度        ②不能 数组s1没有初值和大......
  • 实验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......
  • 实验五
    实验一找到最大最小的数;都指向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......
  • 实验4 现代C++标准库与类模板
    实验任务5#pragmaonce#include<iostream>#include<string>usingnamespacestd;classTextCoder{public:TextCoder()=default;TextCoder(stringstr);stringget_ciphertext();stringget_deciphertext();~TextCoder()=de......