任务五
info.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 using namespace std; 6 class info 7 { 8 public: 9 info(string nn="a",string con="a",string ci="a",int num=0):nickname{nn},contact{con},city{ci},n{num}{} 10 void print() const; 11 private: 12 string nickname,contact,city; 13 int n; 14 }; 15 void info::print() const{ 16 cout << "昵称: \t\t" << nickname << endl; 17 cout << "联系方式:\t" << contact << endl; 18 cout << "所在城市\t" << city << endl; 19 cout << "预定人数\t" << n << endl; 20 }
task5.cpp
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include"info.hpp" 5 #include<iomanip> 6 using namespace std; 7 int main(){ 8 const int capacity=100; 9 string nn,con,ci; 10 int num,count=0; 11 vector <info> audience_info_list; 12 cout << "录入信息" << endl; 13 cout << "昵称\t\t" << "联系方式(邮箱/手机号)\t\t" << "所在城市\t" << "预定参加人数\t" << endl; 14 while(cin >> nn){ 15 cin >> con >> ci; 16 cin >> num; 17 count=count+num; 18 19 if(capacity > count){ 20 info a=info(nn,con,ci,num); 21 audience_info_list.push_back(a); 22 } 23 else{ 24 count=count-num; 25 cout << "对不起,只剩" << (capacity - count) << "个位置." << endl; 26 cout << "1.输入u,更新(update)预定信息" << endl; 27 cout << "2.输入q,退出预定" << endl; 28 cout << "你的选择:"; 29 string n; 30 cin >> n; 31 if(n=="q"){ 32 break; 33 } 34 else if(n=="u"){ 35 cout << "录入信息:" ; 36 continue; 37 } 38 } 39 40 } 41 cout << "截至目前,一共有" << count << "位听众参加预定。预定信息如下:" << endl; 42 for (int i = 0; i < audience_info_list.size(); i++) { 43 audience_info_list.at(i).print(); 44 cout << endl; 45 } 46 }
任务六
textcoder.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 class TextCoder { 6 public: 7 TextCoder (){} 8 TextCoder(string t):text{t}{} 9 ~TextCoder=defult; 10 string get_ciphertext(){ 11 encoder(); 12 return text; 13 } 14 string get_deciphertext(){ 15 decoder(); 16 return text; 17 } 18 19 private: 20 string text; 21 string encoder(); 22 string decoder(); 23 }; 24 string TextCoder::encoder(){ 25 int length=text.length(); 26 for(int i=0;i<length;i++){ 27 if(text.at(i)>='a'&&text.at(i)<='z') 28 { 29 if (text.at(i)<='u') 30 text.at(i) = text.at(i)+5; 31 else if (text.at(i)>'u') 32 text.at(i) = text.at(i)-21; 33 } 34 else if(text.at(i)>='A' && text.at(i)<='Z') 35 { 36 if (text.at(i)<='U') 37 text.at(i) = text.at(i)+5; 38 else if (text.at(i)>'U') 39 text.at(i) = text.at(i)-21; 40 } 41 } 42 } 43 44 string TextCoder::decoder(){ 45 int length=text.length(); 46 for(int i=0;i<length;i++){ 47 if(text.at(i)>='a'&&text.at(i)<='z') 48 { 49 if (text.at(i)>='f') 50 text.at(i) = text.at(i)-5; 51 else if (text.at(i)<='e') 52 text.at(i) = text.at(i)+21; 53 } 54 else if(text.at(i)>='A' && text.at(i)<='Z') 55 { 56 if (text.at(i)>='F') 57 text.at(i) = text.at(i)-5; 58 else if (text.at(i)<='E') 59 text.at(i) = text.at(i)+21; 60 } 61 } 62 }
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 }
实验总结:cpp中包含了字符串和各种类的丰富用法。灵活使用这些用法可以帮助我们解决许多复杂的问题。
标签:info,string,int,text,C++,num,数组,include,指针 From: https://www.cnblogs.com/ljhakuna/p/16809785.html