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 StringBuilder result = new StringBuilder();
- static void Main()
- {
- bool inBrackets = false;
- StringBuilder buffer = new StringBuilder();
- while (true)
- {
- int nextSymbol = Console.Read();
- if (nextSymbol == -1)
- {
- break;
- }
- char nextChar = (char)nextSymbol;
- if (nextChar =='(')
- {
- inBrackets = true;
- }
- if (nextChar == ')')
- {
- inBrackets = false;
- }
- if (nextChar == ';' && inBrackets == false)
- {
- string statement = buffer.ToString();
- if (ProcessConstruct(statement))
- {
- break;
- }
- buffer.Remove(0, buffer.Length); //buffer clear in .net 4.5
- }
- else
- {
- if (inBrackets)
- {
- buffer.Append(nextChar);
- }
- else if (!char.IsWhiteSpace(nextChar))
- {
- buffer.Append(nextChar);
- }
- }
- }
- Console.WriteLine(result.ToString());
- }
- private static bool ProcessConstruct(string statement)
- {
- string[] commands = statement.Split(')');
- long count = 1;
- for (int i = 0; i < commands.Length; i++)
- {
- string cmd = commands[i];
- cmd = cmd.TrimStart();
- if (cmd.StartsWith("EXIT"))
- {
- return true;
- }
- else if (cmd.StartsWith("PRINT"))
- {
- int start = cmd.IndexOf('(') + 1;
- string content = cmd.Substring(start);
- if (content.Length > 0 && count>0)
- {
- for (int j = 0; j < count; j++)
- {
- result.Append(content);
- }
- }
- }
- else if (cmd.StartsWith("FOR"))
- {
- int startIndex = cmd.IndexOf('(') + 1;
- int commaIndex = cmd.IndexOf(',');
- if (commaIndex == -1)
- {
- string forCountStr = cmd.Substring(startIndex);
- int forCount = int.Parse(forCountStr);
- count *= forCount;
- }
- else
- {
- string forStartCountStr = cmd.Substring(startIndex, commaIndex - startIndex);
- int forStartCount = int.Parse(forStartCountStr);
- string forEndCountStr = cmd.Substring(commaIndex + 1);
- int forEndCount = int.Parse(forEndCountStr);
- int forCount = forEndCount - forStartCount + 1;
- count *= forCount;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement