实验3

实验任务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库的多种功能。

posted @ 2022-10-20 18:39  wch824  阅读(10)  评论(0编辑  收藏  举报