Advertisement
Ludmil

Basic Language -Lecture

Feb 28th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /*http://www.youtube.com/watch?v=XR3nkD6Ikrc
  7.  http://bgcoder.com/Contests/Practice/Index/8#4*/
  8. namespace _20.BasicLang
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static StringBuilder output = new StringBuilder(); //reusable
  14.         static List<string> allCommands = new List<string>();
  15.         static void Main(string[] args)
  16.         {
  17.             ReadInput();
  18.             ConvertInputToCommands();
  19.             RunCommands();
  20.             Console.WriteLine(output.ToString());
  21.         }
  22.  
  23.         private static void RunCommands()
  24.         {
  25.             output.Clear();
  26.             for (int i = 0; i < allCommands.Count; i++)//get line of commands ended with ; and split by ) to get sub comd
  27.             {
  28.                 int allLoops = 1;
  29.                 string[] subConmmands = allCommands[i].Split(new char[]{')'},StringSplitOptions.RemoveEmptyEntries);
  30.                 foreach (var command in subConmmands)
  31.                 {
  32.                     string currentCommand = command.TrimStart();
  33.                     if (currentCommand.StartsWith("EXIT"))
  34.                     {
  35.                         //Environment.Exit(0);
  36.                         return;
  37.                     }
  38.                     else if (currentCommand.StartsWith("PRINT"))
  39.                     {
  40.                         int parmStart = currentCommand.IndexOf("(") + 1;
  41.                         string content = currentCommand.Substring(parmStart);
  42.  
  43.                         for (int j = 0; j < allLoops; j++)
  44.                         {
  45.                             output.Append(content);
  46.                         }
  47.                        
  48.                     }
  49.                     else if (currentCommand.StartsWith("FOR"))
  50.                     {
  51.                         int parmStart=currentCommand.IndexOf("(")+1;
  52.                         string allParams = currentCommand.Substring(parmStart);
  53.                         if (allParams.Contains(","))//FOR(a, b)
  54.                         {
  55.                             string[] loopParams = allParams.Split(',');
  56.                             int a = int.Parse(loopParams[0]);
  57.                             int b = int.Parse(loopParams[1]);
  58.  
  59.                             allLoops = allLoops * (b - a + 1);
  60.                         }
  61.                         else //FOR(a)
  62.                         {
  63.                             int value = int.Parse(allParams);
  64.                             allLoops = allLoops * value;
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         private static void ConvertInputToCommands()
  72.         {
  73.             string allInput = output.ToString();
  74.             output.Clear();
  75.             foreach (var symbol in allInput)
  76.             {
  77.                 output.Append(symbol);
  78.                 if (symbol==';')
  79.                 {
  80.                     allCommands.Add(output.ToString());//find and hold all commands
  81.                     output.Clear();
  82.                 }
  83.             }
  84.         }
  85.  
  86.         private static void ReadInput()
  87.         {
  88.             while (true)
  89.             {
  90.                 string line = Console.ReadLine();
  91.                 output.Append(line);
  92.                 if (line.Contains("EXIT;"))
  93.                 {
  94.                     break;
  95.                 }
  96.             }
  97.  
  98.  
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement