如何根据文本文件中的内容使甘特图和表格工作?

问题描述

如何修复甘特图以及平均等待时间和周转时间的计算方法。我遇到了问题,因为每次我运行它时,表都被弄乱了,平均值和总数都错了。我该怎么解决

请帮助。该代码本来是可抢占的,但现在已成为优先级,我无法对其进行调整。

#!/usr/bin/env python
# coding: utf-8
# In[14]:


import os
import time

def findWaitingTime(processes,n,wt):  
    rt = [0] * n 

    # copy the burst time into rt[]  
    for i in range(n):  
        rt[i] = processes[0][i] 
    complete = 0
    t = 0
    minm = 999999999
    short = 0
    check = False

    # Process until all processes gets  
    # completed  
    while (complete != n): 

        # Find process with minimum remaining  
        # time among the processes that  
        # arrives till the current time` 
        for j in range(n): 
            if ((processes[1][j] <= t) and 
                (rt[j] < minm) and rt[j] > 0): 
                minm = rt[j] 
                short = j 
                check = True
        if (check == False): 
            t += 1
            continue

        # Reduce remaining time by one  
        rt[short] -= 1

        # Update minimum  
        minm = rt[short]  
        if (minm == 0):  
            minm = 999999999

        # If a process gets completely  
        # executed  
        if (rt[short] == 0):  

            # Increment complete  
            complete += 1
            check = False

            # Find finish time of current  
            # process  
            fint = t + 1

            # Calculate waiting time  
            wt[short] = (fint - processes[0][short] -    
                                processes[1][short]) 

            if (wt[short] < 0): 
                wt[short] = 0

        # Increment time  
        t += 1
  
# Function to calculate turn around time  
def findTurnAroundTime(processes,wt,tat):  
      
    # Calculating turnaround time  
    for i in range(n): 
        tat[i] = processes[0][i] + wt[i]  

def PriorityPreemptive(processes,n):
    ArrivalTime = []
    ArrivalStatus = []
    waitingTime = 0
    totalTime = 0
    GanttChart = []
    for i in range(0,n):
        ArrivalTime.append(processes[1][i])
        ArrivalStatus.append(False)
        waitingTime -= (processes[0][i]+processes[1][i])
        totalTime += processes[0][i]
    smallestIndex = 0
    smallestValue = totalTime
    for i in range(0,totalTime):
        smallestValue = totalTime
        for j in range(0,n):
            if i == ArrivalTime[j]:
                ArrivalStatus[j] = True
        for j in range(0,n):
            if ArrivalStatus[j] and processes[0][j] < smallestValue and processes[0][j] != 0:
                smallestValue = processes[0][j]
                smallestIndex = j
        GanttChart.append(smallestIndex+1)
        processes[0][smallestIndex] -= 1
        if processes[0][smallestIndex] == 0:
            waitingTime += (i+1)
    waitingTime /= n
    return GanttChart                

def printganttChart(processes,n):
    print("\nGantt Chart")
    
    #display Gantt Chart
    gantt = []
    chart = PriorityPreemptive(processes,n)
    similar = 1
    for i in range(len(chart[:-1])):
        if chart[i] == chart[i + 1]:
            similar += 1
        else:
            gantt.append(similar)
            similar = 1
    gantt.append(similar)
    summary_process = []
    for i in range(len(chart[:-1])):
        if chart[i] != chart[i + 1]:
            summary_process.append(f"P{chart[i]}")
    summary_process.append(f"P{chart[i]}")
    summary_chart = [0]
    for process in gantt:
        summary_chart.append(max(summary_chart) + process)
        
    i=0
    #printing top bar
    print("\u250c",end='')
    
    while i+1 <= len(summary_process):
        j=0
        while j < summary_chart[i+1] - summary_chart[i]:
            print("\u2500\u2500",end='')
            j+=1
        if i+1 < len(summary_process):
            print("\u252c",end='')
        elif i+1 == len(summary_process):
            print("\u2510",end='')   
        i+=1
    
    print("\n\u2502",end='')
   
    i=0
    #middle position
    while i+1 <= len(summary_process):
        j=0
        while j < summary_chart[i+1] - summary_chart[i] - 1:
            print(" ",end='')
            j+=1
        print(summary_process[i],end='')
        j=0
        while j < summary_chart[i+1] - summary_chart[i] - 1:
            print(" ",end='')
            j+=1
        print("\u2502",end='')
        i+=1
    
    print("\n",end='')
    
    i=0
    #printing bottom bar
    print("\u2514",end='')
            j+=1
        if i+1 < len(summary_process):
            print("\u2534",end='')
        elif i+1 == len(summary_process):
            print("\u2518",end='')    
        i+=1
    
    i=0
    #printing waiting time
    print("\n{}".format(summary_chart[0]),end='')
    
    while i+1 <= len(summary_process):
        j=0
        while j < summary_chart[i+1] - summary_chart[i] - 1:
            print(" ",end='')
            j+=1
        print("  ",end='')
        j=0
        if summary_chart[i+1] < 100 and summary_chart[i+1] > 9:
            j+=1
        elif summary_chart[i+1] < 1000 and summary_chart[i+1] > 99:
            j+=2
        elif summary_chart[i+1] < 10000 and summary_chart[i+1] > 999:
            j+=3
        while j < summary_chart[i+1] - summary_chart[i] - 1:
            print(" ",end='')
            j+=1
        
        print(summary_chart[i+1],end='')
        i+=1
        
