实验二
task5:
#include<iostream> #include<vector> #include<string> #include"info.hpp" using namespace std; int main() { int t = 0, i = 0; char choice; cout << "录入信息:" << "\n" <<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; string n_nickname, n_contact, n_city; int n; const int capacity = 100; vector<info> audience_info_list; while(cin >> n_nickname){ cin >> n_contact >> n_city >> n; if(t + n <=capacity){ info amb(n_nickname, n_contact, n_city, n); audience_info_list.push_back(amb); t+=n; if(t == capacity) break; } else{ cout << "对不起,只剩" << (capacity-t) << "个位置。" << endl; cout << "1.输入u,更新(update)预定信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:" ; cin >> choice; if(choice == 'u') cout << "请重新输入: " ; else break; } } cout << "截至目前,一共有" << t << "位听众预定参加,预定听众消息如下:" << endl; for(auto it = audience_info_list.begin(); it != audience_info_list.end(); ++it) it->print(); return 0; }
#include<iostream> #include<string> using namespace std; class info{ public: info(string nickname, string contact, string city, int n): m_nickname(nickname), m_contact(contact), m_city(city), m_n(n){ } void print() const { cout << "称呼: " << m_nickname << endl; cout << "联系方式: " << m_contact << endl; cout << "所在城市: " << m_city << endl; cout << "预定人数: " << m_n << endl; } private: string m_nickname; string m_contact; string m_city; int m_n; };

task6:
#include "TextCoder.hpp" #include <iostream> #include <string> int main() { using namespace std; 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输入英文文本: "; } }
#include<iostream> #include<string> using namespace std; class TextCoder{ public: TextCoder(string text):m_text(text){} string encoder(){ for(int i = 0; i < m_text.size() ; i++){ if((m_text[i] >= 'a' && m_text[i] <= 'u')||(m_text[i] >= 'A' && m_text[i] <= 'U') ) m_text[i] += 5; else if( (m_text[i] > 'u' && m_text[i] <= 'z') || (m_text[i] > 'U' && m_text[i] <='Z') ) m_text[i] -= 21; } return m_text; } string decoder(){ for(int i = 0; i < m_text.size(); i++){ if((m_text[i] >= 'f' && m_text[i] <= 'z') || (m_text[i] >= 'F' && m_text[i] <= 'Z')) m_text[i] -= 5; else if((m_text[i] >= 'a' && m_text[i] < 'f') || (m_text[i] >= 'A' && m_text[i] < 'F')) m_text[i] += 21; } return m_text; } ~TextCoder()=default; private: string m_text; };

五、实验总结
1.isalpha()函数,判断是否是字母,是返回非0值,不是返回0;
2.对一个对象赋值,可以重新创建一个对象,给其赋值以后再将其拷贝给我们需要的对象。

浙公网安备 33010602011771号