Computer Science Canada

Jeremiah's Calculator

Author:  Jeremiah [ Sun Jan 29, 2006 3:27 pm ]
Post subject:  Jeremiah's Calculator

hey all this is my first real program. im a grade 11 student in comp engineering, my first computer class, and this is my simple calculator. i know it culd be done much better but im pretty proud of my first program Very Happy
PS: if you know how i culd interface this with a home made joystick/ fire button, please help me; i have to do it and hand it in tomorrow

code:
View.Set ("graphics:201;251,nobuttonbar,position:center;center")

%variables
var xmouse, ymouse, button : int
var num1, num2 : string := ""
var op : string := ""
var answer : int := 0
var font : int

%calculator look
drawfillbox (0, 200, 200, 250, yellow)
drawfillbox (0, 0, 200, 200, grey)
drawfillbox (0, 0, 50, 50, white)
drawbox (0,0,50,50,black)
drawbox (50, 0, 100, 50, black)
drawbox (100, 0, 150, 50, black)
drawbox (150, 0, 200, 50, black)
drawbox (0, 50, 50, 100, black)
drawbox (50, 50, 100, 100, black)
drawbox (100, 50, 150, 100, black)
drawbox (150, 50, 200, 100, black)
drawbox (0, 100, 50, 150, black)
drawbox (50, 100, 100, 150, black)
drawbox (100, 100, 150, 150, black)
drawbox (150, 100, 200, 150, black)
drawbox (0, 150, 50, 200, black)
drawbox (50, 150, 100, 200, black)
drawbox (100, 150, 150, 200, black)
drawbox (150, 150, 200, 200, black)
drawbox (0, 200, 200, 250, black)

%Calculator numbers
font := Font.New ("arial:18")
Font.Draw ("ESC", 0, 20, font, black)
Font.Draw ("0", 70, 20, font, black)
Font.Draw ("=", 120, 20, font, black)
Font.Draw ("/", 170, 20, font, black)
Font.Draw ("1", 20, 70, font, black)
Font.Draw ("2", 70, 70, font, black)
Font.Draw ("3", 120, 70, font, black)
Font.Draw ("*", 170, 70, font, black)
Font.Draw ("4", 20, 120, font, black)
Font.Draw ("5", 70, 120, font, black)
Font.Draw ("6", 120, 120, font, black)
Font.Draw ("-", 170, 120, font, black)
Font.Draw ("7", 20, 170, font, black)
Font.Draw ("8", 70, 170, font, black)
Font.Draw ("9", 120, 170, font, black)
Font.Draw ("+", 170, 170, font, black)

%Input
loop
    loop
        buttonwait ("down", xmouse, ymouse, button, button)

        exit when xmouse > 0 and xmouse < 50 and ymouse > 0 and ymouse < 50

        if xmouse > 50 and xmouse < 100 and ymouse > 0 and ymouse < 50 then
            num1 := num1 + "0"
        elsif xmouse > 0 and xmouse < 50 and ymouse > 50 and ymouse < 100 then
            num1 := num1 + "1"
        elsif xmouse > 50 and xmouse < 100 and ymouse > 50 and ymouse < 100 then
            num1 := num1 + "2"
        elsif xmouse > 100 and xmouse < 150 and ymouse > 50 and ymouse < 100 then
            num1 := num1 + "3"
        elsif xmouse > 0 and xmouse < 50 and ymouse > 100 and ymouse < 150 then
            num1 := num1 + "4"
        elsif xmouse > 50 and xmouse < 100 and ymouse > 100 and ymouse < 150 then
            num1 := num1 + "5"
        elsif xmouse > 100 and xmouse < 150 and ymouse > 100 and ymouse < 150 then
            num1 := num1 + "6"
        elsif xmouse > 0 and xmouse < 50 and ymouse > 150 and ymouse < 200 then
            num1 := num1 + "7"
        elsif xmouse > 50 and xmouse < 100 and ymouse > 150 and ymouse < 200 then
            num1 := num1 + "8"
        elsif xmouse > 100 and xmouse < 150 and ymouse > 150 and ymouse < 200 then
            num1 := num1 + "9"
        elsif xmouse > 150 and xmouse < 200 and ymouse > 0 and ymouse < 50 then
            op := "/"
            exit
        elsif xmouse > 150 and xmouse < 200 and ymouse > 50 and ymouse < 100 then
            op := "*"
            exit
        elsif xmouse > 150 and xmouse < 200 and ymouse > 100 and ymouse < 150 then
            op := "-"
            exit
        elsif xmouse > 150 and xmouse < 200 and ymouse > 150 and ymouse < 200 then
            op := "+"
            exit
        end if

        drawfillbox (0, 200, 200, 250, yellow)

        Font.Draw (num1, 10, 210, font, black)
    end loop

    exit when xmouse > 0 and xmouse < 50 and ymouse > 0 and ymouse < 50

    loop
        buttonwait ("down", xmouse, ymouse, button, button)

        exit when xmouse > 0 and xmouse < 50 and ymouse > 0 and ymouse < 50

        if xmouse > 50 and xmouse < 100 and ymouse > 0 and ymouse < 50 then
            num2 := num2 + "0"
        elsif xmouse > 0 and xmouse < 50 and ymouse > 50 and ymouse < 100 then
            num2 := num2 + "1"
        elsif xmouse > 50 and xmouse < 100 and ymouse > 50 and ymouse < 100 then
            num2 := num2 + "2"
        elsif xmouse > 100 and xmouse < 150 and ymouse > 50 and ymouse < 100 then
            num2 := num2 + "3"
        elsif xmouse > 0 and xmouse < 50 and ymouse > 100 and ymouse < 150 then
            num2 := num2 + "4"
        elsif xmouse > 50 and xmouse < 100 and ymouse > 100 and ymouse < 150 then
            num2 := num2 + "5"
        elsif xmouse > 100 and xmouse < 150 and ymouse > 100 and ymouse < 150 then
            num2 := num2 + "6"
        elsif xmouse > 0 and xmouse < 50 and ymouse > 150 and ymouse < 200 then
            num2 := num2 + "7"
        elsif xmouse > 50 and xmouse < 100 and ymouse > 150 and ymouse < 200 then
            num2 := num2 + "8"
        elsif xmouse > 100 and xmouse < 150 and ymouse > 150 and ymouse < 200 then
            num2 := num2 + "9"
        elsif xmouse > 150 and xmouse < 200 and ymouse > 0 and ymouse < 50 then
            exit
        elsif xmouse > 150 and xmouse < 200 and ymouse > 50 and ymouse < 100 then
            exit
        elsif xmouse > 150 and xmouse < 200 and ymouse > 100 and ymouse < 150 then
            exit
        elsif xmouse > 150 and xmouse < 200 and ymouse > 150 and ymouse < 200 then
            exit
        elsif xmouse > 100 and xmouse < 150 and ymouse > 0 and ymouse < 50 then
            exit
        end if

        drawfillbox (0, 200, 200, 250, yellow)

        Font.Draw (num2, 10, 210, font, black)
    end loop

    exit when xmouse > 0 and xmouse < 50 and ymouse > 0 and ymouse < 50

