我目前正在做一个随机的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'","B'","C'","D'"];
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'";
} else if (testing =="A'") {
var testing2 ="A";
} else if (testing =="B") {
var testing2 ="B'";
} else if (testing =="B'") {
var testing2 ="B";
}else if (testing =="C") {
var testing2 ="C'";
} else if (testing =="C'") {
var testing2 ="C";
}else if (testing =="D") {
var testing2 ="D'";
} else if (testing =="D'") {
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;
}
}
}
目前我正在尝试创建随机问题,例如
(ABC + C'D'A ')C + C'B'
BAC'D'+(CB'A'D)(B'A)
BCA +(B'C)(A'C')
(BC'D'A + CADB ')A'DC' + B '+(D'A')
(BC'D'A + CADB ')+ B' +(D'A')
基于这4个问题,我可以通过使用替换函数来解决qn 2,qn 3,qn 5,但对于qn1和qn4, 我必须检查括号内是否有加号,如果括号外有变量,我需要在里面添加变量。 如果Qn5之外的括号外没有变量,我只需删除括号。
像这样qn5例如,在函数完成后,qn4应该如下所示
我可以请一些关于此的建议吗? 在检查括号内是否有加号的函数上,检查括号外是否有任何变量,如果有,则必须将变量外部添加到括号内的变量