【解決方法】このコードを C# に変換する方法


from os import system
import sys

def clear_print():
    system('clear')
    

def printer():
    global set_ctr
    clear_print()
    print("SET {0} ALLOCATION".format(set_ctr))

    print("\nMEMORY\t\t\tPARTITION SIZE\tALLOC\\DEALLOC\n""OS Partition\t{0:g}\t\t\t\tOS".format(OS_size))
    ctr = 1
    for item in partitions:
        if ctr >= 10:
            if allocations.get(item) is None:
                print("{0}\t{1:g}".format(item, partitions[item]))
            else:
                print("{0}\t{1:g}\t\t\t\t{2}".format(item, partitions[item], partition_job.get(item)))
        else:
            ctr += 1
            if allocations.get(item) is None:
                print("{0}\t\t{1:g}".format(item, partitions[item]))
            else:
                print("{0}\t\t{1:g}\t\t\t\t{2}".format(item, partitions[item], partition_job.get(item)))

def allocation():
    global set_ctr
    available_storage = memory_size - sum(partitions.values(), OS_size)
    partition_counter = 1
    partition_number = "Partition {0}".format(partition_counter)
    for i in list(jobs.keys()):
        if jobs[i] <= available_storage and i not in currently:
            partitions[partition_number] = jobs[i]
            allocations[partition_number] = jobs[i]
            partition_job[partition_number] = i
            currently.append(i)
            partition_counter += 1
            partition_number = "Partition {0}".format(partition_counter)
            available_storage = memory_size - sum(partitions.values(), OS_size)
    if available_storage != 0:
        partitions[partition_number] = available_storage

    if len(allocations) != 0:
        printer()
        input("\nPress any key to continue...")
        set_ctr += 1
        currently.clear()
        deallocation()

def deallocation():
    for i in list(partition_job.values()):
        for j in list(partition_job.keys()):
            if partition_job[j] == i:
                tat[i] = tat[i] - 1
                if tat[i] == 0:
                    allocations.pop(j)
                    partition_job.pop(j)
                    jobs.pop(i)
    partitions.clear()
    partition_job.clear()
    allocations.clear()
    allocation()

def validator(string, num):
    if is_number(num):
        while float(num) <= 0:
            sys.stdout.write("\033[F")
            sys.stdout.write("\033[K")
            num = validator(string, input(string))
    else:
        sys.stdout.write("\033[F")
        sys.stdout.write("\033[K")
        num = validator(string, input(string))
    return float(num)

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

clear_print()
memory_size = validator("Enter memory size in KB: ", input("Enter memory size in KB: "))
OS_size = validator("Enter OS size in KB: ", input("Enter OS size in KB: "))

partitions = dict()
jobs = dict()
tat = dict()
allocations = dict()
partition_job = dict()
currently = []

set_ctr = 1
exit_ctr = 0

# Validates OS and memory size
while OS_size >= memory_size:
    print("OS size cannot be greater than or equal to memory size.")
    input("\nPress any key to continue...")
    clear_print()
    memory_size = validator("Enter memory size in KB: ", input("Enter memory size in KB: "))
    OS_size = validator("Enter OS size in KB: ", input("Enter OS size in KB: "))

# Gets jobs
clear_print()
for x in range(10):
    jobs["Job {0}".format(x + 1)] = validator("Enter Job {0}: ".format(x + 1), input("Enter Job {0}: ".format(x + 1)))
    tat["Job {0}".format(x + 1)] = validator("Enter TAT for Job {0}: ".format(x + 1), input("Enter TAT for Job {0}: ".format(x + 1)))
    print()
input("\nPress any key to continue...")

# Prints jobs list
clear_print()
print("JOB LIST\n")
for x in range(len(jobs)):
    job = "Job {0}".format(x + 1)
    print("{0}: {1:g}\tTAT:{2:g}".format(job, jobs[job], tat[job]))
input("\nPress any key to continue...")

allocation()
deallocation()

# Prints the conclusion
clear_print()
print("There were {0} set(s) of allocation.".format(set_ctr - 1))
if len(jobs) == 0:
    input("All jobs were served/executed.\n\nPress any key to continue...")
else:
    if len(jobs) == 1:
        print("{0}".format(list(jobs.keys())[0]), end=" ")
    else:
        for jobs_item in jobs:
            if jobs_item == list(jobs.keys())[-1]:
                print("and {0}".format(jobs_item), end=" ")
            else:
                print(jobs_item, end=", ")
    input("were not served/executed.\n\nPress any key to continue...")

私が試したこと:

それを理解して変換するのが難しい。 私を助けてください

解決策 1

単純:

1) Python を学びます。
2) そのコードが何をしているかを読んで理解する。
3) C# を学びます。
4) C# でコードを書き直します。

注意: これは いいえ コード変換サービス。 このコードを変換してくれる人や、そのコードが何を行っているかについての詳細な説明を提供してくれる人を期待していたのなら、あなたは間違ったサイトに来ています。

解決策 2

これはコード変換サービスではありません。コードを翻訳するためにここにいるわけではありません。
仮にそうしたとしても、最終的にターゲット言語の「良いコード」にはなりません。それらは非常に異なるフレームワークに基づいており、ある言語で何かを機能させるものが必ずしも別の言語に直接「翻訳」されるとは限りません。
そのため、最終的には非常に貧弱なコードになり、メンテナンスが不可能ではないにしても難しく、適切にアップグレードできず、元のコードが変更された場合に大きな頭痛の種になります。 そして、「箱から出してすぐに」機能しない場合、デバッグするのは悪夢です。
代わりに、ソース コードを新しいアプリの仕様とターゲット言語/フレームワークの仕様として使用し、オリジナルを “テンプレート” として使用してゼロから記述します。 はるかに優れた結果が得られ、長期的には多くの時間を節約できます。

コメント

タイトルとURLをコピーしました