实验2 数组、指针与c++标准库
作者:互联网
info.hpp:
#ifndef INFO_HPP #define INFO_HPP #include<iostream> #include<string> using namespace std; class Info{ public: Info(string a,string b,string c,int d) : nickname(a),contact(b),city(c),n(d){} void print(){ cout << "称呼: " << nickname << endl; cout << "联系方式:" << contact << endl; cout << "所在城市:" << city << endl; cout << "预定人数:" << n << endl; } private: string nickname; string contact; string city; int n; }; #endif
task5.cpp:
#include"info.hpp" #include<iostream> #include<string> #include<vector> int main() { vector<Info> audience_info_list; const int capacity = 100; string _nickname,_contact,_city; int count = 0,num; char a; cout << "录入信息:" << endl; cout << endl; cout << "称呼/昵称, 联系方式(邮箱/手机号), 所在城市, 预定参加人数" << endl; while( cin >> _nickname){ cin >> _contact; cin >> _city; cin >> num; if( num + count > capacity) { cout << "对不起,只剩" << capacity - count << "个位置。" << endl; cout << "1. 输入u,更新(update)预定信息" << endl; cout << "2. 输入q,退出预定" << endl; cout << "你的选择:"; cin >> a; if(a == 'q'){ break; }else{ cout << "请重新输入:" << endl; continue; } } count += num; audience_info_list.push_back(Info(_nickname,_contact,_city,num)); } cout << "目前一共有" << count << "位听众预定参加,预定听众信息如下:" << endl; for ( int i = 0;i < audience_info_list.size();i++){ audience_info_list[i].print(); } }
运行结果:
textcoder.hpp:
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string x):text(x){} string encoder(){ string str = text; for(int i = 0;i < text.length();i++){ if(text[i] >= 'a' && text[i] <= 'z'){ str[i] = (text[i] + 5 - 'a') % 26 + 'a'; }else if(text[i] >= 'A' && text[i] <= 'Z'){ str[i] = (text[i] + 5 - 'A') % 26 + 'A'; } } return str; } string decoder(){ string str = text; for(int i = 0;i < text.length();i++){ if(text[i] >= 'a' && text[i] <= 'z'){ str[i] = (text[i] - 5 - 'a' + 26) % 26 + 'a'; }else if(text[i] >= 'A' && text[i] <= 'Z'){ str[i] = (text[i] - 5 - 'A' + 26) % 26 + 'A'; } } return str; } private: string text; }; #endif
task6.cpp:
#include "TextCoder.hpp" #include<iostream> #include<string> int main() { string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)) { encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象 cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } }
运行结果:
标签:string,int,text,hpp,cout,c++,数组,include,指针 来源: https://www.cnblogs.com/lw0518/p/15486599.html