将变量放在括号外并将其添加到括号内的变量中

时间:2018-03-13 15:48:22

标签: javascript

我目前正在做一个随机的minterm maxterm迷你程序。

    var totalvar = getRandomInt(3,5);

    var main = "";
    for (var i = 1; i <= totalvar; i++) {
        var test =  getRandomInt(1,4);
        //alert(test);
        var myArray = ["A","B","C","D","A&apos;","B&apos;","C&apos;","D&apos;"];
        var text ="";
        for (var a = 1; a <= test; a++) {
                    function random(array) {
                    return array[Math.floor(Math.random() * array.length)]
                    }
                        var testing = random(myArray);
                        if (testing =="A") {
                            var testing2 ="A&apos;";    

                        } else if (testing =="A&apos;") {
                            var testing2 ="A";  

                        } else if (testing =="B") {
                            var testing2 ="B&apos;";    

                        } else if (testing =="B&apos;") {
                            var testing2 ="B";  

                        }else if (testing =="C") {
                            var testing2 ="C&apos;";    

                        } else if (testing =="C&apos;") {
                            var testing2 ="C";  

                        }else if (testing =="D") {
                            var testing2 ="D&apos;";    

                        } else if (testing =="D&apos;") {
                            var testing2 ="D";  

                        }
                        //alert(testing);
                        //alert(myArray);
                        text += testing
                        var index = myArray.indexOf(testing);
                        if (index > -1) {
                            myArray.splice(index, 1);
                        }
                        var index = myArray.indexOf(testing2);
                        if (index > -1) {
                            myArray.splice(index, 1);
                        }

        }

        var impt = totalvar - i;
                        var frontbracket = main.split("(").length;
            var backbracket = main.split(")").length;
        if (impt >= 2) {

            var brackets =  getRandomInt(1,3);
            var chances =  getRandomInt(1,3);
            var chances1 =  getRandomInt(1,3);
            var lastLetter = main.charAt(main.length - 1);


            alert(frontbracket);
            alert(backbracket);
            if (frontbracket == backbracket) {
            if (brackets == 1) {
             text = "(" + text;

            if (main == "") {
             main = text;


         }else
         main += "+" + text;



         } else {
             if (main == "") {
             main = text;
         }   else if ( lastLetter == ')') {
                    if ( chances !== 1) {
                        main += text;

                    }else
                        main += "+" + text;


            }else
                main += "+" + text;


         }
    }
         else if (frontbracket != backbracket){
         text = text + ")";
                main += "+" + text;


         }


        }    else if (frontbracket != backbracket){
         text = text + ")";
                main += "+" + text;


         }
                 else {
            var brackets =  getRandomInt(1,3);
            var chances =  getRandomInt(1,3);
        var lastLetter = main.charAt(main.length - 1);

        if (brackets == 1) {
             text = "(" + text + ")";

         if (main == "") {
             main = text;
         }  else if ( lastLetter == ')') {
         if ( chances !== '1') {
             main += text;

         }else
            main += "+" + text;


         }else
         main += "+" + text;



         } else {
             if (main == "") {
             main = text;
         }   else if ( lastLetter == ')') {
                    if ( chances !== 1) {
                        main += text;

                    }else
                        main += "+" + text;


            }else
                main += "+" + text;


         }
    }   

    }

目前我正在尝试创建随机问题,例如

  1. (ABC + C'D'A ')C + C'B'

  2. BAC'D'+(CB'A'D)(B'A)

  3. BCA +(B'C)(A'C')

  4. (BC'D'A + CADB ')A'DC' + B '+(D'A')

  5. (BC'D'A + CADB ')+ B' +(D'A')

  6. 基于这4个问题,我可以通过使用替换函数来解决qn 2,qn 3,qn 5,但对于qn1和qn4, 我必须检查括号内是否有加号,如果括号外有变量,我需要在里面添加变量。 如果Qn5之外的括号外没有变量,我只需删除括号。

    像这样qn5

    1. BC'D'A + CADB '+ B' + D'A'
    2. 例如,在函数完成后,qn4应该如下所示

      1. BC'D'AA'DC '+ CADB'A'DC' + B '+ D'A'
      2. 我可以请一些关于此的建议吗? 在检查括号内是否有加号的函数上,检查括号外是否有任何变量,如果有,则必须将变量外部添加到括号内的变量

0 个答案:

没有答案