用C语言设计秒表程序

用C语言设计秒表程序,第1张

int main()

{ char m

struct tm time

time.hours=time.minutes=time.seconds=0

printf("Now, press 's' key to begin the clock...\n")

m=getch()

if(m=='s'||m=='S')

while(1)

{

while(!kbhit()) {

update(&time)

display(&time)}

m=getch()

if(m=='s'||m=='S')break

else{ printf("Only 's' or 'S' can be inputed!!")}

}

}

把main函数改成这样看看行不行

这里的分段计时,我使用空格键实现的,F2比较麻烦。程序开始,输入回车开始计时,中途输入空格可以开始新的计时,最后输入回车完成计时。

文件存在程序目录下的timeout.txt

真麻烦,下次这种求助才给10分,绝对不做。。。

//////////////////////////

我的代码就是在VS2010下写的。。。怎么会无法编译。。。你要建一个空工程,然后加入C++源文件。

/////////////////////////////

请新建一个空工程,不要新建Win32 Console那个工程!

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#include <stdlib.h>

struct tm     //定义时间结构体,包括时分秒和10毫秒

{

int hours,minutes,seconds

int hscd

}time,tmp,total    //time用以计时显示,tmp用以存储上一阶段时间,total记总时间

int cnt

FILE* fout

//每次调用update函数,相当于时间过了10ms

void update(struct tm *t)

{

(*t).hscd++    //10ms单位时间加1

cnt++

if ((*t).hscd==100)   //计时满1s,进位

{

(*t).hscd=0

(*t).seconds++

}

if ((*t).seconds==60)   //计时满一分,进位

{

(*t).seconds=0

(*t).minutes++

}

if ((*t).minutes==60)        //计时满一小时,进位

{

(*t).minutes=0

(*t).hours++

}

if((*t).hours==24) (*t).hours=0

//delay()

Sleep(10)  //Sleep是windows提供的函数,作用是暂停程序,单位毫秒,所以此处暂停10ms

}

void display(struct tm *t)

{

//此处输出计时结果,\r为回车不换行,既一直在同一行更新时间

printf("%d:",(*t).hours)

printf("%d:",(*t).minutes)

printf("%d:",(*t).seconds)

printf("%d\r",(*t).hscd)

//printf("Now, press 'e' key to stop the clock...")

}

void time_init()  //初始化时间

{

time.hours=time.minutes=time.seconds=time.hscd=0

}

void get_total()   //计算总时间

{

total.hscd = cnt % 100

cnt /= 100

total.seconds = cnt % 60

cnt /= 60

total.minutes = cnt % 60

cnt /= 60

total.hours = cnt

}

int main()

{

char m

time_init()

cnt = 0

fout =  fopen("timeout.txt","w")

printf("Now, press Enter key to begin the clock...\n")

while(1)

{

m = getch()

if(m != '\r')     //读入一个输入,如果是回车,那么跳出次循环

printf("Input Error!\n")

else

break

}

printf("While counting, you can press space to start a new time counter!\n")

while(1)

{

if(kbhit())    //此处检查是否有键盘输入

{

m=getch()

if(m == '\r')     //如果等于回车,那么计时结束,跳出循环

break

else if(m == ' ')  //如果等于空格,显示此次计时,初始化计时器

{

tmp = time      //记录上一段计时器结果

fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd) //写入文件

time_init()

printf("\n")

}

else

{

printf("Input Error!\n")

}

}

update(&time)     //更新计时器

display(&time)    //显示计时器时间

}

tmp = time       //输出最后一次即使结果,写入文件

fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd)

get_total()      //计算总的时间,显示,并写入文件

printf("\ntotal time:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd)

fprintf(fout,"total time:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd)

fclose(fout)

getch()

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11120842.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-13
下一篇2023-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存