diondokter

Boolean expresion reducer C#

Apr 17th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net.Sockets;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.IO;
  8. using System.ComponentModel;
  9. using System.Reflection;
  10. using System.Xml;
  11. using System.Globalization;
  12.  
  13. namespace Program
  14. {
  15.     public class MainClass
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             Console.WriteLine(BoolExpressionReducer("t&!(f|f|t&t)") == "t");
  20.             Console.ReadLine();
  21.         }
  22.  
  23.         private static string BoolExpressionReducer(string Source)
  24.         {
  25.             while (Source.Contains('('))
  26.             {
  27.                 int StartSubExpression = 0;
  28.                 int EndSubExpression = 0;
  29.                 int DeltaSubs = 0;
  30.  
  31.                 for (int i = 0; i < Source.Length; i++)
  32.                 {
  33.                     if (Source[i] == '(')
  34.                     {
  35.                         DeltaSubs++;
  36.  
  37.                         if (StartSubExpression == 0)
  38.                         {
  39.                             StartSubExpression = i + 1;
  40.                         }
  41.                     }
  42.                     else if (Source[i] == ')')
  43.                     {
  44.                         DeltaSubs--;
  45.  
  46.                         if (DeltaSubs == 0)
  47.                         {
  48.                             EndSubExpression = i - 1;
  49.                             break;
  50.                         }
  51.                     }
  52.                 }
  53.  
  54.                 Source = Source.Replace(Source.Substring(StartSubExpression - 1, EndSubExpression - StartSubExpression + 3), BoolExpressionReducer(Source.Substring(StartSubExpression, EndSubExpression - StartSubExpression + 1)));
  55.             }
  56.  
  57.             while (Source.Contains('!'))
  58.             {
  59.                 Source = Source.Replace("!t", "f").Replace("!f", "t");
  60.             }
  61.  
  62.             while (Source.Count(x => x == '&' || x == '|') > 1)
  63.             {
  64.                 if (Source.Contains('&'))
  65.                 {
  66.                     int AndIndex = Source.IndexOf('&');
  67.                     string AndString = Source.Substring(AndIndex - 1, 3);
  68.  
  69.                     Source = Source.Replace(AndString, BoolExpressionReducer(AndString));
  70.                 }
  71.                 else // Everything is 'or'
  72.                 {
  73.                     return Source.Contains('t') ? "t" : "f";
  74.                 }
  75.             }
  76.  
  77.             if (Source.Length == 1)
  78.             {
  79.                 return Source;
  80.             }
  81.             else if(Source.Length != 3)
  82.             {
  83.                 return string.Empty;
  84.             }
  85.  
  86.             if (Source.Contains('&'))
  87.             {
  88.                 return Source[0] == 't' && Source[2] == 't' ? "t" : "f";
  89.             }
  90.             else // Or
  91.             {
  92.                 return Source[0] == 't' || Source[2] == 't' ? "t" : "f";
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment