实验2
info.hpp
#pragma once #ifndef INFO_HPP #define INFO_HPP #include <iostream> #include<string> using namespace std; class Info { private: string nickname; string contact; string city; int n; public: Info(string _nickname = "***", string _contact = "***", string _city = "***", int n1 = 0) :nickname{ _nickname }, contact{ _contact }, city{ _city }, n{ n1 } { } void print(); }; void Info::print() { cout << "name:" << nickname << endl; cout << "number:" << contact << endl; cout << "city:" << city << endl; cout << "预定参加人数:" << n << endl; } #endif
task.cpp:
#include<iostream> #include<string> #include<vector> #include "info.hpp" int main() { vector<Info> audience; const auto capacity = 100; string a, b, c; auto m = 0, s = 0, j = 0; int d; cout << "录入信息:" << endl; cout << " " << endl; cout << "称呼..." << "联系方式..." << "所在城市..." << "预定参加人数..." << endl; while ((cin >> a), (cin >> b), (cin >> c), (cin >> d)) { audience.push_back(Info(a, b, c, d)); m += d; j++; s = capacity - m; if (s > 0) { cout << "你好,还剩" << s << "个位置" << endl; cout << "1.输入u更新信息" << endl << "2.输入q退出预定" << endl; cout << "你的选择:"; char f; cin >> f; cout << " " << endl; if (f == 'u') { cout << "称呼... " << "联系方式.... " << "所在城市... " << "预定参加人数... " << endl; } else if (f == 'q') break; } } cout << "截止目前,一共有" << m << "位观众预定,详情如下:" << endl; for (auto z = 0; z < j; z++) { audience[z].print(); } return 0; }
实验运行图:


testcode.hpp:
#ifndef TEXTCODER_HPP #define TEXTCODER_HPP #include <iostream> #include <string> using namespace std; class TextCoder { private: string text; public: string encoder(); string decoder(); TextCoder(string _text) :text{ _text } {}; }; string TextCoder::encoder() { string n; for (auto dm : text) { if (dm >= 'a' && dm <= 'z' || dm >= 'A' && dm <= 'Z') { dm += 5; if (dm < '_' && dm > 'Z' || dm > 'z') { dm -= 26; } } n += dm; } return n; } string TextCoder::decoder() { string m; for (auto dm : text) { if (dm >= 'a' && dm <= 'z' || dm >= 'A' && dm <= 'Z') { dm -= 5; if (dm < 'A' || dm>92 && dm < 'a') { dm += 26; } } m += dm; } return m; } #endif
test.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输入英文文本: "; } }
运行图:

总结:编译的过程不但要有清楚的逻辑实现方法,还要能用C++语言完整的表达出来,还是要多多的练习才行。
浙公网安备 33010602011771号