首页 > 编程语言 >实验三 数组、指针与现代C++标准库

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

时间:2022-10-20 00:00:38浏览次数:45  
标签:info string text void cout C++ 数组 include 指针

实验任务 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++参考手册, 结合示例代码学习用法,熟练掌握。

标签:info,string,text,void,cout,C++,数组,include,指针
From: https://www.cnblogs.com/youxiasaigao/p/16808275.html

相关文章

  • C++课程设计《最短路径》
    C++课程设计《最短路径》课程设计《最短路径》课程设计题目:最短路径实验设计目的与要求:2.1目的:1)熟练应用C++的基本知识、技能。通过本课程设计,总结C++中抽象数据......
  • 为 C++程序添加 C 接口
    为C++程序添加C语言接口总共有两个要注意的点,如下要在接口定义中添加extern"C"的声明进行链接时需要使用C++的标准库原始的C++程序animal.h#ifndef_AN......
  • 实验3 数组,指针与现代C++标准库
    task5//Info.hpp#include<iostream>#include<math.h>#include<string>usingnamespacestd;classInfo{public:Info(stringa,stringb,stringc,......
  • 06-Go语言数组和切片
    数组数组初始化 vararr[4]int arr[0]=1 arr[1]=2 fmt.Println(arr) vara=[4]int{1,2,3,4} fmt.Println(a) varb=[4]int{1,2,3} fmt.Println(b......
  • 委托+继承_c++
    ......
  • C++ 常量
    #include<iostream>//C++有两种定义常量的方式#define修饰const修饰//宏常量定义在函数外#defineconstant7;intmain(intargc,constchar*argv[]){......
  • 力扣525(java&python)-连续数组(中等)
    题目:给定一个二进制数组nums,找到含有相同数量的0和1的最长连续子数组,并返回该子数组的长度。 示例1:输入:nums=[0,1]输出:2说明:[0,1]是具有相同数量......
  • 实验3 数组、指针与现代C++标准库
    实验任务5task5.cpp1#include<iostream>2#include<vector>3#include<string>4#include"Info.hpp"5usingnamespacestd;6intmain()7{8cons......
  • ERROR: <bits/stdc++.h>, 'cstdalign' file not found, running C++17
    Modified ​​1year,1monthago​​Viewed 9ktimes4I'mtryingtorunapieceofcodein VisualStudioCode,onmacOSCatalina.Thecode:#include<bits/stdc+......
  • VSCode搭建C和C++环境
    @目录前言下载安装设置.vscodec_cpp_properties.jsonlaunch.jsonsettings.json下载CodeRunner插件运行代码VSCode我个人的配置项设置前言说明下如何在VSCode下面搭建C/......