秒表(正计时)、倒计时 代码 不喜勿喷
2024-06-14 11:05:50
发布于:江苏
秒表(正计时):
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
double second=0.0;
char ch;
double last=0.0;
void setFontSize(int size) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect;
rect.Top = 0;
rect.Left = 0;
rect.Bottom = 24 * size; // 24 lines, each line size characters wide
rect.Right = 80 * size; // 80 characters wide
SetConsoleWindowInfo(hOut, TRUE, &rect);
}
void clearScreen() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 }; // home for the cursor
SetConsoleCursorPosition( hConsole, coordScreen );
}
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
while_printf_the_new_time() {
// 获取开始时间
LARGE_INTEGER start;
QueryPerformanceCounter(&start);
// 获取计数器频率
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
// 循环直到用户按下任意键退出
printf("\n\n\n 按回车暂停");
while (true) {
// 获取当前时间
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
// 计算经过的秒数
double seconds = ((now.QuadPart - start.QuadPart) / (double)frequency.QuadPart)+ last;
// 输出计时结果
if((int)(seconds*100)!=(int)(second*100)) {
int minute=(int)seconds/60;
int miao=(int)seconds%60;
int maomiao=(int)(seconds*100)% 100;
clearScreen();
printf("\n\n\n\n\n %02d:%02d:%02d",minute,miao,maomiao);
second=seconds;
}
if(kbhit()) {
ch=getch();
if(ch==13) {
int minute=(int)seconds/60;
int miao=(int)seconds%60;
int haomiao=(int)(seconds*100)%100;
system("cls");
printf("\n\n\n %02d:%02d:%02d",minute,miao,haomiao);
last=seconds;
minute=0,miao=0,haomiao=0;
return 1;
}
}
}
}
void SetConsoleWindowSize( SHORT width, SHORT height ) {
HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT wrt = { 0, 0, width-1, height-1 };
SetConsoleWindowInfo( hStdOutput, TRUE, &wrt ); // 设置窗体尺寸
COORD coord = { width, height };
SetConsoleScreenBufferSize( hStdOutput, coord ); // 设置缓冲尺寸
}
void setConsoleFontSize(int size) {
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof(cfi);
GetCurrentConsoleFontEx(hConsoleOutput, FALSE, &cfi);
cfi.dwFontSize.Y = size; // 设置字体高度
cfi.dwFontSize.X = 0; // 设置字体宽度
SetCurrentConsoleFontEx(hConsoleOutput, FALSE, &cfi);
}
int main() {
SetConsoleWindowSize(35,9);//窗口大小
setConsoleFontSize(30);//字体大小
SetWindowLongPtrA(GetConsoleWindow(), GWL_STYLE, GetWindowLongPtrA(GetConsoleWindow(),GWL_STYLE)& ~WS_SIZEBOX & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);//固定窗口
system("color F0");//颜色
HideCursor();//隐藏光标
while(1) {
system("cls");
printf("\n\n\n 秒表\n\n 按回车开始");
ch=getch();
if(ch==13) {
while(1) {
system("cls");
if(while_printf_the_new_time()) {
printf("\n\n 回车继续 按其他键结束\n");
ch=getch();
if(ch!=13) {
last=0;
break;
}
}
}
}
}
return 0;
}
倒计时:
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
int time=0;
int hours_=0,minute_=0,seconds_=0;
double second=0.0;
char ch,ch2;
double last=0.0;
int i=0;
void clearScreen(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 }; // home for the cursor
SetConsoleCursorPosition( hConsole, coordScreen );
}
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
while_printf_the_new_time() {
// 获取开始时间
LARGE_INTEGER start;
QueryPerformanceCounter(&start);
// 获取计数器频率
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
// 循环直到用户按下任意键退出
while (true) {
// 获取当前时间
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
// 计算经过的秒数
double seconds = time - (((now.QuadPart - start.QuadPart) / (double)frequency.QuadPart)+ last);
// 输出计时结果
if((int)(seconds*100)!=(int)(second*100)) {
int hour=(int)seconds/3600;
int minute=(int)seconds%3600/60;
int miao=(int)seconds%60;
int haomiao=(int)(seconds*100)% 100;
if(hour+minute+miao+haomiao<0) {
hour=0;minute=0,miao=0,haomiao=0;
return 0;
}
clearScreen();
printf("\n\n\n 按回车暂停\n\n %02d:%02d:%02d:%02d",hour,minute,miao,haomiao);
second=seconds;
}
if(kbhit()) {
ch=getch();
if(ch==13) {
int hour=(int)seconds/3600;
int minute=(int)seconds%3600/60;
int miao=(int)seconds%60;
int haomiao=(int)(seconds*100)% 100;
system("cls");
printf("\n\n\n %02d:%02d:%02d:%02d",hour,minute,miao,haomiao);
last=time-seconds,second=seconds;
hour=0,minute=0,miao=0,haomiao=0;
return 1;
}
}
}
}
void setConsoleFontSize(int size) {
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof(cfi);
GetCurrentConsoleFontEx(hConsoleOutput, FALSE, &cfi);
cfi.dwFontSize.Y = size; // 设置字体高度
cfi.dwFontSize.X = 0; // 设置字体宽度
SetCurrentConsoleFontEx(hConsoleOutput, FALSE, &cfi);
}
void SetConsoleWindowSize( SHORT width, SHORT height ) {
HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT wrt = { 0, 0, width-1, height-1 };
SetConsoleWindowInfo( hStdOutput, TRUE, &wrt ); // 设置窗体尺寸
COORD coord = { width, height };
SetConsoleScreenBufferSize( hStdOutput, coord ); // 设置缓冲尺寸
}
int main() {
SetConsoleWindowSize(35,9);//窗口大小
setConsoleFontSize(30);//字体大小
SetWindowLongPtrA(GetConsoleWindow(), GWL_STYLE, GetWindowLongPtrA(GetConsoleWindow(),GWL_STYLE)& ~WS_SIZEBOX & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);//固定窗口
system("color F0");//颜色
HideCursor();//隐藏光标
while(1) {
hours_=0,minute_=0,seconds_=0;
system("cls");
printf("\n 倒计时\n\n 用 + 和 - 来调整\n\n 按回车结束");
printf("\n\n 小时:00");
while(1) {
ch2=getch();
if(ch2==13) {
break;
} else if(ch2=='+') {
i++;
if(i==100)i=0;
} else if(ch2=='-') {
i--;
if(i==-1)i=99;
}
clearScreen();
printf("\n\n\n\n\n\n\n 小时:%02d",i);
}
hours_=i;
i=0;
system("cls");
printf("\n 倒计时\n\n 用 + 和 - 来调整\n\n 按回车结束");
printf("\n\n 分钟:00");
while(1) {
ch2=getch();
if(ch2==13) {
break;
} else if(ch2=='+') {
i++;
if(i==60)i=0;
} else if(ch2=='-') {
i--;
if(i==-1)i=59;
}
clearScreen();
printf("\n\n\n\n\n\n\n 分钟:%02d",i);
}
minute_=i;
i=0;
system("cls");
printf("\n 倒计时\n\n 用 + 和 - 来调整\n\n 按回车结束");
printf("\n\n 秒数:00");
while(1) {
ch2=getch();
if(ch2==13) {
break;
} else if(ch2=='+') {
i++;
if(i==60)i=0;
} else if(ch2=='-') {
i--;
if(i==-1)i=59;
}
clearScreen();
printf("\n\n\n\n\n\n\n 秒数:%02d",i);
}
seconds_=i;
i=0;
time=hours_*3600+minute_*60+seconds_;
while(1) {
system("cls");
if(while_printf_the_new_time()) {
printf("\n\n 回车继续 按其他键结束\n");
ch=getch();
if(ch!=13) {
last=0;
break;
}
} else {
system("cls");
printf("\n\n\n 倒计时已结束\n\n 回车重新开始");
ch=getch();
if(ch==13) {
last=0;
break;
} else {
while(1) {
ch=getch();
if(ch==13) {
last=0;
break;
}
}
break;
}
}
}
}
return 0;
}
有需改进的吗?
全部评论 3
搞精准一点(
2024-08-23 来自 广东
0怎么说?
2024-08-28 来自 江苏
0
加一个Beep()函数
2024-08-23 来自 广东
0好东西
2024-08-28 来自 江苏
0
我嘞个豆看不懂
2024-06-14 来自 广东
0
有帮助,赞一个