首页 > 其他分享 >实验3

实验3

时间:2022-10-20 18:44:17浏览次数:36  
标签:std string text void 实验 using include

实验任务5

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<iomanip>
 5 #include<string>
 6 
 7 using std::cout;
 8 using std::endl;
 9 using std::cin;
10 using std::setw;
11 using std::string;
12 
13 class Info{
14     public:
15         Info(string name, string phone, string city_name, int number):nickname{name}, contact{phone}, city{city_name}, n{number} {}
16         void print();
17             
18     private:
19         string nickname; //昵称 
20         string contact; // 联系方式 
21         string city; // 所在城市 
22         int n; // 预定参加人数 
23 };  
24 
25 void Info::print() {
26     cout << "昵称:"  << nickname << endl;
27     cout << "联系方式: " << contact << endl;
28     cout << "所在城市:"  << city << endl;
29     cout << "预定人数:"  << n << endl;
30     cout << endl; 
31 }
Info.hpp
 1 #include "Info.hpp"
 2 #include<string>
 3 #include<iostream>
 4 #include<vector>
 5 #include<iomanip>
 6 #include<limits>
 7 using namespace std;
 8 
 9 class Liveshow{
10     public:
11         void appointment();
12         void print();
13     
14     private:
15         int nums = 0;
16         const int capacity = 100; // 最大容纳听众人数
17         vector<Info> audience_info_list; 
18 };
19 
20 void Liveshow::appointment(){
21     string title{"录入信息:"};
22     cout << title << endl << endl;
23     
24     string s1{"昵称"};
25     string s2{"联系方式(邮箱/手机号)"};
26     string s3{"所在城市"};
27     string s4{"预定参加人数"};
28     
29     cout << left << setw(30) << s1  
30          << left << setw(30) << s2 
31          << left << setw(30) << s3 
32          << left << setw(30) << s4 ;
33      
34     string name;     
35     while(cin >> name){
36         string contact, city;
37         int number;
38         cin >> contact;
39         cin >> city;
40         cin >> number;
41         if(nums+number >= capacity) {
42             cout << "对不起,只剩"<< capacity - nums <<"个位置。" << endl;
43             cout << "1. 输入u,更新(update)预定信息" << endl;
44             cout << "2. 输入q,退出预定" << endl;
45             cout << "你的选择:";
46             string choice;
47             cin >> choice;
48             cout << endl; 
49             break;
50         }else{
51             nums+=number;
52         }
53         audience_info_list.push_back(Info(name, contact, city, number));
54         cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
55     }
56     
57     print();     
58 }
59 
60 void Liveshow::print() {
61     cout << "截至目前,一共有" << nums 
62          << "位听众预定参加。预定听众信息如下:" << endl;
63     
64     for(auto &item : audience_info_list)   item.print();
65 }
66 
67 int main() 
68 {
69     Liveshow show;
70     show. Appointment();
71 }
task5.cpp

程序运行结果截图:

 

 

 

 

 

实验任务6

 1 #include<string>
 2 
 3 using std::string;
 4 
 5 class TextCoder{
 6     public:
 7         TextCoder(string txt): text{txt} {}
 8         string get_ciphertext();
 9         string get_deciphertext();
10     
11     private:
12         string text;
13         void encoder();
14         void decoder();
15 };
16 
17 void TextCoder::encoder(){
18     for(int i = 0; i < text.size(); i++){
19         if( (text.at(i) >= 'V' && text.at(i) <= 'Z') || (text.at(i) >= 'v' && text.at(i) <= 'z') )
20         {
21             text.at(i) = text.at(i) - 21;
22         }
23         else if( (text.at(i) >= 'A' && text.at(i) <= 'U') || (text.at(i) >= 'a' && text.at(i) <= 'u') ){
24             text.at(i) += 5;
25         }
26     }
27 }
28 
29 void TextCoder::decoder(){
30     for(int i = 0; i < text.size(); i++){
31         if( (text.at(i) >= 'A' && text.at(i) <= 'E') || (text.at(i) >= 'a' && text.at(i) <= 'e') )
32         {
33             text.at(i) = text.at(i) + 21;
34         }
35         else if( (text.at(i) >= 'F' && text.at(i) <= 'Z') || (text.at(i) >= 'f' && text.at(i) <= 'z') ){
36             text.at(i) -= 5;
37         }
38     }
39 }
40 
41 string TextCoder::get_ciphertext(){
42     encoder();
43     return text;
44 }
45 
46 string TextCoder::get_deciphertext(){
47     decoder();
48     return text;
49 }
TextCoder.hpp
 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 }
task3.6.cpp

运行结果截图:

 

 

实验总结:

通过这次实验我了解了string类库的多种函数的使用,也了解了array和vector库的多种构造和调用方法,以及两者的多种循环调用方式。

但我还是不能熟练的掌握和使用array以及vector库的多种功能。

标签:std,string,text,void,实验,using,include
From: https://www.cnblogs.com/gkd-gkd/p/16810884.html

相关文章

  • 实验五:开源控制器实践——POX
    (一)基本要求1、POX的forwarding.hubh1pingh2h1pingh3h2pingh3结论:将数据包广播转发2、POX的forwarding.l2_learningh1pingh2h1pingh3h2pingh3......
  • 实验三
    Info.hpp#include<iostream>#include<string>usingnamespacestd;classInfo{public:Info(stringname="",stringcon="",stringci="",intnu=0):ni......
  • 实验5:开源控制器实践——POX
    (一)基本要求:1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,控制器使用部署于本地的POX(默认监听6633端口)1)生成拓扑:sudomn--topo=single,3--mac--controller=remote,ip......
  • 实验三
    实验任务5#pragmaonce#include<iostream>#include<string>usingnamespacestd;classInfo{public:Info(stringnickname1,stringcontact1,stringcity......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • TFboy的实验2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5intmain(){intnumber;inti;srand(time(0));//以当前系统时间作为随机种子......
  • 实验五
    一、实验目的1、能够理解POX控制器的工作原理;2、通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3、能够运用POX控制器编写自定......
  • Python实验报告——第6章 函数
    实验报告实例01:输出每日一帖(共享版)代码如下:deffunction_tips():'''功能:每天输出一条励志文字'''importdatetime#导入日期时间类#定义......
  • python实验报告(函数)
    1.输出每日一站(共享版)  结果:   2.根据身高,体重计算BMI指数  结果:  3.根据身高,体重计算BMI指数  结果:  4.模拟结账功能———计算实付金......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3.......