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

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

时间:2022-10-19 22:36:32浏览次数:58  
标签:Info string text hpp cout C++ 数组 include 指针

task5

//Info.hpp

#include<iostream> #include<math.h> #include<string> using namespace std; class Info{ public: Info (string a, string b, string c, int nn) : nickname{a}, contact{b}, city{c}, n{nn} { cnt += nn; } void print(){ cout << "昵称: " << nickname << endl << "联系方式: " << contact << endl << "所在城市: " << city << endl << "预定人数: " << n << endl << endl; } static int getcnt() { return cnt; } static const int capacity; private: string nickname, contact, city; int n; static int cnt; }; int Info::cnt = 0; const int Info::capacity = 100;
//task5.cpp

#include<iostream> #include<vector> #include<iomanip> #include"Info.hpp" using namespace std; vector<Info> livehouse; int main(){ cout << "录入信息:" << endl; cout << setw(15) << left << "昵称"; cout << setw(15) << left << "联系方式(邮箱/手机号)"; cout << setw(15) << left << "所在城市"; cout << setw(15) << left << "预定参加人数"; cout << endl; string nickname, contact, city; int n; while(cin >> nickname){ cin >> contact >> city >> n; bool in = true; while(n + Info::getcnt() > Info::capacity) { cout << "对不起,只剩" << Info::capacity - Info::getcnt() << "个位置" << endl; cout << "1.输入u,更新(updata)预订信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; char op; cin >> op; if(op == 'q'){ in = false; break; } else{ cout << "更新预定人数:" ; cin >> n; } } if(in){ livehouse.push_back(Info(nickname, contact, city, n)); cout << "预定成功 ~ " << endl; } } cout << "截止目前,一共有" << Info::getcnt() << "位听众预定参加。预定听众信息如下:" << endl; for(auto &now : livehouse) now.print(); return 0; }

测试结果1:

 

测试结果2:

 

更换测试数据的结果:

 

 

 

 

task6

//TextCoder.hpp

#include<bits/stdc++.h> using namespace std; class TextCoder{ public: TextCoder(string a = "") : text{a} {} string get_ciphertext(){ encoder(); return text; } string get_deciphertext(){ decoder(); return text; } private: string text; void encoder(int w = 5){ for(auto &ch : text){ char Aa; if('a' <= ch && ch <= 'z') Aa = 'a'; else if('A' <= ch && ch <= 'Z') Aa = 'A'; else continue; ch = Aa + (ch - Aa + w + 52) % 26; } } void decoder(){ encoder(-5); } };
//task6.cpp

#include<iostream> #include<string> #include"TextCoder.hpp" void test(){ using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本:"; while(getline(cin, text)){ encoded_text = TextCoder(text).get_ciphertext(); cout << "加密后的英文文本:" << encoded_text << endl; decoded_text = TextCoder(encoded_text).get_deciphertext(); cout << "解密后的英文文本:" << decoded_text << endl << endl; cout << "输入英文文本:"; } } int main(){ test(); }

测试结果:

 

 跟换测试数据的结果:

 

标签:Info,string,text,hpp,cout,C++,数组,include,指针
From: https://www.cnblogs.com/lyhy/p/16808084.html

相关文章

  • 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/......
  • React遍历数组的时候报错:key需要保持不一样
    需要加上key值,使每个dom不一样报错:{jigui.map((item,index)=>{return<p>{item?.name}</p>;})}不会报错:{jigui.map((item,index)=>{......
  • 实验3 数组、指针与现代C++标准库
    实验目的知道什么是类模板,会正确定义和使用简单的类模板能够描述数组的特性,会使用C++语法正确定义和访问内置数组,知道其局限性能够解释指针变量的特性,会使用C++语法正......
  • 实验3 数组,指针与现代c++标准库
    实验五:Info.hpp1#include<iostream>2#include<string>3#include<iomanip>4usingnamespacestd;5classInfo6{7private:8......