C语言项目任务时间计算 - 最早完成时间和最晚完成时间
/* This program calculates the earliest and latest completion times for a set of tasks in a project. It takes input for the number of tasks, task names, task times, and task dependencies. It then calculates the earliest completion time for each task and the overall project, and the latest completion time for each task. */\n#include <stdio.h>\n#define MAX_TASKS 10\n\ntypedef struct {\n char name; \n int time; \n int dependencies[MAX_TASKS]; \n int numDependencies; \n} Task;\n\n// Function to calculate the earliest completion time for a task\nint calculateEarliestTime(Task tasks[], int taskId) {\n // If the task has no dependencies, return its time\n if (tasks[taskId].numDependencies == 0) {\n return tasks[taskId].time; \n }\n \n int maxTime = 0; \n // Calculate the earliest completion time for each dependency\n for (int i = 0; i < tasks[taskId].numDependencies; i++) {\n int dependencyId = tasks[taskId].dependencies[i]; \n int dependencyTime = calculateEarliestTime(tasks, dependencyId); \n // Update the maximum time if the dependency time is greater\n if (dependencyTime > maxTime) {\n maxTime = dependencyTime; \n }\n }\n \n // Return the sum of the maximum dependency time and the task time\n return maxTime + tasks[taskId].time; \n}\n\n// Function to calculate the latest completion time for a task\nint calculateLatestTime(Task tasks[], int taskId, int projectTime) {\n // If the task has no dependencies, return the project time minus its own time\n if (tasks[taskId].numDependencies == 0) {\n return projectTime - tasks[taskId].time; \n }\n \n int minTime = projectTime; \n // Calculate the latest completion time for each dependency\n for (int i = 0; i < tasks[taskId].numDependencies; i++) {\n int dependencyId = tasks[taskId].dependencies[i]; \n int dependencyTime = calculateLatestTime(tasks, dependencyId, projectTime); \n // Update the minimum time if the dependency time is smaller\n if (dependencyTime < minTime) {\n minTime = dependencyTime; \n }\n }\n \n // Return the difference between the minimum dependency time and the task time\n return minTime - tasks[taskId].time; \n}\n\nint main() {\n int numTasks; \n printf("请输入任务数量:"); \n scanf("%d", &numTasks); \n \n Task tasks[MAX_TASKS]; \n \n // Input task details from user\n for (int i = 0; i < numTasks; i++) {\n printf("请输入任务名称:"); \n scanf(" %c", &tasks[i].name); \n \n printf("请输入预计完成时间:"); \n scanf("%d", &tasks[i].time); \n \n printf("请输入前置任务数量:"); \n scanf("%d", &tasks[i].numDependencies); \n \n printf("请输入前置任务编号(用空格分隔):"); \n for (int j = 0; j < tasks[i].numDependencies; j++) {\n scanf("%d", &tasks[i].dependencies[j]); \n }\n }\n \n int projectTime = 0; \n // Calculate the earliest completion time for each task and the overall project\n for (int i = 0; i < numTasks; i++) {\n int taskEarliestTime = calculateEarliestTime(tasks, i); \n // Update the project time if the task earliest time is greater\n if (taskEarliestTime > projectTime) {\n projectTime = taskEarliestTime; \n }\n }\n \n printf("整个项目的最短时间为:%d\n", projectTime); \n \n // Calculate the latest completion time for each task\n for (int i = 0; i < numTasks; i++) {\n int taskLatestTime = calculateLatestTime(tasks, i, projectTime); \n printf("任务%c的最晚完成时间为:%d\n", tasks[i].name, taskLatestTime); \n }\n \n return 0; \n}
原文地址: https://www.cveoy.top/t/topic/pJPD 著作权归作者所有。请勿转载和采集!