10.27
#include<iostream> #include<string> #include<iomanip> class info { public: info(std::string theName = "a12", std::string thePhone = "112", std::string theCity = "s12", int number = 0):nickname(theName), contact(thePhone),city(theCity),n(number) {} void print() const; std::string nickname; std::string contact; std::string city; int n; }; void info::print() const { std::cout << std::left << std::setw(15) << "称呼:" << std::left << std::setw(15) << nickname << std::endl; std::cout << std::left << std::setw(15) << "联系方式" << std::left << std::setw(15) << contact << std::endl; std::cout << std::left << std::setw(15) << "所在城市" << std::left << std::setw(15) << city << std::endl; std::cout << std::left << std::setw(15) << "预定人数" << std::left << std::setw(15) << n << std::endl; }
#include"Info.hpp" #include<iostream> #include<string> #include<vector> int main() { std::vector<info> audience_info_list; std::cout << "录入信息:\n" << std::endl; std::cout << "昵称/称呼 联系方式电话/邮箱 城市 预定参加人数" << std::endl; const int capacity = 100; int numbers = 0; std::string inputN, inputP,inputC; int inputNu; while(std::cin >> inputN ) { std::cin >> inputP; std::cin >> inputC; std::cin >> inputNu; info a(inputN, inputP, inputC, inputNu); audience_info_list.push_back(a); numbers += inputNu; if( numbers > capacity) { numbers -= inputNu; audience_info_list.pop_back(); std::cout << "对不起只剩下" << capacity - numbers << "个座位" << std::endl; std::cout << "1.输入u,更新(update)预定数据\n2.输入q,退出\n你的选择:"; char p; std::cin >> p; if(p == 'q') { break; } else if(p == 'u') { } } } std::cout << "截止目前,一共有" << numbers << "位听众预定参加。预定信息如下:" << std::endl; for(auto const &i : audience_info_list) { i.print(); } return 0; }
#include<iostream> #include<string> class textcoder { public: textcoder(std::string theText = "a12"): text(theText) {} std::string encoder(); std::string decoder(); private: std::string text; }; std::string textcoder::encoder() { for(auto &a:text) { if( ( ( a < 'v') && ( a >= 'a')) || ((a < 'V') && (a >= 'A'))) { a += 5; } else if( ((a >= 'v') && (a <= 'z') ) || ((a >= 'V') && (a <= 'Z'))) { a -= 21; } } return text; } std::string textcoder::decoder() { for(auto &a:text) { if( ( ( a <= 'z') && ( a > 'e')) || ((a <= 'Z') && (a > 'E'))) { a -= 5; } else if( ((a >= 'a') && (a <= 'e') ) || ((a >= 'A') && (a <= 'E'))) { a += 21; } } return text; }
#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输入英文文本: "; } }
实验总结:
1.string这个类有自己的确认长度函数length() size()
2.可以直接用+实现两个字符串的相加
3.auto string.find查找某个字符第一次出现的位置4.
4.auto string. substr(a,b)取字串从a~b
5.string.replace(a,b,c)把串中a~b的全部替换成c
6.a = string.swap(b)交换
7.int x = stoi(a)把字符串a全部转换为Int
8.cin.ignore(numeric_limits<streamsize>::max(), '\n');清空输入缓存区
9.capacity检测数组空间大小,但是这个大小是可用大小,不一定是有值的位置的大小