实验任务 5

info.hpp

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

 

task5.cpp

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <vector>
 5 #include "info.hpp"
 6 using namespace std;
 7 
 8 const int capacity = 20;
 9 
10 void printInfo(const vector<Info> &list) {
11     for (int i = 0; i < list.size(); i++) {
12         Info info = list.at(i);
13         info.print();
14         cout << endl;
15     }
16 }
17 
18 int main() {
19     vector<Info> audience_info_list;
20     
21     cout << "录入信息:" << endl;
22     cout << "昵称\t" << "联系方式(邮箱/手机号)\t\t" << "所在城市\t" << "预定参加人数" << endl;
23     
24     int leave = capacity, count = 0;
25     string name, contact, city;
26     int n;
27     bool flag = true;
28     
29     label1:                                                //指定goto 的语句位置
30     while(cin >> name >> contact >> city >> n) {
31 
32         if(leave >= n) {
33             Info info(name, contact, city, n);
34             audience_info_list.push_back(info);
35             leave -= n;
36             count += n;
37         } else {
38             bool flag2 = true;
39             while(true) {
40                 
41                 cout << "对不起,只剩" << leave << "个位置." << endl;
42                 cout << "1.输入u, 更新(update)预定信息" << endl 
43                       << "2.输入q, 退出预订"  << endl << "你的选择: " ;
44                 char c;
45                 cin >> c;
46                 if(c == 'u'){
47                     cout << "请更新信息:" << endl;
48                     goto label1;
49                 }    
50                 else if(c == 'q'){
51                     break;
52                 } else {
53                     cout << "输入有误,请重新输入!!" << endl << endl;
54                 }                
55             }
56             break;
57         }
58     }
59     
60     cout << "截至目前,一共有" << count << "位听众预定参加。预定听众信息如下:" << endl;
61     cout << endl;
62     printInfo(audience_info_list) ;
63     
64     return 0; 
65 }

 

 

 

 

 

 实验任务6

textcoder.hpp

 1 #include <iostream>
 2 #include <string>
 3 #include <cctype>
 4 
 5 using namespace std;
 6 class TextCoder {
 7 
 8 public:
 9     TextCoder(string str):text(str) {}
10     string get_ciphertext();
11     string get_deciphertext();
12     
13 private:
14     string text;
15     void encoder();
16     void decoder();
17 };
18 
19 void TextCoder::encoder() {
20     for(auto &c: text) {
21         if(c <= 'u' && c >= 'a' || c <= 'U' && c >= 'A') {
22             c = c + 5;
23         } else if(c > 'u' && c <= 'z' || c > 'U' && c <= 'Z') {
24             c = c - 21;
25         }
26     }
27 }
28 void TextCoder::decoder() {
29     for(auto &c: text) {
30         if(c <= 'z' && c >= 'f' || c <= 'Z' && c >= 'F') {
31             c = c - 5;
32         } else if(c >= 'a' && c < 'f' || c >= 'A' && c < 'F') {
33             c = c + 21;
34         }
35     }
36 }
37 
38 string TextCoder::get_ciphertext() {
39     encoder();
40     return text;
41 }
42 
43 string TextCoder::get_deciphertext() {
44     decoder();
45     return text;
46 }

 

task6.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 }

 

 

 

实验总结

应掌握序列容器类型的不同遍历方法:

1.基于索引的循环遍历

2.使用迭代器接口

3.基于范围,使用auto自动类型推导的for循环遍历

问题:

对STL中的容器类,标准库中常用库函数的使用还不熟练;

应在今后多加使用, 常查阅c++参考手册, 结合示例代码学习用法,熟练掌握。

原文地址:http://www.cnblogs.com/youxiasaigao/p/16808275.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性