using System; using System.Collections.Generic; namespace BasicFormatting { public enum State {If, For}; static class Data{ public static string[] Input = { "12", "····", "VAR I", "·FOR I=1 TO 31", "»»»»IF !(I MOD 3) THEN", "··PRINT \"FIZZ\"", "··»»ENDIF", "»»»»····IF !(I MOD 5) THEN", "»»»»··PRINT \"BUZZ\"", "··»»»»»»ENDIF", "»»»»IF (I MOD 3) && (I MOD 5) THEN", "······PRINT \"FIZZBUZZ\"", "··»»ENDIF", "»»»»·NEXT" }; } class MainClass { public static void Main () { var state = new List() ; char[] spaceChars = { '»', '·' }; var lines = Int32.Parse (Data.Input[0]); var indent = Data.Input[1]; for (int line = 1; line < lines+1; line++) { var thisLine = Data.Input[line+1].TrimStart(spaceChars); if (thisLine.ToUpper ().StartsWith ("NEXT")) { if (state.Count>0 && state[state.Count - 1] == State.For) state.RemoveAt (state.Count - 1); else{ throw new FormatException (String.Format ("NEXT without FOR (line: {0})", line)); } } if (thisLine.ToUpper ().StartsWith ("ENDIF")) { if (state.Count>0 && state [state.Count - 1] == State.If) state.RemoveAt (state.Count - 1); else { throw new FormatException (String.Format ("ENDIF without IF (line: {0})", line)); } } var indentChars = ""; for (int ind = 0; ind < state.Count; ind++) { indentChars += indent; } thisLine = indentChars+thisLine; if(thisLine.ToUpper().TrimStart(spaceChars).StartsWith("IF ")) state.Add (State.If); if(thisLine.ToUpper().TrimStart(spaceChars).StartsWith("FOR ")) state.Add (State.For); Console.WriteLine(thisLine); } if(state.Count>0) { if(state.Contains (State.If)) Console.WriteLine ("ERROR: Not closed all IF Statements"); if(state.Contains (State.For)) Console.WriteLine ("ERROR: Not closed all FOR Statements"); } } } public class FormatException:Exception{ public FormatException(string message):base(message) { } } }