实验二 数组、指针与c++标准库
task5:
info.hpp源码:
#ifndef Info_Hpp #define Info_Hpp #include <iostream> #include <string> #include <iomanip> using namespace std; class info { public: info(string _nickname, string _contact, string _city, int _n) : nickname { _nickname }, contact { _contact }, city { _city }, n { _n }{} void print() { cout.setf(ios::left); cout << setw(10) << "称呼: " << setw(20) << nickname << endl; cout << setw(10) << "联系方式:" << setw(20) << contact << endl; cout << setw(10) << "所在城市:" << setw(20) << city << endl; cout << setw(10) << "预定人数:" << setw(20) << n << endl; } private: string nickname; string contact; string city; int n; }; #endif
task5.cpp源码:
#include "info.hpp" #include <iostream> #include <vector> #include <string> using namespace std; const int capacity = 100; string _name; string _em; string _city; int _num; int cnt = 0; int main() { vector<info> audience_info_list; cout << "录入信息:" << endl; cout << endl; cout << "称呼/昵称," << "联系方式(邮箱/手机号)," << "所在城市," << "预定参加人数" << endl; while ( cin >> _name ) { cin >> _em; cin >> _city; cin >> _num; if ( cnt + _num > capacity ) { cout << "对不起,只剩" << capacity - cnt << "个位置" << endl; cout << "1.输入u,更新预订信息" << endl; cout << "2.输入q,退出预定" << endl; cout << "你的选择:"; char tmp; cin >> tmp; if ( tmp == 'q' ) { break; } else { cout << "请重新输入:" << endl; continue; } } cnt += _num; info _a1(_name, _em, _city, _num); audience_info_list.push_back(_a1); } cout << "截至目前,一共有" << cnt << "位听众预定参加。预定听众信息如下:" << endl; for ( int i = 0;i < audience_info_list.size();i++ ) { audience_info_list[i].print(); } return 0; }
运行结果:



..................................................
textcoder.hpp源码:
#ifndef Textcoder_Hpp #define Textcoder_Hpp #include <iostream> #include <string> using namespace std; class TextCoder { public: TextCoder(string _text) :text { _text }{} string encoder() { string res = text; for ( int i = 0;i < text.length();i++ ) { if ( (text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') ) { if ( (text[i] > 'u' && text[i] <= 'z') || (text[i] > 'U' && text[i] <= 'Z') ) { res[i] = text[i] - 21; } else { res[i] = text[i] + 5; } } } return res; } string decoder() { string res = text; for ( int i = 0;i < text.length();i++ ) { if ( (text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') ) { if ( (text[i] <= 'e' && text[i] >= 'a') || (text[i] <= 'E' && text[i] >= 'A') ) { res[i] = text[i] + 21; } else { res[i] = text[i] - 5; } } } return res; } private: string text; }; #endif
text6.cpp源码:
#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输入英文文本: "; } }
运行结果:

实验总结:
在task6中,一开始编写encoder()和decoder()函数时没有对res进行初始化,而又由于string是动态分配空间,这就导致了他没有分配空间,res[i]指向的是别的地方。所以一开始得到的输出结果与预期不符。
但是如果是用stl中的push_back();进行赋值,则可以忽略上述问题。

浙公网安备 33010602011771号