Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- namespace Lab2 {
- public partial class Form1 : Form {
- enum AllStates { Recognized = 0, Good = 1, BadSymbol = 2, EmptyCellRecognized = 3, EmptyCellGood = 4 };
- string[] statusString;
- Dictionary<string, Dictionary<string, string>> lang;
- List<string> states;
- List<string> terminal;
- List<string> endStates;
- public Form1() {
- lang = new Dictionary<string, Dictionary<string, string>>();
- states = new List<string>();
- terminal = new List<string>();
- endStates = new List<string>();
- statusString = new string[5] { "Цепочка принадлежит", "Цепочка не пренадлежит", "Неверный Символ", "Нет перехода, цепочка принадлежит", "Нет перехода, цепочка не принадлежит" };
- InitializeComponent();
- textBox_input.ReadOnly = false;
- //dataGridView.RowHeadersWidth = 50;
- }
- private void ShowError(string error) => MessageBox.Show(error, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- private void button_add_row_Click(object sender, EventArgs e) {
- if (dataGridView.ColumnCount == 0) {
- ShowError("Сначала добавьте как минимум один столбец!");
- return;
- }
- string input;
- InputForm inputForm = new InputForm();
- inputForm.label.Text += " состояния";
- if (inputForm.ShowDialog(this) == DialogResult.OK) {
- input = inputForm.textBox_input.Text;
- } else return;
- inputForm.Dispose();
- if (input.Length == 0) {
- ShowError("Пожалуйста, заполните поле!");
- return;
- }
- if (states.Contains(input)) {
- ShowError("Такое состояние уже определено!");
- return;
- } else {
- states.Add(input);
- //if (!((dataGridView.RowCount == 1) && (dataGridView.Rows[0].HeaderCell.Value == string.Empty)))
- dataGridView.Rows.Add();
- dataGridView.Rows[dataGridView.RowCount - 1].HeaderCell.Value = input;
- }
- }
- bool FormDictionary() {
- return true;
- }
- private void button_add_column_Click(object sender, EventArgs e) {
- string input;
- InputForm inputForm = new InputForm();
- inputForm.label.Text += " символа";
- inputForm.textBox_input.MaxLength = 1;
- if (inputForm.ShowDialog(this) == DialogResult.OK) {
- input = inputForm.textBox_input.Text;
- } else return;
- inputForm.Dispose();
- if (input.Length == 0) {
- ShowError("Пожалуйста, заполните поле!");
- return;
- }
- if (terminal.Contains(input)) {
- ShowError("Такой символ уже определено!");
- return;
- } else {
- terminal.Add(input);
- dataGridView.Columns.Add(input, input);
- if (dataGridView.RowCount == 1)
- dataGridView.Rows[dataGridView.RowCount - 1].HeaderCell.Value = string.Empty;
- }
- }
- private void button_make_table_Click(object sender, EventArgs e) {
- if (textBox_states.Text == String.Empty || textBox_terminal.Text == String.Empty)
- return;
- if (dataGridView.Columns.Count > 0) {
- dataGridView.Rows.Clear();
- dataGridView.Columns.Clear();
- dataGridView.Refresh();
- }
- if (terminal.Count > 0)
- terminal.Clear();
- if (states.Count > 0)
- states.Clear();
- terminal = textBox_terminal.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
- if (terminal.Count > 0)
- terminal = terminal.Select(str => str.Trim()).Distinct().ToList();
- if (terminal.Any(x => x.Length != 1)) {
- ShowError("Один из символов алфавита является строкой!");
- return;
- }
- states = textBox_states.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
- states = states.Select(s => s.Trim()).Distinct().ToList();
- if (states.Count == 0 || terminal.Count == 0) {
- ShowError("Поля заполнены неверно!");
- return;
- }
- foreach (string str1 in terminal)
- if (states.Contains(str1)) {
- ShowError("Один из символов алфавита также задан состоянием: " + str1);
- return;
- }
- for (int i = 0; i < terminal.Count(); ++i)
- dataGridView.Columns.Add(terminal[i].ToString(), terminal[i].ToString());
- for (int i = 0; i < states.Count(); ++i)
- dataGridView.Rows.Add();
- for (int i = 0; i < states.Count(); ++i)
- dataGridView.Rows[i].HeaderCell.Value = states[i];
- if (comboBox_start.Items.Count > 0)
- comboBox_start.Items.Clear();
- foreach (string str in states)
- comboBox_start.Items.Add(str);
- comboBox_start.SelectedIndex = 0;
- }
- private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
- if (lang.Count > 0)
- lang.Clear();
- }
- bool CheckTable() {
- for (int i = 0; i < dataGridView.RowCount; ++i)
- for (int j = 0; j < dataGridView.ColumnCount; ++j)
- if (dataGridView.Rows[i].Cells[j].Value.ToString() != string.Empty && !states.Contains(dataGridView.Rows[i].Cells[j].Value.ToString()))
- return false;
- return true;
- }
- bool BuildLanguage() {
- for (int i = 0; i < dataGridView.RowCount; ++i)
- for (int j = 0; j < dataGridView.ColumnCount; ++j)
- if (dataGridView.Rows[i].Cells[j].Value == null)
- dataGridView.Rows[i].Cells[j].Value = string.Empty;
- if (!CheckTable())
- return false;
- if (lang.Count > 0)
- lang.Clear();
- for (int i = 0; i < dataGridView.RowCount; ++i) {
- lang.Add(dataGridView.Rows[i].HeaderCell.Value.ToString(), new Dictionary<string, string>());
- for (int j = 0; j < dataGridView.ColumnCount; ++j) {
- lang[dataGridView.Rows[i].HeaderCell.Value.ToString()].Add(dataGridView.Columns[j].HeaderCell.Value.ToString(), dataGridView.Rows[i].Cells[j].Value.ToString());
- }
- }
- return true;
- }
- void ChangeStatus(AllStates status) {
- label_status.Text = statusString[(int)status];
- }
- private void textBox_input_TextChanged(object sender, EventArgs e) {
- if (lang.Count() == 0)
- if (!BuildLanguage())
- return;
- string lastState = comboBox_start.SelectedItem.ToString();
- if (textBox_input.Text == String.Empty) {
- if (endStates.Contains(lastState))
- ChangeStatus(AllStates.Recognized);
- else
- ChangeStatus(AllStates.Good);
- textBox_states_order.Text = "(" + lastState + ", " + ")";
- return;
- }
- if (textBox_states_order.Text.Length > 0)
- textBox_states_order.Text = string.Empty;
- for (int i = 0; i < textBox_input.Text.Length; ++i) {
- string lastInput = textBox_input.Text[i].ToString();
- if (!terminal.Contains(lastInput)) {
- ChangeStatus(AllStates.BadSymbol);
- textBox_states_order.Text += "(" + lastState + ", " + textBox_input.Text.Substring(i) + ")";
- return;
- }
- textBox_states_order.Text += "(" + lastState + ", " + textBox_input.Text.Substring(i) + ")" + " --> ";
- lastState = lang[lastState][lastInput];
- if (lastState == string.Empty) {
- if (label_status.Text == statusString[(int)AllStates.Good])
- ChangeStatus(AllStates.EmptyCellGood);
- else
- ChangeStatus(AllStates.EmptyCellRecognized);
- return;
- }
- if (endStates.Contains(lastState))
- ChangeStatus(AllStates.Recognized);
- else
- ChangeStatus(AllStates.Good);
- }
- textBox_states_order.Text += "(" + lastState + ", " + ")" ;
- }
- private void textBox_end_TextChanged(object sender, EventArgs e) {
- if (endStates.Count > 0)
- endStates.Clear();
- endStates = textBox_end.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
- endStates = endStates.Select(s => s.Trim()).Distinct().ToList();
- if (endStates.Count == 0) {
- textBox_input.ReadOnly = true;
- return;
- }
- foreach (string str in endStates)
- if (!states.Contains(str)) {
- textBox_input.ReadOnly = true;
- return;
- }
- textBox_input.ReadOnly = false;
- }
- private void comboBox_start_SelectedIndexChanged(object sender, EventArgs e) {
- if (lang.Count > 0)
- lang.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment