# ===== CodeLab: game-c-typing =====
# 以下代码片段按文章出现顺序拼接, 共 6 段

# ----- 片段 1 (c) -----
#include <stdio.h>
#include <string.h>

int main(void) {
    /* 模拟 fgets 读进来的一行,末尾带着换行符 */
    char typed[] = "hello\n";

    /* strcspn 返回第一个换行符的下标,把它替换成字符串结束符 */
    typed[strcspn(typed, "\n")] = '\0';
    printf("处理后的输入:[%s]\n", typed);
    return 0;
}

# ----- 片段 2 (c) -----
#include <stdio.h>
#include <time.h>

int main(void) {
    clock_t start = clock();
    volatile long sum = 0;
    for (long i = 0; i < 10000000; i++)
        sum += i;              /* 模拟一点计算耗时 */
    clock_t end = clock();

    double seconds = (double)(end - start) / CLOCKS_PER_SEC;
    printf("耗时: %.3f 秒\n", seconds);
    return 0;
}

# ----- 片段 3 (c) -----
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *target = "hello world";
    char typed[] = "hello worl";       /* 故意少打一个 d */
    int correct = 0, wrong = 0;
    int len_target = (int)strlen(target);
    int len_typed = (int)strlen(typed);
    int i;

    for (i = 0; i < len_target; i++) {
        if (i < len_typed && typed[i] == target[i])
            correct++;
        else
            wrong++;
    }
    /* 输入比目标长时,多出来的字符也算错 */
    if (len_typed > len_target)
        wrong += len_typed - len_target;

    printf("correct=%d wrong=%d\n", correct, wrong);
    return 0;
}

# ----- 片段 4 (c) -----
#include <stdio.h>

int main(void) {
    int correct = 40, wrong = 5;
    double seconds = 30.0;
    double wpm, accuracy;

    /* 防御:用时太短就兜底,防止除零 */
    if (seconds < 0.001)
        seconds = 0.001;
    wpm = (correct / 5.0) / (seconds / 60.0);
    accuracy = 100.0 * correct / (correct + wrong);

    printf("速度: %.0f WPM\n", wpm);
    printf("正确率: %.1f%%\n", accuracy);
    return 0;
}

# ----- 片段 5 (c) -----
#include <stdio.h>
#include <string.h>
#include <time.h>

#define MAX_LEN 128

int main(void) {
    const char *target = "the quick brown fox jumps over the lazy dog";
    char typed[MAX_LEN];
    clock_t start, end;
    double seconds, wpm, accuracy;
    int i, correct = 0, wrong = 0;
    int len_target, len_typed;

    printf("目标句子:\n%s\n\n", target);
    printf("照着输入,按回车结束:\n");

    start = clock();
    if (fgets(typed, MAX_LEN, stdin) == NULL)
        return 1;                 /* 读入失败(如 EOF),直接退出 */
    end = clock();

    seconds = (double)(end - start) / CLOCKS_PER_SEC;
    typed[strcspn(typed, "\n")] = '\0';

    len_target = (int)strlen(target);
    len_typed = (int)strlen(typed);

    for (i = 0; i < len_target; i++) {
        if (i < len_typed && typed[i] == target[i])
            correct++;
        else
            wrong++;
    }
    if (len_typed > len_target)
        wrong += len_typed - len_target;

    if (seconds < 0.001)
        seconds = 0.001;
    wpm = (correct / 5.0) / (seconds / 60.0);
    accuracy = 100.0 * correct / (correct + wrong);

    printf("\n用时: %.1f 秒\n", seconds);
    printf("正确字符: %d / %d\n", correct, len_target);
    printf("速度: %.0f WPM\n", wpm);
    printf("正确率: %.1f%%\n", accuracy);
    return 0;
}

# ----- 片段 6 (bash) -----
gcc typing.c -o typing
./typing