# Function to calculate average waiting  
# and turn-around times.  
def doremaining(processes,n):  
    wt = [0] * n 
    tat = [0] * n  
  
    # Function to find waiting time  
    # of all processes
    findWaitingTime(processes,wt)  
  
    # Function to find turn around time 
    # for all processes  
    findTurnAroundTime(processes,tat)  
    
    #burst = processes[0];
    #arrival = processes[1];
    
    printganttChart(processes,n)
    
    # display processes along with all details in table 
    print("\n\nTable")
    print("\u250c",end='')
    heading = ['PROCESS','TURNAROUND TIME','WAITING TIME']
    
    a=0
    while a < len(heading):
        b=0
        while b < len(heading[a]):
            print("\u2500",end='')
            b+=1
        if a == 2:
            print("\u2510",end='')
        else:
            print("\u252c",end='')
        a+=1   
    
    print("\n\u2502",end='')
    
    for i in heading:
        print("{}\u2502".format(i),end='')
    
    print("\n\u251c",end='')
    
    a=0
    while a < len(heading):
        b=0
        while b < len(heading[a]):
            print("\u2500",end='')
            b+=1
        if a == 2:
            print("\u2524",end='')
        else:
            print("\u253c",end='')
        a+=1
    
    print("\n",end='')
    
    total_wt = 0
    total_tat = 0
    ctr = 1
    for i in range(n): 
  
        total_wt = total_wt + wt[i]  
        total_tat = total_tat + tat[i]  
        print("\u2502 ","P{}   \u2502".format(ctr),"     {}     ".format(tat[i]),end='')
        if tat[i] > 99 and tat[i] < 1000:
            print(" ",end='')
        elif tat[i] > 9 and tat[i] < 100:
            print("  ",end='')
        elif tat[i] < 10:
            print("   ",end='')
        print("\u2502    {}".format(wt[i]),"   ",end='')
        if wt[i] > 99 and wt[i] < 1000:
            print(" ",end='')
        elif wt[i] > 9 and wt[i] < 100:
            print("  ",end='')
        elif wt[i] < 10:
            print("   ",end='')
        print("\u2502")
        ctr+=1
    
    print("\u251c",end='')
    
    print("\u2502 TOTAL \u2502      {}     ".format(total_tat),end='')
    if total_tat > 99 and total_tat < 1000:
            print(" ",end='')
    elif total_tat > 9 and total_tat < 100:
        print("  ",end='')
    elif total_tat < 10:
        print("   ",end='')
    print("\u2502    {}    ".format(total_wt),end='')
    if total_wt > 99 and total_wt < 1000:
            print(" ",end='')
    elif total_wt > 9 and total_wt < 100:
        print("  ",end='')
    elif total_wt < 10:
        print("   ",end='')
    print("\u2502")
    
    print("\u251c",end='')
    
    print("\u2502AVERAGE\u2502      {}  ".format(total_tat/n),end='')
    if len(str(total_tat/n)) == 6:
            print(" ",end='')
    elif len(str(total_tat/n)) == 5:
        print("  ",end='')
    elif len(str(total_tat/n)) == 4:
        print("   ",end='')
    elif len(str(total_tat/n)) == 3:
        print("    ",end='')
    print("\u2502    {} ".format(total_wt/n),end='')
    if len(str(total_wt/n)) == 6:
            print(" ",end='')
    elif len(str(total_wt/n)) == 5:
        print("  ",end='')
    elif len(str(total_wt/n)) == 4:
        print("   ",end='')
    elif len(str(total_wt/n)) == 3:
        print("    ",end='')
    print("\u2502")
    
    print("\u2514",end='')
            b+=1
        if a == 2:
            print("\u2518",end='')
        else:
            print("\u2534",end='')
        a+=1
        
def main():
    print("Programmed by: Timothy Kyle B. Chan")
    print("MP01 - Priority-Preemptive")
    print("\nProgram reading input file....")
    time.sleep(2)
    print("input file: MP01_check.txt")

    x = open('MP01_check.txt')
    y = int(x.readline())

    print("\nEnter No. of Processes: {}".format(y))
    print("Arrival Time:")

    z = []
    i = 1
    while i <= int(y):
        z.append(int(x.readline()))
        print("P{}: {}".format(i,z[i-1]))
        i += 1

    print("Burst Time:")

    z2 = []
    i = 1
    while i <= int(y):
        z2.append(int(x.readline()))
        print("P{}: {}".format(i,z2[i-1]))
        i += 1

    print("Priority:")

    z3 = []
    i = 1
    while i <= int(y):
        z3.append(int(x.readline()))
        print("P{}: {}".format(i,z3[i-1]))
        i += 1

    proc = [z3,z2,z]
    doremaining(proc,y)
    repeat()
    
def repeat():    
    print("\nDo you want to run again [y/n]:",end=' ')
    iu = input()
    if iu == 'y':
        os.system('clear')
        main()
    elif iu == 'n':
        os.system('pause')
    else:
        print("Please type only what is indicated inside the square brackets")
        repeat()

main()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)