求一个javascript描述的解决方案

查看:58
本文介绍了求一个javascript描述的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and its coefficient.

    If there is no solution for the equation, return "No solution".

    If there are infinite solutions for the equation, return "Infinite solutions".

    If there is exactly one solution for the equation, we ensure that the value of x is an integer.

    

Example 1:
   Input: "x+5-3+x=6+x-2"
    Output: "x=2"

Example 2:
            Input: "x=x"
            Output: "Infinite solutions"
            Example 3:
    
                Input: "2x=x"
                Output: "x=0"
                Example 4:
        
                    Input: "2x+3x-6x=x+2"
                    Output: "x=-1"<
                    Example 5:
                
                        Input: "x=x+2"
                        Output: "No solution"

有什么javascript描述的解决方案

解决方案

        var solveEquation = function(equation) {
                 let leftValue=0;
                let countOfLeftX = 0;
                let rightValue=0;
                let countOfRightX=0;
                let isLeft=true;
                let lastOperator='+';
                let temp='';
                for(let i=0;i<=equation.length;i++){
                    if(isLeft){
                        if(equation[i]==='+' || equation[i]==='-' || equation[i]==='='){
                            if(temp.length>0){
                                if(!!~temp.indexOf('x')){
                                    let v = temp.substring(0,temp.length-1);
                                    if(!v){
                                        v=1;
                                    }
                                    countOfLeftX = countOfLeftX +(lastOperator==='+'?+parseFloat(v):-parseFloat(v));
                                    
                                } else {
                                    leftValue = leftValue + (lastOperator==='+'?+parseFloat(temp):-parseFloat(temp));
                                }
                                temp = '';
                            } 
                            lastOperator = equation[i];

                        } else {
                            temp+=equation[i];
                        }
                        if(equation[i]==='='){
                            lastOperator='+';
                            isLeft=false;
                        }

                    } else {
                        if(equation[i]==='+' || equation[i]==='-' || i==equation.length){
                            if(temp.length>0){
                                if(!!~temp.indexOf('x')){
                                    let v = temp.substring(0,temp.length-1);
                                    if(!v){
                                        v=1;
                                    }
                                    countOfRightX = countOfRightX +(lastOperator==='+'?+parseFloat(v):-parseFloat(v));
                                    
                                } else {
                                    rightValue = rightValue + (lastOperator==='+'?+parseFloat(temp):-parseFloat(temp));
                                }
                                temp = '';
                            } 
                            if(i!==equation.length){
                                lastOperator = equation[i];
                            }
                            

                        } else {
                            temp+=equation[i];
                        }
                    }

                }
                let count = countOfLeftX - countOfRightX;
                let value = rightValue - leftValue;
                if(count ===0 && value===0){
                    return "Infinite solutions";
                } else if(count ===0 && value!==0){
                    return "No solution";
                } else {
                    return "x="+value/count;
                }
            };

这篇关于求一个javascript描述的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