program for user algebra




Comment hidden because of low score. Click to expand.
1
of 1 vote

function solve(exp)
{
    
    var rank = {'+':1, '-':1, '/':2, '*':2};
    var func = {
        '+': function(e1, e2) {
            return parseFloat(e1)+parseFloat(e2);
        },
        '-': function(e1, e2) {
            return parseFloat(e1)-parseFloat(e2);
        },
        '/': function(e1, e2) {
            return parseFloat(e1)/parseFloat(e2);
         },
        '*': function(e1, e2) {
            return parseFloat(e1)*parseFloat(e2);
        }
    };
   
                            
    var stack=new Array();
    var fexp = new Array();
    for(var i = 0; i < exp.length; i++)
    {
        if(IsNumeric(exp[i]))
        {
            fexp.push(exp[i]);
        }
        else
        {
            console.log('rank: ' + exp[i] + ' ' + rank[exp[i]]);
            if(stack.length == 0 || rank[exp[i]] > rank[stack[stack.length - 1]])
                stack.push(exp[i]);
            else
            {
                do
                {
                    fexp.push(stack.pop());
                }
                while(rank[exp[i]] <= rank[stack[stack.length - 1]]);
                
                stack.push(exp[i]);
            }
        }
    }
    
    while(stack.length != 0)
        fexp.push(stack.pop());
    
    console.log(fexp);
    for(var i = 0; i< fexp.length; i++)
    {
        if(IsNumeric(fexp[i]))
        {
            stack.push(fexp[i]);
        }
        else
        {
            var e1 = stack.pop();
            var e2 = stack.pop();
            console.log(fexp[i] + ': ' + [e2, e1]);
            var val = func[fexp[i]](e2, e1);
            console.log('res: ' + val); 
            stack.push(val);
        }
    }
    
    alert(stack.pop());
    
}
           
function IsNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}

solve('1+8/4/2')

- shivkalra1 December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a hash table to store variable names with the values associated with them.

- peanuts December 19, 2013 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More