task5
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<iomanip> 5 using namespace std; 6 class Info { 7 friend void print(vector<Info>& person); 8 private: 9 string nickname, contact, city; 10 int n; 11 public: 12 static int num; 13 Info(string name, string Contact, string City, int N) { 14 nickname = name; 15 contact = Contact; 16 city = City; 17 n = N; 18 } 19 }; 20 int Info::num = 0;//用于记录已经预定位置数 21 void print(vector<Info>& person) { 22 for (int i = 0;i<person.size();i++) { 23 cout << left << setw(10) << "昵称" << setw(15) << "联系方式" << setw(10) << "所在城市" << setw(10) << "人数" << endl; 24 cout << setw(10) << person[i].nickname; 25 cout << setw(15) << person[i].contact; 26 cout << setw(10) << person[i].city << setw(10); 27 cout << person[i].n << endl; 28 } 29 } 30 //录入预定信息 31 void creat(vector<Info>& inform,string name,string Contact,string City,int N) { 32 Info temp(name, Contact,City, N); 33 inform.push_back(temp); 34 Info::num += N;//计入座位数 35 } 36 int main(){ 37 cout << "录入信息" << endl; 38 cout << left << setw(10) << "昵称" << setw(20)<<"联系方式" << setw(20) << "所在城市" << setw(20) << "人数" << endl; 39 vector<Info>inform; 40 string name, City, Contact; 41 int N; 42 while (cin>>name>>Contact>>City>>N) { 43 //判断剩余容量是否能够容纳 44 if (N + Info::num <= 100) { 45 creat(inform, name, Contact, City, N); 46 } 47 else { 48 cout << "对不起,只剩余" << 100 - Info::num << "个位置" << endl; 49 cout << "1.输入u,更新预定信息" << endl; 50 cout << "2.输入p,退出预定" << endl; 51 char p; 52 cin >> p; 53 if (p == 'u') { 54 continue;//跳过这次循环,重新录入信息 55 } 56 else if (p == 'p') { 57 break;//退出预定录入信息 58 } 59 } 60 } 61 print(inform); 62 }
textcoder.hpp
1 #pragma once 2 #include<string> 3 using namespace std; 4 class TextCoder { 5 private: 6 string text; 7 void encoder(string &text1) { 8 for (int i = 0; i < text.size(); i++) { 9 if(text[i]<='z'&&text[i]>='a'||text[i]<='Z'&&text[i]>='A') 10 text1[i] += 5; 11 } 12 } 13 void decoder(string &text1) { 14 for (int i = 0; i < text.size(); i++) { 15 if (text[i] <='z' && text[i]>='a' || text[i] <='Z' && text[i]>='A') 16 text1[i] -= 5; 17 } 18 } 19 public: 20 string get_ciphertext() { 21 encoder(text); 22 return text; 23 24 } 25 string get_deciphertext() { 26 decoder(text); 27 return text; 28 } 29 TextCoder(string str) { 30 text = str; 31 } 32 };
task6.cpp
1 #include "textcoder.hpp" 2 #include <iostream> 3 #include <string> 4 void test() { 5 using namespace std; 6 string text, encoded_text, decoded_text; 7 cout << "输入英文文本: "; 8 while (getline(cin, text)) { 9 encoded_text = TextCoder(text).get_ciphertext(); // 这里使用的是临时无名对象 10 cout << "加密后英文文本:\t" << encoded_text << endl; 11 decoded_text = TextCoder(encoded_text).get_deciphertext(); // 这里使用的是临时无名对象 12 cout << "解密后英文文本:\t" << decoded_text << endl; 13 cout << "\n输入英文文本: "; 14 } 15 } 16 int main() { 17 test(); 18 }
标签:Info,string,int,text,实验,include,name From: https://www.cnblogs.com/1916wenle/p/16814964.html