面向物件->實驗報告二(C++)
阿新 • • 發佈:2021-10-28
task5
實驗要求
介於學習了一些新的庫函式,下面用兩種方法實現,一種使用了大量c++中的庫函式,一種是隻為實現目的而採用的精簡版。可以按需求學習。
* 法一
main.cpp
/* main.cpp */ #include <iostream> #include "info.hpp" #include "vector" #include "array" #include "regex" #include "sstream" int main() { using namespace std; vector<Info> audience_info_list; array<string, 4> vec_temp{}; int temp, flag = 0; char x, choose; const int capacity = 100; // 最大容量 int now_capacity = 0; // 當前人數 string buffer; regex reg("\\s+"); istringstream iss(buffer); stringstream stream; /* 輸入模組 */ main_menu(); while (cin >> x) { getline(cin, buffer); buffer = x + buffer; buffer = regex_replace(buffer, reg, " "); /* 去多餘空格 */ for (auto &i: vec_temp) getline(iss, i, ' '); stream << vec_temp.at(3); stream >> temp; if(now_capacity+temp > capacity){ cout << "對不起,只剩" << capacity-now_capacity << "個位置." << endl; do{ quit_menu(); cin >> choose; switch (choose) { case 'u': flag = 0; break; case 'q': flag = 1; break; default: flag = -1; break; } }while(flag == -1); }else{ now_capacity += temp; // 建構函式初始化 audience_info_list.emplace_back(vec_temp.at(0), vec_temp.at(1), vec_temp.at(2), temp); } if (flag)break; } /* 輸出模組 */ cout << "\n截至目前,一共有" << now_capacity << "位聽眾預定參加。預定聽眾資訊如下:" << endl; for (auto _t: audience_info_list) { _t.print(); } return 0; }
info.hpp
/* info.hpp */ #ifndef CPP_INFO_HPP #define CPP_INFO_HPP #include "iostream" #include "iomanip" using namespace std; class Info{ string nickname; string contact; string city; int n; public: Info(string &a,string &b, string &c, int &d): nickname(a), contact(b), city(c), n(d){}; void print(); }; void Info::print() { cout << left << setw(19) <<"稱呼:"<< left << nickname << endl; cout << left << setw(21) <<"聯絡方式:" << left << contact << endl; cout << left << setw(21) <<"所在城市:" << left << city << endl; cout << left << setw(21) <<"預定人數:" << left << n << endl; } void quit_menu(){ cout << "1. 輸入u,更新(update)預定商品" << endl; cout << "2. 輸入q,退出預定" << endl; cout << "你的選擇:"; } void main_menu(){ cout << "錄入資訊:"<<endl<<endl; cout << "稱呼/暱稱,聯絡方式(郵箱/手機號),所在城市,預定參加人數" << endl; } #endif //CPP_INFO_HPP
* 法二
main.cpp
/* main.cpp */ #include <iostream> #include "info.hpp" #include "vector" int main() { using namespace std; vector<Info> audience_info_list; string name, contact, city; int n; char choose; const int capacity = 100; // 最大容量 int now_capacity = 0; // 當前人數 /* 輸入模組 */ cout << "錄入資訊:\n\n" "稱呼/暱稱,聯絡方式(郵箱/手機號),所在城市,預定參加人數" << endl; while (cin >> name >> contact >> city >> n) { if(now_capacity+n > capacity){ cout << "對不起,只剩" << capacity-now_capacity << "個位置." << endl; quit_menu(); cin >> choose; if(choose == 'q')break; }else{ now_capacity += n; audience_info_list.emplace_back(name, contact, city, n); } } /* 輸出模組 */ cout << "\n截至目前,一共有" << now_capacity << "位聽眾預定參加。預定聽眾資訊如下:" << endl; for (auto _t: audience_info_list) { _t.print(); } return 0; }
info.hpp
/* info.hpp */
#ifndef CPP_INFO_HPP
#define CPP_INFO_HPP
#include "iostream"
#include "iomanip"
using namespace std;
class Info{
string nickname;
string contact;
string city;
int n;
public:
Info(string &a,string &b, string &c, int &d):
nickname(a), contact(b), city(c), n(d){};
void print();
};
void Info::print() {
cout << left << setw(19);
cout << "稱呼:"<< nickname << endl;
cout << left << setw(21);
cout << "聯絡方式:" << contact << endl;
cout << left << setw(21);
cout << "所在城市:" << city << endl;
cout << left << setw(21);
cout << "預定人數:" << n << endl;
}
void quit_menu(){
cout << "1. 輸入u,更新(update)預定商品" << endl;
cout << "2. 輸入q,退出預定" << endl;
cout << "你的選擇:";
}
#endif
執行結果
測試環境 : MacOS。在windows下由於非等寬字型的問題可能會出現輸出不對齊,將
稱呼
前的setw(19)
改為setw(21)
即可。
另外值得注意的是:所有Linux和類Unix作業系統都是以^D
結束操作,Windows是以^Z
結束操作。
總結
-
法一練習了
vector
、array
、getline
、regex
、istringstream
、stringstream
、emplace_back
、基於範圍的for迴圈
、迭代迴圈
等技術,將其綜合運用與具體的程式碼實現中。對於一些常見的技術手法都有了瞭解。更區分了c
與c++
程式碼風格的不同。 -
法二僅為了達成目標而沒有練習使用上述技術,雖然程式碼更簡潔但對於練習和提升沒有太大幫助。
task6
實驗要求
main.cpp
/* main.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輸入英文文字: ";
}
}
textcoder.hpp
/* textcoder.hpp */
#ifndef CPP_TEXTCODER_HPP
#define CPP_TEXTCODER_HPP
#include "iostream"
using namespace std;
class TextCoder{
string text;
public:
TextCoder(string &temp):text(temp){};
string encoder();
string decoder();
};
string TextCoder::encoder() {
for(auto &i : text){
if(i>='a' && i<='z') i=(i-'a'+5)%26+'a';
if(i>='A' && i<='Z') i=(i-'A'+5)%26+'A';
}
return text;
}
string TextCoder::decoder() {
for(auto &i : text){
if(i>='a' && i<='z') i=(i-'a'+21)%26+'a';
if(i>='A' && i<='Z') i=(i-'A'+21)%26+'A';
}
return text;
}
#endif //CPP_TEXTCODER_HPP
執行結果
注意:測試環境為MacOS,在所有Linux和類Unix作業系統中都是以D結束操作,windows是以Z結束操作。
總結
- 核心就是一個加解密演算法(字元偏移)。
總結
- 初步體會了
getline
、cin
、gets
、cin.getline()
的使用與區別 - 體會了
迭代器迴圈
和基於範圍的for迴圈
的使用手法 - 對於
正則表示式
在實際操作過程中的應用有了更深的體會