實驗二 陣列、指標和c++標準庫
阿新 • • 發佈:2021-11-01
實驗任務5
Info.hpp
#ifndef INFO_HPP #define INFO_hpp #include<iostream> #include<string> #include<iomanip> using namespace std; class Info{ private: string nickname; string contack; string city; int n; public: Info(string nickname0,stringcontack0,string city0,int n0); void print(); }; Info::Info(string nickname0,string contack0,string city0,int n0): nickname{nickname0},contack{contack0},city{city0},n{n0}{} void Info::print(){ cout<<left<<setw(10)<<"稱呼:"<<nickname<<endl; cout<<left<<setw(10)<<"聯絡方式:"<<contack<<endl; cout<<left<<setw(10)<<"所在城市:"<<city<<endl; cout<<left<<setw(10)<<"預訂人數:"<<n<<endl; } #endif
task5.cpp
#include"Info.hpp" #include<iostream> #include<vector> int main(){ vector<Info>audience_info_list; const int capacity=100; string nickname0,contack0,city0; int n0,k=0; cout<<"錄入資訊"<<endl<<endl; cout<<"稱呼/暱稱,聯絡方式(郵箱/手機號),所在城市,預定參加人數"<<endl; while(cin>>nickname0,cin>>contack0,cin>>city0,cin>>n0){ k+=n0; Info c1(nickname0,contack0,city0,n0); audience_info_list.push_back(c1); if(k==capacity) break; else if(k>capacity){ cout<<"對不起,只剩"<<capacity-k+n0<<"個位置."<<endl; cout<<"1.輸入u,更新(update)預定資訊"<<endl; cout<<"2.輸入q,退出預定" <<endl; cout<<"你的選擇:"; k-=n0; char z; cin>>z; int t=0; if(z=='q'){ audience_info_list.pop_back(); break; } else if(z=='u'){ audience_info_list.pop_back(); continue; } } } cout<<endl<<"截至目前,一共有"<<k<<"位聽眾預定參加。預定聽眾資訊如下:"<<endl; for(auto it=audience_info_list.begin();it!=audience_info_list.end();++it){ it->print(); } }
執行測試結果截圖:
實驗任務6
Textcoder.hpp
#ifndef TEXCODER_HPP #define TEXTCODER_HPP #include<iostream> #include<string> using namespace std; class TextCoder{ private: string text; public: TextCoder(string text0):text{text0}{} string encoder(); string decoder(); }; string TextCoder::encoder(){ for(auto &ch:text){ if(ch>='a'&&ch<='u'||ch>='A'&&ch<='U') ch+=5; else if(ch>='v'&&ch<='z'||ch>='V'&&ch<='Z') ch-=21; } return text; } string TextCoder::decoder(){ for(auto &ch:text){ if(ch>='f'&&ch<='z'||ch>='F'&&ch<='Z') ch-=5; else if(ch>='a'&&ch<='e'||ch>='A'&&ch<='F') ch+=21; } return text; }
#endif
task6.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輸入英文文字: "; } }
執行測試結果截圖: