// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <conio.h>//提供kbhit函数
using namespace std;
class RandomChoice
{
private:
char option[6][20];
short size;
HANDLE handle_out;//句柄活的输出设备的句柄
COORD crd;
short pointer;//保存当前剪头指向的下标
short delay;//箭头切换位置的延迟时间
public:
RandomChoice(char*option0, char*option1, char*option2 = NULL, char*option3 = NULL, char*option4 = NULL, char*option5 = NULL)
{
strcpy_s(option[0], option0);
strcpy_s(option[1], option1);
size = 2;
handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
crd.X = 5;
crd.Y = 4+(pointer<<1);//向左位移1就是乘以2
if (option2!=NULL)
{
strcpy_s(option[2], option2);
size++;
}
else return;
if (option3 != NULL)
{
strcpy_s(option[3], option3);
size++;
}
else return;
if (option4 != NULL)
{
strcpy_s(option[4], option4);
size++;
}
else return;
if (option5 != NULL)
{
strcpy_s(option[5], option5);
size++;
}
else return;
}
void Start()//显示选项
{
cout << "\n您今天的活动安排是什么?做个选择吧\n\n\n";
for (short i = 0; i < size; i++)
{
cout << " " << option[i] << endl << endl;
}
}
void Refresh()//更新箭头显示
{
SetConsoleCursorPosition(handle_out, crd);//将输出坐标定位到上次显示箭头的位置
cout << " ";//擦掉上一次输出的箭头
pointer = (pointer + 1) % size;//pointer再0-size-1之间魂环
crd.Y = 4 + (pointer << 1);
SetConsoleCursorPosition(handle_out, crd);//输出坐标定位在新位置
cout << "->";
}
short SetDelay(short d) { return delay = d; }
void Wait() { Sleep(delay); }//毫秒级别
short Play()
{
while (!_kbhit())//getch()捕捉用户按键 ,按下后退出
{
Refresh();//擦掉上一次箭头,并在新位置画箭头,pointer跟踪
Wait();
}
return pointer;
}
void ShowResult()
{
crd.Y = 18;
SetConsoleCursorPosition(handle_out, crd);
cout << "今天我就" << option[pointer] << "吧。天意啊!\n\n\n";
}
};
int main()
{
RandomChoice rc("看电影", "写代码", "逛公园", "约妹子", "睡懒觉", "打豆豆");
rc.SetDelay(200);
char choice;
do
{
system("cls");
rc.Start();
rc.Play();
rc.ShowResult();
_getch();
cout << "是否再来一次?Y/N";
cin >> choice;
} while (choice!='q');
system("pause");
return 0;
}
07-20
07-20
08-14
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交