Advertisement
Guest User

RobotCommunication

a guest
Dec 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _02.RobotCommunication
  7. {
  8.    public class Program
  9.     {
  10.      public   static void Main(string[] args)
  11.         {
  12.             string line = Console.ReadLine();
  13.  
  14.             string pattern = @"[,|_]{1}([\d|\w]+)";
  15.  
  16.             List<string> data = new List<string>();
  17.  
  18.             Regex regex = new Regex(pattern);
  19.  
  20.  
  21.  
  22.             FindData(line, data);
  23.             NewMethod(data, regex);
  24.  
  25.             while (true)
  26.             {
  27.                 line = Console.ReadLine();
  28.  
  29.                 if (line == "Report")
  30.                 {
  31.                     break;
  32.                 }
  33.  
  34.                 data.Clear();
  35.  
  36.                 FindData(line, data);
  37.  
  38.                 NewMethod(data, regex);
  39.  
  40.             }
  41.  
  42.         }
  43.  
  44.         private static void NewMethod(List<string> data, Regex regex)
  45.         {
  46.             StringBuilder stringBuilder = new StringBuilder();
  47.  
  48.             for (int i = 0; i < data.Count; i++)
  49.             {
  50.                 stringBuilder.AppendLine(data[i]);
  51.  
  52.             }
  53.  
  54.             string something = stringBuilder.ToString();
  55.  
  56.  
  57.             var matchColection = regex.Matches(something);
  58.  
  59.             for (int i = 0; i < matchColection.Count; i++)
  60.             {
  61.                 string decoded = string.Empty;
  62.                 int sum = 0;
  63.  
  64.                 string stoinost = matchColection[i].Value;
  65.  
  66.                 char digid = stoinost[0];
  67.                 for (int j = 0; j < stoinost.Length; j++)
  68.                 {
  69.                     int num;
  70.  
  71.                     bool isNum = int.TryParse(stoinost[j].ToString(), out num);
  72.  
  73.                     if (isNum)
  74.                     {
  75.                         if (digid == ',')
  76.                         {
  77.                             sum += num;
  78.                         }
  79.                         else if(digid=='_')
  80.                         {
  81.                             sum -= num;
  82.                         }
  83.                     }
  84.  
  85.                 }
  86.  
  87.                 for (int j = 1; j < stoinost.Length; j++)
  88.                 {
  89.                     int num;
  90.  
  91.                     bool isNum = int.TryParse(stoinost[j].ToString(), out num);
  92.  
  93.                     if (!isNum)
  94.                     {
  95.                         int code = stoinost[j];
  96.  
  97.                        
  98.                             code += sum;
  99.  
  100.                        
  101.                        
  102.                      
  103.  
  104.                         char s = Convert.ToChar(code);
  105.  
  106.                         decoded += s;
  107.  
  108.  
  109.  
  110.                     }
  111.                     else
  112.                     {
  113.                         break;
  114.                     }
  115.  
  116.                 }
  117.  
  118.                 Console.Write(decoded + " ");
  119.  
  120.             }
  121.             Console.WriteLine();
  122.         }
  123.  
  124.         private static void FindData(string line, List<string> data)
  125.         {
  126.             for (int i = 0; i < line.Length; i++)
  127.             {
  128.  
  129.                 if (line[i] == '_' || line[i] == ',')
  130.                 {
  131.                     int startIndex = i;
  132.                     int startLine = startIndex + 1;
  133.  
  134.                     while (true)
  135.                     {
  136.                         if (line[startLine] == '_' || line[startLine] == ',' || startLine + 1 == line.Length)
  137.                         {
  138.                             if (startLine + 1 == line.Length)
  139.                             {
  140.                                 data.Add(line.Substring(startIndex));
  141.                             }
  142.                             else
  143.                             {
  144.                                 data.Add(line.Substring(startIndex, startLine - startIndex));
  145.                             }
  146.                             break;
  147.                         }
  148.                         startLine++;
  149.                         if (startLine == line.Length)
  150.                         {
  151.  
  152.                             break;
  153.                         }
  154.  
  155.                     }
  156.  
  157.                 }
  158.  
  159.             }
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement