Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- /*http://www.youtube.com/watch?v=XR3nkD6Ikrc
- http://bgcoder.com/Contests/Practice/Index/8#4*/
- namespace _20.BasicLang
- {
- class Program
- {
- static StringBuilder output = new StringBuilder(); //reusable
- static List<string> allCommands = new List<string>();
- static void Main(string[] args)
- {
- ReadInput();
- ConvertInputToCommands();
- RunCommands();
- Console.WriteLine(output.ToString());
- }
- private static void RunCommands()
- {
- output.Clear();
- for (int i = 0; i < allCommands.Count; i++)//get line of commands ended with ; and split by ) to get sub comd
- {
- int allLoops = 1;
- string[] subConmmands = allCommands[i].Split(new char[]{')'},StringSplitOptions.RemoveEmptyEntries);
- foreach (var command in subConmmands)
- {
- string currentCommand = command.TrimStart();
- if (currentCommand.StartsWith("EXIT"))
- {
- //Environment.Exit(0);
- return;
- }
- else if (currentCommand.StartsWith("PRINT"))
- {
- int parmStart = currentCommand.IndexOf("(") + 1;
- string content = currentCommand.Substring(parmStart);
- for (int j = 0; j < allLoops; j++)
- {
- output.Append(content);
- }
- }
- else if (currentCommand.StartsWith("FOR"))
- {
- int parmStart=currentCommand.IndexOf("(")+1;
- string allParams = currentCommand.Substring(parmStart);
- if (allParams.Contains(","))//FOR(a, b)
- {
- string[] loopParams = allParams.Split(',');
- int a = int.Parse(loopParams[0]);
- int b = int.Parse(loopParams[1]);
- allLoops = allLoops * (b - a + 1);
- }
- else //FOR(a)
- {
- int value = int.Parse(allParams);
- allLoops = allLoops * value;
- }
- }
- }
- }
- }
- private static void ConvertInputToCommands()
- {
- string allInput = output.ToString();
- output.Clear();
- foreach (var symbol in allInput)
- {
- output.Append(symbol);
- if (symbol==';')
- {
- allCommands.Add(output.ToString());//find and hold all commands
- output.Clear();
- }
- }
- }
- private static void ReadInput()
- {
- while (true)
- {
- string line = Console.ReadLine();
- output.Append(line);
- if (line.Contains("EXIT;"))
- {
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement