Advertisement
Guest User

TastyPasta

a guest
Dec 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.11 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 Perebor
  8. {
  9.    public class Program
  10.     {
  11.         public static string expression = "!X*X +X1 + NOT X AND X OR X && !X || NOT X AND NOT NOT X";
  12.         public static Node Eparser(ref string expression)
  13.         {
  14.             expression = expression.Replace("AND", "*").Replace("OR", "+").Replace("NOT", "!");
  15.             expression = expression.Replace(@"/\", "*").Replace(@"\/", "+").Replace("-", "!");
  16.             expression = expression.Replace("^", "*").Replace("V", "+");
  17.             expression = expression.Replace("&&", "*").Replace("||", "+");
  18.             expression = expression.Replace("&", "*");
  19.             expression = expression.Replace(" ", "");
  20.             Node t = new Node();
  21.             if (expression != "")
  22.                 t = E(ref expression);
  23.             return t;
  24.         }
  25.         public static Node E(ref string expression)
  26.         {
  27.             Node t = new Node();
  28.             t = T(ref expression);
  29.             if (expression != "")
  30.                 while (expression != "" && (expression[0] == '+'))
  31.                 {
  32.                     char op = expression[0];
  33.                     expression = expression.Remove(0, 1);
  34.                     Node t1 = T(ref expression);
  35.                     Node MkNode = new Node(op, null, null, t, t1);
  36.                     t = MkNode;
  37.                 }
  38.             return t;
  39.         }
  40.         public static Node T(ref string expression)
  41.         {
  42.             Node t = new Node();
  43.             t = P(ref expression);
  44.             if (expression != "")
  45.                 while (expression != "" && expression[0] == '*')
  46.                 {
  47.                     char op = expression[0];
  48.                     expression = expression.Remove(0, 1);
  49.                     Node t1 = P(ref expression);
  50.                     Node MkNode = new Node(op, null, null, t, t1);
  51.                     t = MkNode;
  52.                 }
  53.             return t;
  54.         }
  55.  
  56.         public static List<string> varNames = new List<string>();
  57.  
  58.         public static Node P(ref string expression)
  59.         {
  60.             string name = string.Empty;
  61.             Node t = new Node();
  62.  
  63.                 if (expression[0]==' ')
  64.                 {
  65.                     expression = expression.Remove(0, 1);
  66.                 }
  67.                 if (expression != "" && expression[0] != ' ' && expression[0] != '+'
  68.                         && expression[0] != '*' && expression[0] != '!' && expression[0] != '(' && expression[0] != ')')
  69.                 {
  70.                     name += expression[0];
  71.                     expression = expression.Remove(0, 1);
  72.                     while (expression != "" && expression[0] != ' ' && expression[0] != '+'
  73.                         && expression[0] != '*' && expression[0] != '!' && expression[0] != '(' && expression[0] != ')')
  74.                     {
  75.                         name += expression[0];
  76.                         expression = expression.Remove(0, 1);
  77.                     }
  78.                     if (!varNames.Contains(name))
  79.                         varNames.Add(name);
  80.                     Node leaf = new Node(null, name, false, null, null);
  81.                     return leaf;
  82.                 }
  83.                 else if (expression[0] == '(')
  84.                 {
  85.                     expression = expression.Remove(0, 1);
  86.                     t = E(ref expression);
  87.                     if (expression[0] == ')')
  88.                         expression = expression.Remove(0, 1);
  89.                     return t;
  90.                 }
  91.                 else if (expression[0] == '!')
  92.                 {
  93.                     expression = expression.Remove(0, 1);
  94.                     t = P(ref expression);
  95.                     Node not = new Node('!', null, null, t, null);
  96.                     return not;
  97.                 }
  98.                 else throw new ArgumentOutOfRangeException("Something wrong happened");
  99.            
  100.  
  101.         }
  102.         static void Main(string[] args)
  103.         {
  104.             Console.WriteLine(Node.Perebirator(Eparser(ref expression)));
  105.         }
  106.         public class Node
  107.         {
  108.             char? operation;
  109.             bool? value;
  110.             string name;
  111.             Node LeftChild;
  112.             Node RightChild;
  113.             public Node() { }
  114.             public Node(char? inputOper, string inputName, bool? inputVal, Node inputLftChld, Node inputRghtChld)
  115.             {
  116.                 operation = inputOper;
  117.                 name = inputName;
  118.                 value = inputVal;
  119.                 LeftChild = inputLftChld;
  120.                 RightChild = inputRghtChld;
  121.             }
  122.             public static Node Setter(Node node, string varName, bool value)
  123.             {
  124.                 if (node.name == varName)
  125.                     node.value = value;
  126.                 if (node.LeftChild != null)
  127.                     Setter(node.LeftChild, varName, value);
  128.                 if (node.RightChild != null)
  129.                     Setter(node.RightChild, varName, value);
  130.                 return node;
  131.             }
  132.             public static bool Evaluate(Node node)
  133.             {
  134.                 if (node.value != null)
  135.                     return (bool)node.value;
  136.                 else
  137.                 if (node.operation == '+')
  138.                     return Evaluate(node.LeftChild) || Evaluate(node.RightChild);
  139.                 else if (node.operation == '*')
  140.                     return Evaluate(node.LeftChild) && Evaluate(node.RightChild);
  141.                 else if (node.operation == '!')
  142.                     return !(Evaluate(node.LeftChild));
  143.                 else throw new Exception();
  144.             }
  145.  
  146.             public static bool Perebirator(Node node, int count = 0)
  147.             {
  148.                 if (count<varNames.Count)
  149.                 {
  150.                    return Perebirator(Setter(node, varNames[count], true), count+1)
  151.                         ||
  152.                    Perebirator(Setter(node, varNames[count], false), count+1);
  153.                 }
  154.                 return Evaluate(node)==true;
  155.  
  156.             }
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement