C/C++教程

实验二:数组、指针与C++标准库

本文主要是介绍实验二:数组、指针与C++标准库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实验五:

以下为Info.cpp文件源码

 1 #include <string>
 2 #include <iostream>
 3 using namespace std;
 4 class info {
 5 private:
 6     string nickname;
 7     string contact;
 8     string city;
 9     int n;
10     static int have;
11 public:
12     info() {};
13     info(string nickname1, string contact1, string city1, int n1)
14         :nickname(nickname1), contact(contact1), city(city1), n(n1) {};
15     ~info() = default;
16     void print() const {
17         cout << "称呼:        " << nickname << endl;
18         cout << "联系方式:    " << contact << endl;
19         cout << "所在城市:    " << city << endl;
20         cout << "预订人数:    " << n << endl;
21     };
22     static int returnhave() {
23         return have;
24     };
25     static void sethave(int obj) {
26         have = obj;
27     };
28 };
29 int info::have = 0;
View Code

以下为task5.cpp源码

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include"info.hpp"
 5 using namespace std;
 6 int main() {
 7     vector<info> audience_info_list;
 8     const int capacity = 100;
 9     string nickname, contact, city;
10     int n, flag = 1, i = 0;
11     cout << "录入信息:\n" << endl;
12     cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
13     while (    cin >> nickname) {
14         cin >> contact;
15         cin >> city;
16         cin >> n;
17         if (n + info::returnhave() <= capacity) {
18             info A (nickname,contact,city,n);
19             audience_info_list.push_back(A);
20             info::sethave(n + info::returnhave());
21         }
22         else {
23             char c;
24             cout << "对不起,只剩" << capacity-info::returnhave() << "个位置" << endl;
25             cout << "1. 输入u,更新(update)预订信息" << endl;
26             cout << "2. 输入q,退出预定" << endl;
27             cout << "请输入你的选择:";
28             cin >> c;
29             if (c == 'u') {
30                 cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl;
31                 continue;
32             }
33             else if (c == 'q')
34                 break;
35             
36         }
37     }
38     cout << endl;
39     cout << "截至目前,一共有" << info::returnhave() << "位听众预定参加。预定听众信息如下:" << endl;
40     for (auto i : audience_info_list) {
41         if (i.returnhave() > 0)
42             i.print();
43     }
44     return 0;
45 }
View Code

运行结果截图:

 情况二:

 实验六:

TextCoder.hpp文件源码如下:

 1 #include <string>
 2 #include <iostream>
 3 using namespace std;
 4 class TextCoder {
 5 private:
 6     string text;
 7 public:
 8     TextCoder(string object) :text(object){};
 9     TextCoder(TextCoder& a) : text(a.text) {};
10     ~TextCoder() = default;
11     string encoder();
12     string decoder();
13 };
14 string TextCoder::encoder() {
15 for (auto& ch : text) {
16         if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
17             if ((ch > 'u' && ch <= 'z') || (ch > 'U' && ch <= 'Z')) {
18                 ch = ch - 21;
19             }
20             else {
21                 ch = ch + 5;
22             }
23         }
24     }
25     return text;
26 }
27 string TextCoder::decoder() {
28     for (auto& ch : text) {
29         if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
30             if ((ch >= 'a' && ch <'e') || (ch < 'E' && ch >= 'A')) {
31                 ch = ch + 21;
32             }
33             else {
34                 ch = ch - 5;
35             }
36         }
37     }
38     return text;
39 }
View Code

task6.cpp源码如下:

 1 #include "textcoder.hpp"
 2 #include <iostream>
 3 #include <string>
 4 
 5 int main()
 6 {
 7     using namespace std;
 8 
 9     string text, encoded_text, decoded_text;
10 
11     cout << "输入英文文本: ";
12     while (getline(cin, text))
13     {
14         encoded_text = TextCoder(text).encoder();  // 这里使用的是临时无名对象
15         cout << "加密后英文文本:\t" << encoded_text << endl;
16 
17         decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象
18         cout << "解密后英文文本:\t" << decoded_text << endl;
19         cout << "\n输入英文文本: ";
20     }
21 }
View Code

运行结果截图如下:

实验总结: 

1.Task2_2中

若尝试去掉line12关于清空输入缓冲区的代码,

在第一行输入一段不带空格的字符串,回车后第一个s1正常,第二个s1为空白。

若输入带空格的字符串 回车后第一个s1输出空格前的,第二个为空格之后的所有内容,缓冲区正常工作。

这是因为cin碰到空格回车tab键结束

而getline直到换行才结束。

如果不去掉清空输入缓冲区的代码,空格前的小串被正常输入,剩余空格之后的字符串在缓冲区被清空。

 

2.Ctrl+z可以结束输入循环,但在实验五中,输入ctrl+z会发生死循环,是while括号中格式错误的原因,改成while(cin >> n)的形式之后解决了。

 

3.task3_3中

Capacity的量怎么数,要知道,每次容量不够时,都会生成比自身大小多一倍的空间。

因此capacity是2的次方

 

4.C++里的字符串string,严格来说,不是一个真正的类型,它其实是模板类basic_string类的特化形式,需要在开头include。

使用string时,可以像使用基础数据类型一样进行赋值、比较、输入输出等操作。相较于传统的字符数 组或字符指针(char*)方式处理字符串

string的好处是提供了丰富的字符串处理方法。

 

5.范围for及自动类型推导,使用的是引用类型。尝试把引用类型改成普通类型,发现不能成功修改目的字符串,仅仅是引用。

这篇关于实验二:数组、指针与C++标准库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!