%Process
    if op = "+" then
        answer := strint (num1) + strint (num2)
    elsif op = "-" then
        answer := strint (num1) - strint (num2)
    elsif op = "*" then
        answer := strint (num1) * strint (num2)
    elsif op = "/" then
        answer := strint (num1) div strint (num2)
    end if

    drawfillbox (0, 200, 200, 250, yellow)

%Final Output
    Font.Draw (intstr (answer), 10, 210, font, black)

    num1 := ""
    num2 := ""
    op := ""
    answer := 0
end loop


Author:  Delos [ Sun Jan 29, 2006 4:55 pm ]
Post subject: 

Not bad for a first attempt. I've seen worse, far worse.

You do have quite a ways to go, mind you, but indented! I like the way you used 'div' instead of '/' to avoid strint() errors. However, it is a bit misleading to see a '/' and not get a division occuring.
Try use reals instead of ints, you won't have to worry as much.

It's still very buggy. For instance, clicking repeatedly on any of the operations crashes it. Why would someone do that? Because that's what people do...

Here's a challenge:
Remake this programme, but using the keyboard as input instead. Sounds easy, but a well proofed version will keep you occupied for quite some time.

+bits for the effort.

Author:  Jeremiah [ Sun Jan 29, 2006 10:15 pm ]
Post subject: 

thanx man, this is the first program with a purpose i ever made lol, so im glad it isnt completely a disaster.

Author:  impact2cool [ Mon Jan 12, 2009 8:26 pm ]
Post subject:  RE:Jeremiah\'s Calculator

thnx man
hey insted of using strint what if u used strreal

Author:  andrew. [ Mon Jan 12, 2009 8:48 pm ]
Post subject:  RE:Jeremiah\'s Calculator

strreal would work, but you obviously have to define the variable as real instead of int.

e.g. var number : real

Author:  syntax_error [ Tue Jan 13, 2009 2:36 am ]
Post subject:  RE:Jeremiah\'s Calculator

Look into for loops for parts of your code that tend to repeat themselves in an sequencial manner. Such as the drawing of the cal you have done.

Author:  Nick [ Tue Jan 13, 2009 7:12 am ]
Post subject:  RE:Jeremiah\'s Calculator

suggestions are great and all but this is 3 years old and the OP has 7 total posts... I doubt here's still around to hear the criticism

Author:  evilisgood [ Wed May 13, 2009 10:59 am ]
Post subject:  RE:Jeremiah\'s Calculator

how do you make the buttons work for the calculator?

Author:  DanTheMan [ Fri May 15, 2009 8:50 am ]
Post subject:  Re: RE:Jeremiah\'s Calculator

evilisgood @ Wed May 13, 2009 10:59 am wrote:
how do you make the buttons work for the calculator?


http://compsci.ca/v3/viewtopic.php?t=3583

Author:  Wajih [ Fri Jan 14, 2011 10:01 pm ]
Post subject:  RE:Jeremiah\'s Calculator

hey guys i know i am very late, but jeremiah and/ or others : can you please explain what you did to get 2+ digit numbers? mine can only do single digit operations. i do not know how to make it a double, triple or higher digit calculator

Author:  Insectoid [ Fri Jan 14, 2011 10:31 pm ]
Post subject:  RE:Jeremiah\'s Calculator

Just keep a running total of the current value. Always use that as the 'left' value in a calculation and update it as the program executes.

Author:  Wajih [ Fri Jan 14, 2011 11:26 pm ]
Post subject:  RE:Jeremiah\'s Calculator

but my program is a bit different. there are tow sets of numbers. the numbers on the left side represent num, while the numbers 0n the right side represent num2. if you want to creata total for each time a number has been entered, would you have t create a new variable and loop it?


: