最简单的模拟计时器:
#include<stdio.h>
#include<conio.h>
#include<windows.h>
int m=0,s=0,ms=0 //m是分 s是秒 ms是毫秒
//以下是5个自编函数
void csh( ) //初始化界面
void yinc(int x,int y) //隐藏光标的函数(y值设为0就会隐藏)
void jishi( ) //计时器运行(每100毫秒变化一次)
void Color (short x, short y) //设定颜色的函数(y设为0就是黑底)
void gtxy (int x, int y) 闷带//控制光标位置的函数
int main( ) //主函数
{ csh( )
getch( )
while(1)
{ jishi( )
Sleep(100) //间隔100毫秒
if( kbhit( ) )break //有键按下就退出循环
}
return 0
}
void csh( ) //初始化界面
{Color(14,0) //设定淡黄字配黑底
printf(“\n\n\t 计时器”)
Color(10,0) //设定淡绿字配黑底
printf("\n\t┌───────────┐")
printf("\n\t│ 汪罩橘 困团 │")
printf("\n\t└───────────┘")
gtxy(10,4) //光标到屏幕第10列4行处输出
Color(7,0) //恢复白字黑底
printf(" 00:00:00 ")
yinc(1,0 ) //隐藏光标(yinc代表隐藏)
return
}
void jishi( ) //计时器运行
{ms+=1
if(ms==10){s+=1ms=0}
if(s==60){m+=1s=0}
gtxy(10,4)
Color(9,0) //设定淡蓝字配黑底
if(m>9) printf(" %d:",m)
else printf(" 0%d:",m)
Color(14,0) //设定淡黄字配黑底
if(s>9) printf("%d:",s)
else printf("0%d:",s)
Color(12,0) //设定淡红字配黑底
printf("0%d",ms)
}
void gtxy (int x, int y) //控制光标位置的函数
{ COORD pos
pos.X = x
pos.Y = y
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos )
}
void Color (short ForeColor= 7, short BackGroundColor= 0) //设定颜色的函数
{ HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE )
SetConsoleTextAttribute ( handle, ForeColor + BackGroundColor * 0x10 )
}
void yinc(int x,int y) //隐藏光标的设置(gb代表光标)
{ CONSOLE_CURSOR_INFO gb={x,y} //x为1-100,y为0就隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb)
}
学习“推箱子”C语言编码:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0 //m代表第几关
struct maps{short a[9][11]}
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
衡蚂巧 1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
物野 1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
咐键 1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 }
void DrMap( ) //绘制地图
{ CONSOLE_CURSOR_INFO cursor_info={1,0} //隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info)
printf("\n\n \t\t\b推箱子")
printf("\n \t")
for (int i = 0i <9i++)
{for (int j = 0j <11j++)
{switch (map[m].a[i][j])
{case 0: printf(" ")break
case 1: printf("■")break
case 3: printf("◎")break
case 4: printf("□")break
case 5: printf("♀")break //5是人
case 7: printf("□")break //4 + 3箱子在目的地中
case 8: printf("♀")break // 5 + 3人在目的地中
}
}
printf("\n\t")
}
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord
coord.X = x
coord.Y = y
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord)
}
void start( ) //开始游戏
{ int r, c //r,c用于记录人的下标
for (int i = 0i <9i++)
{ for (int j = 0j <11j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i c = j} } //i j 人的下标
}
char key
key = getch( )
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3)printf("♀") // gtxy(2*c+8,r-1+3)是到指定位置输出字符
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r - 1][c] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3)printf("□")gtxy(2*c+8,r-1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r - 2][c] += 4 map[m]. a [r - 1][c] += 1
map[m]. a [r][c] -= 5}
} break
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r + 1][c] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3)printf("□")gtxy(2*c+8,r+1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r + 2][c] += 4map[m]. a [r + 1][c] += 1
map[m]. a [r][c] -= 5}
}break
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r ][c - 1] += 5map[m]. a [r][c] -= 5}
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3)printf("□")gtxy(2*(c-1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r ][c - 2] += 4map[m]. a [r ][c - 1] += 1
map[m]. a [r][c] -= 5}
}break
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r][c + 1] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3)printf("□")gtxy(2*(c+1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r][c + 2] += 4map[m]. a [r][c + 1] += 1
map[m]. a [r][c] -= 5}
}break
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&&map[m].a[5][3]==7&&
map[m].a[6][2]==7&&map[m].a[6][3]==7) return 1}
if(m==1){if(map[m].a[5][2]==7&&map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1}
if(m==2){if(map[m].a[7][1]==7&&map[m].a[7][2]==7&&map[m].a[7][3]==7&&
map[m].a[7][4]==7&&map[m].a[7][5]==7) return 1}
if(m==3){if(map[m].a[4][4]==7&&map[m].a[4][5]==7&&map[m].a[4][6]==7&&
map[m].a[5][4]==7&&map[m].a[5][5]==7&&map[m].a[5][6]==7) return 1}
if(m==4){if(map[m].a[3][2]==7&&map[m].a[4][1]==7&&map[m].a[4][2]==7&&
map[m].a[5][1]==7&&map[m].a[5][2]==7) return 1}
return 0
}
int main( ) //主函数
{ while (1)
{ system("cls")
DrMap( )
while (1)
{ start( )
if(ifwan()){printf(" }7")break} //完成后响铃
m+=1
}
return 0
}
#include <stdio.h>#include <stdlib.h>
#define NUM 10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//冒泡排序算法
//基本思想:比较相邻的两个数,如果前者比后者大,则进行交换。每一轮排序结束,选出一个未排序中最大的数放到数组后面。
void bubbleSort(int *arr, int n) {
int i,j
for (i = 0i<裤激埋n - 1i++)
for (j = 0j <n - i - 1j++) {
//如果前面的数比后面大,进行交换
if (arr[j] >arr[j + 1]) {
int temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
//最差时间复杂度为O(n^2),平均时间复杂度为O(n^2)。稳定性:稳定。辅助空间O(1)。
//升级版冒泡排序法:通过从低到高选出最大的数放到后面,再从高到低选出最小的数放到前面,
//如此反复,直到左边界和右边界重合。当数组中有已排序好的数时,这种排序比传统冒泡排序性能稍好。
//升级版冒泡排序算法
void bubbleSort_1(int *arr, int n) {
//设置数组左右边界
int left = 0, right = n - 1
//当左右铅腊边界未重合时,进行排序
while (left<=right) {
int i,j
//从左到右遍历选出最大的数放到数组右边
for (i =lefti <righti++) {
if (arr[i] >arr[i + 1]) {
int temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
}
}
right--
//从右到左遍历选出最小的数放到数组左边
for (j = rightj>leftj--) {
if (arr[j + 1] <arr[j]) {
int temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
left++
}
}
int main(int argc, char *argv[]) {
int arr[NUM],i,j,temp
printf("请输入10个数:\n")
for(i=0i<NUMi++) {
printf("请输入第(%d)个数:",i+1)
scanf("%d",&arr[i])
}
printf("\n输入如下排列:\n")
for(i=0i<NUMi++) {
printf("%4d",arr[i])
}/*
for(i=0i<NUMi++) {
for(j=i+1j<NUMj++) {
if(arr[i]>arr[j]) {
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
}
}
}*/
bubbleSort_1(arr,NUM)
/*printf("\n从小到大如下排列:\n")
for(i=0i<NUMi++) {
printf("%4d",arr[i])
}*/
printf("\n从大到小如下排列:胡蚂\n")
for(i=NUM-1i>=0i--) {
printf("%4d",arr[i])
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)