Compare commits

..

No commits in common. "quiz4" and "master" have entirely different histories.

1 changed files with 6 additions and 47 deletions

View File

@ -3,31 +3,12 @@
#include <sstream> #include <sstream>
#include <limits.h> #include <limits.h>
/**
*
*
*/
struct node { struct node {
int id; /** 链表数据体,也就是小人的 id */ int id;
node* next; node* next;
node* prev; node* prev;
}; };
/**
* ,
* .
*
* >
* >
*
* ""
*
*
* ****
*
* @param first ""
* @param curr ""
*/
void insert_one (node* first, node* curr) { void insert_one (node* first, node* curr) {
node* insertion_prev = (*first).prev; node* insertion_prev = (*first).prev;
node* insertion_next = first; node* insertion_next = first;
@ -37,15 +18,6 @@ void insert_one (node* first, node* curr) {
(*curr).prev = insertion_prev; (*curr).prev = insertion_prev;
} }
/**
* .
*
*
*
*
*
* @param removal
*/
void remove_one (node* removal) { void remove_one (node* removal) {
node* removal_prev = (*removal).prev; node* removal_prev = (*removal).prev;
node* removal_next = (*removal).next; node* removal_next = (*removal).next;
@ -56,41 +28,35 @@ void remove_one (node* removal) {
int main () { int main () {
// 接受用户输入中
/** 用户输入的小人数量 */
int n = 0; int n = 0;
while (n == 0) { // 由于任何输入问题导致没有成功获得输入数据都会重新进行输入请求 while (n == 0) {
std::string input; std::string input;
int input_n; int input_n;
std::cout << "请输入小人个数: "; std::cout << "请输入小人个数: ";
std::cin >> input; std::cin >> input;
try { // 尝试解析用户输入 try {
input_n = stoi(input); input_n = stoi(input);
if (input_n < 1) throw std::exception(); if (input_n < 1) throw std::exception();
} catch (std::out_of_range) { // 如果输入的数字太大以至于超出了系统处理范围的报错 } catch (std::out_of_range) {
std::cout << "你输入的数字 \"" << input << "\" 太大了,它最大只能是 " << INT_MAX << std::endl; std::cout << "你输入的数字 \"" << input << "\" 太大了,它最大只能是 " << INT_MAX << std::endl;
continue; continue;
} catch (std::exception) { // 如果输入的不是一个数字,或者输入的数字不符合要求(当然不会有小于 0 个人0 个人(没有人)也显然不符合预设现实)的报错 } catch (std::exception) {
std::cout << "你的输入是 \"" << input << "\", 但小人的数量必须是一个大于 0 的整数!" << std::endl; std::cout << "你的输入是 \"" << input << "\", 但小人的数量必须是一个大于 0 的整数!" << std::endl;
continue; continue;
} }
// 二次确认输入数值(防呆)
std::cout << "小人个数为 " << input_n << " ? [y/n]"; std::cout << "小人个数为 " << input_n << " ? [y/n]";
std::cin >> input; std::cin >> input;
if (input == "y") if (input == "y")
n = input_n; n = input_n;
} }
// 招募第一个小人(为链表创建 "头",同时也构建出最小的链表体)
node* first = new node; node* first = new node;
(*first).id = 1; (*first).id = 1;
(*first).prev = first; (*first).prev = first;
(*first).next = first; (*first).next = first;
/** 记录这个队里目前有多少人(记得手动操作) */
int count = 1; int count = 1;
std::cout << "招募了小人 #" << 1 << " ." << std::endl; std::cout << "招募了小人 #" << 1 << " ." << std::endl;
// 招募从第 2 个开始的剩下的小人(按照顺序一个个把它们加到链表尾)
for (int i = 2; i <= n; i++) { for (int i = 2; i <= n; i++) {
node* curr = new node; node* curr = new node;
(*curr).id = i; (*curr).id = i;
@ -99,17 +65,11 @@ int main () {
std::cout << "招募了小人 #" << 1 << " , 它前面是 #" << (*(*curr).prev).id << " , 它后面是 #" << (*(*curr).next).id << std::endl; std::cout << "招募了小人 #" << 1 << " , 它前面是 #" << (*(*curr).prev).id << " , 它后面是 #" << (*(*curr).next).id << std::endl;
} }
/**
* .
*
*
*/
std::stringstream out_roll; std::stringstream out_roll;
int counting = 0; int counting = 0;
node* next_one = (*first).prev; node* next_one = (*first).prev;
while (count > 1) { // 让大家轮流出队直到队里只剩下最后一个人 while (count > 1) {
node* current = next_one; node* current = next_one;
next_one = (*next_one).next; next_one = (*next_one).next;
counting++; counting++;
@ -126,7 +86,6 @@ int main () {
std::cout << std::endl; std::cout << std::endl;
} }
// 出队总结
std::cout << "=====" << std::endl std::cout << "=====" << std::endl
<< "最终剩下了小人 #" << (*next_one).id << " !" << std::endl; << "最终剩下了小人 #" << (*next_one).id << " !" << std::endl;
std::cout << "小人的出列顺序是: " << out_roll.str() << "." << std::endl; std::cout << "小人的出列顺序是: " << out_roll.str() << "." << std::endl;