问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求一个用c++编写的时钟程序的源代码

发布网友 发布时间:2023-02-04 04:32

我来回答

1个回答

热心网友 时间:2024-12-02 23:43

/*******************************/
/* 时钟源程序 */
/* 仿WINDOWS界面设计 */
/* Turboc3下调试通过 */
/* 包含两个源文件: */
/* clock.cpp win.cpp */
/*******************************/
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"time.h"
#include"string.h"
#include"math.h"
#include"dos.h"
#include"bios.h"
#include"win.cpp" //调用窗体显示
#define TRUE 1
#define FALSE 0
#define PI 3.1415926
void cursor(int x,int y) //光标
{
int count=0;
while(count<=10){
delay(20);
count++;
if(count<=5)
setcolor(9);
else
setcolor(7);
line(x,y,x,y+12);
line(x+1,y,x+1,y+12);
}
}
int keyscancode() //检测按键
{
int key;
while(bioskey(1)==0) return 0;
key=bioskey(0);
key=key&0xff ? key&0xff:key;
return(key);
}
void message(int x,int y,int n) //状态栏信息
{
char *msg[6]={"Press ESC to quit clock.",
"Press T or t to set time.",
"Press D or d to set date.",
"Press ESC to cancel set.",
"Press Enter to confirm set.",
"Press BackSpace to delete a number."};
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+50*8,y+12);
setcolor(10);
outtextxy(x,y+2,msg[n]);
}

