|
| 1 | +using System; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace Boolean_Expression_Solver |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + //XOR NOT OR AND = ^, ', +, * |
| 11 | + public static void Main(string[] args) |
| 12 | + { |
| 13 | + Console.WriteLine("YOYOYO WHATS UP MIZZIA Please enter the how many variables you wish to use"); |
| 14 | + |
| 15 | + int numberOfVariables = Convert.ToInt32(Console.ReadLine()); |
| 16 | + Console.WriteLine("Infix to Postfix"); |
| 17 | + string userInput = Console.ReadLine(); |
| 18 | + |
| 19 | + Console.WriteLine("[TRUTH TABLE GENERATOR]"); |
| 20 | + bool[] valueOfVariables = new bool[numberOfVariables]; |
| 21 | + int biggestValue = Convert.ToInt32(Math.Pow(2, numberOfVariables)) - 1; |
| 22 | + int biggestDigitLength = Convert.ToString(biggestValue, 2).Length; |
| 23 | + for (int i = 0; i < Math.Pow(2, numberOfVariables); i++) |
| 24 | + { |
| 25 | + string binary = Convert.ToString(i, 2); |
| 26 | + binary = binary.PadLeft(biggestDigitLength, '0'); |
| 27 | + bool[] binaryExpression = binary.Select(c => c == '1').ToArray(); |
| 28 | + foreach(var b in binaryExpression) |
| 29 | + { |
| 30 | + Console.Write(b + " "); |
| 31 | + } |
| 32 | + List<string> token = Tokenize(userInput); |
| 33 | + List<string> RPN = GetRPN(token); |
| 34 | + Console.WriteLine("Output " + solver(RPN, binaryExpression)); |
| 35 | + Console.WriteLine(); |
| 36 | + } |
| 37 | + } |
| 38 | + public static List<string> Tokenize(string input) |
| 39 | + { |
| 40 | + List<string> TokenResult = new List<string>(); |
| 41 | + input = Regex.Replace(input, @"\s+", ""); |
| 42 | + string numberString = ""; |
| 43 | + foreach (char c in input) |
| 44 | + { |
| 45 | + if (Char.IsLetter(c)) |
| 46 | + { |
| 47 | + numberString += c; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + TokenResult.Add(numberString); |
| 52 | + numberString = ""; |
| 53 | + TokenResult.Add(c.ToString()); |
| 54 | + } |
| 55 | + } |
| 56 | + return TokenResult; |
| 57 | + } |
| 58 | + static List<string> GetRPN(List<string> token) |
| 59 | + { |
| 60 | + Stack<string> s = new Stack<string>(); |
| 61 | + List<string> PostFix = new List<string>(); |
| 62 | + |
| 63 | + Console.Write("\nEnter your expression: "); |
| 64 | + try |
| 65 | + { |
| 66 | + ConvertToPostFix(token, s, ref PostFix); |
| 67 | + |
| 68 | + Print(PostFix); |
| 69 | + return PostFix; |
| 70 | + } |
| 71 | + catch (InvalidOperationException) |
| 72 | + { |
| 73 | + Console.WriteLine("Invalid expression"); |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | + static void ConvertToPostFix(List<string> symbols, Stack<string> s, ref List<string> PostFix) |
| 78 | + { |
| 79 | + int n; |
| 80 | + foreach (string c in symbols) |
| 81 | + { |
| 82 | + if (int.TryParse(c.ToString(), out n)) |
| 83 | + { |
| 84 | + PostFix.Add(c); |
| 85 | + } |
| 86 | + if (c == "(") s.Push(c); |
| 87 | + if (c == ")") |
| 88 | + { |
| 89 | + while (s.Count != 0 && s.Peek() != "(") |
| 90 | + { |
| 91 | + PostFix.Add(s.Pop()); |
| 92 | + } |
| 93 | + s.Pop(); |
| 94 | + } |
| 95 | + if (IsOperator(c)) |
| 96 | + { |
| 97 | + while (s.Count != 0 && Priority(s.Peek()) >= Priority(c)) |
| 98 | + { |
| 99 | + PostFix.Add(s.Pop()); |
| 100 | + } |
| 101 | + s.Push(c); |
| 102 | + } |
| 103 | + } |
| 104 | + while (s.Count != 0) |
| 105 | + { |
| 106 | + PostFix.Add(s.Pop()); |
| 107 | + } |
| 108 | + } |
| 109 | + static int Priority(string c) |
| 110 | + { |
| 111 | + if(c == "'") |
| 112 | + { |
| 113 | + return 3; |
| 114 | + } |
| 115 | + else if (c == "*" || c== "^") |
| 116 | + { |
| 117 | + return 2; |
| 118 | + } |
| 119 | + else if (c == "+") |
| 120 | + { |
| 121 | + return 1; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + return 0; |
| 126 | + } |
| 127 | + } |
| 128 | + static bool IsOperator(string c) |
| 129 | + { |
| 130 | + if (c == "*" || c == "+" || c == "^" || c == "'") |
| 131 | + { |
| 132 | + return true; |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + return false; |
| 137 | + } |
| 138 | + } |
| 139 | + static void Print(List<string> PostFix) |
| 140 | + { |
| 141 | + for (int i = 0; i < PostFix.Count; i++) |
| 142 | + { |
| 143 | + Console.Write("{0} ", PostFix[i]); |
| 144 | + } |
| 145 | + } |
| 146 | + public static bool solver(List<string> token, bool[] valuesOfVariables) |
| 147 | + { |
| 148 | + Console.WriteLine("[SOLVER]"); |
| 149 | + Stack<bool> stack = new Stack<bool>(); |
| 150 | + |
| 151 | + for (int i = 0; i < token.Count; i++) |
| 152 | + { |
| 153 | + if (int.TryParse(token[i], out int n)) |
| 154 | + { |
| 155 | + stack.Push(Convert.ToBoolean(valuesOfVariables[Convert.ToInt32(token[i]) - 1])); |
| 156 | + } |
| 157 | + else if (token[i] == "^" || token[i] == "*" || token[i] == "+" || token[i] == "'") |
| 158 | + { |
| 159 | + switch (token[i]) |
| 160 | + { |
| 161 | + case "+": |
| 162 | + stack.Push(stack.Pop() | stack.Pop()); |
| 163 | + break; |
| 164 | + case "^": |
| 165 | + stack.Push(stack.Pop() ^ stack.Pop()); |
| 166 | + break; |
| 167 | + case "*": |
| 168 | + stack.Push(stack.Pop() & stack.Pop()); |
| 169 | + break; |
| 170 | + case "'": |
| 171 | + stack.Push(!stack.Pop()); |
| 172 | + break; |
| 173 | + default: |
| 174 | + throw new Exception("ERROR: Invalid Operation"); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + return stack.Pop(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments