Skip to content
This repository was archived by the owner on May 27, 2023. It is now read-only.

Commit 06884ba

Browse files
committed
Inital Commit
Inital commit for boolean expression evaluator using reverse polish notation, created by the shunting-yard algorithmn
1 parent 95bfbb0 commit 06884ba

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29209.62
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Boolean-Expression-Solver", "Boolean-Expression-Solver\Boolean-Expression-Solver.csproj", "{4EDB0D9A-C180-43D0-94C6-B3CF1F3E7DCD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4EDB0D9A-C180-43D0-94C6-B3CF1F3E7DCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4EDB0D9A-C180-43D0-94C6-B3CF1F3E7DCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4EDB0D9A-C180-43D0-94C6-B3CF1F3E7DCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4EDB0D9A-C180-43D0-94C6-B3CF1F3E7DCD}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {44F2FEB6-9F74-4C1E-A35E-6BCBC6AED618}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RootNamespace>Boolean_Expression_Solver</RootNamespace>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

Comments
 (0)