static void Start(string path) { using (var stream = new StreamReader(path)) { while (!stream.EndOfStream) { var input = stream.ReadLine(); BoolBash.Reset(); var expr = BoolBash.ToCorrect(input); Console.WriteLine(input); BoolBash.GetNumberOfArguments(expr); var result = LogicCalculator.ParseExpression(expr, LogicCalculator.commands, null); Console.WriteLine(); BoolBash.PrintTable(result); Console.WriteLine(); var sknf = BoolBash.ToCorrect(result.SKNF()); var sdnf = BoolBash.ToCorrect(result.SDNF()); Console.WriteLine($"SKNF:\n{BoolBash.DelSpaces(sknf)}"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine($"SDNF:\n{BoolBash.DelSpaces(sdnf)}"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); } } }
public static BoolBash Operation(BoolBash left, BoolBash right, string operation) { List <byte> resultBytes = new List <byte>(); for (var i = 0; i < left?._values.Count; i++) { resultBytes.Add(OperationsDictionary[operation].Invoke((left._values)[i], right._values[i])); } return(new BoolBash($"{left?._name} {operation} {right._name}", resultBytes)); }
//A AND (B OR C) //A * (B + C) //(A XOR B AND C) public static BoolBash ParseExpression(string expression, List <string> commands, Action <BoolBash> logged = null) { var separator = new char[] { ' ' }; var stackOperations = new Stack <string>(); var stackArguments = new Stack <BoolBash>(); BoolBash argument1; BoolBash argument2; var lexemes = expression.Split(separator, StringSplitOptions.RemoveEmptyEntries); foreach (var lexeme in lexemes) { if (commands.Contains(lexeme)) { if (stackOperations.Count != 0) { while (GetPriorytyOperation(lexeme) < GetPriorytyOperation(stackOperations.Peek()) && stackOperations.Peek() != "(") { argument2 = stackArguments?.Pop() ?? null; argument1 = stackArguments.Count != 0 ? stackArguments?.Pop() : null; BoolBash tmp = BoolBash.Operation(argument1, argument2, stackOperations.Pop()); stackArguments.Push(tmp); logged?.Invoke(tmp); } if (lexeme == ")") { stackOperations.Pop(); } else { stackOperations.Push(lexeme); } } else { stackOperations.Push(lexeme); } } else { stackArguments.Push(new BoolBash(lexeme, BoolBash.CountBool)); } } return(stackArguments.Pop()); }
public static void PrintTable(BoolBash result) { _usedVariables.Add(" res", result); foreach (var item in _usedVariables) { Console.Write($"{item.Key} "); } Console.WriteLine(); for (int i = 0; i < _countRow; i++) { foreach (var item in _usedVariables) { Console.Write($" {item.Value._values[i]} "); } Console.WriteLine(); } _usedVariables.Remove(" res"); }
public static void Logged(BoolBash boolBash) { Console.WriteLine(boolBash.ToString()); Console.WriteLine(); }