实验二 数组、指针与C++标准库
实验任务5
#ifndef INFO_HPP #define INFO_HPP #include<iostream> #include<string> using namespace std; struct Info{ public: Info(string nn0="a",string c0="b",string ct0="c",int n0=0):nickname(nn0),contact(c0),city(ct0),n(n0){} void print(); private: string nickname; string contact; string city; int n; }; void Info::print(){ cout<<"称呼:"<<nickname<<endl; cout<<"联系方式:"<<contact<<endl; cout<<"所在城市:"<<city<<endl; cout<<"预订人数:"<<n<<endl; } #endif
#include"info.hpp" #include<iostream> #include<string> #include<vector> using namespace std; int main(){ cout<<"录入信息:"<<endl<<endl; cout<<"称呼/昵称,联系方式(邮箱/手机号),所在城市,预定参加人数"<<endl; vector<Info>audience_info_list; const int capacity=100; string s,t,d; int i=0,n; int cn=0;int p;char o; while(cin>>s){ cin>>t; cin>>d; cin>>n; audience_info_list.push_back(Info(s,t,d,n)); p=capacity-cn-n; if(p<0){ cout<<"对不起,只剩"<<capacity-cn<<"个位置"; cout<<endl<<"1.输入u,更新(update)预定信息"<<endl; cout<<"2.输入q,退出预定"<<endl; cout<<"你的选择:"; cin>>o; cout<<endl; if(o=='q'){ break; } if(o=='u'){ audience_info_list.pop_back(); continue; } } cn=n+cn; i++; } cout<<"截至目前,一共有"<<cn<<"位听众预定参加。预定听众消息如下:"<<endl; for(int k=0;k<i;k++){ audience_info_list[k].print(); } return 0; }
实验任务6
#ifndef TEXTCODER_hpp #define TEXTCODER_hpp #include<iostream> using namespace std; class TextCoder{ public: TextCoder(string x):text(x){} string encoder(); string decoder(); private: string text; }; string TextCoder::encoder() { for(int i=0;i<text.length();i++) { if((text[i]>='A'&&text[i]<='U')||(text[i]>='a'&&text[i]<='u')) text[i]=text[i]+5; else if((text[i]>='V'&&text[i]<='Z')||(text[i]>='v'&&text[i]<='z')) text[i]=text[i]-21; } return text; } string TextCoder::decoder() { for(int i=0;i<text.length();i++) { if((text[i]>='f'&&text[i]<='z')||(text[i]>='F'&&text[i]<='Z')) text[i]=text[i]-5; else if((text[i]>='a'&&text[i]<='e')||(text[i]>='A'&&text[i]<='E')) text[i]=text[i]+21; } return text; } #endif
#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输入英文文本: "; } }