Advertisement
stanevplamen

02.09.04.01.BasicLanguage

Jul 12th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class BasicLanguage
  6. {
  7.     static List<int> AddIndexes(string target)
  8.     {
  9.         int index = -1;
  10.         List<int> currentList = new List<int>();
  11.         while (true)
  12.         {
  13.             index = allText.IndexOf(target, index + 1);
  14.             if (index == -1)
  15.             {
  16.                 break;
  17.             }
  18.             else
  19.             {
  20.                 currentList.Add(index);
  21.             }
  22.         }
  23.         return currentList;
  24.     }
  25.  
  26.     static string allText;
  27.  
  28.     static void Main()
  29.     {
  30.         StringBuilder sb = new StringBuilder();
  31.  
  32.         while (true)
  33.         {
  34.             string currentLine = Console.ReadLine();
  35.             int indexOfExit = currentLine.IndexOf("EXIT;");
  36.             if (indexOfExit == -1)
  37.             {
  38.                 sb.Append(currentLine);
  39.                 sb.AppendLine();
  40.             }
  41.             else
  42.             {
  43.                 sb.Append(currentLine);
  44.                 break;
  45.             }
  46.         }
  47.  
  48.         allText = sb.ToString();
  49.         List<int> printList = AddIndexes("PRINT");
  50.         List<int> forList = AddIndexes("FOR");
  51.         List<int> endList = AddIndexes("EXIT;");
  52.  
  53.         //int max = (Math.Max(printList.Count, forList.Count));
  54.         int i = 0;
  55.         int j = 0;
  56.         long printTimes = 1;
  57.         while (true)
  58.         {
  59.             int currentPrintIndex = 200000; // out of the input bonds
  60.             if (i < printList.Count)
  61.             {
  62.                 currentPrintIndex = printList[i];
  63.             }
  64.  
  65.             int currentForIndex = 200000;
  66.             if (j < forList.Count)
  67.             {
  68.                 currentForIndex = forList[j];
  69.             }
  70.  
  71.             if (currentPrintIndex > endList[0] && currentForIndex > endList[0])
  72.             {
  73.                 break;
  74.             }
  75.             else if (currentPrintIndex < currentForIndex)
  76.             {
  77.                 Print(currentPrintIndex, printTimes);
  78.                 i++;
  79.                 printTimes = 1;
  80.                 continue;
  81.             }
  82.             else
  83.             {
  84.                 printTimes = ForLoop(currentForIndex, printTimes);
  85.                 j++;
  86.                 continue;
  87.             }
  88.  
  89.         }
  90.     }
  91.  
  92.     static void Print(int currentIndex, long printTimes)
  93.     {
  94.         int start = currentIndex + 5;
  95.         string textLeft = allText.Substring(start, allText.Length - start);
  96.         int indexOpen = textLeft.IndexOf("(");
  97.         int indexClose = textLeft.IndexOf(")");
  98.  
  99.         int startOutput = currentIndex + 5 + indexOpen + 1;
  100.         int stringLength = indexClose - indexOpen - 1;
  101.  
  102.         string currentOutput = allText.Substring(startOutput, stringLength);
  103.         for (long i = 1; i <= printTimes; i++)
  104.         {
  105.             Console.Write(currentOutput);
  106.         }
  107.     }
  108.  
  109.     static long ForLoop(int currentIndex, long printTimes)
  110.     {
  111.         int start = currentIndex + 3;
  112.         string textLeft = allText.Substring(start, allText.Length - start);
  113.         int indexOpen = textLeft.IndexOf("(");
  114.         int indexClose = textLeft.IndexOf(")");
  115.  
  116.         int startOutput = currentIndex + 3 + indexOpen + 1;
  117.         int stringLength = indexClose - indexOpen - 1;
  118.  
  119.         string currentLoop = allText.Substring(startOutput, stringLength);
  120.  
  121.         int commaIndex = currentLoop.IndexOf(",");
  122.         if (commaIndex == -1)
  123.         {
  124.             int number = int.Parse(currentLoop.Trim());
  125.             printTimes = printTimes * number;
  126.         }
  127.         else
  128.         {
  129.             string a = currentLoop.Substring(0, commaIndex);
  130.             string b = currentLoop.Substring(commaIndex + 1, currentLoop.Length - commaIndex - 1);
  131.             int numberA = int.Parse(a.Trim());
  132.             int numberB = int.Parse(b.Trim());
  133.             int number = numberB - numberA + 1;
  134.             printTimes = printTimes * number;
  135.         }
  136.         return printTimes;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement