Правки по lab2

This commit is contained in:
2025-04-28 22:57:53 +03:00
parent 7030059bbb
commit 3963d304ec
7 changed files with 115 additions and 84 deletions

View File

@@ -27,25 +27,28 @@ class FiniteAutomaton:
for char in input_string:
if char not in self.alphabet:
return f"Символ '{char}' не из алфавита", transitions_path
return f"Символ '{char}' не из алфавита", transitions_path
next_state = self._get_next_state(current_state, char)
if next_state is None:
return "Строка не соответствует", transitions_path
return "Строка не соответствует", transitions_path
transitions_path.append(next_state)
current_state = next_state
return (
"Строка соответствует"
"Строка соответствует"
if current_state in self.final_states
else "Строка не соответствует"
else "Строка не соответствует"
), transitions_path
def generate_random_string(self, stop_probability: float = 0.3) -> str:
def generate_random_string(
self, stop_probability: float = 0.3
) -> tuple[str, list[str]]:
result = []
current_state = self.initial_state
path = [current_state]
while True:
if (
@@ -59,5 +62,6 @@ class FiniteAutomaton:
char = random.choice(transition)
result.append(char)
current_state = next_state
path.append(current_state)
return "".join(result)
return "".join(result), path

View File

@@ -2,20 +2,22 @@ from finite_automaton import FiniteAutomaton
def main():
alphabet = set("+-0123456789.,eE")
alphabet = set("+-0123456789.eE")
initial_state = "S0"
final_states = {"S2", "S4", "S7", "S8", "S9"}
final_states = {"S2", "S3", "S5", "S7", "S10"}
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")],
"S0": [("+-", "S1"), ("123456789", "S2"), ("0", "S3"), (".", "S6")],
"S1": [("123456789", "S2"), ("0", "S3"), (".", "S6")],
"S2": [("0123456789", "S2"), (".", "S5"), ("eE", "S8")],
"S3": [("0", "S3"), ("123456789", "S4"), (".", "S5"), ("eE", "S8")],
"S4": [("0123456789", "S4"), (".", "S5"), ("eE", "S8")],
"S5": [("0123456789", "S5"), ("eE", "S8")],
"S6": [("0123456789", "S7")],
"S7": [("0123456789", "S7"), ("eE", "S8")],
"S8": [("+-", "S9"), ("0123456789", "S10")],
"S9": [("0123456789", "S10")],
"S10": [("0123456789", "S10")],
}
automaton = FiniteAutomaton(
@@ -71,8 +73,9 @@ def main():
print(f"Ошибка: {e}")
continue
random_string = automaton.generate_random_string(stop_prob)
random_string, path = automaton.generate_random_string(stop_prob)
print(f"Сгенерированная строка: {random_string}")
print("Путь переходов:", " -> ".join(path))
else:
print(f"Неизвестная команда: {cmd}")