C# :: Aufgabe #198 :: Lösung #2

3 Lösungen Lösungen öffentlich
#198

Logische Ausdrücke 'ausklammern'

Fortgeschrittener - C# von polie - 25.01.2018 um 19:12 Uhr
Schreibe ein Programm welches aus dem Ausdruck

A&(B|C)&(D|E)

den folgenden erzeugt:

A&B&D|A&B&E|A&C&D|A&C&E

Hier ist das & ein logisches UND und das | das logische ODER. Die Klammern sind wie bei der Multipikation/Division zu behandeln.
#2
vote_ok
von hollst (13980 Punkte) - 07.05.2018 um 11:03 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
Schreibe ein Programm welches aus dem Ausdruck
A&(B|C)&(D|E)
den folgenden erzeugt:
A&B&D|A&B&E|A&C&D|A&C&E
Hier ist das & ein logisches UND und das | das logische ODER. 
Die Klammern sind wie bei der Multipikation/Division zu behandeln. 
*/

namespace aufgabe_198
{
    class Program
    {
        static string NL = Environment.NewLine;
        static void Main()
        {
            //string input = "A*(B+C)*(D+E)";
            string input = "(B+C)*(D+E)*A";
            WriteLine("input: " + NL + input);

            Dictionary<string, string> output = Substituts(input);
            List<string> init_strings = plus_token(output["all"]);
            List<string> expanded = expand(init_strings, output);

            StringBuilder sb = new StringBuilder();
            for (var i = 0; i < expanded.Count; i++)
                sb.Append(expanded[i] + "+");
            string result = sb.ToString();
            WriteLine("result:" + NL + result.Remove(result.Length - 1));

            ReadKey(true);
        }

        static string eliminate_brackets(string token, string sub, 
            ref int subi, ref Dictionary<string, string> dic)
        {
            string ssub = sub + subi.ToString();
            subi++;
            char[] c = token.ToCharArray();
            char[] ct = new char[c.Length - 2];
            Array.Copy(c, 1, ct, 0, ct.Length);
            dic.Add(ssub, new string(ct));
            return ssub;
        }

        static Dictionary<string, string> Substituts(string input)
        {
            //step 0: remove spaces
            Char LZ = ' ';
            string result = string.Empty;
            for (var i = 0; i < input.Length; i++)
                if (input[i] != LZ)
                    result += input[i].ToString();
            
            int kmax = int.MaxValue;
            string sub = "sub";
            int subi = 0;

            Dictionary<string, string> dic = new Dictionary<string, string>();

            //step 1: substitute brackets
            while (kmax > 1)
            {
                int[] klammer = new int[result.Length];
                int[] ebene = new int[result.Length];
                int ka = 0;
                kmax = 0;

                for (var i = 0; i < result.Length; i++)
                {
                    if (result[i] == '(')
                        { klammer[i] = ka++; if (ka > kmax) kmax = ka; };
                    ebene[i] = ka;
                    if (result[i] == ')')
                        { klammer[i] = ka; ka--; };
                }

                string token = result[0].ToString();
                int index = ebene[0];

                string ohne_klammer = string.Empty;

                for (var i = 1; i < result.Length; i++)
                {
                    if (ebene[i] == index)
                        token += result[i].ToString();
                    else
                    {
                        if (token[0] == '(' && token[token.Length - 1] == ')')
                            token = eliminate_brackets(token, sub, ref subi, ref dic);
                        ohne_klammer += token;

                        index = ebene[i];
                        token = result[i].ToString();
                    }
                }

                if (token[0] == '(' && token[token.Length - 1] == ')')
                    token = eliminate_brackets(token, sub, ref subi, ref dic);
                ohne_klammer += token;

                result = ohne_klammer;
            }//while
            dic.Add("all", result);
            return dic;
        }

        static List<string> plus_token(string input)
        {
            string[] inputs = input.Split('+');
            List<string> result = new List<string>();
            for (var i = 0; i < inputs.Length; i++)
                result.Add(inputs[i]);
            return result;
        }

        static List<string> expand(List<string> input, Dictionary<string, string> substitutes)
        {
            List<string> result = new List<string>();
            for(var i = 0; i < input.Count; i++)
            {
                string mult_token = input[i];
                string[] factors = mult_token.Split('*');
                string new_mult_token = string.Empty;
                bool bo_key = false;
                for (var j = 0; j < factors.Length; j++)    {
                    if (substitutes.Keys.Contains(factors[j]))  {
                        bo_key = true;
                        string plus_factor = substitutes[factors[j]];
                        List<string> Lplus_factor = plus_token(plus_factor);
                        for (var k = 0; k < Lplus_factor.Count; k++)    {
                            string new_mult_token_temp = new_mult_token;

                            new_mult_token_temp += Lplus_factor[k] + "*";
                            for (var kk = j + 1; kk < factors.Length; kk++)
                                new_mult_token_temp += factors[kk] + "*";
                            new_mult_token_temp = 
                                new_mult_token_temp.Remove(new_mult_token_temp.Length - 1);
                            result.Add(new_mult_token_temp);
                        }
                    }
                    else
                        new_mult_token += factors[j] + "*";

                    if (bo_key)
                        break;//j
                }//j
                if (!bo_key)
                    result.Add(new_mult_token.Remove(new_mult_token.Length - 1));
            }//i

            if (result.Count == input.Count)
                return result;
            else
                return expand(result, substitutes);
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben