因为老师那边例会需要,今天就自己diy了一个简单的计时器。因为需要统计所有人所用的时间,因此依然需要用到链表,事实证明链表用的还是不够熟练,以后还得多写一写这方面的程序。
这个程序实现的主要功能是,多次计时,最后将所有人的时间汇总,并从小到大输出。
贴代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#define MAX 100
#define NUM 20
typedef struct member
{
char name[MAX];
int second;
int minute;
int hour;
int time;
struct member *pNext;
}Node, *pNode;
int main(void)
{
int i, num;
pNode pHead = NULL;
pNode CreatList(void);
pNode CreatMember(pNode);
void TraverseList(pNode);
void SortList(pNode, int);
void Money(pNode);
printf("请输入汇报人数:");
scanf_s("%d", &num);
printf("\n");
for (i = 0; i < num; )
{
if (i == 0)
{
pHead = CreatList();
i++;
}
else
{
pHead = CreatMember(pHead);
i++;
}
}
SortList(pHead, num);
TraverseList(pHead);
Money(pHead);
Sleep(10000000000);
return 0;
}
pNode CreatList(void)
{
int flag;
clock_t start, end;
char c = '\0';
pNode pHead, pTail, pNew;
pHead = (pNode)malloc(sizeof(Node));
pNew = (pNode)malloc(sizeof(Node));
pTail = pHead;
pTail->pNext = NULL;
printf("请输入汇报同学姓名:");
scanf_s("%s", pNew->name, MAX);
printf("按1开始计时,按Esc结束计时。");
scanf_s("%d", &flag);
if (flag == 1)
{
start = clock();
}
while (c != 27)
{
end = clock();
pNew->time = (end - start) / CLK_TCK;
{
/*小时计算*/
pNew->hour = pNew->time / 3600;
/*分钟计算*/
if (pNew->hour == 0)
{
pNew->minute = pNew->time / 60;
}
else
{
pNew->minute = (pNew->time - pNew->hour * 3600) / 60;
}
/*秒计算*/
pNew->second = (int)(pNew->time % 60);
}
printf("%02d:%02d:%02d", pNew->hour, pNew->minute, pNew->second);
Sleep(1000);
printf("\b\b\b\b\b\b\b\b");
if (_kbhit())
{
c = _getch();
}
}
printf("%s的汇报时间为:", pNew->name);
printf("%02d:%02d:%02d\n\n", pNew->hour, pNew->minute, pNew->second);
pHead->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
return(pHead);
}
pNode CreatMember(pNode p)
{
int flag;
clock_t start, end;
char c = '\0';
pNode pHead, pTail, pNew, pSwap;
pNew = (pNode)malloc(sizeof(Node));
pHead = p;
pSwap = NULL;
while (p != NULL)
{
pSwap = p;
p = p->pNext;
}
pTail = pSwap;
pTail->pNext = NULL;
printf("请输入汇报同学姓名:");
scanf_s("%s", pNew->name, MAX);
printf("按1开始计时,按Esc结束计时。");
scanf_s("%d", &flag);
if (flag == 1)
{
start = clock();
}
while (c != 27)
{
end = clock();
pNew->time = (end - start) / CLK_TCK;
{
/*小时计算*/
pNew->hour = pNew->time / 3600;
/*分钟计算*/
if (pNew->hour == 0)
{
pNew->minute = pNew->time / 60;
}
else
{
pNew->minute = (pNew->time - pNew->hour * 3600) / 60;
}
/*秒计算*/
pNew->second = (int)(pNew->time % 60);
}
printf("%02d:%02d:%02d", pNew->hour, pNew->minute, pNew->second);
Sleep(1000);
printf("\b\b\b\b\b\b\b\b");
if (_kbhit())
{
c = _getch();
}
}
printf("%s的汇报时间为:", pNew->name);
printf("%02d:%02d:%02d\n\n", pNew->hour, pNew->minute, pNew->second);
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
return(pHead);
}
void TraverseList(pNode pHead)
{
pNode p;
p = pHead->pNext;
while (p != NULL)
{
printf("%-6s 汇报时间:%02d:%02d:%02d\n", p->name, p->hour, p->minute, p->second);
p = p->pNext;
}
printf("\n");
}
void SortList(pNode pp, int num)
{
pNode p, q;
int i, j, m = num;
for (i = 0; i < num - 1; i++)
{
/*头指针重置*/
p = pp;
q = pp->pNext;
/*单论冒泡排序,将最大的数送到最下面*/
for (j = 0; j < m - 1; j++)
{
if (q->time > q->pNext->time)
{
p->pNext = q->pNext;
q->pNext = q->pNext->pNext;
p->pNext->pNext = q;
q = p->pNext;
}
q = q->pNext;
p = p->pNext;
}
m--;
}
}
void Money(pNode p)
{
pNode pHead, pTail, pNew, pSwap;
pNew = (pNode)malloc(sizeof(Node));
pHead = p;
pSwap = NULL;
while (p != NULL)
{
pSwap = p;
p = p->pNext;
}
pTail = pSwap;
pTail->pNext = NULL;
if (pTail->minute > 4)
{
printf("%s的汇报时间最长,发%d元的红包!", pTail->name, (pTail->minute - 4) * 2);
}
}
收获:
- 键入指定按键中断循环,不用scanf,下面语句是键入Esc来中断循环的
while (c != 27)
{
statement
if (kbhit())
{
c = getch();
}
}