Advertisement
Guest User

Untitled

a guest
Sep 10th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.46 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.  
  7. namespace BasicBasic
  8. {
  9.     class Program
  10.     {
  11.         static List<string> text = new List<string>();
  12.         static string[] lines;
  13.         static Dictionary<char, int> nums = new Dictionary<char, int>();
  14.         //static long x = 0;
  15.         //static long v = 0;
  16.         //static long w = 0;
  17.         //static long y = 0;
  18.         //static long z = 0;
  19.         static int i = 0;
  20.         static int j = 0;
  21.         string command = "";
  22.         static char curchar;
  23.         static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  24.         static char[] digit = { '-','0','1','2','3','4','5','6','7','8','9'};
  25.  
  26.         static StringBuilder num = new StringBuilder();
  27.  
  28.         static void Main(string[] args)
  29.         {
  30.             ReadInput();
  31.             DoCode();
  32.         }
  33.  
  34.         static void DoCode()
  35.         {
  36.             for (i = 0; i < lines.Length; i++)
  37.             {
  38.                 for (j = 0; j < lines[i].Length; j++)
  39.                 {
  40.                     curchar = lines[i][j];
  41.                     if (CheckForCommand())
  42.                     {
  43.                         break;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.  
  49.         static bool CheckForCommand()
  50.         {
  51.             if (nums.ContainsKey(curchar))
  52.             {
  53.                 CheckForEqualCommand();
  54.                 return true;
  55.             }
  56.             if (curchar == 'I')
  57.             {
  58.                 if (IfCommand())
  59.                 {
  60.                     THENCommand();
  61.                 }
  62.                 return true;
  63.             }
  64.             if (curchar == 'P')
  65.             {
  66.                 PrintCommand();
  67.                 return true;
  68.             }
  69.             if (curchar == 'S')
  70.             {
  71.                 StopCommand();
  72.                 return true;
  73.             }
  74.             if (curchar == 'C')
  75.             {
  76.                 CLSCommand();
  77.                 return true;
  78.             }
  79.             if (curchar == 'G')
  80.             {
  81.                 GoToCommand();
  82.                 return true;
  83.             }
  84.             return false;
  85.         }
  86.  
  87.         static void GoToCommand()
  88.         {
  89.             for (int g = j; g < lines[i].Length; g++)
  90.             {
  91.                 if (lines[i][g] == 'G' || lines[i][g] == 'O' || lines[i][g] == 'T' || lines[i][g] == 'O')
  92.                 {
  93.                     continue;
  94.                 }
  95.                 num.Clear();
  96.                 while(digits.Contains(lines[i][g]))
  97.                 {
  98.                     num.Append(lines[i][g]);
  99.                     g++;
  100.                     if (g >= lines[i].Length)
  101.                     {
  102.                         g--;
  103.                         break;
  104.                     }
  105.                 }
  106.                 for (int k = 0; k < lines.Length; k++)
  107.                 {
  108.                     bool isTrue = true;
  109.                     int d;
  110.                     for ( d = 0; d < num.Length; d++)
  111.                     {
  112.                         if (lines[k][d] == num[d])
  113.                         {                            
  114.                         }
  115.                         else
  116.                         {
  117.                             isTrue = false;
  118.                             break;
  119.                         }
  120.                     }
  121.                     if (isTrue && !digits.Contains(lines[k][d+1]))
  122.                     {
  123.                         i = k - 1;
  124.                         return;
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.  
  130.         static void THENCommand()
  131.         {
  132.             for (j = j; j < lines[i].Length; j++)
  133.             {
  134.                 curchar = lines[i][j];
  135.  
  136.                 if (curchar == 'T' || curchar == 'H' || curchar == 'E' || curchar == 'N')
  137.                 {
  138.                     continue;
  139.                 }
  140.                 CheckForCommand();
  141.             }
  142.         }
  143.  
  144.         static void CLSCommand()
  145.         {
  146.             Console.Clear();
  147.         }
  148.        
  149.         static void StopCommand()
  150.         {
  151.             Environment.Exit(0);
  152.         }
  153.  
  154.         static void PrintCommand()
  155.         {
  156.             while (lines[i][j] != 'T')
  157.             {
  158.                 j++;
  159.             }
  160.             j++;
  161.             Console.WriteLine(nums[lines[i][j]]);
  162.         }
  163.  
  164.         static bool IfCommand()
  165.         {
  166.             bool isTrue = false;
  167.             for ( j = j ; j < lines[i].Length; j++)
  168.             {
  169.                 char currentChar = lines[i][j];
  170.                 num.Clear();
  171.                 if (currentChar == 'T' && lines[i][j + 1] == 'H')
  172.                 {
  173.                     break;
  174.                 }
  175.                 if (currentChar == 'F')
  176.                 {
  177.                     continue;
  178.                 }
  179.  
  180.                 if (lines[i][j + 1] == '=')
  181.                 {
  182.                     if (nums.ContainsKey(currentChar))
  183.                     {
  184.                         if (nums.ContainsKey(lines[i][j + 2]))
  185.                         {
  186.                             if (nums[currentChar] == nums[lines[i][j + 2]])
  187.                             {
  188.                                 isTrue = true;
  189.                             }
  190.                         }
  191.                         else
  192.                         {
  193.                             while (digit.Contains(lines[i][j+2]))
  194.                             {
  195.                                 num.Append(lines[i][j + 2]);
  196.                                 j++;
  197.                                 if (j + 2 >= lines[i].Length)
  198.                                 {
  199.                                     break;
  200.                                 }
  201.                             }
  202.                             if (nums[currentChar] == int.Parse(num.ToString()))
  203.                             {
  204.                                 isTrue = true;
  205.                             }
  206.                         }
  207.                     }
  208.                 }
  209.  
  210.                 if (lines[i][j + 1] == '<')
  211.                 {
  212.                     if (nums.ContainsKey(currentChar))
  213.                     {
  214.                         if (nums.ContainsKey(lines[i][j + 2]))
  215.                         {
  216.                             if (nums[currentChar] < nums[lines[i][j + 2]])
  217.                             {
  218.                                 isTrue = true;
  219.                             }
  220.                         }
  221.                         else
  222.                         {
  223.                             while (digit.Contains(lines[i][j + 2]))
  224.                             {
  225.                                 num.Append(lines[i][j + 2]);
  226.                                 j++;
  227.                                 if (j + 2 >= lines[i].Length)
  228.                                 {
  229.                                     break;
  230.                                 }
  231.                             }
  232.                             if (nums[currentChar] < int.Parse(num.ToString()))
  233.                             {
  234.                                 isTrue = true;
  235.                             }
  236.                         }
  237.                     }
  238.                 }
  239.  
  240.                 if (lines[i][j + 1] == '>')
  241.                 {
  242.                     if (nums.ContainsKey(currentChar))
  243.                     {
  244.                         if (nums.ContainsKey(lines[i][j + 2]))
  245.                         {
  246.                             if (nums[currentChar] > nums[lines[i][j + 2]])
  247.                             {
  248.                                 isTrue = true;
  249.                             }
  250.                         }
  251.                         else
  252.                         {
  253.                             while (digit.Contains(lines[i][j + 2]))
  254.                             {
  255.                                 num.Append(lines[i][j + 2]);
  256.                                 j++;
  257.                                 if (j + 2 >= lines[i].Length)
  258.                                 {
  259.                                     break;
  260.                                 }
  261.                             }
  262.                             if (nums[currentChar] > int.Parse(num.ToString()))
  263.                             {
  264.                                 isTrue = true;
  265.                             }
  266.                         }
  267.                     }
  268.                 }
  269.             }
  270.             return isTrue;
  271.         }
  272.         static void CheckForEqualCommand()
  273.         {
  274.             j+=2;
  275.             num.Clear();
  276.             int opa = lines[i].Length - 1;
  277.             int sign = 1;
  278.             int firstNum = 0;
  279.             char operation = '+';
  280.  
  281.             if (nums.ContainsKey(lines[i][j]))
  282.             {
  283.                 firstNum = nums[lines[i][j]];
  284.             }
  285.             else
  286.             {
  287.                 if (lines[i][j] == '-')
  288.                 {
  289.                     sign = -1;
  290.                     j++;
  291.                 }
  292.                 while (digits.Contains(lines[i][j]))
  293.                 {
  294.                     num.Append(lines[i][j]);
  295.                     j++;
  296.                     if (j >= lines[i].Length)
  297.                     {
  298.                         break;
  299.                     }
  300.                 }
  301.                 if (j >= lines[i].Length)
  302.                 {
  303.                     nums[curchar] = int.Parse(num.ToString()) * sign;
  304.                     return;
  305.                 }
  306.                 firstNum = int.Parse(num.ToString()) * sign;
  307.             }
  308.             j++;
  309.             int secondNum = 0;
  310.             sign = 1;
  311.             num.Clear();
  312.             if (j>= lines[i].Length)
  313.             {
  314.                 nums[curchar] = firstNum;
  315.                 return;
  316.             }
  317.  
  318.             if (lines[i][j] == '+')
  319.             {
  320.                 j++;
  321.             }
  322.             else if (lines[i][j] == '-')
  323.             {
  324.                 operation = '-';
  325.                 j++;
  326.             }
  327.             if (nums.ContainsKey(lines[i][j]))
  328.             {
  329.                 secondNum = nums[lines[i][j]];
  330.             }
  331.             else
  332.             {
  333.                 if (lines[i][j] == '-')
  334.                 {
  335.                     sign = -1;
  336.                     j++;
  337.                 }
  338.                 while (digits.Contains(lines[i][j]))
  339.                 {
  340.                     num.Append(lines[i][j]);
  341.                     j++;
  342.                     if (j >= lines[i].Length)
  343.                     {
  344.                         break;
  345.                     }
  346.                 }
  347.                 secondNum = int.Parse(num.ToString()) * sign;
  348.             }
  349.             if (operation == '+')
  350.             {
  351.                 nums[curchar] = firstNum + secondNum;
  352.             }
  353.             else
  354.             {
  355.                 nums[curchar] = firstNum - secondNum;
  356.             }
  357.             //if (digits.Contains(lines[i][j]) && j == lines[i].Length - 1)
  358.             //{
  359.             //    nums[curchar] = int.Parse(lines[i][j].ToString());
  360.             //}
  361.             //else if (nums.ContainsKey(lines[i][j]))
  362.             //{
  363.             //    nums[curchar] = nums[lines[i][j]];
  364.             //}
  365.             //else if (lines[i][j] == '-' && digits.Contains(int.Parse(lines[i][j + 1].ToString())) && j + 1 == lines[i].Length - 1)
  366.             //{
  367.             //    nums[curchar] = int.Parse(lines[i][j + 1].ToString()) * -1;
  368.             //}
  369.             //else if (nums.ContainsKey(lines[i][j]) && lines[i][j+ 1] == '+' && nums.ContainsKey(lines[i][j+2]))
  370.             //{
  371.             //    //nums[curchar] =
  372.             //}
  373.         }
  374.  
  375.         static void ReadInput()
  376.         {
  377.             int counter = 0;
  378.             text.Add(Console.ReadLine());
  379.             while (text[counter] != "RUN")
  380.             {
  381.                 text.Add(Console.ReadLine());
  382.                 counter++;
  383.             }
  384.  
  385.             StringBuilder removedSpaces = new StringBuilder();
  386.             for (int g = 0; g < text.Count; g++)
  387.             {
  388.                 StringBuilder sb = new StringBuilder(text[g]);
  389.  
  390.                 for (int b = 0; b < sb.Length; b++)
  391.                 {
  392.                     if (sb[b] != ' ')
  393.                     {
  394.                         removedSpaces.Append(sb[b]);
  395.                     }
  396.                 }
  397.                 removedSpaces.AppendLine();        
  398.             }
  399.  
  400.             lines = removedSpaces.ToString().Split(new string[] {"\r\n"},StringSplitOptions.RemoveEmptyEntries);
  401.  
  402.             nums.Add('X', 0);
  403.             nums.Add('Y', 0);
  404.             nums.Add('V', 0);
  405.             nums.Add('W', 0);
  406.             nums.Add('Z', 0);
  407.         }
  408.     }
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement