Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Net.Sockets;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.IO;
- using System.ComponentModel;
- using System.Reflection;
- using System.Xml;
- using System.Globalization;
- namespace Program
- {
- public class MainClass
- {
- static void Main(string[] args)
- {
- Console.WriteLine(BoolExpressionReducer("t&!(f|f|t&t)") == "t");
- Console.ReadLine();
- }
- private static string BoolExpressionReducer(string Source)
- {
- while (Source.Contains('('))
- {
- int StartSubExpression = 0;
- int EndSubExpression = 0;
- int DeltaSubs = 0;
- for (int i = 0; i < Source.Length; i++)
- {
- if (Source[i] == '(')
- {
- DeltaSubs++;
- if (StartSubExpression == 0)
- {
- StartSubExpression = i + 1;
- }
- }
- else if (Source[i] == ')')
- {
- DeltaSubs--;
- if (DeltaSubs == 0)
- {
- EndSubExpression = i - 1;
- break;
- }
- }
- }
- Source = Source.Replace(Source.Substring(StartSubExpression - 1, EndSubExpression - StartSubExpression + 3), BoolExpressionReducer(Source.Substring(StartSubExpression, EndSubExpression - StartSubExpression + 1)));
- }
- while (Source.Contains('!'))
- {
- Source = Source.Replace("!t", "f").Replace("!f", "t");
- }
- while (Source.Count(x => x == '&' || x == '|') > 1)
- {
- if (Source.Contains('&'))
- {
- int AndIndex = Source.IndexOf('&');
- string AndString = Source.Substring(AndIndex - 1, 3);
- Source = Source.Replace(AndString, BoolExpressionReducer(AndString));
- }
- else // Everything is 'or'
- {
- return Source.Contains('t') ? "t" : "f";
- }
- }
- if (Source.Length == 1)
- {
- return Source;
- }
- else if(Source.Length != 3)
- {
- return string.Empty;
- }
- if (Source.Contains('&'))
- {
- return Source[0] == 't' && Source[2] == 't' ? "t" : "f";
- }
- else // Or
- {
- return Source[0] == 't' || Source[2] == 't' ? "t" : "f";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment