c语言时钟程序整点报时,C语言 · 报时助手(示例代码)_Mage Anti的博客-CSDN博客
#include "stdafx.h"
#include "util.h"
#include<math.h>
#include<time.h>
#define PI 3.14159265358979323846
#define ESC 27
float s0=-1,m0=-1,h0=-1;
// 绘制整个时钟
void timer()
{
/************************************************************************************************/
struct tm*when;/*考试要求6:添加代码,定义一个指向结构体tm的指针when*/
float h,m,s;
/************************************************************************************************/
/************************************************************************************************/
/*考试要求7:添加2行代码,利用time函数和time_t类型的now变量,实现获取系统当前时间
//函数功能:获取当前时间
//函数原型:time_t time(time_t *timer) */
/************************************************************************************************/
time_t now;
now = time(NULL);
when = localtime(&now); // when结构指针指向当前时间结构
h=when->tm_hour; // 提取时分秒
m=when->tm_min;
s=when->tm_sec;
if(s0>=0) // 用白色重绘上一次的时分秒对应的三根针
{
setpen(1, WHITE);
line(320,240,320+45*cos((h0+(m0*60+s0)/3600)*PI/6-PI/2), 240+45*sin((h0+(m0*60+s0)/3600)*PI/6-PI/2));
line(320,240,320+55*cos((m0+s0/60)*(PI/30)-PI/2),240+55*sin((m0+s0/60)*(PI/30)-PI/2));
line(320,240,320+65*cos((s0)*(PI/30)+PI/2),240+65*sin((s0)*(PI/30)+PI/2));
}
// 用红、灰、蓝三种颜色分别绘画时分秒三根针
/************************************************************************************************/
/*考试要求9:添加代码,利用line函数绘制时针,并将时针长度缩短为38*/
/************************************************************************************************/
setpen(1, RED);
line(320,240,320+38*cos((h+(m*60+s)/3600)*PI/6-PI/2), 240+38*sin((h+(m*60+s)/3600)*PI/6-PI/2));
setpen(1, GRAY);
line(320,240,320+55*cos((m+s/60)*(PI/30)-PI/2),240+55*sin((m+s/60)*(PI/30)-PI/2));
setpen(1, BLUE);
line(320,240,320+65*cos((s)*(PI/30)+PI/2),240+65*sin((s)*(PI/30)+PI/2));
s0=s; // 记录最新时间的时分秒
m0=m;
h0=h;
/************************************************************************************************/
/*考试要求10:添加代码,利用circle函数和rectangle函数在空白画布画出下图中的图形,
其中两个圆形相切,两个圆心坐标分别为(200,400)(400,400),
且左边的圆形内接于一正方形内。*/
/************************************************************************************************/
/*setpen(1, BLUE);
circle(200,400,100);*/
}
void main()
{
int i=3;
char H[10];
float n;
// 画小熊耳朵
/************************************************************************************************/
/*考试要求2:添加三行代码,实现小熊时钟耳朵外沿用红色包裹*/
/************************************************************************************************/
setpen(35, YELLOW);
circle(405, 175, 8);
circle(235,175,8);
setpen(1, RED);
circle(405, 175, 19);
setpen(1, RED);
circle(235,175,19);
setpen(1, GREEN);
circle(405, 175, 18);
circle(235,175,18);
// 画小熊脸
/************************************************************************************************/
/*考试要求3:添加两行代码,利用circle函数(线宽为5)实现蓝色的小熊脸蛋*/
/************************************************************************************************/
setpen(5, BLUE);
circle(320, 240, 90);
setpen(1, BLUE);
for(n=0;n<=2*PI;n+=PI/30)
{
/************************************************************************************************/
/*考试要求3:添加代码,利用putpixel函数实现蓝色的分钟小刻度*/
/************************************************************************************************/
}
for(n=0;n<=2*PI;n+=PI/6)
{
setpen(1, BLUE);
circle(320+80 * cos(-n), 240+80 * sin(-n),1); /*画标识小时的大刻度*/
}
setpen(1, RGB(0, 255, 0));
/************************************************************************************************/
/*考试要求4:添加代码,利用for语句实现小时大刻度对应的数字*/
/************************************************************************************************/
for(n=0;n<=2*PI;n+=PI/6)
{
itoa(i,H,10);
text(315+73 * cos(-n), 232+73 * sin(-n),H); /*画标识小时的大刻度对应的数字*/
i--;
if(i<1)
i+=12;
}
/************************************************************************************************/
/*考试要求5:添加代码,从时钟的数字9的位置开始显示“STHU”四个字母 */
/************************************************************************************************/
while(getkey()<0)
{
timer();
delay(1000);
}
}
下面是补充完成整点报时功能的代码:
#include "stdafx.h"
#include "util.h"
#include<math.h>
#include<time.h>
#define PI 3.14159265358979323846
#define ESC 27
float s0=-1,m0=-1,h0=-1;
// 绘制整个时钟
void timer()
{
struct tm*when;/考试要求6:添加代码,定义一个指向结构体tm的指针when/
float h,m,s;
// 获取当前时间
time_t now;
now = time(NULL);
when = localtime(&now); // when结构指针指
向当前时间结构
h=when->tm_hour; // 提取时分秒
m=when->tm_min;
s=when->tm_sec;
if(s0>=0) // 用白色重绘上一次的时分秒对应的三根针
{
setpen(1, WHITE);
line(320,240,320+45*cos((h0+(m0*60+s0)/3600)*PI/6-PI/2), 240+45*sin((h0+(m0*60+s0)/3600)*PI/6-PI/2));
line(320,240,320+55*cos((m0+s0/60)*(PI/30)-PI/2),240+55*sin((m0+s0/60)*(PI/30)-PI/2));
line(320,240,320+65*cos((s0)*(PI/30)+PI/2),240+65*sin((s0)*(PI/30)+PI/2));
}
// 用红、灰、蓝三种颜色分别绘画时分秒三根针
setpen(1, RED);
line(320,240,320+38*cos((h+(m*60+s)/3600)*PI/6-PI/2), 240+38*sin((h+(m*60+s)/3600)*PI/6-PI/2));
setpen(1, GRAY);
line(320,240,320+48*cos((m+s/60)*(PI/30)-PI/2),240+48*sin((m+s/60)*(PI/30)-PI/2));
setpen(1, BLUE);
line(320,240,320+58*cos((s)*(PI/30)+PI/2),240+58sin((s)(PI/30)+PI/2));
// 更新当前时分秒值
s0=s;
m0=m;
h0=h;
// 如果是整点,则显示圆圈
if (m == 0 && s == 0)
{
setpen(1, BLACK);
setbrush(1, BLACK);
circle(320, 240, 60);
sleep(60); // 等待 60 秒
setbrush(1, WHITE);
circle(320, 240, 60); // 隐藏圆圈
}
}
int main()
{
initwindow(640, 480, "", -3, -3, 0); // 初始化窗口
while (1)
{
timer(); // 绘制时钟
sleep(1); // 等待 1 秒
}
return 0;
}