Advanced Calculator

Advanced Calculator

I have posted a "Basic Calculator" tutorial here. That was more like a representation of how you would calculate with a paper and a pencil. You provide INPUT A then a MATHEMATICAL OPERATION like a "+" or a "-" and then an INPUT B.

This is more a representation of how you would use a regular hand-held calculator complete with buttons for NUMBERS, OPERATIONS and CLEAR TEXT.


<!--- As usual, declare all your variables here. "calc_txt" is for variables coming in from previous form, "calc_btn" are variables coming in from pressing those buttons and "resolve" is the "equal to" command variable
Sorry, I have a wierd naming convention
--->

<cfparam name="calc_txt" default="">
<cfparam name=
"calc_btn" default="">
<cfparam name=
"resolve" default="">

<!--- Remember........ there are people out there who would still expect to see a result when they divide by zero.
Thus the "cftry" tag and "cfcatch" tag can handle this error --->


<cftry>
    <!--- code cfform that redirects to itself --->
    <cfform action="calc.cfm" method="POST" enablecab="Yes">

    <!--- declare conditional variables. --->
    <cfset expression="#calc_txt##calc_btn#">
    <cfif resolve eq
"C">
        <cfset expression=
"">
    <cfelseif resolve eq
"=">
        <cfset expression=
"#evaluate(expression)#">
    </cfif>


    <!--- This is where the fun begins. --->

    <table border="1">
        <tr>
            <td colspan=
"2" align="center">
                <cfinput type="Text" name="calc_txt" value="#expression#" required="No">
            </td>
        </tr>
        <tr>
            <td>

            <!--- Because the #expression# was set-up as the conditional variable, it will either return a string or nothing--->
            <!--- __________________________________________________________________________________--->

   
            <table border="1">
                <tr>

                    <!--- Use this loop to create characters from 0 to 9 --->
                    <cfloop index="calc_btn" from="48" to="57">
                    <cfset chr_btn=
"#chr(calc_btn)#">
                    <!--- this is a just a formatting condition. if 0, button width used by the stylesheet is 85px, else use 25px. --->
                    <cfif chr_btn eq '0'>
                        <cfset wide=
"85px">
                        <cfset col_spn=
"3">
                    <cfelse>
                        <cfset wide=
"25px">
                        <cfset col_spn=
"1">
                    </cfif>

                    <td colspan="<cfoutput>#col_spn#</cfoutput>" align="center">
                        <input type="submit" name="calc_btn" value="<cfoutput>#chr_btn#</cfoutput>" style="width:<cfoutput>#wide#</cfoutput>;height;25px;">
                        <!--- Again just a formatting condition within a cell. If there are 3 buttons, go to next row --->
        <cfif calc_btn mod 3 eq 0>
                </td>
            </tr>

        </cfif>
        </td>
        </cfloop>
        <!--- __________________________________________________________________________________--->
   
        <!--- __________________________________________________________________________________--->

        </tr>
        </table>
        </td>
        <td>
            <table>
                <tr>

                    <!--- This is the second loop to create characters like + - * / squares, roots, etc. all these charates fall within the range chr(40) to chr(47). Of course a , (comma) fall under this range too. So if we come across a comma use ^ (exponential or exp) instead. Simple ? --->
                    <cfloop index="calc_btn" from="40" to="47">
                        <cfset chr_btn=
"#chr(calc_btn)#">
                        <cfif chr_btn eq
','>
                            <cfset chr_btn=
"^">
                        </cfif>

                        <td>
                            <!--- Again the same formatting IF statements. if we have two buttons in a row, use second row. --->
                            <input type="submit" name="calc_btn" value="<cfoutput>#chr_btn#</cfoutput>" style="width:25px;height;25px;">
                    <cfif calc_btn mod 2 neq 0>
                        </td>
                    </tr>

                    </cfif>
                </td>
                </cfloop>
                <!--- __________________________________________________________________________________--->
           
            </tr>
        </table>

        <!--- __________________________________________________________________________________--->
        </td>
    </tr>
    <tr>

    <!--- Once we have an expression ready, we need to resolve it. So you can either ask for a result or "cacnel it". If you cancel it, it will clear the text box above. If you ask for a result, it will 'try' to evaluate the expression --->
        <td colspan="2" align="center">
            <input type="submit" name="resolve" value="=" style="width:75px;">
            <input type=
"submit" name="resolve" value="C" style="width:75px;">
        </td>
    </tr>
</table>

</cfform>
<!--- __________________________________________________________________________________--->
<cfcatch type="Any">
    <!--- and ofcourse if you ask it to do something wierd, you can relocate the user to a blank calculator for. You could also direct your user to a page that says "are u crazy, didn't you go to school. Don't you know you cannot divide by zero."--->

    <cflocation url="calc.cfm">
</cfcatch>
</cftry>

<!--- End all tags here --->
<!--- __________________________________________________________________________________--->
 



All ColdFusion Tutorials By Author: Anang A Phatak
  • A DataSet just like VB.Net
    This tutorial shows you how to create a "dataset" just like the one in VB.Net In VB.Net you would create a dataset with "edit" button in an extra column. Once you click "edit", you get an option to "update", "delete" or "cancel edit mode" This is just like a cfgrid tag. Although a cfgrid tag lets you bulk insert, bulk update or bulk delete, the dataset object does it one by one. But cfgrid is slower, and may give users Java errors, depending on their browser settings.
    Author: Anang A Phatak
    Views: 18,417
    Posted Date: Wednesday, November 17, 2004
  • A Mp3 Streaming Server
    This is a small application that shows you how to create an MP3 streaming server.
    Author: Anang A Phatak
    Views: 14,865
    Posted Date: Monday, November 8, 2004
  • A plot to plot a line
    I had no work one day due to a worm attack on our servers, thus a plot to plot a line on a graph was hatched in my empty mind. These files show you, how to plot a line using no database, no java, no long wait times for aplet loading, just 3 tools, Loop, table and text.
    Author: Anang A Phatak
    Views: 10,505
    Posted Date: Thursday, May 6, 2004
  • A random password generator
    RANDOM PASSWORD GENERATOR SCRIPT ! I know there is a random password tutorial here already. This is just another way to do the same. I think this is a little easier to understand. Refresh it to generate a new password string everytime !
    Author: Anang A Phatak
    Views: 12,528
    Posted Date: Monday, May 24, 2004
  • Advanced Calculator
    I have posted a "Basic Calculator" tutorial here. That was more like a representation of how you would calculate with a paper and a pencil. You provide INPUT A then a MATHEMATICAL OPERATION like a "+" or a "-" and then an INPUT B. This is more a represntation of how you would use a regular hand-held calculator complete with buttons for NUMBERS, OPERATIONS and CLEAR TEXT.
    Author: Anang A Phatak
    Views: 11,082
    Posted Date: Friday, June 18, 2004
  • Automatic Form Generator
    This is not a tutorial as such, more like an application that you can put in a directory. It could boring if you use the CF editor, to pick "cfform" fill its attributes, then pick "cfinputs" one by one. fill out those attributes... one by one, then change tabs and pick the "submit" button... so on and so forth. Even if you code in a note pad, it might get lengthy to code individual element. Wouldn't it be nicer to code all these elements at once, then just copy the code and paste it in your editor?
    Author: Anang A Phatak
    Views: 11,645
    Posted Date: Thursday, May 20, 2004
  • Automatically Query To CFM
    This is a custom tag application. The cf_QueryRender custom tag takes your query arguments and gives you a final page table and all...
    Author: Anang A Phatak
    Views: 13,740
    Posted Date: Friday, October 29, 2004
  • Breaking down your query results into pages (Paging Tutorial)
    I havent come across a "paging" tutorial on this site. I know there are JavaScripts available that help you achieve this, and the DataSet object in VB.Net comes with paging. All you do is "enable paging". But how do you do it in ColdFusion ?
    Author: Anang A Phatak
    Views: 18,131
    Posted Date: Tuesday, November 16, 2004
  • Breaking down your query results into pages (Paging Tutorial) Part-II
    This is an extension to my last tutorial "Breaking down your query results into pages (Paging Tutorial)" which is posted here on www.easycfm.com In the last tutorial, you could retrieve a dataset with a , then use a technique to seperate the results over several pages. It simply ; - took the total "recordCount" - divided that with the "number of records per page" - then displayed the number of pages at the bottom of the table. This is a little more sophisticated than that. Read on...
    Author: Anang A Phatak
    Views: 10,135
    Posted Date: Wednesday, January 11, 2006
  • ColdFusion MX 6.1 Installation on Linux (Ubuntu -- Hoary Hedgehog)
    I have tried hoards of websites on how to install coldfusion on Fedora Core 3 with apache webserver. For some reason the connectors always failed. I had "Ubuntu" on my laptop, basically because "acpi" suspend/hibernate actually works. I decided I might try to install CF there to find out what was going wrong. Surprisingly everything worked like a charm. Make sure you use "apt-get install apache2" before you try this. BEST OF LUCK ....
    Author: Anang A Phatak
    Views: 12,802
    Posted Date: Tuesday, May 10, 2005
  • Dynamic textbox and progress bar for your pages
    The principle of this tutorial is similar to "Dynamic time and date for your pages" tutorial. Except that this one generates messages, and that one updated time. Read on, you will get the hang of it....
    Author: Anang A Phatak
    Views: 14,652
    Posted Date: Thursday, May 19, 2005
  • Dynamic time and date for your pages
    Have you seen the "www.EasyCFM" page closely? On the main page, top right, there is a place for time, and top left a place for day-date. Ever wonder how Pablo does it ? This is not a ColdFusion tutorial. Its JavaScript.
    Author: Anang A Phatak
    Views: 12,034
    Posted Date: Wednesday, January 5, 2005
  • Getting ColdFusion Studio for Linux
    I like using HomeSite+ for windows, and I am getting used to Dreamweaver Mx. But I still need something just as good for Linux. For some reason, I couldnt get "wine and Dreamweaver Mx" to work. So I "Googled" a bit and stumbled upon Eclipse and cfEclipse. Here is how to set it up.
    Author: Anang A Phatak
    Views: 12,903
    Posted Date: Wednesday, May 25, 2005