Created
November 14, 2011 17:56
-
-
Save vvondra/1364603 to your computer and use it in GitHub Desktop.
automat.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static private bool IsValidFormula(string formula) { | |
// Ja bych byl pouzil Regexpy a mel to na tri radky, jenze to neprojde na cas, takze automat :/// | |
ValidatorState state = ValidatorState.START; | |
if (formula[0] != '=') | |
{ | |
return false; | |
} | |
for (int i = 1; i < formula.Length; i++) | |
{ | |
if (Char.IsUpper(formula[i])) | |
{ | |
if (state == ValidatorState.START || state == ValidatorState.FIRSTCOORDCOL) | |
{ | |
state = ValidatorState.FIRSTCOORDCOL; | |
} | |
else if (state == ValidatorState.OPERATOR) | |
{ | |
state = ValidatorState.SECONDCOORDCOL; | |
} | |
else if (state != ValidatorState.SECONDCOORDCOL) | |
{ | |
return false; | |
} | |
} | |
else if (Char.IsDigit(formula[i])) | |
{ | |
if (state == ValidatorState.OPERATOR || state == ValidatorState.START) | |
{ | |
return false; | |
} | |
else if (state == ValidatorState.FIRSTCOORDCOL) | |
{ | |
state = ValidatorState.FIRSTCOORDROW; | |
} | |
else if (state == ValidatorState.SECONDCOORDCOL) | |
{ | |
state = ValidatorState.SECONDCOORDROW; | |
} | |
} | |
else if (formula[i] == '+' || formula[i] == '-' || formula[i] == '*' || formula[i] == '/') | |
{ | |
if (state == ValidatorState.FIRSTCOORDROW) | |
{ | |
state = ValidatorState.OPERATOR; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
if (state != ValidatorState.SECONDCOORDROW) | |
{ | |
return false; | |
} | |
return true; | |
} | |
enum ValidatorState | |
{ | |
START, | |
FIRSTCOORDCOL, | |
FIRSTCOORDROW, | |
OPERATOR, | |
SECONDCOORDCOL, | |
SECONDCOORDROW, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment