有哪些简单有趣的c/c++小程序
简单有趣的小程序有忆年共享相册、云梦助眠引导、番茄闹钟等。
1. 忆年共享相册:忆年是一款为用户提供免费的照片存储及共享的应用软件。界面非常简洁,可点击底部按钮生成相册,原图照片都储存在云端,不过期,支持批量上传和下载。在微信中一键邀请分享,能够和亲朋好友共享照片。
2. 云梦助眠引导:它是一款有专业音乐人打造的音频助眠应用。清爽的页面设计,精选了10个睡眠场景。舒缓的音乐+白噪音+人声催眠,专业度高。晚上睡不着觉是最让人头疼的事情了,这款专业的软件可以伴你迅速入眠。
3. 番茄闹钟:有别于一般的闹钟,你可以在接下来的25分钟选择一个任务然后专注的做这件事,番茄闹钟会开始计时。同时提供白噪音,让人专心沉浸在事物中。能够有效地使人集中精力做一件事,随时保持克制。
2048小游戏
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int jsk(); //计算空格数
void rsgm(); //重置游戏
void inkey(); //按键输入
void mvl(); //向左移动
void mvr(); //向右移动
void mvu(); //向上移动
void mvd(); //向下移动
void shc(); //输出界面
void adnum(); //添加随机数
void ifov(); //游戏是否结束
int bd[4][4]; //存储16个格子中的数字
int score = 0; //每局得分
int best = 0; //最高得分
int ifnum; //1为需要添加0不需要
int over; // 1为游戏结束0没有结束
int i,j,k;
int main( )
{ rsgm( ); //重置游戏
inkey( ); //按键输入
return 0;
}
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
} //用于控制字符颜色的函数
void rsgm( ) //重置游戏
{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //启动随机数发生器
int n = rand( ) % 16; //随机函数产生0-15的数字
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
{ if (n == 0) { int k = rand() % 3; if (k == 0 || k == 1) { bd[i][j] = 2; }
else { bd[i][j] = 4; } n--; }
else { bd[i][j] = 0; n--; }
}
}
adnum( );
system("cls");
shc( );
}
void inkey( ) //按键输入
{ int key;
while (1)
{ key = getch( );
if (over) { if (key == 89|| key == 121) {rsgm( ); continue; }
else if (key == 78|| key == 110) { return; }
else continue; }
ifnum = 0;
if(key==224)key=getch( );
switch (key)
{ case 75: mvl( ); break;
case 77: mvr( ); break;
case 72: mvu( ); break;
case 80: mvd( );break;
}
if (score > best) { best = score; }
if (ifnum) { adnum( ); shc( ); }
}
}
void shc( ) //输出界面
{COORD pos = { 0, 0 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
setColor(12, 0); //设置字体淡红色,背景为黑色
printf(" 游戏: 2048 得分: %d 最高: %d\n", score, best); setColor(7, 0);
printf("***********************************************\n\n");
printf(" ┌──────┬──────┬──────┬──────┐\n");
for (int i = 0; i < 4; i++)
{ printf(" │");
for (int j = 0; j < 4; j++)
{ if (bd[i][j] != 0)
{ if (bd[i][j] < 10)
{if (bd[i][j] == 2) {setColor(14, 0); }
else if (bd[i][j] == 4) {setColor(6, 0); }
else if (bd[i][j] == 8) {setColor(12, 0); }
printf(" %d", bd[i][j]); setColor(7, 0); printf(" │"); }
else if (bd[i][j] < 100)
{if (bd[i][j] == 16) {setColor(4, 0); }
else if (bd[i][j] == 32) {setColor(10, 0); }
else if (bd[i][j] == 64) {setColor(2, 0); }
printf(" %d", bd[i][j]); setColor(7, 0); printf(" │"); }
else if (bd[i][j] < 1000)
{if (bd[i][j] == 128) {setColor(9, 0); }
else if (bd[i][j] == 256) {setColor(1, 0); }
else if (bd[i][j] == 512) {setColor(13, 0); }
printf(" %d", bd[i][j]); setColor(7, 0); printf(" │");}
else if (bd[i][j] < 10000)
{if (bd[i][j] == 1024) {setColor(5, 0); }
else {setColor(15, 0); }
printf(" %4d", bd[i][j]); setColor(7, 0); printf(" │");}
else {int n = bd[i][j];
for (int k = 1; k < 30; k++)
{ n /= 2;
if (n == 1) {setColor(7, 0); printf("2^%2d |", k); break; }
}
}
}
else {setColor(0, 0); printf(" ");setColor(7, 0); printf(" │"); }
}
setColor(7, 0);
if (i <= 2) { printf("\n ├──────┼──────┼──────┼──────┤\n"); }
else printf ("\n └──────┴──────┴──────┴──────┘\n\n");
}
printf("***********************************************\n\n"); setColor(13, 0);
printf(" 操作:↑ ↓ ← → ");
if (jsk( ) == 0) { ifov( );
if (over) { setColor(10, 0); printf("\n\n 游戏结束!是否继续? [ Y/N ]:"); }
}
}
int jsk( ) //计算空格数
{ int n = 0;
for (i = 0; i < 4; i++)
{ for (j = 0; j < 4; j++) { if ( bd[i][j] == 0) {n++;}} }
return n;
}
void mvl( ) //向左移动
{ for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{ if (bd[i][j] > 0)
{ if ( bd[i][k] == bd[i][j])
{ bd[i][k] *= 2; k++;
score = score + 2 * bd[i][j];
bd[i][j] = 0; ifnum = 1; }
else if ( bd[i][k] == 0) { bd[i][k] = bd[i][j]; bd[i][j] = 0; ifnum = 1; }
else { bd[i][k + 1] = bd[i][j]; if ((k + 1) != j) { bd[i][j] = 0; ifnum = 1; }
k++; }
}
}
}
}
void mvr( ) //向右移动
{for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (bd[i][j] > 0)
{ if (bd[i][k] == bd[i][j])
{bd[i][k] *= 2; k--; score = score + 2 * bd[i][j]; bd[i][j] = 0; ifnum = 1; }
else if ( bd[i][k] == 0) {bd[i][k] = bd[i][j]; bd[i][j] = 0; ifnum = 1; }
else { bd[i][k - 1] = bd[i][j]; if ((k - 1) != j) { bd[i][j] = 0; ifnum = 1; } k--; }
}
}
}
}
void mvu( ) //向上移动
{for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{if (bd[j][i] > 0)
{if ( bd[k][i] == bd[j][i]) { bd[k][i] *= 2; k++;score = score + 2 * bd[j][i];
bd[j][i] = 0; ifnum = 1; }
else if ( bd[k][i] == 0) { bd[k][i] = bd[j][i]; bd[j][i] = 0; ifnum = 1; }
else { bd[k + 1][i] = bd[j][i]; if ((k + 1) != j) { bd[j][i] = 0; ifnum = 1; }
k++; }
}
}
}
}
void mvd( ) //向下移动
{ for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (bd[j][i] > 0)
{if (bd[k][i] == bd[j][i])
{bd[k][i] *= 2; k--;score = score + 2 * bd[j][i]; bd[j][i] = 0; ifnum = 1; }
else if (bd[k][i] == 0) {bd[k][i] = bd[j][i]; bd[j][i] = 0; ifnum = 1; }
else {bd[k - 1][i] = bd[j][i];
if ((k - 1) != j) {bd[j][i] = 0; ifnum = 1; } k--; }
}
}
}
}
void adnum( ) //添加随机数
{ srand(time(0)); int n = rand( ) % jsk( );
for (int i = 0; i < 4; i++)
{for (int j = 0; j < 4; j++)
{ if (bd[i][j] == 0) {if (n != 0) { n--; }
else {int k = rand() % 3;
if (k == 0 || k == 1) {bd[i][j] = 2; return; }
else {bd[i][j] = 4; return; } }
}
}
}
}
void ifov( ) //游戏是否结束
{ for (int i = 0; i < 4; i++)
{for (int j = 0; j < 3; j++)
{if (bd[i][j] == bd[i][j + 1] || bd[j][i] == bd[j + 1][i]) {over = 0; return; }}
}
over = 1;
}