private void state8(CharChain chain) { char next = chain.GetNext(); if (next == '0') { state = 11; } else if (next >= '1' && next <= '9') { state = 9; int digit = ((int)next) - ((int)'0'); result = result * 10 + digit; } else if (next == '-') { state = 10; isNegative = true; } else if (next == '_') { state = 12; skip = true; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась цифра."); } }
private void state9(CharChain chain) { char next = chain.GetNext(); if (next >= '0' && next <= '9') { state = 9; int digit = ((int)next) - ((int)'0'); result = result * 10 + digit; } else if (next == '\n' || next == '\0') { state = 13; if (isNegative) { result *= -1; } isNegative = false; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидался перенос строки или конец файла."); } }
private void state5(CharChain chain) { char next = chain.GetNext(); if (next >= '0' && next <= '9') { state = 5; int digit = ((int)next) - ((int)'0'); secondNumber = secondNumber * 10 + digit; } else if (next == '=') { state = 8; if (isNegative) { secondNumber *= -1; } isNegative = false; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалось '='."); } }
private void state4(CharChain chain) { char next = chain.GetNext(); if (next == '0') { state = 7; } else if (next >= '1' && next <= '9') { state = 5; int digit = ((int)next) - ((int)'0'); secondNumber = secondNumber * 10 + digit; } else if (next == '-') { state = 6; isNegative = true; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась цифра."); } }
private void state1(CharChain chain) { char next = chain.GetNext(); if (next >= '0' && next <= '9') { state = 1; int digit = ((int)next) - ((int)'0'); firstNumber = firstNumber * 10 + digit; } else if (isOperation(next)) { state = 4; operation = next; if (isNegative) { firstNumber *= -1; } isNegative = false; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась операция."); } }
private void state12(CharChain chain) { char next = chain.GetNext(); if (next == '\n' || next == '\0') { state = 13; } else { throw new Exception("Обнаружен символ : '" + charView(next) + "', ожидался перенос строки или конец файла."); } }
private void state7(CharChain chain) { char next = chain.GetNext(); if (next == '=') { state = 8; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидался '='."); } }
private void state3(CharChain chain) { char next = chain.GetNext(); if (isOperation(next)) { state = 4; operation = next; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидался знак операции."); } }
private void state10(CharChain chain) { char next = chain.GetNext(); if (next >= '1' && next <= '9') { state = 9; int digit = ((int)next) - ((int)'0'); result = result * 10 + digit; } else { throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась цифра."); } }
private void toolStripButton1_Click(object sender, EventArgs e) { CharChain chain = new CharChain(CodeField.Text); Automat automat = new Automat(); bool result = false; int correct = 0; int wrong = 0; for (int i = 1; i <= 5; i++) { try { result = automat.Check(chain); } catch (Exception ex) { ResultField.Text += "\r\nПример №" + i.ToString() + " " + ex.Message; return; } if (result) { correct++; } else { wrong++; } } char next = chain.GetNext(); if (next != '\0') { ResultField.Text += "\r\nВведено больше 5 примеров."; return; } ResultField.Text += "\r\nВерно: " + correct.ToString() + ", неверно: " + wrong.ToString(); }
private bool tryStop() { char next = chain.Next().Char; if (next == '\0' || next == ';') { chain.GetNext(); state = 13; return(true); } return(false); }