Skip to content

Commit

Permalink
1st commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceoutdoors committed Nov 30, 2014
1 parent 0d182cc commit 6dbde01
Show file tree
Hide file tree
Showing 17 changed files with 1,469 additions and 0 deletions.
64 changes: 64 additions & 0 deletions AboutDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions AboutDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TTGen
{
public partial class AboutDialog : Form
{
public AboutDialog()
{
InitializeComponent();

AboutTxt.Text =
"Truth Table Generator v1.0 \n" +
"By Lee Zhen Yong AKA bruceoutdoors \n\n" +
"Built date: 30 Nov 2014 \n\n" +
"Uses Dijkstra's Shunting Yard algorithm \n" +
"and reverse polish notation. \n\n"
;
}
}
}
120 changes: 120 additions & 0 deletions AboutDialog.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
169 changes: 169 additions & 0 deletions BoolExpr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TTGen
{
class BoolExpr
{
private List<Token> Expression;
private const string mismatchParenErrorMsg =
"Error! Mismatched parenthesis in your expression!";

public BoolExpr(string input)
{
Expression = Token.Tokenize(input);
InsertANDs();

// convert to Reverse Polish Notation
Expression = ShuntingYard(Expression);
}

public bool Solve()
{
Stack<bool> stack = new Stack<bool>();

foreach (Token t in Expression) {
if (t.Category == Token.TokenCategory.Bool) {
stack.Push(t.BoolVal);
} else if (t.Category == Token.TokenCategory.Op) {
if (t.ArgCount > stack.Count) {
throw new Exception("The user has not input sufficient values in the expression!");
}

// evaluate the operator:
switch (t.Symbol) {
case '+':
stack.Push(stack.Pop() | stack.Pop());
break;
case '^':
stack.Push(stack.Pop() ^ stack.Pop());
break;
case '*':
stack.Push(stack.Pop() & stack.Pop());
break;
case '\'':
stack.Push(!stack.Pop());
break;
default:
throw new Exception("Error: Invalid operation!!");
}
}
}

if (stack.Count > 1) throw new Exception("Error: The user input has too many values.");

return stack.Pop();
}

// converts the token list to Reverse Polish Notation (RPN)
// using the Dijkstra's Shunting Yard algorithm
private List<Token> ShuntingYard(List<Token> list)
{
List<Token> outputQueue = new List<Token>();
Stack<Token> operatorStack = new Stack<Token>();

foreach (Token t in list) {
if (t.Category == Token.TokenCategory.Bool) {
outputQueue.Add(t);
} else if (t.Category == Token.TokenCategory.Op) {
while ((operatorStack.Count > 0) &&
(operatorStack.Peek().Category == Token.TokenCategory.Op) &&
(
(t.IsLeftAssoc && t.Precedence <= operatorStack.Peek().Precedence) ||
(!t.IsLeftAssoc && t.Precedence < operatorStack.Peek().Precedence)
)) {
outputQueue.Add(operatorStack.Pop());
}
operatorStack.Push(t);
} else if (t.Category == Token.TokenCategory.LeftParen) {
operatorStack.Push(t);
} else if (t.Category == Token.TokenCategory.RightParen) {
try {
while (operatorStack.Peek().Category != Token.TokenCategory.LeftParen) {
outputQueue.Add(operatorStack.Pop());
}
// pop left parenthesis from the stack
operatorStack.Pop();
} catch (InvalidOperationException) {
throw new Exception(mismatchParenErrorMsg);
}
}
}

// pop remaining operators in the stack to the output queue:
while (operatorStack.Count > 0) {
// If the operator token on the top of the stack is a parenthesis
if (operatorStack.Peek().Category == Token.TokenCategory.LeftParen ||
operatorStack.Peek().Category == Token.TokenCategory.RightParen) {
throw new Exception(mismatchParenErrorMsg);
}
outputQueue.Add(operatorStack.Pop());
}

return outputQueue;
}

private void InsertANDs()
{
// compare 2 tokens at a time:
int a = 0; int b = 1;
while (b < Expression.Count) {
// iterates through every possible location
// where * should be inserted
if ((Expression[a].Category == Token.TokenCategory.Bool &&
Expression[b].Category == Token.TokenCategory.LeftParen)
||
(Expression[a].Category == Token.TokenCategory.Bool &&
Expression[b].Category == Token.TokenCategory.Bool)
||
(Expression[a].Category == Token.TokenCategory.RightParen &&
Expression[b].Category == Token.TokenCategory.LeftParen)
||
(Expression[a].Category == Token.TokenCategory.RightParen &&
Expression[b].Category == Token.TokenCategory.Bool)
|| // conditions involving NOT (')
(Expression[a].Symbol == '\'' &&
Expression[b].Category == Token.TokenCategory.Bool)
||
(Expression[a].Symbol == '\'' &&
Expression[b].Category == Token.TokenCategory.LeftParen)
) {
Expression.Insert(b, new Token('*', Token.TokenCategory.Op, 4));
a++; b++;
}
a++; b++;
}
}

public List<char> GetBoolVars()
{
List<char> boolVars = new List<char>();
foreach (var t in Expression) {
if (t.Category == Token.TokenCategory.Bool && t.isVariable) {
boolVars.Add(t.Symbol);
}
}
boolVars = boolVars.Distinct().ToList();
boolVars.Sort();

return boolVars;
}

// set variable value, returns true if successfully changed
public bool SetValue(char c, bool val)
{
bool success = false;
char ch = Char.ToUpper(c);
foreach (var t in Expression) {
if (t.Symbol == ch) {
t.BoolVal = val;
success = true;
}
}

return success;
}
}
}
Loading

0 comments on commit 6dbde01

Please sign in to comment.