class CLOCK:public WIN //定义CLOCK为WIN的继承类
{
int hour,min,sec;
int year,mon,day;
int x,y,radio;
float ak,aj,ai;
int xs,ys,xm,ym,xh,yh;
public:
CLOCK(char *,int,int,int,int);
int isleapyear(int);
int isweek(int,int);
void showclock();
void runclock();
void setclock();
void inittodaydate();
void initnowtime();
void showdate();
void hidedate();
void pip();
void showtime();
void setdates();
};
//构造函数
CLOCK::CLOCK(char *n,int a,int b,int c,int d):WIN(n,a,b,c,d){
x=a+130;
y=b+155;
radio=120;
}
int CLOCK::isleapyear(int year) //判断是否闰年
{
if(year%100!=0&&year%4==0)
return TRUE;
if(year%400==0)
return TRUE;
return FALSE;
}
int CLOCK::isweek(int year,int mon) //计算year年mon月1号的星期数
{
int days=0,week=0;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleapyear(year)) month[1]=29;
else month[1]=28;
for(int i=1;i<year;i++)
{
days=365;
if(isleapyear(i)) days=366;
week=(days+week)%7;
}
for(i=0;i<mon-1;i++)
week=(month[i]+week)%7;
return week;
}
void CLOCK::setdates() //设置曰期
{
struct date set;
int key=0,count=0,index=0;
int textx=x+140,texty=y+42,tcnt=0;
char text[2];
int dateset[10];
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+150,texty+12);
do{
message(80+6,380-12,index+3);
count++;
index=count/10;
if(count==29) count=0;
runclock();
key=keyscancode();
cursor(textx,texty);
if((key>='0'&&key<='9')&&tcnt!=10)
{ //*按键,防止误操作
if(tcnt<4||(tcnt==5&&key<='1')||((tcnt==6&&dateset[tcnt-1]==1&&key<='2')||(tcnt==6&&dateset[tcnt-1]==0))||(tcnt==8&&key<='3')||(tcnt==9))
{
sprintf(text,"%c",key);
text[1]='\0';
setcolor(LIGHTBLUE);
outtextxy(textx,texty+2,text);
dateset[tcnt]=key-'0';
textx+=10;
tcnt++;
if((tcnt==4||tcnt==7)&tcnt!=10){
outtextxy(textx,texty+2,"-");
textx+=10;
tcnt++;
}
}
}
if(key==8&&tcnt>0){
if(tcnt==5||tcnt==8)
{
tcnt-=2;
textx-=20;
}
else{
tcnt--;
textx-=10; }
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+8,texty+12);
}
}while(key!=13&&key!=27);
if(key==27) return; //如果是ESC键,则退出设置
if(key==13) //如果ENTER键,则设置曰期
{
set.da_year=dateset[0]*1000+dateset[1]*100+dateset[2]*10+dateset[3];
set.da_mon=dateset[5]*10+dateset[6];
set.da_day=dateset[8]*10+dateset[9];
setdate(&set);
}
}
void CLOCK::setclock() //设置时间
{
struct time set;
int key=0,count=0,index=0;
int textx=x+140,texty=y+6,tcnt=0;
char text[2];
int timeset[8];
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+150,texty+12);
do{
message(80+6,380-12,index+3);
count++;
index=count/10;
if(count==29) count=0;
key=keyscancode();
cursor(textx,texty);
if((key>='0'&&key<='9')&&tcnt!=8)
{ //*按键,防止误操作
if((tcnt==0&&key<='2')||((tcnt==1&&key<='3'&×et[tcnt-1]==2)||(tcnt==1&×et[tcnt-1]!=2))||(tcnt==3&&key<='5')||(tcnt==6&&key<='5')||tcnt==4||tcnt==7)
{
sprintf(text,"%c",key);
text[1]='\0';
setcolor(9);
outtextxy(textx,texty+2,text);
timeset[tcnt]=key-'0';
textx+=10;
tcnt++;
if((tcnt==2||tcnt==5)&&tcnt!=8) {
outtextxy(textx,texty+2,":");
textx+=10;
tcnt++;
}
}
}
if(key==8&&tcnt>0){
if(tcnt==3||tcnt==6){
tcnt-=2;
textx-=20;
}
else{
tcnt--;
textx-=10;
}
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+8,texty+12);
}
}while(key!=13&&key!=27);
if(key==27) return;
if(key==13)
{
set.ti_hour=timeset[0]*10+timeset[1];
set.ti_min=timeset[3]*10+timeset[4];
set.ti_sec=timeset[6]*10+timeset[7];
settime(&set);
}
}
void CLOCK::showdate() //在窗体上显示曰期
{
char nowdate[15];
char days[4];
char *week[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayx=x+135,dayy=y-88,todayweek=0,control;
struct date d;
getdate(&d);
setcolor(12);
for(int i=0;i<7;i++)
outtextxy(x+i*8*4+135,y-100,week[i]);
todayweek=isweek(d.da_year,d.da_mon); // 得到本月1号的星期数
if(todayweek+1!=7)
for(i=1;i<=todayweek+1;i++) dayx=dayx+4*8;
control=todayweek+1;
if(isleapyear(d.da_year)) month[1]=29;
else month[1]=28;
setcolor(11);
for(i=1;i<=month[d.da_mon-1];i++)
{
control++;
sprintf(days,"%3d",i);
if(i==d.da_day) setcolor(LIGHTMAGENTA);
outtextxy(dayx,dayy,days);
setcolor(11);
dayx=dayx+4*8;
if(control%7==0) { dayx=x+135; dayy=dayy+12; }
}
todayweek=(todayweek+day)%7; //得到今天的星期数
sprintf(nowdate,"%04d-%02d-%02d %s",d.da_year,d.da_mon,d.da_day,week[todayweek]);
nowdate[14]='\0';
setcolor(LIGHTBLUE);
outtextxy(x+185,y-120,nowdate);
if(d.da_year!=year||d.da_mon!=mon||d.da_day!=day) //如果曰期改变了,则先隐藏曰期,再显示
hidedate();
}
void CLOCK::hidedate() //隐藏曰期
{
inittodaydate();
setfillstyle(SOLID_FILL,LIGHTGRAY); //抹去曰期,如果曰期改变了
bar(x+135,y-90,x+135+6*8*4+3*8,y-90+12*6);
bar(x+165,y-122,x+170+16*8+10,y-112);
}
void CLOCK::showtime() //显示时间
{
char nowtime[9];
sprintf(nowtime,"%02d:%02d:%02d",hour,min,sec);
nowtime[8]='\0';
setcolor(14);
outtextxy(x-30,y+82,nowtime);
ak=(90+30*(12-hour)-min*5/60.*6)*PI/180; //时针角度
aj=(90+6*(60-min))*PI/180; //分针角度
ai=(90+6*(60-sec))*PI/180; //秒针角度
xs=x+cos(ai)*(radio-10);
ys=y-sin(ai)*(radio-10);
xm=x+cos(aj)*(radio-25);
ym=y-sin(aj)*(radio-25);
xh=x+cos(ak)*(radio-45);
yh=y-sin(ak)*(radio-45);
setcolor(RED); //画时针
setlinestyle(0,0,THICK_WIDTH);
line(x-cos(ak)*(radio-118),y+sin(ak)*(radio-118),xh,yh);
setlinestyle(0,0,NORM_WIDTH); //画时针在钟面上指示位置
circle(x+cos(ak)*radio,y-sin(ak)*radio,2);
setcolor(LIGHTGREEN);
circle(x+cos(aj)*radio,y-sin(aj)*radio,2); //画分针在钟面上的指示位置
line(x-cos(aj)*(radio-115),y+sin(aj)*(radio-115),xm,ym); //画分针
setcolor(LIGHTBLUE);
line(x-cos(ai)*(radio-110),y+sin(ai)*(radio-110),xs,ys); //画秒针
circle(x+cos(ai)*radio,y-sin(ai)*radio,2); //画秒针在钟面上的指示位置
}
void CLOCK::runclock() //运行时钟
{
int h,m,s;
float ah;
struct time tim;
gettime(&tim);
s=tim.ti_sec;
m=tim.ti_min;
h=tim.ti_hour;
ah=(90+30*(12-h)-m*5/60.*6)*PI/180;
showdate();
showtime();
delay(200);
nosound();
if(ah!=ak) {
setcolor(LIGHTGRAY);
setlinestyle(0,0,THICK_WIDTH);
line(x-cos(ak)*(radio-118),y+sin(ak)*(radio-118),xh,yh);
setlinestyle(0,0,NORM_WIDTH);
if(hour%5==0) setcolor(YELLOW);
circle(x+cos(ak)*radio,y-sin(ak)*radio,2);
setcolor(LIGHTGRAY);
hour=h;
}
if(m!=min) {
setcolor(LIGHTGRAY);
line(x-cos(aj)*(radio-115),y+sin(aj)*(radio-115),xm,ym);
if(min%5==0) setcolor(YELLOW);
circle(x+cos(aj)*radio,y-sin(aj)*radio,2);
min=m;
}
if(s!=sec) {
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x-30,y+80,x-30+8*8,y+80+8);
setcolor(LIGHTGRAY);
if(sec%5==0) setcolor(YELLOW);
circle(x+cos(ai)*radio,y-sin(ai)*radio,2);
setcolor(LIGHTGRAY);
line(x-cos(ai)*(radio-110),y+sin(ai)*(radio-110),xs,ys);
sec=s;
if(hour>=12) //整点报时
h=hour-12;
else
h=hour;
if(min==59&&sec>=60-h)
sound(392);
if(min==00&&sec==00)
sound(784);
}
showclock();
}
void CLOCK::inittodaydate() //初始化今天的曰期
{
struct date initdate;
getdate(&initdate);
year=initdate.da_year;
mon=initdate.da_mon;
day=initdate.da_day;
}
void CLOCK::initnowtime() //初始化时间
{
struct time inittime;
gettime(&inittime);
hour=inittime.ti_hour;
min=inittime.ti_min;
sec=inittime.ti_sec;
}
void CLOCK::showclock() //显示时钟
{
float alpha;
int x0,y0,i;
char s[3];
for(i=60;i>=1;i--) //显示钟面上数字
{
alpha=(90+6*(60-i))*PI/180;
x0=x+cos(alpha)*radio;
y0=y-sin(alpha)*radio;
setcolor(14);
if(i%5==0){
circle(x0,y0,2);
sprintf(s,"%d",i/5);
outtextxy(x+cos(alpha)*(radio-10)-4,y-sin(alpha)*(radio-10)-2,s);
}
else
circle(x0,y0,1);
}
}

void about(int x,int y,int index) //关于与帮助信息
{
int i=0,t=0;
char *abouttext[]={"You can get message from",
" the status bar at the ",
"bottom.",
"When you set time,you just",
"input like this:080819.",
"The symbol ':' isn't needed.",
"Set date is the same.",
"This clock is designed by",
" Nie Shiqiu.(C99102-07)",
"I'm very interested in C/C++",
"This is my course design."};
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+27*8+6,y+4*12);
t=index-3;
if(index<4){
y=y+(3-index)*12;
t=0;
}
for(i=t;i<=index;i++)
{
if(i<11){
if(i==8) setcolor(RED); //名字着重显示
else setcolor(LIGHTBLUE);
outtextxy(x+2,y+2,abouttext[i]);
}
y+=12;
}
}
void main() //主程序
{
int driver=DETECT,mode;
int exitflag=FALSE,key,index=0,count=0,aboutindex=0,acnt=0;
WIN winCLK("Clock",80,80,570,385); //创建窗体
CLOCK c1("Clock",80,80,570,385);
initgraph(&driver,&mode,"");
winCLK.Form(FALSE);
c1.showclock();
c1.initnowtime();
do{
about(342,80+185+44,aboutindex);
acnt++;
aboutindex=acnt/10;
if(acnt==139) acnt=0;
message(80+6,380-12,index);
count++;
index=count/10;
if(count==29) count=0;
key=keyscancode();
c1.runclock();
if(key==27) exitflag=TRUE; //按ESC退出
if(key=='T'||key=='t') {
c1.showtime();
c1.setclock();
}
if(key=='D'||key=='d') {
c1.setdates();
}
}while(exitflag==FALSE);
winCLK.draw_closebutton(570-18,80+4,BLACK,TRUE);
delay(300);
closegraph();
}
/*******************/
/* 窗体文件 win.cpp*/
/*******************/
#include"graphics.h"
#define TRUE 1
#define FALSE 0
class WIN
{
char title[20];
protected:
int left,top,right,bottom;
public:
WIN(char *n,int a,int b,int c,int d){strcpy(title,n);left=a;top=b;right=c;bottom=d;}
void draw_minbutton(int left,int top,int color,int state);
void draw_maxbutton(int left,int top,int color,int state);
void draw_closebutton(int left,int top,int color,int state);
void Form(int);
void Button(int,int,int,int,int);
void line3d(int,int,int,int,int);
void Frame(int,int,int,int,char *);
};
void WIN::Frame(int left,int top,int right,int bottom,char *s)
{
line3d(left,top+2,right,top+2,FALSE);
line3d(left,top+3,left,bottom,FALSE);
line3d(left,bottom,right,bottom,FALSE);
line3d(right,top+2,right,bottom,FALSE);
bar(left+4,top,left+4+8*strlen(s)+4,top+8);
setcolor(10);
outtextxy(left+5,top,s);
}
void WIN::line3d(int left,int top,int right,int bottom,int state) /*画有凹凸感的线条函数*/
{
if(state==FALSE){
setcolor(0);
line(left,top,right,bottom);
setcolor(15);
if(top==bottom){
line(left,top+1,right,bottom+1);
}
if(left==right){
line(left+1,top,right+1,bottom);
} }
else
{ setcolor(15);
line(left,top,right,bottom);
setcolor(0);
if(top==bottom){
line(left,top+1,right,bottom+1);
}
if(left==right){
line(left+1,top,right+1,bottom);
} }
}
void WIN::Form(int state)
{
Button(left,top,right,bottom,state);
setfillstyle(1,1);
bar(left+2,top+2,right-2,top+20);
setcolor(YELLOW);
circle(left+12,top+12,8);
line(left+12,top+12,left+12,top+4);
line(left+12,top+12,left+16,top+12);
outtextxy(left+16+10,top+8,title);
draw_minbutton(right-16*3-4,top+4,BLACK,FALSE);
draw_maxbutton(right-16*2-3,top+4,BLACK,FALSE);
draw_closebutton(right-18,top+4,BLACK,FALSE);
Frame(left+5,top+25,left+255,top+278,"Time");
Frame(left+260,top+25,right-5,top+25+20*6,"Date");
line3d(left+280,top+25+20,right-25,top+25+20,FALSE);
Frame(left+260,top+150,right-5,top+150+30,"SetTime(T)");
Button(left+265,top+160,right-8,top+150+25,TRUE);
Frame(left+260,top+185,right-5,top+185+30,"SetDate(D)");
Button(left+265,top+195,right-8,top+185+25,TRUE);
Button(left+4,bottom-20,right-4,bottom-4,TRUE);
Frame(left+260,top+185+35,right-5,bottom-27,"About");
}
void WIN::Button(int left,int top,int right,int bottom,int state)
{
if(state==-1){
setfillstyle(1,7);
bar(left,top,right,bottom);}
setfillstyle(1,7);
bar(left,top,right,bottom);
if(state==FALSE)
{
setcolor(15);
line(left,top,right,top);
line(left,top,left,bottom);
setcolor(0);
line(left,bottom,right,bottom);
line(right,top,right,bottom);
setcolor(8);
line(left+1,bottom-1,right-1,bottom-1);
line(right-1,top+1,right-1,bottom-1);
}
if(state==TRUE)
{
setcolor(8);
line(left,top,right,top);
line(left,top,left,bottom);
setcolor(15);
line(left,bottom,right,bottom);
line(right,top,right,bottom);
}
}
void WIN::draw_minbutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
line(left+3,top+11,left+12,top+11);
line(left+3,top+12,left+12,top+12);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
line(left+3,top+13,left+12,top+13);
line(left+3,top+12,left+12,top+12);
}
}
void WIN::draw_maxbutton(int left,int top,int color,int state)
{

if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
rectangle(left+4,top+4,left+12,top+12);
rectangle(left+4,top+5,left+12,top+12);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
rectangle(left+4,top+4,left+12,top+14);
rectangle(left+4,top+3,left+12,top+14);
}
}
void WIN::draw_closebutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
line(left+4,top+4,left+12,top+12);
line(left+5,top+4,left+13,top+12);
line(left+4,top+12,left+12,top+4);
line(left+5,top+12,left+13,top+4);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
line(left+4,top+4,left+12,top+12);
line(left+5,top+4,left+13,top+12);
line(left+4,top+12,left+12,top+4);
line(left+5,top+12,left+13,top+4);
}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
什么药止痒效果好 慕容三藏履历 职业cf电脑配置cf电脑配置要求是什么 玩3a电脑配置要求想玩3a大作需要什么配置 台式电脑哪些可以畅享3a大作想玩3a大作需要什么配置 七彩虹i7-10700台式机 性能强劲 高效静音 小黄鱼游戏台式电脑主机(i7-10700F、16GB、512GB、RTX3070)-适用... 工伤保险记什么科目 在4×4的正方形格纸中,有一个以格点为顶点的△ABC,请你找到点C,使三角... 在4×4方格中, 要求C点在格点上,且以AB为边的△ABC为直角三角形,标出所... 一个人开始转运的迹象,人倒霉的时候怎么样才可以转运? 风水轮流转,明天到我家!7件事预示你将转运 ! z6ii读写速度 索尼a7c和尼康z6ii直出效果 尼康z6ii像素位移 尼康z62套机的镜头是不是不好 见怪不怪下一句是什么 有没有关于时光飞逝的诗句 凉拌鹿角菜的介绍 云片鹿角菜的介绍 梦到自己癌症早期 什么才叫认真工作? 平板三角支架怎么用 这个名片夹怎么用 中国农业银行小额账管怎么扣了12块钱 农业银行账管费是什么意思 最好的早安问候语有哪些 回馈型电池组老化柜原理 梦见男人爱自己什么意思? 梦见有个男人喜欢自己 从模因论在现实生活中的体现 emoji黄豆为什么有的不能打 ‘珍惜’的涵义是什么 2022年女人正能量激励人的好句子(50句) 七年级上册语文期中复习提纲 马蹄莲的形态特征是什么? 该死的腾讯 你是后脑勺被鬼拍了吗??? 九层塔和鬼针草的区别是什么 苹果树可以怎么修剪呢 桂花树的籽怎么吃? 防腐木地板保养方法是什么 防腐木地板如何进行防腐处理 在复活节吃之前,祭司应该祝福什么食物 办临时身份证流程 怎样能最快解酒(一直吐得很厉害),不在家里,可不可以去便利店买点啥解 ... 常见的强酸强碱分别有哪些 梦见老公对别人毛手毛脚 这几个梦代表什么? 梦见生病没有人来感到失望预示什么? 铁板土豆怎么穿串才合适,穿大了,一个铁板炸不了几串,穿小了又不合适...