Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help Gui. Calculator
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Evilkilla




PostPosted: Sat May 29, 2004 1:31 pm   Post subject: Help Gui. Calculator

Hey guys i need some help
ok i am trying to make a calculator with GUI using functions and strreal
i am trying to find out how, too...... use the = button to output, as in say i use my variable adder to hold the strings while i use strreal and put it in num1 somehow using a if statement or...... i am not sure, any pointers would be nice thanks. to output say 1 + 1 = 2 thanks
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

code:


import GUI

var adder,Ea:string:=""
var num1,num2:real:=0
var num1,num2:real

%function to add
function add (num1, num2 : real) : real
    result num1 + num2
end add

%function to subtract
function subtract (num1, num2 : real) : real
    result num1 - num2
end subtract

%function to times
function times (num1, num2 : real) : real
    result num1 * num2
end times

%function to divide
function divide (num1, num2 : real) : real
    result num1 / num2
end divide

procedure b1

put "1"..
adder:=adder+"1"

end b1

procedure b2

put "2"..
adder:=adder+"2"
end b2

procedure b3

put "3"..
adder:=adder+"3"
end b3

procedure b4

put "4"..
adder:=adder+"4"
end b4

procedure b5

put "5" ..
adder:=adder+"5"
end b5

procedure b6

put "6" ..
adder:=adder+"6"
end b6

procedure b7

put "7" ..
adder:=adder+"7"
end b7

procedure b8

put "8" ..
adder:=adder+"8"
end b8

procedure b9
 
put "9" ..
adder:=adder+"9"
end b9

procedure b10

put "/" ..
Ea:=Ea+"/"
end b10

procedure b11

put "X" ..
Ea:=Ea+"*"
end b11

procedure b12

put "+" ..
Ea:=Ea+"+"
end b12

procedure b13

put "-" ..
Ea:=Ea+"-"
end b13

procedure b14

put "0" ..
Ea:=Ea+"0"
end b14

procedure b15

put "=" ..
Ea:=Ea+"="
put adder
end b15

procedure b16

put "+/-" ..
Ea:=Ea+"l"
end b16


loop
var button1 : int := GUI.CreateButton (10, 100, 60, "1", b1)
var button2 : int := GUI.CreateButton (80, 100, 60, "2", b2)
var button3 : int := GUI.CreateButton (150, 100, 60, "3", b3)
var button4 : int := GUI.CreateButton (10, 70, 60, "4", b4)
var button5 : int := GUI.CreateButton (80, 70, 60, "5", b5)
var button6 : int := GUI.CreateButton (150, 70, 60, "6", b6)
var button7 : int := GUI.CreateButton (10, 40, 60, "7", b7)
var button8 : int := GUI.CreateButton (80, 40, 60, "8", b8)
var button9 : int := GUI.CreateButton (150, 40, 60, "9", b9)
var button10 : int := GUI.CreateButton (220, 100, 60, "/", b10)
var button11 : int := GUI.CreateButton (220, 70, 60, "X", b11)
var button12 : int := GUI.CreateButton (220, 40, 60, "+", b12)
var button13 : int := GUI.CreateButton (220, 10, 60, "-", b13)
var button14 : int := GUI.CreateButton (80, 10, 60, "0", b14)
var button15 : int := GUI.CreateButton (150, 10, 60, "=", b15)
var button16 : int := GUI.CreateButton (10, 10, 60, "+/-", b16)


var quitBtn : int := GUI.CreateButton (290, 10, 60, "Quit", GUI.Quit)
     

exit when GUI.ProcessEvent


end loop
Sponsor
Sponsor
Sponsor
sponsor
guruguru




PostPosted: Sat May 29, 2004 3:43 pm   Post subject: (No subject)

You have to make 'adder' a integer, nd then when a button is clicked, you add that button's number value to 'adder'. When you press equal, you display adder.

Then you have to include divide and such. In your button procedures, you should if statements to see if divide or whatever has been pressed, and then change adder accordingly by the next number pressed.
Evilkilla




PostPosted: Sat May 29, 2004 3:58 pm   Post subject: (No subject)

huh.... but i have to use Adder with is a string to store the buttons pressed and the numbers and then convert it with strreal to a real number.. i am stuck at there... i dunno what to do next help plz thanks
like below to store the numbers pressed.
code:
num1:= strreal (adder)
guruguru




PostPosted: Sat May 29, 2004 6:33 pm   Post subject: (No subject)

But thats my point... you should make adder an integer. Its easier to do everything. You can do all calculations at any point in the program, not just at the end where you convert it.
Evilkilla




PostPosted: Sat May 29, 2004 6:59 pm   Post subject: (No subject)

could you gimme an example i dun understand
guruguru




PostPosted: Sat May 29, 2004 7:08 pm   Post subject: (No subject)

code:

var num1 : int
var num2 : int
var currentNum : int

proc b4
    put "4 " ..
    if currentNum := 1 then
        num1 := 4
    elsif currentNum := 2 then
        num2 := 4
    end if
    currentPress += 1   
end b1

proc b2
    put "2 " ..
    if currentNum := 1 then
        num1 := 2
    elsif currentNum := 2 then
        num2 := 2
    end if
    currentPress += 1   
end b1

proc b10
    put "/ ", num1 / num2
end b10

proc b12
    put "+ ", num1 + num2
end b12

% All your button stuff here
Evilkilla




PostPosted: Sat May 29, 2004 7:51 pm   Post subject: (No subject)

i tried, it says must be boolean type Sad
p.s your end procedures are wrong end b1 for b4
guruguru




PostPosted: Sat May 29, 2004 9:28 pm   Post subject: (No subject)

Sorry, the error is my mistake. In the if statements, any that say ":=" replace with "=".

Quote:
p.s your end procedures are wrong end b1 for b4

I cannot for the life of me understand what you just said.
Sponsor
Sponsor
Sponsor
sponsor
Evilkilla




PostPosted: Sat May 29, 2004 10:02 pm   Post subject: (No subject)

ahhh sorry

Quote:
proc b4
put "4 " ..
if currentNum := 1 then
num1 := 4
elsif currentNum := 2 then
num2 := 4
end if
currentPress += 1
end b1

proc b2
put "2 " ..
if currentNum := 1 then
num1 := 2
elsif currentNum := 2 then
num2 := 2
end if
currentPress += 1
end b1


see procedure b2, end is b1
same with b4
Evilkilla




PostPosted: Sat May 29, 2004 10:09 pm   Post subject: (No subject)

also Currentnum and CurrentPress is not declared what should it be... and what is it used for.............program doesn't run,
guruguru




PostPosted: Sat May 29, 2004 10:22 pm   Post subject: (No subject)

I made a slight modification so it works. I made a operation variable that tells what operation to perform. Kinda self-explanitory. If you have questions please ask.

code:

import GUI

var num1 : int
var num2 : int
var currentNum : int := 1
var operation : string

proc b4
    put "4 " ..
    if currentNum = 1 then
        num1 := 4
    elsif currentNum = 2 then
        num2 := 4
    end if
    currentNum += 1
end b4

proc b2
    put "2 " ..
    if currentNum = 1 then
        num1 := 2
    elsif currentNum = 2 then
        num2 := 2
    end if
    currentNum += 1
end b2

proc b10
    operation := "divide"
    put "/ " ..
end b10

proc b12
    operation := "add"
    put "+ " ..
end b12

proc b15
    put "= " ..
    if operation = "add" then
        put num1 + num2
    elsif operation = "divide" then
        put num1 / num2
    end if
end b15

var button2 : int := GUI.CreateButton (80, 100, 60, "2", b2)
var button4 : int := GUI.CreateButton (10, 70, 60, "4", b4)
var button10 : int := GUI.CreateButton (220, 100, 60, "/", b10)
var button12 : int := GUI.CreateButton (220, 40, 60, "+", b12)
var button15 : int := GUI.CreateButton (150, 10, 60, "=", b15)

loop     % loop should start here and not above
    exit when GUI.ProcessEvent
end loop


PS: You can add in rest of buttons and procs.
Evilkilla




PostPosted: Sat May 29, 2004 11:29 pm   Post subject: (No subject)

Thanks man you helped me out alot!, i will post finished product for you guys when i am done and with you for credit!!
guruguru




PostPosted: Sun May 30, 2004 12:05 am   Post subject: (No subject)

Sweet Very Happy ! I wanna see the finished product 8) .
Evilkilla




PostPosted: Wed Jun 02, 2004 8:29 pm   Post subject: (No subject)

ahhhh i need help again, each person who helps with legit info gets 5 bits from me, first come first serve. Guru imma give you 5. So first 2 gets the other bits. Well heres the problem now.. I can't use more than one operator. and numbers.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: