实验三作业
实验五: #include <iostream> #include <string> #include <iomanip> using namespace std; class Info { private: string nickname; string contact; string city; int num; public: static int all; Info(string s1 = "", string s2 = "", string s3 = "", int n = 0) : nickname(s1), contact(s2), city(s3), num(n) { all += n; } void print() const { cout << setw(16) << nickname << setw(16) << contact << setw(16) << city << setw(16) << num << endl; } void pop_back() { all -= num; } }; int Info::all = 0; #include "info.hpp" #include <vector> using namespace std; const int capability = 100; void input(string& nickname, string& contact, string& city, int& num) { cout << "请输入姓名:"; cin >> nickname; cout << "请输入联系方式:"; cin >> contact; cout << "请输入城市:"; cin >> city; cout << "请输入人数:"; cin >> num; } int main() { string nickname, contact, city; int num, flag = 1; vector<Info> reserve{ 0 }; while (flag) { cout << "当前人数 : " << Info::all << endl; input(nickname, contact, city, num); Info temp(nickname, contact, city, num); reserve.push_back(temp); if (Info::all > capability) { reserve.pop_back(); temp.pop_back(); cout << "人数过多! 剩余容量为: " << capability - Info::all << endl; cout << "请输入1修改人数或者输入0退出程序:"; cin >> flag; } else if (Info::all == capability) { flag = 0; cout << "停止录入!" << endl; } else { cout << "是否继续录入?请输入1继续,否则请输入0: "; cin >> flag; } cout << endl; } cout.setf(ios::left); cout << setw(16) << "nickname:" << setw(16) << "contact:" << setw(16) << "city: " << setw(16) << "num: " << endl; for (const auto& i : reserve) i.print(); return 0; }
实验六
#include <iostream>
#include <string>
using namespace std;
bool islower(char text)
{
if ('a' <= text && text <= 'z')
return true;
else
return false;
}
bool isupper(char text)
{
if ('A' <= text && text <= 'Z')
return true;
else
return false;
}
string encode(char text)
{
string s;
if (islower(text))
return s = text + 5 - (text + 4) / ('z') * 26;
else if (isupper(text))
return s = text + 5 - (text + 4) / ('Z') * 26;
else
return s = text;
}
string decode(char text)
{
string s;
if (islower(text))
return s = text - 5 + ('a') / (text - 4) * 26;
else if (isupper(text))
return s = text - 5 + ('A') / (text - 4) * 26;
else
return s = text;
}
class TextCoder
{
private:
string text;
string encoder()
{
for (int i = 0; text[i]; ++i)
text.replace(i, 1, encode(text[i]));
return text;
}
string decoder()
{
for (int i = 0; text[i]; ++i)
text.replace(i, 1, decode(text[i]));
return text;
}
public:
TextCoder(string code) : text(code) {}
string get_ciphertext()
{
return encoder();
}
string get_deciphertext()
{
return decoder();
}
};
![]()



浙公网安备 33010602011771号