Advertisement
Guest User

IsFormula

a guest
Oct 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         public static readonly List<char> Variables = new List<char> { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', /*'V'*/ 'W', 'X', 'Y', 'Z' };
  11.         public static readonly List<string> StringOperators = new List<string> {"V", "v", "&", "->", "<->", "|", "+"};
  12.         public static readonly List<char> Operators = new List<char> {'V', 'v', '&', '2',   '3',  '|', '+'};
  13.         public static readonly char Denial = '~';
  14.  
  15.         class Stack
  16.         {
  17.             public List<char> stack = new List<char>();
  18.  
  19.             public void PushBack(char item)
  20.             {
  21.                 stack.Add(item);
  22.             }
  23.  
  24.             public void PopBack()
  25.             {
  26.                 stack.RemoveAt(stack.Count - 1);
  27.             }
  28.         }
  29.  
  30.         static string DeleteSpaces(string input)
  31.         {
  32.             var result = new StringBuilder(input);
  33.             int i = 0;
  34.             while (i < result.Length)
  35.             {
  36.                 if (result[i] == ' ')
  37.                 {
  38.                     result = result.Remove(i, 1);
  39.                 }
  40.                 else
  41.                     i++;
  42.             }
  43.             return result.ToString();
  44.         }
  45.  
  46.         static string DeleteBrackets(string input)
  47.         {
  48.             var result = new StringBuilder(input);
  49.             int i = 0;
  50.             while (i < result.Length)
  51.             {
  52.                 if (result[i] == '(')
  53.                 {
  54.                         if (Operators.Contains(result[i + 1]) || result[i + 1] == Denial)
  55.                             return null;
  56.  
  57.                     result.Remove(i, 1);
  58.                     continue;
  59.                 }
  60.  
  61.                 if (result[i] == ')')
  62.                 {
  63.                         if (Operators.Contains(result[i - 1]) || result[i - 1] == Denial)
  64.                             return null;
  65.  
  66.                     result.Remove(i, 1);
  67.                     continue;
  68.                 }
  69.  
  70.                 i++;
  71.             }
  72.             return result.ToString();
  73.         }
  74.  
  75.         static bool CheckBracketBalance(string input)
  76.         {
  77.             var bracketBalance = new Stack();
  78.             for (int element = 0; element < input.Length; element++)
  79.             {
  80.                 if (input[element] == '(')
  81.                     bracketBalance.PushBack('(');
  82.  
  83.                 if (input[element] == ')')
  84.                 {
  85.                     if (bracketBalance.stack.Count != 0)
  86.                         bracketBalance.PopBack();
  87.                     else
  88.                         return false;
  89.                 }
  90.             }
  91.  
  92.             if (bracketBalance.stack.Count == 0)
  93.                 return true;
  94.             else
  95.                 return false;
  96.         }
  97.  
  98.         static bool IsVariable(char item)
  99.         {
  100.             return Variables.Contains(item);
  101.         }
  102.  
  103.         static bool IsOperator(char item)
  104.         {
  105.             return Operators.Contains(item);
  106.  
  107.         }
  108.  
  109.         static string AbsorbeDenials(string input)
  110.         {
  111.             var result = new StringBuilder(input);
  112.             int i = 0;
  113.             while (i < result.Length)
  114.             {
  115.                 if (result[i] == Denial)
  116.                 {
  117.                     if (i < result.Length - 1)
  118.                     {
  119.                         if (IsVariable(result[i + 1]) || result[i + 1] == '(')
  120.                             result = result.Remove(i, 1);
  121.                         else
  122.                             return null;
  123.                     }
  124.                     else
  125.                         return null;
  126.                 }
  127.                 else
  128.                     i++;
  129.             }
  130.             return result.ToString();
  131.         }
  132.  
  133.         static bool CheckStriping(string input)
  134.         {
  135.             var striping = new List<string>();
  136.  
  137.             for (int i = 0; i < input.Length; i++)
  138.             {
  139.                 if (IsVariable(input[i]))
  140.                     striping.Add("variable");
  141.                 else if (IsOperator(input[i]))
  142.                     striping.Add("operator");
  143.                 else
  144.                     return false;
  145.             }
  146.  
  147.             if (striping[0] != "variable" || striping[striping.Count - 1] != "variable")
  148.                 return false;
  149.  
  150.             for (int i = 0; i < striping.Count - 1; i++)
  151.             {
  152.                 if (striping[i] == striping[i + 1])
  153.                     return false;
  154.             }
  155.  
  156.             return true;
  157.         }
  158.  
  159.         static bool IsFormula(string inputString)
  160.         {
  161.             inputString = FormatString(inputString);
  162.  
  163.             if (!CheckBracketBalance(inputString))
  164.                 return false;
  165.  
  166.             inputString = DeleteBrackets(inputString);
  167.  
  168.             if (inputString == null)
  169.                 return false;
  170.  
  171.             inputString = AbsorbeDenials(inputString);
  172.  
  173.             if (inputString == null)
  174.                 return false;
  175.  
  176.             if (CheckStriping(inputString))
  177.                 return true;
  178.             else
  179.                 return false;
  180.         }
  181.  
  182.         static string FormatString(string input)
  183.         {
  184.             input = DeleteSpaces(input);
  185.             input = input.Replace("<->", "3");
  186.             input = input.Replace("->", "2");
  187.             return input;
  188.         }
  189.  
  190.         static void Main(string[] args)
  191.         {
  192.             var inputString = Console.ReadLine();
  193.             if (IsFormula(inputString))
  194.                 Console.WriteLine("Формула");
  195.             else
  196.                 Console.WriteLine("Не формула");
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement