实验二 数组、指针与C++标准库
//实验任务5 //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(); private: string nickname; string contact; string city; int n; }; void Info::print() { cout << "称呼:\t\t" << nickname << endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预订人数:\t" << n << endl; } #endif
//task5.cpp #include"info.hpp" #include<iostream> #include<string> #include<vector> int main() { using namespace std; cout << "录入信息:" << endl; cout << "" << endl; cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl; vector<Info>audience_info_list; const int Capacity = 100; int i = 0,j=0 , k; char h; string e, f, g; int m; while ((cin >> e),(cin >> f),(cin >> g),(cin >> m)) { audience_info_list.push_back(Info(e, f, g, m)); k = Capacity - i - m; if (k < 0) { cout << "对不起,只剩" << Capacity - i << "个位置." << endl; cout << "1.输入u,更新(update)预订信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; cin >> h; cout << "" << endl; if (h == ' u') { cout << "称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数" << endl; cin >> e >> f >> g >> m; Info update(e, f, g, m); audience_info_list.pop_back(); continue; } if (h == 'q')break; } i = i + m; j++; } cout << "截至目前,一共有" << i << "位听众预定参加。预定听众信息如下:" << endl; for (int z = 0; z < j; z++) { audience_info_list[z].print(); } return 0; }

实验任务6
//TextCoder.hpp
#ifndef _TEXTCODER_HPP #define _TEXTCODER_HPP #include<iostream> class TextCoder { public: TextCoder(std::string a):text(a){} //A65 a97 std::string encoder(); std::string decoder(); private: std::string text; }; std::string TextCoder::encoder() { for (int i = 0; i < text.length(); i++) { if (text.at(i) >= 'a' && text.at(i) <= 'u' || text.at(i) >= 'A' && text.at(i) <= 'U') text.at(i) += 5; else if (text.at(i) >= 'v' && text.at(i) <= 'z' || text.at(i) >= 'V' && text.at(i) <= 'Z') text.at(i) = text.at(i) + 5 - 26; } return text; } std::string TextCoder::decoder() { for (int i = 0; i < text.length(); i++) { if (text.at(i) >= 'f' && text.at(i) <= 'z' || text.at(i) >= 'F' && text.at(i) <= 'Z') text.at(i) -= 5; else if (text.at(i) >= 'a' && text.at(i) <= 'e' || text.at(i) >= 'A' && text.at(i) <= 'E') text.at(i) = text.at(i) + 26 - 5; } return text; } #endif
//task6.cpp
#include "TextCoder.hpp" #include <iostream> #include <string> int main() { using namespace std; string text, encoded_text, decode_text; cout << "输入英文文本:"; while (getline(cin, text)) { encoded_text = TextCoder(text).encoder();// 这里使用的是临时无名对象 cout << "加密后英文文本:" << encoded_text << endl; decode_text = TextCoder(encoded_text).decoder();// 这里使用的是临时无 名对象 cout << "解密后英文文本:" << decode_text << endl; cout << "\n输入英文文本:"; }
}


浙公网安备 33010602011771号