在Windows系统上,用C语言编写闹钟程序。
一、用do...while语句实现循环响铃
1 /*此程序用do...while语句实现循环响铃*/ 2 3 #include <stdio.h> 4 5 #include <windows.h> 6 7 #include <winbase.h> 8 9 int main() 10 11 { char f='Y'; 12 13 SYSTEMTIME time; 14 15 SYSTEMTIME clock; 16 17 GetLocalTime(&time); 18 19 printf("当前时间为:%2d:%2d:%2d\n",time.wHour,time.wMinute,time.wSecond); 20 21 printf("请设定闹钟时间:\n"); 22 23 printf("时:"); 24 25 scanf("%2d",&clock.wHour); 26 27 printf("分:"); 28 29 scanf("%2d",&clock.wMinute); 30 31 getchar();//读取掉回车符 32 33 Sleep(1000); 34 35 GetLocalTime(&time); 36 37 if((time.wHour==clock.wHour)&&(time.wMinute==clock.wMinute)) 38 39 { 40 41 printf("\a\a\a时间到!!!"); 42 43 } 44 45 do 46 47 {printf("是否继续响铃?输入y继续,其他任意键取消:"); 48 49 scanf("%c",&f); 50 51 getchar();//读取掉回车符 52 53 Sleep(2000); 54 55 if(f=='y') 56 57 {printf("\a\a\a时间到!!!"); 58 59 } 60 61 }while(f=='y'); 62 63 return 0; 64 65 }
二、用for循环实现循环响铃
1 #include <stdio.h> 2 3 #include <windows.h> 4 5 int main() 6 7 { 8 9 SYSTEMTIME time; 10 11 SYSTEMTIME clock; 12 13 GetLocalTime(&time); 14 15 char flag; 16 17 int i; 18 19 printf("当前时间为:%2d:%2d:%2d\n",time.wHour,time.wMinute,time.wSecond); 20 21 printf("请设定闹钟时间:\n"); 22 23 printf("时:"); 24 25 scanf("%d",&clock.wHour); 26 27 printf("分:"); 28 29 scanf("%d",&clock.wMinute); 30 31 getchar(); 32 33 Sleep(1000); 34 35 GetLocalTime(&time); 36 37 if((time.wHour==clock.wHour)&&(time.wMinute==clock.wMinute)) 38 39 { 40 41 for(i=1;i<10000;i++) 42 43 { 44 45 printf("\a\a\a时间到!!!\n"); 46 47 Sleep(3000); 48 49 printf("按n键取消,其他任意键继续响铃!"); 50 51 scanf("%c",&flag); 52 53 getchar(); 54 55 Sleep(5000); 56 57 if (flag=='n') 58 59 { 60 61 i=9999; 62 63 } 64 65 } 66 67 } 68 69 }