Files
mathlogic/lab2/programm/main.py
2025-04-23 20:24:23 +03:00

83 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from finite_automaton import FiniteAutomaton
def main():
alphabet = set("+-0123456789.,eE")
initial_state = "S0"
final_states = {"S2", "S4", "S7", "S8", "S9"}
transitions = {
"S0": [("+-", "S1"), ("123456789", "S2"), ("0", "S8")],
"S1": [("123456789", "S2"), ("0", "S8")],
"S2": [("0123456789", "S2"), (".,", "S3"), ("eE", "S5")],
"S3": [("0123456789", "S4")],
"S4": [("0123456789", "S4"), ("eE", "S5")],
"S5": [("+-", "S6"), ("123456789", "S7"), ("0", "S9")],
"S6": [("123456789", "S7"), ("0", "S9")],
"S7": [("0123456789", "S7")],
"S8": [(".,", "S3")],
}
automaton = FiniteAutomaton(
transitions=transitions,
initial_state=initial_state,
final_states=final_states,
alphabet=alphabet,
)
print("Конечный автомат для распознавания форматов вещественных чисел")
print("=" * 60)
print("Варианты команд:")
print(" - check <строка> - проверить, соответствует ли строка автомату")
print(
" - gen [<вероятность_остановки>] - сгенерировать случайную строку (по умолчанию 0.3)"
)
print(" - q - выход из программы")
print("=" * 60)
while True:
command = input("\nВведите команду: ").strip()
if not command:
continue
parts = command.split()
cmd = parts[0].lower()
if cmd == "q":
print("Выход из программы.")
break
elif cmd == "check":
input_string = ""
if len(parts) > 1:
input_string = " ".join(parts[1:]).strip()
message, transitions = automaton.process_input(input_string)
print(f"Результат: {message}")
print("Путь переходов:", " -> ".join(transitions))
elif cmd == "gen":
stop_prob = 0.3
if len(parts) > 1:
try:
stop_prob = float(parts[1])
if not (0 < stop_prob <= 1):
raise ValueError(
"Вероятность должна быть больше 0 и меньше либо равна 1"
)
except ValueError as e:
print(f"Ошибка: {e}")
continue
random_string = automaton.generate_random_string(stop_prob)
print(f"Сгенерированная строка: {random_string}")
else:
print(f"Неизвестная команда: {cmd}")
if __name__ == "__main__":
main()