实验3 数组、指针与现代C++标准库

 1 #include"Info.hpp"
 2 #include<iostream>
 3 #include<vector>
 4 #include<iomanip>
 5 using namespace std;
 6 const int capacity = 100;
 7 int main(){
 8     vector<Info> audience_info_list;
 9     string s1,s2,s3;
10     int num,sum = 0;
11     cout << "录入信息:" << endl << endl;
12     cout << left << setw(20) << "昵称" 
13          << left << setw(50) << "联系方式(邮箱/手机号)"
14          << left << setw(30) << "所在城市" 
15          << left << setw(30) << "预定参加人数" << endl;
16     while(cin >> s1 >> s2 >> s3 >> num){
17         sum += num;
18         if(sum > capacity){
19             char choice;
20             sum -= num;
21             cout << "对不起,只剩" << capacity-sum << "个位置." <<endl;
22             cout << "1.输入u,更新(update)预定信息" <<endl
23                  << "2.输入q,退出预定" << endl
24                  << "你的选择:";
25             cin >> choice;
26             if (choice == 'u') continue;
27             if (choice == 'q') break;
28         }
29 
30         else audience_info_list.push_back(Info(s1, s2, s3, num));
31             
32     }
33     cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl;
34     for(auto &item : audience_info_list)   item.print();
35     return 0;
36 }
task5.cpp
 1 #pragma once
 2 #include<iostream>
 3 #include<iomanip>
 4 using namespace std;
 5 class Info{
 6     public:
 7         Info(string nickname0 = " ",string contact0 = " ",string city0 = " ",int n0 = 0):
 8             nickname{nickname0},contact{contact0},city{city0},n{n0} { }
 9         void print(){
10                 cout << left << setw(8) << "昵称:" << nickname <<endl;
11                 cout << left << setw(8) << "联系方式:" << contact <<endl;
12                 cout << left << setw(8) << "所在城市:" << city <<endl;
13                 cout << left << setw(8) << "预定人数:" << n <<endl << endl;
14         }
15 
16     private:
17         string nickname,contact,city;
18         int n;
19 
20 };
Info.hpp

测试结果

task5_1

 

 

 task5_2

 

 

 

 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 }
task6.cpp
 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 class TextCoder{
 6     public:
 7         TextCoder(string text0):text{text0} { }
 8         string get_ciphertext(){encoder(); return text;}
 9         string get_deciphertext(){decoder(); return text;}
10     private:
11         string text;
12         void encoder();
13         void decoder();
14 };
15 
16 void TextCoder::encoder(){
17     for(int i = 0; i < text.length(); i++){
18         if(text.at(i) >= 'a' && text.at(i) <= 'u') text.at(i) += 5;
19         else if(text.at(i) >= 'v' && text.at(i) <= 'z') text.at(i) -= 21;
20         else if(text.at(i) >= 'A' && text.at(i) <= 'U') text.at(i) += 5;
21         else if(text.at(i) >= 'V' && text.at(i) <= 'Z') text.at(i) -= 21;
22     }
23 }
24 
25 void TextCoder::decoder(){
26     for(int i = 0; i < text.length(); i++){
27         if(text.at(i) >= 'f' && text.at(i) <= 'z') text.at(i) -= 5;
28         else if(text.at(i) >= 'a' && text.at(i) <= 'e') text.at(i) += 21;
29         else if(text.at(i) >= 'F' && text.at(i) <= 'Z') text.at(i) -= 5;
30         else if(text.at(i) >= 'A' && text.at(i) <= 'E') text.at(i) += 21;
31     }
32 }
textcoder.hpp

测试结果

task6

 

 实验总结

string函数与vector,array数组用法尚不能熟练运用。

posted @ 2022-10-20 14:13  xrzjjy  阅读(12)  评论(1编辑  收藏  举报