NaN误差方程

时间:2015-04-30 19:54:28

标签: javascript nan

在这个javascript代码中我尝试解决二次方程式,我已经研究了一个小时,这应该告诉我a,b和c的值,其中y是a(x ^ 2)。我是一个相对javascript初学者,并希望得到一些帮助。为什么a,b和c的值不是数字?变量名是意大利语,英语和其他东西(甚至我不知道是什么),但我评论它们是什么。这是我作为一个通常单独工作的学生的不良特质之一,如果不容易理解,那就很抱歉。

<script type="text/javascript">
        var equa=prompt("Scrivi l'equazione senza spazi usando x come incognita e y come x^2");
        var a = 0.0; b = 0.0; c = 0.0;//ax2+bx+c
        var prima = true; //before or after equal?
        var ope = 1;//1add 2sub 3mul 4div
        var lasto = 0.0;//last number, used for div and mul
        var lastos = 3; //1a2b3c
        var errore=0;//is something messed up?
        for(var i = 0; i < equa.length;i=i){
            if(equa.charAt(i)=='='){
                prima = false;
                i++;
            }else if(equa.charAt(i)=='+'){
                ope=1;
                i++;
            }else if(equa.charAt(i)=='-'){
                ope=2;
                i++;
            }else if(equa.charAt(i)=='*'){
                ope=3;
                i++;
            }else if(equa.charAt(i)=='/'){
                ope=4;
                i++;
            }else{
                var nume = "";//Current number in string form
                while(i<equa.length && equa.charAt(i)>'0' && equa.charAt(i)<'9'){
                    nume+=equa.charAt(i);
                    i++;
                }
                var lasnum = 0.0;//current number in float form 
                var lasval = 3; //1a2b3c
                if(equa.charAt(i) == 'x'){
                    lasnum=parseFloat(nume);
                    lasval = 2;
                }else if(equa.charAt(i) == 'y'){
                    lasnum=parseFloat(nume);
                    lasval = 1;
                }else{
                    lasnum = parseFloat(nume);
                    lasval=3;
                }
                i++;
                if( (ope == 1 || ope == 2) && !(equa.charAt(i) =='*' || equa.charAt(i) == '/')){
                    if(lasval == 1){
                        if(prima) a+=lasnum;
                        else a-=lasnum;
                    }
                    else if(lasval == 2){
                        if(prima) b+=lasnum;
                        else b-=lasnum;
                    }
                    else {
                        if(prima) c+=lasnum;
                        else c-=lasnum;
                    }
                }else if( (ope==1 || ope == 2) && (equa.charAt(i) =='*' || equa.charAt(i) == '/')){
                    //zitto e fermo
                    lasto=lasnum;
                    lastos=lasval;
                }else if( (ope==3||ope == 4)){
                    if(ope==3){
                        if(lastos==3){
                            lasnum*=lasto;
                        }
                        if(lastos == 2){

                            if(lastval==3){
                                lasnum*=lasto;
                                lastval=2;
                            }
                            if(lastval==2){
                                lasnum*=lasto;
                                lastval=1;
                            }
                            if(lastval==1){
                                errore=1;
                            }

                        }
                        if(lastos == 1){
                            if(lastval == 3){
                                lasnum*=lasto;
                                lastval=1;
                            }else{
                                errore=1;
                            }
                        }

                    }
                    if(ope == 4){
                        if(lastos == 1){

                            if(lastval==3){
                                lasnum/=lasto;
                                lastval=1;
                            }
                            if(lastval==2){
                                lasnum/=lasto;
                                lastval=2;
                            }
                            if(lastval==1){
                                lasnum/=lasto;
                                lastval=3;
                            }

                        }
                        if(lastos == 2){
                            if(lastval==1){
                                errore=1;
                            }
                            if(lastval==2){
                                lasnum/=lasto;
                                lastval=3;
                            }
                            if(lastval==3){
                                lasnum/=lasto;
                                lastval=2;
                            }

                    }
                        if(lastos == 3){
                            if(lastval==3){
                                lasnum/=lasto;
                            }else{
                                errore=1;
                            }

                        }
                    }
                    if(equa.charAt(i) =='*' || equa.charAt(i) == '/'){
                        lasto=lasnum;
                        lasto=lasval;
                    }else{
                        if(lasval == 1){
                            if(prima) a+=lasnum;
                            else a-=lasnum;
                        }
                        else if(lasval == 2){
                            if(prima) b+=lasnum;
                            else b-=lasnum;
                        }
                        else {
                            if(prima) c+=lasnum;
                            else c-=lasnum;
                        }
                        lasto=0;
                        lastos=3;
                    }
                }

            }
        }
        if(errore==0){
            alert("a ="+a+" b="+b+" c="+c);
        }else{
            alert("AOOOOOOo");
        }
    </script>

1 个答案:

答案 0 :(得分:0)

由于预期输入的格式应为&#34; 15y + 3x + 5 = 20&#34;例如,那么这是一个简单的正则表达式:

var equa = prompt("Scrivi l'equazione senza spazi usando x come incognita e y come x^2");
var regex = /^([0-9.]+)y\+([0-9.]+)x\+([0-9.]+)=([0-9.]+)$/;
var matches = regex.exec(equa);
if (matches) {
  var a = parseFloat(matches[1]);
  var b = parseFloat(matches[2]);
  var c = parseFloat(matches[3]) - parseFloat(matches[4]);
  var discriminant = b*b - 4*a*c;
  if (discriminant < 0) {
    alert('No real solutions');
  }
  else {
    var root = Math.sqrt(discriminant);
    alert('Root: ' + ((-b + root)/(2*a)) + ', ' + ((-b - root)/(2*a)));
  }
}
else {
  alert("AOOOOOOo");
}