#include<stdlib.h>
#include<math.h>
void add(float a,float b)
void sub(float a,float b)
void mul(float a,float b)
void dv(float a,float b)
void p(float a,float b)
void main()
{ float a,b,m
char ch
while(1)
{printf("input first number:")
scanf("%f",&a)
getchar()
printf("input char:")
scanf("%c",&ch)
switch(ch)
{ case '+':printf("input second number:")scanf("%f",&b)add(a,b)break
case '-':printf("input second number:")scanf("%f",&b)sub(a,b)break
case '*':printf("input second number:")scanf("%f",&b)mul(a,b)break
case '/':printf("input second number:")scanf("%f",&b)
{if(b==0)
{printf("Divided by zero.\n")break}
else
{dv(a,b)break}
}
case 's':m=sqrt(a)printf("result=%f\n",m)break
case 'x':m=1.0/aprintf("result=%f\n",m)break
case 'c':a=0break
case 'p':printf("input second number:")scanf("%f",&b)p(a,b)break
case 'q':printf("User exit.\n")
return
default:printf("illegal operator.\n")
}
}
}
void add(float a,float b)
{ float m=0,n
char ch
m=a+b
printf("result=%f\n",m)
getchar()
while(1)
{printf("input char:")
scanf("%c",&ch)
switch(ch)
{ case '+':printf("input second number:")scanf("%f",&n)add(m,n)break
case '-':printf("input second number:")scanf("%f",&n)sub(m,n)break
case '*':printf("input second number:")scanf("%f",&n)mul(m,n)break
case '/':printf("input second number:")scanf("%f",&n)
{if(n==0)
{printf("Divided by zero.\n")break}
else
{dv(m,n)break}
}
case 's':m=sqrt(m)printf("result=%f\n",m)break
case 'x':m=1.0/mprintf("result=%f\n",m)break
case 'p':printf("input second number:")scanf("%f",&n)p(m,n)break
case 'c':printf("User return.\n")
return
case 'q':exit(0)
default:printf("illegal operator.\n")
}
}
}
void sub(float a,float b)
{ float m=0,n
char ch
m=a-b
printf("result=%f\n",m)
getchar()
while(1)
{printf("input char:")
scanf("%c",&ch)
switch(ch)
{ case '+':printf("input second number:")scanf("%f",&n)add(m,n)break
case '-':printf("input second number:")scanf("%f",&n)sub(m,n)break
case '*':printf("input second number:")scanf("%f",&n)mul(m,n)break
case '/':printf("input second number:")scanf("%f",&n)
{if(n==0)
{printf("Divided by zero.\n")break}
else
{dv(m,n)break}
}
case 's':m=sqrt(m)printf("result=%f\n",m)break
case 'x':m=1.0/mprintf("result=%f\n",m)break
case 'p':printf("input second number:")scanf("%f",&n)p(m,n)break
case 'c':printf("User return.\n")
return
case 'q':exit(0)
default:printf("illegal operator.\n")
}
}
}
void mul(float a,float b)
{ float m=0,n
char ch
m=a*b
printf("result=%f\n",m)
getchar()
while(1)
{printf("input char:")
scanf("%c",&ch)
switch(ch)
{ case '+':printf("input second number:")scanf("%f",&n)add(m,n)break
case '-':printf("input second number:")scanf("%f",&n)sub(m,n)break
case '*':printf("input second number:")scanf("%f",&n)mul(m,n)break
case '/':printf("input second number:")scanf("%f",&n)
{if(n==0)
{printf("Divided by zero.\n")break}
else
{dv(m,n)break}
}
case 's':m=sqrt(m)printf("result=%f\n",m)break
case 'x':m=1.0/mprintf("result=%f\n",m)break
case 'p':printf("input second number:")scanf("%f",&n)p(m,n)break
case 'c':printf("User return.\n")
return
case 'q':exit(0)
default:printf("illegal operator.\n")
}
}
}
void dv(float a,float b)
{ float m=0,n
char ch
m=a/b
printf("result=%f\n",m)
getchar()
while(1)
{printf("input char:")
scanf("%c",&ch)
switch(ch)
{ case '+':printf("input second number:")scanf("%f",&n)add(m,n)break
case '-':printf("input second number:")scanf("%f",&n)sub(m,n)break
case '*':printf("input second number:")scanf("%f",&n)mul(m,n)break
case '/':printf("input second number:")scanf("%f",&n)
{if(n==0)
{printf("Divided by zero.\n")break}
else
{dv(m,n)break}
}
case 's':m=sqrt(m)printf("result=%f\n",m)break
case 'x':m=1.0/mprintf("result=%f\n",m)break
case 'p':printf("input second number:")scanf("%f",&n)p(m,n)break
case 'c':printf("User return.\n")
return
case 'q':exit(0)
default:printf("illegal operator.\n")
}
}
}
void p(float a,float b)
{ float m=1,n
int i
char ch
for(i=1i<=bi++)
{m=m*a
}
printf("result=%f\n",m)
getchar()
while(1)
{printf("input char:")
scanf("%c",&ch)
switch(ch)
{ case '+':printf("input second number:")scanf("%f",&n)add(m,n)break
case '-':printf("input second number:")scanf("%f",&n)sub(m,n)break
case '*':printf("input second number:")scanf("%f",&n)mul(m,n)break
case '/':printf("input second number:")scanf("%f",&n)
{if(n==0)
{printf("Divided by zero.\n")break}
else
{dv(m,n)break}
}
case 's':m=sqrt(m)printf("result=%f\n",m)break
case 'x':m=1.0/mprintf("result=%f\n",m)break
case 'p':printf("input second number:")scanf("%f",&n)p(m,n)break
case 'c':printf("User return.\n")
return
case 'q':exit(0)
default:printf("illegal operator.\n")
}
}
}
老师修改过的,很准确
最简单的模拟计时器:
#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)
}
一个“歼灭敌机”的小游戏,DEVc++通过编译:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#define zlx 10 //增量坐标(x)让游戏框不靠边
#define zly 3 //增量坐标(y)让游戏框不靠边
#define W 26 //游戏框的宽度
#define H 24 //游戏框的高度
int jiem[22][22]={0}, wj=10 //界面数组, 我机位置(初值为10)
int speed=4,density=30, score=0,death=0//敌机速度, 敌机密度, 玩家成绩,死亡次数
int m=0,n=0 // m,n是控制敌机的变量
void gtxy (int x, int y) //控制光标位置的函数
{ COORD pos
pos.X = x pos.Y = y
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos )
}
void Color(int a) //设定颜色的函数(a应为1-15)
{ SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), a )}
void yinc(int x=1,int y=0) //隐藏光标的函数
{ CONSOLE_CURSOR_INFO gb={x,y} //y设为0即隐藏
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb)
}
void csh( ) //初始化函数
{ int i
Color(7)
gtxy(zlx,zly)printf("╔") gtxy(zlx+W-2,zly)printf("╗") //左上角和右上角的框角
gtxy(zlx,zly+H-1)printf("╚")gtxy(zlx+W-2,zly+H-1)printf("╝")//下边两框角
for(i=2i<W-2i+=2) {gtxy(zlx+i,zly) printf("═")} //打印上横框
for(i=2i<W-2i+=2) {gtxy(zlx+i,zly+H-1)printf("═")} //打印下横框
for(i=1i<H-1i++) { gtxy(zlx,zly+i) printf("║")} //打印左竖框
for(i=1i<H-1i++) {gtxy(zlx+W-2,zly+i)printf("║")} //打印右竖框
Color(14)gtxy(19,2)printf("歼灭敌机")Color(10)
gtxy(37,5)printf("设置:Esc ")
gtxy(37,7)printf("发射:↑ ")
gtxy(37,9)printf("控制:← → ")
gtxy(37,11)printf("得分:%d",score)
gtxy(37,13)printf("死亡:%d",death)
yinc(1,0)
}
void qcjm( ) //清除界面函数
{int i,j
for(i=0i<H-2i++)
for(j=0j<W-4j++){gtxy(zlx+2+j,zly+1+i)printf(" ")}
}
void feiji( ) //飞机移动函数
{int i,j
for(i=21i>=0i--) //从底行往上是为了避免敌机直接冲出数组
for(j=0j<22j++)
{if(i==21&&jiem[i][j]==3) jiem[i][j]=0 //底行赋值0 以免越界
if(jiem[i][j]==3) jiem[i][j]=0, jiem[i+1][j]=3
}
if(jiem[20][wj]==3&&jiem[21][wj]==1) death++
}
void zidan( ) //子d移动函数
{ int i,j
for(i=0i<22i++)
for(j=0j<22j++)
{if(i==0&&jiem[i][j]==2) jiem[i][j]=0
if(jiem[i][j]==2) { if(jiem[i-1][j]==3) score+=100,printf("\7")
jiem[i][j]=0,jiem[i-1][j]=2}
}
}
void print( ) //输出界面函数
{int i,j
qcjm( )
for(i=0i<22i++)
for(j=0j<22j++)
{ gtxy(12+j,4+i)
if(jiem[i][j]==3) {Color(13)printf("□")}
if(jiem[i][j]==2) {Color(10)printf(".")}
if(jiem[i][j]==1) {Color(10)printf("■")}
}
gtxy(37,11)Color(10)printf("得分:%d",score)
gtxy(37,13)printf("死亡:%d",death)
}
void setting( ) //游戏设置函数
{ qcjm( )
gtxy(12,4)printf("选择敌机速度:")
gtxy(12,5)printf(" 1.快 2.中 3.慢>>")
switch(getche( ))
{case '1': speed=2break
case '2': speed=4break
case '3': speed=5break
default: gtxy(12,6)printf(" 错误!默认值")
}
gtxy(12,7)printf("选择敌机密度:")
gtxy(12,8)printf(" 1.大 2.中 3.小>>")
switch(getche( ))
{case '1': density=20break
case '2': density=30 break
case '3': density=40break
default: gtxy(12,9)printf(" 错误!默认值")
}
for(int i=0i<22i++)
for(int j=0j<22j++) jiem[i][j]=0
jiem[21][wj=10]=1jiem[0][5]=3
gtxy(12,10)printf(" 按任意键保存...")
getch( )
qcjm( )
}
void run( ) //游戏运行函数
{ jiem[21][wj]=1 //值为1代表我机(2则为子d)
jiem[0][5]=3 //值为3代表敌机
SetConsoleTitle("歼灭敌机") //设置窗口标题
while(1)
{ if (kbhit( )) //如有键按下,控制我机左右移动、发射或进行设定
{int key
if((key=getch( ))==224) key=getch( )
switch(key)
{case 75: if(wj>0) jiem[21][wj]=0,jiem[21][--wj]=1break
case 77: if(wj<20) jiem[21][wj]=0,jiem[21][++wj]=1 break
case 72: jiem[20][wj]=2break
case 27: setting( )
}
}
if(++n%density==0) //控制产生敌机的速度
{ n=0srand((unsigned)time(NULL))
jiem[0][rand( )%20+1]=3
}
if(++m%speed==0) { feiji( )m=0} //控制敌机移动速度(相对子d而言)
zidan( ) //子d移动
print( ) //输出界面
Sleep(120) //延时120毫秒
}
}
int main( )
{csh( )
run( )
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)