本文僅在博客園發布,若在其他網站發現均為盜取,請認准原文鏈接:
https://www.cnblogs.com/jisuanjizhishizatan/p/15521112.html
先上效果:
輸入設定時間后,會出現時間。
如果時間結束,則顯示time's up。
代碼的實現很簡單,就是每間隔1秒刷新一次時間。間隔1秒可以使用Sleep(1000)實現。
關於輸出時間和輸入設定時間,我們可以使用easyx的inputbox來實現。
對於時間的60進制,我們可以判斷當前的seconds是否為0,如果為0,那么把minutes減去一,然后把seconds重置為59.
代碼:
// Timer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
int main(void){
initgraph(300,200);
setbkcolor(WHITE);
settextcolor(BLACK);
cleardevice();
settextstyle(48,0,"Consolas");
char s[100],t[100];
InputBox(s,sizeof(s),"輸入設定時間:(分鍾為單位)","設定時間");
int minutes=0,seconds=0;
sscanf(s,"%d",&minutes);
while(minutes!=0 || seconds!=0){
if(seconds==0){
minutes--;seconds=59;
}
else seconds--;
Sleep(1000);
sprintf(t,"%02d:%02d",minutes,seconds);
cleardevice();
outtextxy(50,50,t);
}
cleardevice();
outtextxy(50,50,"Time's up!");
_getch();
closegraph();
return 0;
}
注意需要提前把項目設置為使用多字節字符集而不是Unicode,否則會出編譯錯誤。