physics-lab-41/main.py

57 lines
2.1 KiB
Python
Raw Normal View History

2023-02-25 23:11:14 +02:00
import os, sys
def avg(a):
return sum(a) / len(a)
def process_table(file):
table = [["L2", "L1", "t2", "m", "Δm", "t2i", "v1", "(v1)^2", "a", "ai", "❬a❭", "A", "B"]]
supported_args = ["r"]
config = {"r": 6}
2023-02-25 23:11:14 +02:00
with open(file, "r", encoding = "UTF-8") as f:
for i in f:
if i.rstrip("\r\n") != "":
if i.rstrip("\r\n")[0] == "#" or i.rstrip("\r\n")[0] == ";":
print(f"Special line: {i}")
try:
cline = i.rstrip("\r\n")[1:].split()
if len(cline) == 2 and cline[0] in supported_args:
config[cline[0]] = int(cline[1])
except:
pass
else:
print(f"Table line: {i}")
table.append(list(map(lambda x: float(x), i.rstrip("\r\n").replace(",", ".").split())))
r = config["r"]
2023-02-25 23:11:14 +02:00
for i in table[1:]:
# first table
i.append(round(i[0] / i[2], r))
i.append(round(i[6]**2, r))
i.append(round((i[0]**2)/(2*(i[2]**2)*i[1]), r))
# precalculations
preset_L2 = table[1][0]
preset_L1 = table[1][1]
2023-02-25 23:11:14 +02:00
preset_ai = [(preset_L2**2) / (2 * (avg(list(zip(*table[1:][i:i+3]))[5])**2) * preset_L1) for i in range(0, 12, 3)]
for i in range(4):
for k in range(3):
table[1 + i*3 + k].append(round(preset_ai[i], r))
preset_avg_a = [(preset_L2**2) / (2 * (avg(list(zip(*table[1:][i:i+3]))[9])**2) * preset_L1) for i in range(0, 12, 3)]
for i in range(4):
for k in range(3):
table[1 + i*3 + k].append(round(preset_avg_a[i], r))
2023-02-25 23:11:14 +02:00
for i in table[1:]:
i.append(round(i[4]/(2*i[3]), r))
i.append(round(i[10]/(9.81-i[10]), r))
w = max([max(list(map(lambda x: len(str(x)), i))) for i in table])
print("\n".join([" | ".join(list(map(lambda x: str(x).center(w), i))) for i in table]))
if len(sys.argv) == 2 and os.path.exists(sys.argv[1]):
process_table(sys.argv[1])
else:
print("Будь ласка, вкажіть файл для обробки")