📜  使用C语言的秒表

📅  最后修改于: 2021-05-25 20:29:29             🧑  作者: Mango

用C创建一个可在Linux基本系统上运行的数字秒表程序。

keyboardhit()函数只是代表键盘点击。按下键后,它将生成信号并返回非零整数。其中有4个循环,第一个循环持续数小时,第二个循环持续数分钟,第3个持续秒数,第4个循环保持秒数的速度(3个循环)。运行该程序后,它等待键盘启动键被按下,当按下该键时,它会产生一个信号。为了存储键盘键,有一个变量(c),如果c等于p键,则它调用wait函数。该线程在后台运行,我们正在等待按下开始键。按下s键后,线程再次跳至thread_join函数,如果按下r键,则跳入复位标签,所有循环再次从零开始,如果按下s键,则跳入开始标签,如果e键为按下它会调用exit()函数,程序将终止。

先决条件: c中的线程,等待系统调用

要执行该程序,我们使用以下命令:

输入:按一个键:s->开始e->退出r->重置p->暂停输出:

// C code to create stop watch
  
// Header file for necessary system library
#include 
#include 
#include 
#include 
#include 
#include 
  
// starting from zero
#define MIN 0
  
// 1 hr= 60 min ; 1 min= 60 sec
#define MAX 60
  
#define MILLI 200000
  
int i, j, k, n, s;
char c;
pthread_t t1;
  
// Function to perform operations
// according keyboeard hit.
int keyboardhit(void)
{
    struct termios oldt, newt;
    int ch;
    int oldf;
  
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  
    ch = getchar();
  
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);
  
    if (ch != EOF) {
        ungetc(ch, stdin);
        return 1;
    }
  
    return 0;
}
  
// display stopwatch on screen
void print()
{
    // clear screen escape sequence
    printf("\033[2J\033[1;1H");
  
    // Display Hour Min Sec in screen
    printf("TIME\t\t\t\tHr: %d Min: %d Sec: %d", n, i, j);
}
  
// function to pause stopwatch
void* wait(void* arg)
{
  
    while (1) {
  
        // wait for keyboard signal if keyboard
        // hit it return non integer number
        if (keyboardhit()) {
            // take input from user and do
            // operation accordingly
            c = getchar();
            if (c == 'S' || c == 's') {
                break;
            }
            else if (c == 'r' || c == 'R') {
                s = 1;
                break;
            }
            else if (c == 'e' || c == 'E')
  
                exit(0);
        }
    }
}
  
// driver code
int main()
{
  
    // label to reset the value
reset:
    n = MIN;
    i = MIN;
    j = MIN;
    k = MIN, s = MIN;
  
    print();
  
    while (1) {
  
        /* s for start
        e for exit
        p for pause
        r for reset
        */
        if (keyboardhit()) {
            c = getchar();
  
            if (c != 's')
                continue;
  
            for (n = MIN; n < MAX; n++) {
                for (i = MIN; i < MAX; i++) {
                    for (j = MIN; j < MAX; j++) {
                        for (k = MIN; k < MILLI; k++) {
                        start:
                            print();
                            if (keyboardhit()) {
                                c = getchar();
  
                                if (c == 'r' || c == 'R')
                                    goto reset;
                                else if (c == 'e' || c == 'E')
                                    exit(0);
                                else if (c == 's' || c == 'S')
                                    goto start;
                                else if (c == 'P' || c == 'p') {
  
                                    pthread_create(&t1, NULL, &wait, NULL);
  
                                    // waiting for join a thread
                                    pthread_join(t1, NULL);
                                    if (s == 1)
                                        goto reset;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
  
    return 0;
}

输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。