How to code a calculator script like on Google

Discussion in 'Programming' started by misohoni, Mar 14, 2006.

  1. #1
    I have a search directory and think the Google Calculator script is pretty cool (not sure if Yahoo has one).

    My thinking is that the device to make a calculator should already be on my server and found some perl links on how to do it, just don't know if this can be integrated into a search box like Google.

    Anyone done it and is it easy to implement?
     
    misohoni, Mar 14, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From a programming point of view, if you know perl/php etc., it's not that hard. Once the form is submitted and before you output general results, match the submitted strings against typical culculation operators like +, - or search for measurement units like litre, pounds etc. Then do the magic.

    Not familiar with implementing any existing scripts though but if I were to get someone on Get A Freelancer to do this I'd expect to pay no more than $100.
     
    T0PS3O, Mar 14, 2006 IP
  3. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do it the easy way and use the google api.

    You can pass any query to them and output the answer without having to try and copy a script that they have already done.
     
    mad4, Mar 14, 2006 IP
    T0PS3O likes this.
  4. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #4
    misohoni, Mar 14, 2006 IP
  5. marty

    marty Peon

    Messages:
    154
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm just curious if there's a reason this can't be done on the client in JavaScript?
     
    marty, Mar 15, 2006 IP
  6. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #6
    I can't see why not...
     
    misohoni, Mar 15, 2006 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Then your code is in plain view for everyone to steal. Mad4's suggestion with the API is probablt easiest indeed.
     
    T0PS3O, Mar 16, 2006 IP
  8. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #8
    But since I'm creating a Search Engine, I'd prefer to use my own site rather than keep on using tools from the God that is known as Google.
     
    misohoni, Mar 16, 2006 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    True, good point. If you want to be independant, get it done for you / code it yourself.
     
    T0PS3O, Mar 16, 2006 IP
  10. marty

    marty Peon

    Messages:
    154
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    So misohoni, all you're looking for is a text box that when someone types a number in it, it multiplies the entered number by a conversion factor and then displays that number somewhere?
     
    marty, Mar 16, 2006 IP
  11. marty

    marty Peon

    Messages:
    154
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Here's a small proof of concept. Obviously it's no where near ready for production, but it shows what's possible:

    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head><title>POC JavaScript Calculator</title></head>
    <body>
    <input id="src" type="text" onkeypress="document.getElementById('dest').innerHTML='';document.getElementById('dest').innerHTML=this.value*1.5" onkeyup="document.getElementById('dest').innerHTML=this.value*1.5">
    <div id="dest"></div>
    </body>
    </html>
    
    
    HTML:
     
    marty, Mar 16, 2006 IP
  12. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #12
    Thanks Tops30 and Marty. Yep looking for someone to code it for me, pretty rubbish at doing it myself.

    Thanks Marty for the code, I see what you are doing, but want some thing simlar to the Google Calc - http://www.google.com/help/features.html#calculator, so that it recognises +, x, - and division.

    Just keep on thinking that the code used on the server is capable of calculation, just need to know how to get to it...
     
    misohoni, Mar 17, 2006 IP
  13. marty

    marty Peon

    Messages:
    154
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hmmm.

    Every programming language I know of has built in functions for doing basic math. The tricky part for your project is going to be parsing the "x","+","-" , and "/" out of the string and then forming the appropriate mathematical formulas.

    I think the person that helps you do this will need to examine every character and the evaluate whether that character is a number, blank space, or command. Then once they've got that they will need to evaluate whether the string makes any sense from a math perspective. If it doesn't make any sense then you don't have an easy fallback like google (they just render search results try 2*2a on google) you need to know exactly how you want to handle errors.

    I don't know if anyone has submitted a proposal to you for this job, but if no one has it's probably because it's hard to know exactly what you need. These types of requests tend to start off with simple requirements, but then the requirements tend to grow as the customer sees the final product.

    Good luck misohoni
     
    marty, Mar 17, 2006 IP
  14. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #14
    Thanks. All I'm requesting at the moment is the simple calculator script. My setup is like:
    <form id="search-form-id" class="v5" name="schina" action="/index.php">

    If anyone can do it, please let me know. Thanks
     
    misohoni, Mar 17, 2006 IP
  15. Phunk

    Phunk Peon

    Messages:
    95
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Search calculator on pixel2life .com there are quite a few in javascript, not sure if it's quite what you want though
     
    Phunk, Mar 19, 2006 IP