fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BasicFormatting
  5. {
  6. public enum State {If, For};
  7.  
  8. static class Data{
  9. public static string[] Input = {
  10. "12",
  11. "····",
  12. "VAR I",
  13. "·FOR I=1 TO 31",
  14. "»»»»IF !(I MOD 3) THEN",
  15. "··PRINT \"FIZZ\"",
  16. "··»»ENDIF",
  17. "»»»»····IF !(I MOD 5) THEN",
  18. "»»»»··PRINT \"BUZZ\"",
  19. "··»»»»»»ENDIF",
  20. "»»»»IF (I MOD 3) && (I MOD 5) THEN",
  21. "······PRINT \"FIZZBUZZ\"",
  22. "··»»ENDIF",
  23. "»»»»·NEXT"
  24. };
  25. }
  26.  
  27. class MainClass
  28. {
  29. public static void Main ()
  30. {
  31. var state = new List<State>() ;
  32. char[] spaceChars = { '»', '·' };
  33. var lines = Int32.Parse (Data.Input[0]);
  34. var indent = Data.Input[1];
  35.  
  36. for (int line = 1; line < lines+1; line++) {
  37. var thisLine = Data.Input[line+1].TrimStart(spaceChars);
  38.  
  39. if (thisLine.ToUpper ().StartsWith ("NEXT")) {
  40. if (state.Count>0 && state[state.Count - 1] == State.For)
  41. state.RemoveAt (state.Count - 1);
  42. else{
  43. throw new FormatException (String.Format ("NEXT without FOR (line: {0})", line));
  44. }
  45. }
  46.  
  47.  
  48. if (thisLine.ToUpper ().StartsWith ("ENDIF")) {
  49. if (state.Count>0 && state [state.Count - 1] == State.If)
  50. state.RemoveAt (state.Count - 1);
  51. else {
  52. throw new FormatException (String.Format ("ENDIF without IF (line: {0})", line));
  53. }
  54. }
  55.  
  56. var indentChars = "";
  57. for (int ind = 0; ind < state.Count; ind++) {
  58. indentChars += indent;
  59. }
  60. thisLine = indentChars+thisLine;
  61.  
  62. if(thisLine.ToUpper().TrimStart(spaceChars).StartsWith("IF ")) state.Add (State.If);
  63. if(thisLine.ToUpper().TrimStart(spaceChars).StartsWith("FOR ")) state.Add (State.For);
  64.  
  65. Console.WriteLine(thisLine);
  66. }
  67.  
  68. if(state.Count>0)
  69. {
  70. if(state.Contains (State.If)) Console.WriteLine ("ERROR: Not closed all IF Statements");
  71. if(state.Contains (State.For)) Console.WriteLine ("ERROR: Not closed all FOR Statements");
  72. }
  73. }
  74. }
  75.  
  76. public class FormatException:Exception{
  77. public FormatException(string message):base(message)
  78. {
  79. }
  80. }
  81.  
  82. }
Success #stdin #stdout 0.07s 24136KB
stdin
Standard input is empty
stdout
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