Jump to content

My Google Calculator


Paulie
 Share

Recommended Posts

There is a Yahoo Widget that does this and probably does it WAY better, but i figured i'd give it a stab.

#include <GUIConstants.au3>
#include <INet.au3>
#include <String.au3>
#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("Google Calculator", 400, 100,-1,-1,"",$WS_EX_TOOLWINDOW)
$Button1 = GUICtrlCreateButton("Calculate", 296, 8, 81, 25, 0)
$Input = GUICtrlCreateInput("", 8, 8, 273, 21)
$Result = GUICtrlCreateInput("", 8, 48, 375, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SearchCalc(GUICtrlRead($Input))
    EndSwitch
WEnd
Func _SearchCalc($Phrase)
    $Phrase=StringLower($Phrase)
                If Not StringInStr($Phrase,"=") then 
                      $Phrase &= "="
                EndIf 
    If StringInStr($Phrase,"%") then $Phrase = StringReplace($Phrase, "%", "%25")
    If StringInStr($Phrase,"+") then $Phrase = StringReplace($Phrase, "+", "%2B")
    If StringInStr($Phrase," ") then $Phrase = StringReplace($Phrase, " ", "+")
    If StringInStr($Phrase,"?") then $Phrase = StringReplace($Phrase, "?", "%3F")
    If StringInStr($Phrase,"/") then $Phrase = StringReplace($Phrase, "/", "%2F")
    If StringInStr($Phrase,"(") then $Phrase = StringReplace($Phrase, "(", "%28")
    If StringInStr($Phrase,")") then $Phrase = StringReplace($Phrase, ")", "%29")
    If StringInStr($Phrase,"=") then $Phrase = StringReplace($Phrase, "=", "%3D")
    If StringInStr($Phrase,"!") then $Phrase = StringReplace($Phrase, "!", "%21")
    If StringInStr($Phrase,"^") then $Phrase = StringReplace($Phrase, "^", "%5E")
    $Source = _INetGetSource("http://www.google.com/search?hl=en&q="&$Phrase)
    $Answer = _StringBetween($Source, "+1><b>","</b>")
    If IsArray($Answer) Then 
        If StringInStr($Answer[0], "<font size=-2> </font>") then $Answer[0]=StringReplace($Answer[0],"<font size=-2> </font>",",")
        If StringInStr($Answer[0], "× 10<sup>") then 
            $Answer[0]=StringReplace($Answer[0],"× 10<sup>","e"); replace x with "&# 215" (No spaces)
            $Answer[0]=StringReplace($Answer[0],"</sup>"," ")
        EndIf
        GUICtrlSetData($Result, $Answer[0])
    Else
        GUICtrlSetData($Result, "Invalid Query")
    EndIf
EndFunc

I have basically solved all of the problems I came across, please tell me if there are any odd answers, i'm not sure if this returns the answer every time..

Screenshot:

post-10893-1184620885_thumb.jpg

Edited by Paulie
Link to comment
Share on other sites

There is a Yahoo Widget that does this and probably does it WAY better, but i figured i'd give it a stab.

#include <GUIConstants.au3>
#include <INet.au3>
#include <String.au3>
#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("Google Calculator", 390, 85)
$Button1 = GUICtrlCreateButton("Calculate", 296, 8, 81, 25, 0)
$Input = GUICtrlCreateInput("", 8, 8, 273, 21)
$Result = GUICtrlCreateInput("", 8, 48, 375, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SearchCalc(GUICtrlRead($Input))
    EndSwitch
WEnd
Func _SearchCalc($Phrase)
    If StringInStr($Phrase,"%") then $Phrase = StringReplace($Phrase, "%", "%25")
    If StringInStr($Phrase,"+") then $Phrase = StringReplace($Phrase, "+", "%2B")
    If StringInStr($Phrase," ") then $Phrase = StringReplace($Phrase, " ", "+")
    If StringInStr($Phrase,"?") then $Phrase = StringReplace($Phrase, "?", "%3F")
    If StringInStr($Phrase,"/") then $Phrase = StringReplace($Phrase, "/", "%2F")
    If StringInStr($Phrase,"(") then $Phrase = StringReplace($Phrase, "(", "%28")
    If StringInStr($Phrase,")") then $Phrase = StringReplace($Phrase, ")", "%29")
    If StringInStr($Phrase,"=") then $Phrase = StringReplace($Phrase, "=", "%3D")
    If StringInStr($Phrase,"!") then $Phrase = StringReplace($Phrase, "!", "%21")
    If StringInStr($Phrase,"^") then $Phrase = StringReplace($Phrase, "^", "%5E")
    $Source = _INetGetSource("http://www.google.com/search?hl=en&q="&$Phrase)
    $Answer = _StringBetween($Source, "+1><b>","</b>")
    If IsArray($Answer) Then 
        If StringInStr($Answer[0], "<font size=-2> </font>") then $Answer[0]=StringReplace($Answer[0],"<font size=-2> </font>","")
        If StringInStr($Answer[0], "× 10<sup>") then 
            $Answer[0]=StringReplace($Answer[0],"× 10<sup>","e")
            $Answer[0]=StringReplace($Answer[0],"</sup>"," ")
        EndIf
        GUICtrlSetData($Result, $Answer[0])
    Else
        GUICtrlSetData($Result, "Invalid Query")
    EndIf
EndFunc

I have basically solved all of the problems I came across, please tell me if there are any odd answers, i'm not sure if this returns the answer every time..

One thing i noticed if you put blank string your program will go respond.
Link to comment
Share on other sites

Maybe someting wrong with AutoIt, now it works fine.

I didn't know you had written this and I did a similar thing herefor a google calc

Since you did it first I will put a note on my post.

You've set more options than I did, but I've found that you need to convert letters to lower case or the calculation can fail. Eg Sqrt(4) doesn't work but sqrt(4) does.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thanks martin,

I fixed the lowercase thing, thanks for pointing it out

I'm using the $WS_EX_TOOLWINDOW style does anyone know how to use that style, but keep the close button in the corner?

Edited by Paulie
Link to comment
Share on other sites

Very neat I mentioned wanting to do something like this in another thread but didnt know exactly how to accomplish it. Thanks Paulie!

Anyone know where I might be able to find more information on #Region Its the only part I don't get and I dont see anything about it in the help file.

AutoIt changed my life.

Link to comment
Share on other sites

Very neat I mentioned wanting to do something like this in another thread but didnt know exactly how to accomplish it. Thanks Paulie!

Anyone know where I might be able to find more information on #Region Its the only part I don't get and I dont see anything about it in the help file.

It's just a comment but it can be used for code folding. If you have a section of code which is, say, setting all the Global variables,then you could write

#region -global variables

all the declarations here

#endregion -global variables

Then in an editor like Scite you can click on the little box with a - sign in it at the start of the #region line and the code down to the #endregion will be hidden. This way you can hide boring bits of code or code that you're not intersted in at the moment and it's easier to read.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

So how is it different then #cs #ce?

Code between #region and #endregion isn't ignored it just marks a section. Because of this you can nest #regions.

Code after #cs is ignored until #ce is found so you couldn't nest commented areas.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...