Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- class BasicLanguage
- {
- static List<int> AddIndexes(string target)
- {
- int index = -1;
- List<int> currentList = new List<int>();
- while (true)
- {
- index = allText.IndexOf(target, index + 1);
- if (index == -1)
- {
- break;
- }
- else
- {
- currentList.Add(index);
- }
- }
- return currentList;
- }
- static string allText;
- static void Main()
- {
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- string currentLine = Console.ReadLine();
- int indexOfExit = currentLine.IndexOf("EXIT;");
- if (indexOfExit == -1)
- {
- sb.Append(currentLine);
- sb.AppendLine();
- }
- else
- {
- sb.Append(currentLine);
- break;
- }
- }
- allText = sb.ToString();
- List<int> printList = AddIndexes("PRINT");
- List<int> forList = AddIndexes("FOR");
- List<int> endList = AddIndexes("EXIT;");
- //int max = (Math.Max(printList.Count, forList.Count));
- int i = 0;
- int j = 0;
- long printTimes = 1;
- while (true)
- {
- int currentPrintIndex = 200000; // out of the input bonds
- if (i < printList.Count)
- {
- currentPrintIndex = printList[i];
- }
- int currentForIndex = 200000;
- if (j < forList.Count)
- {
- currentForIndex = forList[j];
- }
- if (currentPrintIndex > endList[0] && currentForIndex > endList[0])
- {
- break;
- }
- else if (currentPrintIndex < currentForIndex)
- {
- Print(currentPrintIndex, printTimes);
- i++;
- printTimes = 1;
- continue;
- }
- else
- {
- printTimes = ForLoop(currentForIndex, printTimes);
- j++;
- continue;
- }
- }
- }
- static void Print(int currentIndex, long printTimes)
- {
- int start = currentIndex + 5;
- string textLeft = allText.Substring(start, allText.Length - start);
- int indexOpen = textLeft.IndexOf("(");
- int indexClose = textLeft.IndexOf(")");
- int startOutput = currentIndex + 5 + indexOpen + 1;
- int stringLength = indexClose - indexOpen - 1;
- string currentOutput = allText.Substring(startOutput, stringLength);
- for (long i = 1; i <= printTimes; i++)
- {
- Console.Write(currentOutput);
- }
- }
- static long ForLoop(int currentIndex, long printTimes)
- {
- int start = currentIndex + 3;
- string textLeft = allText.Substring(start, allText.Length - start);
- int indexOpen = textLeft.IndexOf("(");
- int indexClose = textLeft.IndexOf(")");
- int startOutput = currentIndex + 3 + indexOpen + 1;
- int stringLength = indexClose - indexOpen - 1;
- string currentLoop = allText.Substring(startOutput, stringLength);
- int commaIndex = currentLoop.IndexOf(",");
- if (commaIndex == -1)
- {
- int number = int.Parse(currentLoop.Trim());
- printTimes = printTimes * number;
- }
- else
- {
- string a = currentLoop.Substring(0, commaIndex);
- string b = currentLoop.Substring(commaIndex + 1, currentLoop.Length - commaIndex - 1);
- int numberA = int.Parse(a.Trim());
- int numberB = int.Parse(b.Trim());
- int number = numberB - numberA + 1;
- printTimes = printTimes * number;
- }
- return printTimes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement