实验任务5: info.hpp:
#include<iostream> #include<string> using namespace std; class info{ public: info(string ni,string co,string ci,int n); void print(); private: string nickname,contact,city; int n_people; }; info::info(string ni,string co,string ci,int n):nickname{ni},contact{co},city{ci},n_people{n}{} void info::print(){ cout<<"昵称:\t"<<nickname<<endl; cout<<"联系方式\t"<<contact<<endl; cout<<"所在城市\t"<<city<<endl; cout<<"预定人数\t"<<n_people<<endl; }
task5.cpp:
#include"info.hpp" #include<iostream> #include<vector> #include<string> using namespace std; const int capacity = 100; int n;string ni,co,ci,ch; int main(){ int capacityplus=0,j=0; vector<info>audience_info_list; cout<<"录入信息:\n\n"<<"昵称 " <<"联系方式(邮箱/手机号码) "<<"所在城市 " <<"预定参加人数 \n"; while(cin>>ni&&ni!="end"){ cin>>co>>ci>>n; capacityplus+=n; if(capacity>capacityplus) audience_info_list.push_back(info(ni,co,ci,n)); else{ capacityplus-=n; cout<<"对不起,只剩"<<capacity-capacityplus<<"个位置。\n"; cout<<"1.输入u,更新(update)预定信息\n"; cout<<"2.输入q,退出预定\n"<<"你的选择:"; cin>>ch; if(ch=="q") return 0; if(ch=="u"){ cout<<"录入信息:\n\n"<<"昵称 " <<"联系方式(邮箱/手机号码) "<<"所在城市 " <<"预定参加人数 \n"; } } } cout << "截至目前,一共有" <<capacityplus<< "位听众预定参加。预定听众信息如下:\n"; while(j<audience_info_list.size()){ audience_info_list[j].print(); cout<<"\n"; j++; } }
运行结果:
实验任务6 textcoder.hpp:#include<iostream> #include<string> #include <cctype> using namespace std; class TextCoder{ public: TextCoder(string t); string get_ciphertext(); string get_deciphertext(); private: string text; void encoder(); void decoder(); }; TextCoder::TextCoder(string t):text{t}{} string TextCoder::get_ciphertext(){ encoder(); return text; } string TextCoder::get_deciphertext(){ decoder(); return text; } void TextCoder::encoder(){ for(auto &obj:text){ if(obj>='a'&&obj<='u'||obj>='A'&&obj<='U') obj+=5; else if('u'<obj&&obj<='z'||'U'<obj&&obj<='Z') obj-=21; } } void TextCoder::decoder(){ for(auto &obj:text){ if('f'<=obj&&obj<='z'||'F'<=obj&&obj<='Z') obj-=5; else if(obj>='a'&&obj<'f'||obj>='A'&&obj<'F') obj+=21; } }
task 6:
#include "textcoder.hpp" #include <iostream> #include <string> void test() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)&&text!="^z") { encoded_text = TextCoder(text).get_ciphertext(); cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).get_deciphertext(); cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } } int main() { test(); }
运行结果:
标签:info,ni,string,int,text,C++,数组,include,指针 From: https://www.cnblogs.com/laver-8/p/16823356.html