#define
的正确用法
#include<bits/stdc++.h>
#define hello signed
#define hel main
#define h (
#define e )
#define l {
#define hehehe return
#define a 0
#define ll ;
#define lll }
#define hellohello "hello"
#define hell printf
#define enter "\n"
#define int "%d"
#define space " "
#define double "%.2f"
#define hellohe scanf
#define o ,
#define helloh [
#define helloe ]
#define he for
#define llo ++
#define hellollhe using
#define hellollhel namespace
#define hhhehe std
hellollhe hellollhel hhhehe ll
hello ap helloh 100010 helloe ll
hello hel h e l
hello w=0 ll
hell h hellohello e ll
hell h enter e ll
hell h double o 3.14 e ll
hell h enter e ll
hell h int o w e ll
hell h enter e ll
hellohe h int o & w e ll
hell h int o w e ll
hell h enter e ll
he h hello i=1 ll i <= w ll i llo e l
hellohe h int o & ap helloh i helloe e ll
lll
he h hello i=1 ll i <= w ll i llo e l
hell h int o ap helloh i helloe e ll
hell h space e ll
lll
hehehe a ll
lll
真的能运行 不信你放编译器上试试
运行结果:输出 hello 换行 3.14 换行 0,然后输入一个变量w,输出w,输入w个整数存入数组,然后输出w个整数,以空格隔开
一个好玩的
#include<bits/stdc++.h>
#include<conio.h>
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<ctime>
#include<cstdlib>
using namespace std;
int main(){
MessageBox(NULL,"请先保存好您电脑中的数据,否则一旦您的电脑出现任何问题,作者概不负责","温馨提示",MB_OK);
string s;
system("shutdown -s -t 60");
again:
MessageBox(NULL,"请按确定后键入3位密码","系统提示",MB_OK|MB_ICONWARNING);
cin>>s;
if (s=="549"){
system(" shutdown -a");
MessageBox(NULL,"密码正确,你是狗(549)","系统提示",MB_OK);
}
else{
int k=MessageBox(NULL,"密码错误\n重试请按第二个键","系统提示",MB_ABORTRETRYIGNORE|MB_ICONHAND);
if(k==3||k==5){
int k=1;
while(k<=30){
system("color 07");
system("color 17");
system("color 27");
system("color 47");
system("color 57");
system("color 67");
system("color 87");
system("color 97");
system("color A7");
system("color B7");
system("color C7");
system("color D7");
system("color E7");
k++;
}
system("shutdown -s -t 0");
}
else{
system("cls");
goto again;
}
}
return 0;
}
关于如何创建一个自己的头文件
先新建一个文本文档
将其命名为头文件名称
打开文本文档->文件->另存为
改为.h后缀名
打开头文件
//假设头文件名为:node.h
#ifndef node_h //防止重复引用
#define node_h
//写头文件中的函数和语句
//不用写main()
//不要写using namespace std;(引用std的内容时加std::)
#endif
创建.cpp文件(和头文件在同一文件夹)
#include"node.h" //用""引用
using namespace std;
int main(){
//... ...
return 0;
}
#ifndef _header_h
#define _header_h
#include<bits/stdc++.h>
#include<conio.h>
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<ctime>
#include<cstdlib>
#define CS_HELP "cstr函数使用详解\n\n函数原型 cstr(std::string s,signed a=4,signed f=1)\n\n参数1\t必填:输出字符串\n\n参数2\t选填:输出速度(默认为4)\n\n参数3\t选填:输出速度档位(0为 参数2 *10,1为 参数2 *100)\n\n\n函数内填入CS_HELP可查看说明\n\t\n\t\t\n\n\t\t\n"
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
void color(std::string Forg, std::string Back);
class _flag{ //_flag 开关
public:
void flag_return(bool a){ //初始化开关
flag=a;
return ;
}
void flag_pd(){ //按下开关
if(flag){
flag=0;
}
else{
flag=1;
}
}
bool flag_obtain(){ //返回开关的值
return flag;
}
private:
bool flag;
};
void color(std::string Forg, std::string Back){ //color函数 1.底色 2.字色
int ForgC,BackC;
if(Forg=="hei"){
ForgC=0;
}
else if(Forg=="lan"){
ForgC=1;
}
else if(Forg=="lv"){
ForgC=2;
}
else if(Forg=="qing"){
ForgC=3;
}
else if(Forg=="hong"){
ForgC=4;
}
else if(Forg=="zi"){
ForgC=5;
}
else if(Forg=="huang"){
ForgC=6;
}
else if(Forg=="mi bai"||Forg=="ru bai"){
ForgC=7;
}
else if(Forg=="hui"){
ForgC=8;
}
else if(Forg=="qian lan"){
ForgC=9;
}
else if(Forg=="qian lv"||Forg=="liang lv"){
ForgC=10;
}
else if(Forg=="lan lv"){
ForgC=11;
}
else if(Forg=="cheng"){
ForgC=12;
}
else if(Forg=="fen"){
ForgC=13;
}
else if(Forg=="mi huang"||Forg=="dan huang"){
ForgC=14;
}
else if(Forg=="bai"){
ForgC=15;
}
if(Back=="hei"){
BackC=0;
}
else if(Back=="lan"){
BackC=1;
}
else if(Back=="lv"){
BackC=2;
}
else if(Back=="qing"){
BackC=3;
}
else if(Back=="hong"){
BackC=4;
}
else if(Back=="zi"){
BackC=5;
}
else if(Back=="huang"){
BackC=6;
}
else if(Back=="mi bai"||Back=="ru bai"){
BackC=7;
}
else if(Back=="hui"){
BackC=8;
}
else if(Back=="qian lan"){
BackC=9;
}
else if(Back=="qian lv"||Back=="liang lv"){
BackC=10;
}
else if(Back=="lan lv"){
BackC=11;
}
else if(Back=="cheng"){
BackC=12;
}
else if(Back=="fen"){
BackC=13;
}
else if(Back=="mi huang"||Back=="dan huang"){
BackC=14;
}
else if(Back=="bai"){
BackC=15;
}
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
char mpF(char a,int b){ //加密
int c=a;
c=c+b;
if(c>126) c=c-(126-32);
return c;
}
char mpR(char a,int b){ //解密
int c=a;
c=c-b;
if(c<33) c=c+(126-32);
return c;
}
std::string smpF(std::string a,int b){ //加密(字符串)
std::string c;
for(int i=0;a[i];i++){
c.push_back(mpF(a[i],b));
}
return c;
}
std::string smpR(std::string a,int b){ //解密(字符串)
std::string c;
for(int i=0;a[i];i++){
c.push_back(mpR(a[i],b));
}
return c;
}
std::string d_r(signed n,int r){ //十进制转r进制
if(n==0) return 0;
std::string s="";
while(n){
if(r==16){
if(n%r==10) s.push_back('A');
else if(n%r==11) s.push_back('B');
else if(n%r==12) s.push_back('C');
else if(n%r==13) s.push_back('D');
else if(n%r==14) s.push_back('E');
else if(n%r==15) s.push_back('F');
else s.push_back(n%r+'0');
}
else s.push_back(n%r+'0');
n/=r;
}
reverse(s.begin(),s.end());
return s;
}
signed r_d(std::string s,int r){ //r进制转十进制
signed sum=0,quan=1;
for(int i=s.size()-1;i>=0;i--){
sum=sum+(s[i]-48)*quan;
quan=quan*r;
}
return sum;
}
void rgb_init(){ //rgb_set的初始化函数 // 初始化
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); //输入句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //输出句柄
DWORD dwInMode, dwOutMode;
GetConsoleMode(hIn, &dwInMode); //获取控制台输入模式
GetConsoleMode(hOut, &dwOutMode); //获取控制台输出模式
dwInMode |= 0x0200; //更改
dwOutMode |= 0x0004;
SetConsoleMode(hIn, dwInMode); //设置控制台输入模式
SetConsoleMode(hOut, dwOutMode); //设置控制台输出模式
return ;
}
void rgb_set(int wr,int wg,int wb,int br,int bg,int bb) { //设置RGB(比color颜色多)
printf("\033[38;2;%d;%d;%dm\033[48;2;%d;%d;%dm",wr,wg,wb,br,bg,bb); //\033[38表示前景,\033[48表示背景,三个%d表示混合的数
}
void lm(){ //乱码恶搞
rgb_init();
std::string s="aghgjhrvjkhfgkuajhrvjfvkajdsgf,jasdhvfkUSDDFVAGAIDSHDFLAKGFKAJDSHVFAGHSDFVJHDSVFsfskdyfksfgjhsgdfjshgdfjshgdfjhsgdvfhsgdfvghsfvhgvshgf";
do{
for(int i=1;i<=s.size();i++){
rgb_set(255,0,0,0,0,0);
std::cout<<s[i];
Sleep(10);
}
sleep(0.5);
for(int i=1;i<=30;i++) std::cout<<s;
std::cout<<"\a";
MessageBox(NULL,"错误!","提示",MB_ABORTRETRYIGNORE|MB_ICONSTOP);
system("cls");
}while(1);
return ;
}
void cstr(std::string s,signed a=4,signed f=1){ //一个字符一个字符的输出字符串 详解输入CS_HELP
signed Sleep_n;
if(s==CS_HELP){
std::cout<<s;
system("pause");
return ;
}
if(f) Sleep_n=a*100;
else Sleep_n=a*10;
for(int i=0;s[i];i++){
std::cout<<s[i];
Sleep(Sleep_n);
}
return ;
}
void hidden(){ //隐藏光标
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;
SetConsoleCursorInfo(hOut,&cci);
}
void ShowC(){ //显示光标
CONSOLE_CURSOR_INFO cur={1,1};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
}
void HideC(){ //同hidden()
CONSOLE_CURSOR_INFO cur={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
}
std::string getexePath(int argc,char *argv[]){ //返回exe路径
return argv[0];
}
std::string getpath(int argc,char*argv[]){ //返回exe上一级路径
std::string _exePath=getexePath(argc,argv);
return _exePath.substr(0,_exePath.find_last_of('\\'));
}
std::string getexeName(int argc,char* argv[]){ //返回exe名称
std::string _path=getpath(argc,argv);
std::string _exePath=getexePath(argc,argv);
return _exePath.substr(_exePath.find_last_of(_path)+1);
}
//以上3个函数参数为main自带参数:int argc,char*argv[]
#endif
头文件以后会持续更新 不许催更
贪吃蛇
#include<stdio.h>
#include<Windows.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#define H 20
#define W 60
typedef struct body {
int x; int y;
}BODY;
typedef struct snake {
int size;//蛇的长度
BODY list[W * H];//蛇的位置,最大可以铺满整个墙
BODY food;
COORD coord;
int dx;//x轴移动方向
int dy;//y轴移动方向
int score;//分数
BODY tail;
}SNAKE;
//展示界面
void show_wall()
{
for (int i = 0; i <= H; i++)
{
for (int j = 0; j <= W; j++)
{
if (i == H || j == W)printf("+");
else printf(" ");
}
puts("");
}
}
void init_food(BODY* food)
{
srand(time(NULL));
food->x = rand() % W;
food->y = rand() % H;
while (food->x == 0 || food->y == 0)
{
food->x = rand() % W;
food->y = rand() % H;
}
}
void init_snake(SNAKE* snake)
{
snake->size = 2;
snake->list[0].x = W / 2; snake->list[1].x = W / 2 - 1;
snake->list[0].y = H / 2; snake->list[1].y = H / 2;
snake->score = 0;
snake->dx = 1; snake->dy = 0;
init_food(&(snake->food));
}
void hide_cur()//隐藏光标,里面是windows特有的函数,不需要刻意去记,拿来就用
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = sizeof(cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
void show_ui(SNAKE* snake)
{
for (int i = 0; i < snake->size; i++)
{ //定位光标位置,windows自带函数
snake->coord.X = snake->list[i].x; snake->coord.Y = snake->list[i].y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
if (i == 0)printf("@");
else printf("*");
}
snake->coord.X = snake->food.x; snake->coord.Y = snake->food.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("#");
snake->coord.X = snake->tail.x; snake->coord.Y = snake->tail.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf(" ");
}
void move_snake(SNAKE* snake)
{
snake->tail.x = snake->list[snake->size - 1].x;
snake->tail.y = snake->list[snake->size - 1].y;
for (int i = snake->size - 1; i > 0; i--)
{
snake->list[i].x = snake->list[i - 1].x;
snake->list[i].y = snake->list[i - 1].y;
}
snake->list[0].x += snake->dx;
snake->list[0].y += snake->dy;
}
void control_move(SNAKE* snake)
{
char key = 0;
while (_kbhit())
{
key = _getch();
}
switch (key)
{
case 'd':
snake->dx = 1;
snake->dy = 0;
break;
case 's':
snake->dx = 0;
snake->dy = 1;
break;
case 'a':
snake->dx = -1;
snake->dy = 0;
break;
case 'w':
snake->dx = 0;
snake->dy = -1;
break;
}
}
void eat_food(SNAKE* snake)
{
if (snake->food.x == snake->list[0].x && snake->food.y == snake->list[0].y)
{
snake->size++;
snake->score += 10;
init_food(&snake->food);
}
}
void game_over(SNAKE* snake)
{
snake->coord.X = 30; snake->coord.Y = 25;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("游戏结束!!! 得分:%d", snake->score);
system("pause");
}
void eat_snake_body(SNAKE* snake)
{
for (int i = 1; i < snake->size; i++)
{
if (snake->list[0].x == snake->list[i].x && snake->list[0].y == snake->list[i].y)
{
game_over(snake);
exit(0);
}
}
}
void start_game(SNAKE* snake)
{
while (snake->list[0].x >= 0 && snake->list[0].x <= W - 1 && snake->list[0].y >= 0 && snake->list[0].y <= H - 1)
{
show_ui(snake);
control_move(snake);
eat_food(snake);
move_snake(snake);
eat_snake_body(snake);
Sleep(300);
}
game_over(snake);
}
int main()
{
hide_cur();
SNAKE* snake = (SNAKE*)malloc(sizeof(SNAKE));
init_snake(snake);
show_wall();
show_ui(snake);
start_game(snake);
free(snake);
system("pause");
return 0;
}
(加障碍物版)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include<conio.h>
/*
注意:
w:上移
s:下移
a:左移
d:右移
不可以用getchar()来进行控制,getchar()之后要有回车才行,而我们只希望通过wsad这些按键进行操作
但有专门的按键操作函数
#include<conio.h>
char key;//key要给初始值,否则key没值,进不了循环,但初始值也不能是wasd等
while(_kbhit())//判断是否按下按键,按下不等于0
{
key=_getch();
}
*/
char temp;
//1.设置地图边界
#define W 60
#define H 20
//2.设置贪吃蛇结构体
typedef struct _body {
int x; //坐标x
int y; //坐标y
}BODY;
typedef struct snake{
BODY list[W * H];//贪吃蛇最大占满整个地图
int size;//实际身体个数,蛇头(@)和蛇身蛇尾(*)
BODY food;//食物,#
COORD coord;//(定位光标)的坐标,显示
int dx;//移动方向
int dy;//移动方向
BODY tail;//记录蛇尾,用于清除蛇尾痕迹
int score;//分数
BODY disability;
}SNAKE;
//3.实现显示地图,初始化蛇函数,初始化食物函数
void test_ui()
{
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
printf("=");
printf("\n");
}
}
void disability_snake(BODY* disability)
{
Sleep(5);
//设置障碍物坐标
disability->x = rand() % W;
disability->y = rand() % H;
while (disability->x == 0 || disability->x == W - 1 || disability->y == 0 || disability->y == H - 1)
{
disability->x = rand() % W;
disability->y = rand() % H;
}
}
void init_food(BODY* food)
{
//设置随机数种子
srand(time(NULL));
//设置食物坐标
food->x = rand()% W;//[0,W-1],注意%的操作很巧妙
food->y = rand() % H;//[0,H-1]
while(food->x == 0 || food->x == W - 1 || food->y == 0 || food->y == H - 1)
{
food->x = rand() % W;
food->y = rand() % H;
}
}
void init_snake(SNAKE* snake)//初始化
{
snake->list[0].x = W / 2;
snake->list[0].y = H / 2;
snake->list[1].x = W / 2 - 1;
snake->list[1].y = H / 2;//这里老师写成H/2-1应该是错的
snake->size = 2;//初始时蛇的长度为2,因为有头有尾
//设置移动方向
snake->dx = 1;//默认移动方向向右
snake->dy = 0;//默认移动方向向左
snake->score = 0;
init_food(&snake->food);
disability_snake(&snake->disability);
while (snake->food.x == snake->disability.x && snake->food.y == snake->disability.y)
{
init_food(&snake->food);
disability_snake(&snake->disability);
}
};
void show_ui(SNAKE* snake)
{
//显示蛇
for (int i = 0; i < snake->size; i++)
{
snake->coord.X = snake->list[i].x;//这个函数是在显示地图后执行的,画完地图后光标是在地图之外的,用这个函数可以重置光标位置,让光标放到我们想要的地方
snake->coord.Y = snake->list[i].y;
//定位光标位置,windows自带函数
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
if (i == 0) {//显示蛇头
printf("@");
}
else{//显示蛇身和蛇尾
printf("*");
}
}
//显示食物
snake->coord.X = snake->food.x;
snake->coord.Y = snake->food.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("#");
//显示障碍物
snake->coord.X = snake->disability.x;
snake->coord.Y = snake->disability.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("?");
//清除蛇尾的痕迹,否则一连串的*
snake->coord.X = snake->tail.x;
snake->coord.Y = snake->tail.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf(" ");
}
void show_wall()//展示墙
{
for (int i = 0; i <=H; i++)
{
for (int j = 0; j <=W; j++)
{
if (i == H || j == W)printf("+");
else printf(" ");
}
puts("");
}
}
void hide_cur()//隐藏光标,里面是windows特有的函数,不需要刻意去记,拿来就用
{
CONSOLE_CURSOR_INFO cci;
cci.dwSize = sizeof(cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
void move_snake(SNAKE* snake)
{
//记录蛇尾,清除痕迹
snake->tail = snake->list[snake->size - 1];
//蛇身的移动,算法逻辑从后往前赋值
//蛇头的移动,单独赋值
for (int i = snake->size - 1; i > 0; i--)
{
snake->list[i] = snake->list[i - 1];
}
snake->list[0].x += snake->dx;
snake->list[0].y += snake->dy;
}
void control_snake(SNAKE* snake)
{
char key = 0;
while (_kbhit())//检测到按下按键
{
if (temp == 'w' && _kbhit() == 's')key = temp;
else if (temp == 's' && _kbhit() == 'w')key = temp;
else if (temp == 'a' && _kbhit() == 'd')key = temp;
else if (temp == 'd' && _kbhit() == 'a')key = temp;
else key = _getch();
}
//循环之外,抬起按下动作,或没有按下动作
//根据按键,修改移动方向
temp = key;
switch (key)
{
case 'w':
snake->dy = -1;
snake->dx = 0;
break;
case 's':
snake->dy = 1;
snake->dx = 0;
break;
case 'a':
snake->dy = 0;
snake->dx = -1;
break;
case 'd':
snake->dy = 0;
snake->dx = 1;
}
}
void snake_eat(SNAKE* snake)
{
//判断是否吃到食物
if (snake->list[0].x == snake->food.x && snake->list[0].y == snake->food.y)
{
//吃到食物,身体加长,食物消失,重新生成新的食物
snake->size++;//食物消失也不用重新写,因为一旦生成了食物原来的地方就消失了
snake->coord.X = snake->disability.x;
snake->coord.Y = snake->disability.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf(" ");
init_food(&(snake->food));
disability_snake(&snake->disability);
printf(" ");
while (snake->food.x == snake->disability.x && snake->food.y == snake->disability.y)
{
init_food(&snake->food);
disability_snake(&snake->disability);
}
snake->score += 10;
}
}
void game_over(SNAKE* snake)//游戏结束,指定到具体位置打印游戏结束
{
snake->coord.X = 40;
snake->coord.Y = 25;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), snake->coord);
printf("游戏结束 总分数:%d", snake->score);
system("pause");
}
void snake_eat_body(SNAKE* snake)
{
for (int i = 1; i < snake->size; i++)
{
if (snake->list[0].x == snake->list[i].x && snake->list[0].y == snake->list[i].y)
{
game_over(snake);//吃到身体;游戏结束
exit(0);
}
}
}
void snake_push_disability(SNAKE* snake)
{
if (snake->disability.x == snake->list[0].x && snake->disability.y == snake->list[0].y)
{
game_over(snake);
exit(0);
}
}
void start_game(SNAKE* snake)
{
while (snake->list[0].x >=0 && snake->list[0].x < W &&
snake->list[0].y>=0 && snake->list[0].y < H)//在范围内移动
{
//显示蛇和食物
show_ui(snake);
//控制移动方向
control_snake(snake);
//蛇吃到食物
snake_eat(snake);
//蛇吃到身体
snake_eat_body(snake);
//蛇撞到障碍物
snake_push_disability(snake);
//移动蛇
move_snake(snake);
//延迟300ms
Sleep(300);
}
game_over(snake);//碰到墙壁,游戏结束
}
int main(int argc,char *argv[])
{
hide_cur();//隐藏光标
SNAKE* snake = (SNAKE*)malloc(sizeof(SNAKE));//创建空间
init_snake(snake);
//显示地图边界
show_wall();
//显示蛇和食物
show_ui(snake);
//启动游戏
start_game(snake);
free(snake);//释放空间
//while (1);//先不让程序结束,就暂时不显示程序结束的那段字
return 0;
}
有兴趣的话 可以再看一遍
再看一遍
↑