题目18:模拟时钟 功能:通过编程最终使电脑屏幕显示模拟时钟,并正常运行显示表针时间电脑上呈现一个图种的模型表示电脑上的时间,并可以模拟时钟走动。难度系数为A 基本要求: 1.在屏幕上最终显示一个图形时钟(用程序绘制一个与时钟样式相似即 可),能模拟机械钟表行走; 2.数字式显示时间与指针式显示时间一致,且各指针运动规律正确; 3.能准确的利用数字显示时间和日期; 4.按ESC可以退出程序; 5.能整点报时; 6.可以设置闹钟。要一段c语言代码
时间: 2024-03-21 20:43:51 浏览: 157
以下是一个简单的C语言代码实现,基于Windows环境,使用了BGI图形库:
```
#include <graphics.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#define pi 3.1415926
void drawClock(int hour, int minute, int second);
void drawTime(int hour, int minute, int second);
void drawHands(int hour, int minute, int second);
void drawAlarm(int hour, int minute);
void alarm(int hour, int minute);
int main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
while(1)
{
// 获取当前时间
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
// 绘制时钟界面
cleardevice();
drawClock(timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
// 绘制当前时间
drawTime(timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
// 绘制指针
drawHands(timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
// 整点报时
if(timeinfo->tm_min == 0 && timeinfo->tm_sec == 0)
{
alarm(timeinfo->tm_hour, timeinfo->tm_min);
}
// 设置闹钟
if(kbhit())
{
char c = getch();
if(c == 27) // ESC键退出程序
{
break;
}
else if(c == 'a') // 设置闹钟
{
int hour, minute;
printf("请输入闹钟时间(格式:小时 分钟):");
scanf("%d %d", &hour, &minute);
drawAlarm(hour, minute);
while(1)
{
time(&rawtime);
timeinfo = localtime(&rawtime);
if(timeinfo->tm_hour == hour && timeinfo->tm_min == minute)
{
alarm(hour, minute);
break;
}
}
}
}
delay(1000); // 延时1秒
}
closegraph();
return 0;
}
void drawClock(int hour, int minute, int second)
{
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
circle(300, 200, 150);
floodfill(300, 200, GREEN);
int i;
for(i = 1; i <= 12; i++)
{
int x = 300 + 120 * sin(i * pi / 6);
int y = 200 - 120 * cos(i * pi / 6);
char str[2];
sprintf(str, "%d", i);
outtextxy(x, y, str);
}
setcolor(WHITE);
circle(300, 200, 155);
setcolor(YELLOW);
line(300, 200, 300 + 100 * sin(hour * pi / 6 + minute * pi / 360), 200 - 100 * cos(hour * pi / 6 + minute * pi / 360));
line(300, 200, 300 + 130 * sin(minute * pi / 30), 200 - 130 * cos(minute * pi / 30));
setcolor(RED);
line(300, 200, 300 + 130 * sin(second * pi / 30), 200 - 130 * cos(second * pi / 30));
}
void drawTime(int hour, int minute, int second)
{
char str[10];
sprintf(str, "%02d:%02d:%02d", hour, minute, second);
settextstyle(2, 0, 8);
outtextxy(200, 350, str);
}
void drawHands(int hour, int minute, int second)
{
setcolor(YELLOW);
line(300, 200, 300 + 100 * sin(hour * pi / 6 + minute * pi / 360), 200 - 100 * cos(hour * pi / 6 + minute * pi / 360));
line(300, 200, 300 + 130 * sin(minute * pi / 30), 200 - 130 * cos(minute * pi / 30));
setcolor(RED);
line(300, 200, 300 + 130 * sin(second * pi / 30), 200 - 130 * cos(second * pi / 30));
}
void drawAlarm(int hour, int minute)
{
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(600, 200, 50);
floodfill(600, 200, RED);
char str[10];
sprintf(str, "%02d:%02d", hour, minute);
settextstyle(2, 0, 4);
outtextxy(580, 190, str);
}
void alarm(int hour, int minute)
{
阅读全文