Pythonディスクスケジューリングアルゴリズム実装

9233 ワード

def loaddata(fileName):     #     
    f = open(fileName)
    start = f.readline()    #   data            
    data = f.readline()     #                   
    return start, data


def loadnext(now, next):    #                  
    length = abs(int(now) - int(next))  #     
    str_length =str(length)
    if len(next) == 3:
        if len(str_length) == 1:
            print("|         ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("        |")
        elif len(str_length) == 2:
            print("|         ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("       |")
        else:
            print("|         ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("      |")
    elif len(next) == 2:
        if len(str_length) == 1:
            print("|          ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("        |")
        elif len(str_length) == 2:
            print("|          ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("       |")
        else:
            print("|          ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("      |")
    elif len(next) == 1:
        if len(str_length) == 1:
            print("|           ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("        |")
        elif len(str_length) == 2:
            print("|           ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("       |")
        else:
            print("|           ", end="")
            print(next, end="")
            print("         |   ", end="")
            print("    ", end="")
            print(length, end="")
            print("      |")
    return length


def FCFS():
    l = 0
    start, data = loaddata('data')
    print("|--------------------------------------|")
    print("|              |              |")
    print("|--------------------------------------|")
    for d in data.split():
        l += loadnext(start, d)     #                ,        
        start = d                   #                   
    n = len(data.split())
    print("|--------------------------------------|")
    print("|      (FCFS)       :%.1f      |" % (l / n))
    print("|--------------------------------------|")


def findnextindex(start, datas):    # SSTF  
    length = []
    for data in datas:
        l = abs(int(start) - int(data))     #            
        length.append(l)                    #               
    minIndex = length.index(min(length))    #                  
    return minIndex


def SSTF():
    l = 0
    start, data = loaddata('data')
    data2 = data.split().copy()
    n = len(data2)
    print("|--------------------------------------|")
    print("|              |              |")
    print("|--------------------------------------|")
    for d in data.split():
        nextIndex = findnextindex(start, data2)     #            
        l += loadnext(start, data2[nextIndex])
        start = data2[nextIndex]
        data2.remove(data2[nextIndex])

    print("|--------------------------------------|")
    print("|        (SSTF)       :%.1f  |" % (l / n))
    print("|--------------------------------------|")


def findnext1(now, data):                   # SCAN
    biggerList = []
    smallerList = []
    for d in data:
        if int(d) > int(now):               #                 
            biggerList.append(d)            #                   
    if (len(biggerList) == 0):              #                  
        if len(data) != 0:
            for d2 in data:
                if int(d2) < int(now):
                    smallerList.append(d2)  #         
            return max(smallerList)
        else:
            return None
    return min(biggerList)


def SCAN():
    l = 0
    start, data = loaddata('data')
    data2 = data.split().copy()
    print("|--------------------------------------|")
    print("|              |              |")
    print("|--------------------------------------|")
    n = len(data2)
    for d in data:
        next = findnext1(start, data2)         #
        if next == None:
            break
        l += loadnext(start, next)
        start = next
        data2.remove(next)
    print("|--------------------------------------|")
    print("|      (SCAN)      :%.1f       |" % (l / n))
    print("|--------------------------------------|")


def findnext2(now, data):               # CSCAN
    biggerList = []
    smallerList = []
    for d in data:
        if int(d) > int(now):
            biggerList.append(d)
    if (len(biggerList) == 0):
        if len(data) != 0:
            now = 0
            return (findnext2(now, data))
        else:
            return None
    return min(biggerList)


def CSCAN():
    l = 0
    start, data = loaddata('data')
    data2 = data.split().copy()
    n = len(data2)
    print("|--------------------------------------|")
    print("|              |              |")
    print("|--------------------------------------|")
    for d in data:
        next = findnext2(start, data2)
        if next == None:
            break
        l += loadnext(start, next)
        start = next
        data2.remove(next)
    print("|--------------------------------------|")
    print("|      (CSCAN)      :%.1f      |" % (l / n))
    print("|--------------------------------------|")


def main():
    num4 = int(input("     (1、 ;0、 ):"))
    if num4 == 1:
        filename = 'data'
        with open(filename, 'w') as f:
            first = "100"
            second = "55 58 39 18 90 160 150 38 184"
            f.write(first)
            f.write("
") f.write(second) else: print("") start, data = loaddata('data') print("|------------ -------------|") print("|------1、 (FCFS ) -----|") print("|------2、 (SSTF ) -----|") print("|------3、 (SCAN ) -----|") print("|------4、 (CSCAN) -----|") print("|---------------------------------------|") print(" :" + start + data) num = int(input(" (1~4):")) if num == 1: FCFS() elif num == 2: SSTF() elif num == 3: SCAN() elif num == 4: CSCAN() else: num1 = int(input(" , ?(1: 0: )")) if num1 == 1: # 100 # 55 58 39 18 90 160 150 38 184 print(" :100
"+"55 58 39 18 90 160 150 38 184") filename = 'data' with open(filename, 'w') as f: first = int(input(" :")) second = input(" ( ):") f.write(str(first)) f.write("
") f.write(str(second)) else: num3 = int(input(" ?(1: 0: )")) if num3 == 1 : print(" ! 。") exit() elif num3 == 0: main() else: print(" !") main() num2 = int(input(" ?(1: 0: , )")) if num2 == 1: main() elif num2 == 0: print(" ! 。") exit() else: print(" !") main() if __name__ == "__main__": main()