You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复小型规划程序代码,实现可用实时时钟与工作计时秒表?

修复小型规划程序的时钟运行与工作耗时统计问题

问题根源分析

  1. 时钟运行异常:

    • 12小时制逻辑错误:原代码中小时超过12时重置为0,不符合常规12小时制规则(应重置为1)。
    • 时钟更新依赖用户操作:原程序仅在用户输入选择后才执行sleep(1)并更新时间,导致时钟无法持续自动走时,仅在用户操作后跳变。
  2. 工作耗时统计失效:

    • 原逻辑仅支持用户手动触发一次加1秒,无法自动统计任务的持续时长。
  3. 缺少持续秒表:原程序无独立的持续计时模块,无法满足侧边秒表需求。

修复后的完整代码

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>

void displayTime(int h, int m, int s)
{
    printf("Clock: %02d:%02d:%02d\n", h, m, s);
}

void displayStopwatch(int sw_h, int sw_m, int sw_s)
{
    printf("Stopwatch: %02d:%02d:%02d\n", sw_h, sw_m, sw_s);
}

void displayTasks(char tasks[][50], int timeSpent[], int taskCount, int workingTask)
{
    printf("\nTask List:\n");
    for (int i = 0; i < taskCount; i++)
    {
        printf("%d. %s\tTime Spent: %d seconds\n", i + 1, tasks[i], timeSpent[i]);
    }

    if(workingTask != -1)
    {
        printf("\nWorking on: %s\n", tasks[workingTask]); 
    }
}

int main()
{
    int h = 0, m = 0, s = 0;
    int sw_h = 0, sw_m = 0, sw_s = 0; // 秒表计时变量
    int d = 1;

    char tasks[100][50];
    int taskCount = 0;
    int timeSpent[100] = {0};
    int workingTask = -1;
    int clockStarted = 0;
    int stopwatchStarted = 1; // 秒表默认启动

    printf("Set time (HH MM SS): ");
    scanf("%d%d%d", &h, &m, &s);

    // 修正时间合法性判断,小时范围限定为1-12
    if(h > 12 || h < 1 || m >= 60 || s >= 60)
    {
        printf("ERROR: Invalid time\n");
        return 1;
    }

    while(1)
    {
        // 清屏模拟侧边显示效果
        for(int i = 0; i < 50; i++)
        {
            printf("\n");
        }

        // 显示时钟与持续运行的秒表
        if(clockStarted)
        {
            displayTime(h, m, s);
        }
        else
        {
            printf("Clock: not started\n");
        }
        displayStopwatch(sw_h, sw_m, sw_s);

        displayTasks(tasks, timeSpent, taskCount, workingTask);

        printf("\n1. Add Task\n");
        printf("2. Start Working on Task\n");
        printf("3. Stop Working on Task\n");
        printf("4. Start Clock\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");

        int choice;
        scanf("%d", &choice);

        if(choice == 1)
        {
            printf("Enter task description: ");
            scanf(" %[^\n]s", tasks[taskCount]);
            taskCount++;
        } 
   
        else if (choice == 2) 
        {
            if (taskCount > 0)
            {
                printf("Enter the task number to start working on: ");
                scanf("%d", &workingTask);
                workingTask--; // 转为数组索引
            }
            else 
            {
                printf("No tasks available to start working on.\n");
            }
        }

        else if (choice == 3)
        {
            workingTask = -1;
            printf("Stopped working on current task.\n");
        }
        else if (choice == 4)
        {
            clockStarted = 1;
        }
        else if (choice == 5)
        {
            printf("\nEnd of the day summary:\n");
            displayTasks(tasks, timeSpent, taskCount, -1);
            return 0;
        }
        else
        {
            printf("Invalid choice. Please enter a valid option.\n");
        }

        sleep(d);

        // 自动更新秒表(持续运行)
        sw_s++;
        if (sw_s >= 60)
        {
            sw_m++;
            sw_s = 0;
        }
        if (sw_m >= 60)
        {
            sw_h++;
            sw_m = 0;
        }

        // 自动更新系统时钟
        if (clockStarted)
        {
            s++;
            if (s >= 60)
            {
                m++;
                s = 0;
            }
            if (m >= 60)
            {
                h++;
                m = 0;
            }
            if (h > 12)
            {
                h = 1; // 修正12小时制逻辑
            }
        }

        // 自动统计当前任务耗时
        if (workingTask != -1)
        {
            timeSpent[workingTask]++;
        }
    }
    return 0;
}

关键修复说明

  • 时钟修复:

    • 调整时间合法性校验,限定小时范围为1-12。
    • 修正12小时制循环逻辑,小时超过12时重置为1。
    • 将时钟与秒表的更新逻辑移至sleep(1)之后,实现每秒自动走时,不再依赖用户操作。
  • 耗时统计修复:

    • 将原“Complete Working Task”选项改为“Stop Working on Task”,功能改为停止当前任务。
    • 在每秒循环中自动检查是否有正在进行的任务,若有则自动累加对应任务的耗时。
  • 秒表实现:

    • 新增独立的秒表计时变量与显示函数,默认启动并持续运行,放在时钟下方模拟侧边效果。

内容的提问来源于stack exchange,提问作者windthief

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:05:24