예제 #1
0
        public void Generate(string strExp)
        {
            lexer = new Lexer();

            var tokens = lexer.Tokenize(strExp);

            expression = parser.Parse(tokens);
            if (!expression.ResultType.HasFlagNI(ExpressionResultType.Boolean))
                throw new NotSupportedException();

            expressions = Helpers.ConvertExpressionToCollection(expression);
            parameters = Helpers.GetParameters(tokens);
            table = new List<TruthTableRowViewModel>();

            var parametersCount = parameters.Count();
            for (int i = (int)Math.Pow(2, parametersCount) - 1; i >= 0; i--)
            {
                SetBits(i, parametersCount);

                var b = (bool)expression.Execute(parameters);

                var row = new TruthTableRowViewModel(parametersCount, expressions.Count());

                row.Index = (int)Math.Pow(2, parametersCount) - i;
                for (int j = 0; j < parametersCount; j++)
                    row.VarsValues[j] = (bool)parameters[parameters.ElementAt(j).Key];

                for (int j = 0; j < expressions.Count() - 1; j++)
                    row.Values[j] = (bool)expressions.ElementAt(j).Execute(parameters);

                if (expressions.Count() != 0)
                    row.Result = b;

                table.Add(row);
            }
        }
예제 #2
0
        public void Generate(string strExp)
        {
            expression = parser.Parse(strExp);
            if (!expression.ResultType.HasFlag(ExpressionResultType.Boolean))
                throw new InvalidOperationException();

            expressions = parser.ConvertExpressionToCollection(expression);
            parameters = parser.GetParameters(strExp);
            table = new List<TruthTableRowViewModel>();

            var parametersCount = parameters.Count();
            for (int i = (int)Math.Pow(2, parametersCount) - 1; i >= 0; i--)
            {
                SetBits(i, parametersCount);

                var b = (bool)expression.Calculate(parameters);

                var row = new TruthTableRowViewModel(parametersCount, expressions.Count());

                row.Index = (int)Math.Pow(2, parametersCount) - i;
                for (int j = 0; j < parametersCount; j++)
                    row.VarsValues[j] = (bool)parameters[parameters.ElementAt(j).Key];

                for (int j = 0; j < expressions.Count() - 1; j++)
                    row.Values[j] = (bool)expressions.ElementAt(j).Calculate(parameters);

                if (expressions.Count() != 0)
                    row.Result = b;

                table.Add(row);
            }
        }
예제 #3
0
        public void Generate(string strExp)
        {
            expression = parser.Parse(strExp);
            expressions = parser.ConvertLogicExpressionToCollection(expression);
            parameters = parser.GetLogicParameters(strExp);
            table = new List<TruthTableRowViewModel>();

            for (int i = (int)Math.Pow(2, parameters.Count) - 1; i >= 0; i--)
            {
                parameters.Bits = i;
                bool b = expression.Calculate(parameters);

                var row = new TruthTableRowViewModel(parameters.Count, expressions.Count());

                row.Index = (int)Math.Pow(2, parameters.Count) - i;
                for (int j = 0; j < parameters.Count; j++)
                {
                    row.VarsValues[j] = parameters[parameters[j]];
                }

                for (int j = 0; j < expressions.Count() - 1; j++)
                {
                    row.Values[j] = expressions.ElementAt(j).Calculate(parameters);
                }

                if (expressions.Count() != 0)
                    row.Result = b;

                table.Add(row);
            }
        }