Advertisement
illlitr8

Untitled

Nov 3rd, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.73 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 TruthTables
  8. {
  9.     public class node
  10.     {
  11.         public node left;
  12.         public node right;
  13.         public node super;
  14.         public string body;
  15.  
  16.         public node(node super, string body)
  17.         {
  18.             this.super = super;
  19.             this.body = body;
  20.         }
  21.  
  22.         public bool isexpression()
  23.         {
  24.             return Program.kw.Contains(body);
  25.         }
  26.     }
  27.  
  28.     class Program
  29.     {
  30.         public static bool evaluate(node n, bool A = false, bool B = false, bool C = false, bool D = false)
  31.         {
  32.             if (!n.isexpression())
  33.             {
  34.                 bool not = n.body.Contains("!");
  35.                 n.body = n.body.TrimStart('!');
  36.                 bool value = n.body == "A" ? A : n.body == "B" ? B : n.body == "C" ? C : D;
  37.                 if (not) { n.body = "!" + n.body; return !value; }
  38.                 return value;
  39.             }
  40.             else
  41.             {
  42.                 bool L = evaluate(n.left,A,B,C,D);
  43.                 bool R = evaluate(n.right,A,B,C,D);
  44.                
  45.                 switch (n.body)
  46.                 {
  47.                     case "and":
  48.                         return L && R;
  49.                     case "or":
  50.                         return L || R;
  51.                     case "nor":
  52.                         return !L && !R;
  53.                     case "nand":
  54.                         return !L || !R;
  55.                 }
  56.             }
  57.             return false;
  58.         }
  59.  
  60.         public static string[] kw = new string[] { "and", "or", "nor", "nand" };
  61.  
  62.         public static void run()
  63.         {
  64.             Console.Write("> ");
  65.             string input = Console.ReadLine();
  66.  
  67.             List<string> words = new List<string>();
  68.             int len = input.Split(' ').ToList().Count;
  69.  
  70.             for (int i = 0; i < len; i++)
  71.             {
  72.                 List<string> inp = input.Split(' ').ToList();
  73.                 if (inp[i] == "not")
  74.                 {
  75.                     words.Add("!" + inp[i + 1]);
  76.                     len--;
  77.                 }
  78.                 else
  79.                 {
  80.                     words.Add(inp[i]);
  81.                 }
  82.             }
  83.             input = "";
  84.             words.ForEach((x) => input += x + " ");
  85.             input = input.Trim();
  86.  
  87.             node root = new node(null, input);
  88.             node current;
  89.  
  90.             List<node> _nodes = new List<node>();
  91.             List<node> nodes = new List<node>();
  92.             _nodes.Add(root); nodes.Add(root);
  93.  
  94.             while (true)
  95.             {
  96.                 current = _nodes[0];
  97.                 _nodes.Remove(current);
  98.  
  99.                 bool done = false;
  100.  
  101.                 List<string> spl = new List<string>();
  102.  
  103.                 foreach (string w in (spl = current.body.Split(' ').ToList()))
  104.                 {
  105.                     if (!done)
  106.                     {
  107.                         if (kw.Contains(w))
  108.                         {
  109.                             string bodyL = ""; for (int i = 0; i < spl.IndexOf(w); i++) { bodyL += spl[i]; }
  110.                             var asffa = spl.IndexOf(w);
  111.                             string bodyR = ""; for (int i = spl.IndexOf(w) + 1; i < spl.Count; i++) { bodyR += spl[i] + " "; }
  112.                             bodyR = bodyR.Trim(' ');
  113.                             current.body = w;
  114.                             current.left = new node(current, bodyL); /*nodes.Add(current.left);*/ nodes.Add(current.left);
  115.                             current.right = new node(current, bodyR); _nodes.Add(current.right); nodes.Add(current.right);
  116.                             done = true;
  117.                         }
  118.                     }
  119.                 }
  120.                 if (_nodes.Count <= 0) break;
  121.             }
  122.  
  123.             foreach (node n in nodes)
  124.             {
  125.                 if (n.body == "nor")
  126.                 {
  127.                     if (n.left.body.Contains('!')) n.left.body.Substring(1, n.left.body.Length - 1);
  128.                     else { n.left.body = "!" + n.left.body; }
  129.                     if (n.right.body.Contains('!')) n.right.body.Substring(1, n.right.body.Length - 1);
  130.                     else { n.right.body = "!" + n.right.body; }
  131.                     n.body = "and";
  132.                 }
  133.                 if (n.body == "nand")
  134.                 {
  135.                     if (n.left.body.Contains('!')) n.left.body.Substring(1, n.left.body.Length - 1);
  136.                     else { n.left.body = "!" + n.left.body; }
  137.                     if (n.right.body.Contains('!')) n.right.body.Substring(1, n.right.body.Length - 1);
  138.                     else { n.right.body = "!" + n.right.body; }
  139.                     n.body = "or";
  140.                 }
  141.             }
  142.  
  143.             List<node> nodesCopy = new List<node>();
  144.             nodes.ForEach((x) => nodesCopy.Add(x));
  145.  
  146.             bool hit = false;
  147.  
  148.             while (true)
  149.             {
  150.                 hit = false;
  151.                 foreach (node n in nodesCopy)
  152.                 {
  153.                     if (n.isexpression())
  154.                     {
  155.                         if (n.super != null)
  156.                         {
  157.                             if (kw.ToList().IndexOf(n.body) > kw.ToList().IndexOf(n.super.body))
  158.                             {
  159.                                 hit = true;
  160.  
  161.                                 node subL = n.left;
  162.                                 node subR = n.right;
  163.                                 int supIndex = nodes.IndexOf(n.super);
  164.                                 int subIndex = nodes.IndexOf(n);
  165.  
  166.                                 n.left = n.super;
  167.                                 n.super.right = subL;
  168.                                 node supsuper = n.super.super;
  169.                                 n.super.super = n;
  170.                                 n.super = supsuper;
  171.                                 nodes[supIndex] = n;
  172.                                 nodes[subIndex] = n.left;
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.                 if (!hit) break;
  178.             }
  179.  
  180.             root = nodes[0];
  181.  
  182.             int varcount = 0;
  183.  
  184.             if (input.Contains('A')) varcount++;
  185.             if (input.Contains('B')) varcount++;
  186.             if (input.Contains('C')) varcount++;
  187.             if (input.Contains('D')) varcount++;
  188.  
  189.             if (varcount == 0)
  190.             {
  191.                 Console.Write("No input given.");
  192.             }
  193.             else
  194.             {
  195.                 for (int i = 0; i < (int)Math.Pow(2, varcount); i++)
  196.                 {
  197.                     string binstring = Convert.ToString(i, 2);
  198.                     for (int j = binstring.Length; j < 4; j++) { binstring = "0" + binstring; }
  199.                     var a = binstring.ToCharArray()[3] == '1';
  200.                     var b = binstring.ToCharArray()[2] == '1';
  201.                     var c = binstring.ToCharArray()[1] == '1';
  202.                     var d = binstring.ToCharArray()[0] == '1';
  203.                     var res = evaluate(root, a, b, c, d);
  204.                     string output = "";
  205.                     if (varcount > 0) output += "A: " + a + " \t";
  206.                     if (varcount > 1) output += "B: " + b + " \t";
  207.                     if (varcount > 2) output += "C: " + c + " \t";
  208.                     if (varcount > 3) output += "D: " + d;
  209.                     Console.WriteLine(output + " \t" + res);
  210.                 }
  211.             }
  212.            
  213.         }
  214.  
  215.         static void Main(string[] args)
  216.         {
  217.             while (true)
  218.             {
  219.                 run();
  220.                 Console.ReadKey();
  221.                 Console.Clear();
  222.             }
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement