用dosbox写 体育比赛计时器,有记忆的功能,程序运行开始计时,屏幕显示00:00:00
时间: 2023-08-22 12:06:03 浏览: 119
好的,我可以为您提供一些思路和代码示例。
首先,您可以使用 DOSBox 中的 Turbo C++ 编译器来编写程序。以下是示例代码:
```c
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#define BASE 1000
#define TICKS_PER_SECOND 18.2
void interrupt (*oldtimer)();
void interrupt newtimer();
int main()
{
int hours = 0, minutes = 0, seconds = 0;
int last_hours = 0, last_minutes = 0, last_seconds = 0;
int memory_hours = 0, memory_minutes = 0, memory_seconds = 0;
int running = 1;
clrscr();
printf("Press any key to start the timer");
getch();
// Set up the new timer interrupt
oldtimer = getvect(0x08);
setvect(0x08, newtimer);
// Main loop
while (running) {
if (kbhit()) {
// Stop the timer if a key is pressed
running = 0;
}
if (last_seconds != seconds) {
// Update the display every second
gotoxy(1, 2);
printf("%02d:%02d:%02d", hours, minutes, seconds);
last_hours = hours;
last_minutes = minutes;
last_seconds = seconds;
}
// Sleep for a short while to avoid using too much CPU
delay(10);
}
// Save the current time to memory
memory_hours = hours;
memory_minutes = minutes;
memory_seconds = seconds;
// Restore the old timer interrupt
setvect(0x08, oldtimer);
clrscr();
printf("Press any key to show the saved time");
getch();
// Display the saved time
gotoxy(1, 2);
printf("%02d:%02d:%02d", memory_hours, memory_minutes, memory_seconds);
getch();
return 0;
}
void interrupt newtimer()
{
static unsigned long ticks = 0;
ticks += BASE;
if (ticks >= TICKS_PER_SECOND) {
ticks -= TICKS_PER_SECOND;
// Increment the timer
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}
}
// Call the old timer interrupt
(*oldtimer)();
}
```
这段代码实现了一个简单的计时器,每秒更新一次屏幕上的时间显示。当用户按下任意键时,程序将停止计时并将当前时间保存到内存中。当用户按下任意键时,程序将显示保存的时间。
注意这只是一个简单的示例程序,您可能需要根据实际需要进行更改和优化。
阅读全文