/* This program calculates the earliest and latest completion times for a project consisting of multiple tasks. The program takes user input for the number of tasks and their details, including the task name, estimated completion time, and any dependencies on other tasks. It then calculates the earliest completion time for the entire project and the latest completion time for each individual task. */ #include <stdio.h> #define MAX_TASKS 10

typedef struct { char name; int time; int dependencies[MAX_TASKS]; int numDependencies; } Task;

// Function to calculate the earliest completion time for a task int calculateEarliestTime(Task tasks[], int taskId) { if (tasks[taskId].numDependencies == 0) { return tasks[taskId].time; }

int maxTime = 0;
for (int i = 0; i < tasks[taskId].numDependencies; i++) {
    int dependencyId = tasks[taskId].dependencies[i];
    int dependencyTime = calculateEarliestTime(tasks, dependencyId);
    if (dependencyTime > maxTime) {
        maxTime = dependencyTime;
    }
}

return maxTime + tasks[taskId].time;

}

// Function to calculate the latest completion time for a task int calculateLatestTime(Task tasks[], int taskId, int projectTime) { if (tasks[taskId].numDependencies == 0) { return projectTime - tasks[taskId].time; }

int minTime = projectTime;
for (int i = 0; i < tasks[taskId].numDependencies; i++) {
    int dependencyId = tasks[taskId].dependencies[i];
    int dependencyTime = calculateLatestTime(tasks, dependencyId, projectTime);
    if (dependencyTime < minTime) {
        minTime = dependencyTime;
    }
}

return minTime - tasks[taskId].time;

}

int main() { int numTasks; printf("请输入任务数量:"); scanf("%d", &numTasks);

Task tasks[MAX_TASKS];

for (int i = 0; i < numTasks; i++) {
    printf("请输入任务名称:");
    scanf(" %c", &tasks[i].name);
    
    printf("请输入预计完成时间:");
    scanf("%d", &tasks[i].time);
    
    printf("请输入前置任务数量:");
    scanf("%d", &tasks[i].numDependencies);
    
    printf("请输入前置任务编号(用空格分隔):");
    for (int j = 0; j < tasks[i].numDependencies; j++) {
        scanf("%d", &tasks[i].dependencies[j]);
    }
}

int projectTime = 0;
for (int i = 0; i < numTasks; i++) {
    int taskEarliestTime = calculateEarliestTime(tasks, i);
    if (taskEarliestTime > projectTime) {
        projectTime = taskEarliestTime;
    }
}

printf("整个项目的最短时间为:%d\n", projectTime);

for (int i = 0; i < numTasks; i++) {
    int taskLatestTime = calculateLatestTime(tasks, i, projectTime);
    printf("任务%c的最晚完成时间为:%d\n", tasks[i].name, taskLatestTime);
}

return 0;

}

C语言项目时间管理:计算项目的最短时间和任务的最晚完成时间

原文地址: https://www.cveoy.top/t/topic/pJPH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录