Студопедия

Главная страница Случайная страница

КАТЕГОРИИ:

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Calculator






Every programming tutorial includes av requisite calculator example. Here's one for RFO Basic:

! Start by assigning some initial variables, to be used later: prevVal = 0 curVal = 0 displayFlag = 0! Open the graphics screen and set initial display parameters: gr.open 255, 248, 248, 248gr.color 255, 0, 0, 0, 0gr.orientation 1gr.text.bold 1! Draw a rectangle on the screen to be used as the number display: gr.rect displayBox, 10, 10, 300, 50! Draw some text on the screen, which will display the numbers typed by! the user and the result of any calculation: gr.text.draw displayText, 20, 35, " 0"! This whole next section diplays the buttons on the screen. It would! have been a bit more straightforward to simply draw each rectangle and! text manually. Instead, an array holding the text and coordinates of! each button is first created, and then a FOR loop is used to position! each item on screen. This allows for easy repositioning and resizing! for different screen dimensions, and other future layout changes/! features:! This array holds the text and coordinates of each button: array.load buttonData$[], ~ " 1", " 30", " 60", " 2", " 90", " 60", " 3", " 150", " 60", ~ " 4", " 30", " 120", " 5", " 90", " 120", " 6", " 150", " 120", ~ " 7", " 30", " 180", " 8", " 90", " 180", " 9", " 150", " 180", ~ " 0", " 30", " 240", " +", " 90", " 240", " -", " 150", " 240", ~ " *", " 30", " 300", " /", " 90", " 300", " =", " 150", " 300"! This line sets the variable to the number of items in the above list.! This allows more buttons to be added later, without having to manually! edit any variables.: array.length numButtons, buttonData$[]! Create 2 new empty arrays that will hold references to each rectangle! and text item that makes up each graphical button: dim rectNames[numButtons]dim textNames[numbuttons]! Set some variables containing the size and horizontal/vertical offset of! all buttons (these values could easily by calculated as products of the! width and height of the screen): bs = 20 % button sizexshift = 65 % this moves all buttons over a given # of pixelsyshift = 40 % this moves all buttons down a given # of pixels! This loop creates each button by running though the buttonData$[] array! above, pulling out groups of 3 items from the list: for i = 1 to numButtons step 3! Set the left and top positions for each consecutive text item: lPos = val(buttonData$[i+1]) + xshift tPos = val(buttonData$[i+2]) + yshift! Draw a blue rectangle around the location of each text item.! Notice that the positions of the rectangle are based on the text! positions, the button size variable (bs), and ten extra pixels of! offset on the top and right sides, to accomodate the size of the! text (this offset could also be calculated automatically): gr.color 255, 0, 0, 255, 1 gr.rect rectNames[i], lPos-bs, tPos-bs-10, lPos+bs+10, tPos+bs! Draw the text item in white: gr.color 255, 255, 255, 255, 1 gr.text.draw textNames[i], lPos, tPos, buttonData$[i] next i! Set the color back to black and show the screen: gr.color 255, 0, 0, 0, 0gr.render! This endless loop waits for user input: getInput:! Wait for text input and save the coordinates in the variables " x"! and " y": do gr.touch touched, x, y until touched! This FOR loop runs through the buttonData$[] array every time the! user touches the screen, to check which button has been pressed,! and responds with appropriate action(s): for i = 1 to numbuttons step 3! Each consecutive time through the loop, the coordinates of each! button text are evaluated: lPos = val(buttonData$[i+1]) + xshift tPos = val(buttonData$[i+2]) + yshift! The location of the touch is compared with the location of each! button in the list: if x > (lpos-bs) & x < (lpos+bs+10) & ~ y > (tpos-bs-10) & y < (tpos+bs)! If the touched occured within the rectangle surrounding the! current button text, set the variable q$ to the matching! button's text: q$ = buttonData$[i]! Number buttons behave differently than operator buttons: if q$ = " 1" | q$ = " 2" | q$ = " 3" | q$ = " 4" | q$ = " 5" |~ q$ = " 6" | q$ = " 7" | q$ = " 8" | q$ = " 9" | q$ = " 0"! This flag is used to update the screen properly (the! display must be cleared if a total is currently! showing - this flag is used to track that state): if displayFlag = 1 gr.modify displayText, " text", display$ gr.render displayFlag = 0 endif! If a " 0" was on the screen, clear and update the! display: if display$ = " 0" display$ = " " gr.modify displayText, " text", display$ gr.render endif! Append the text of the selected button to the current! display text, and update the screen: display$ = display$ + buttonData$[i] gr.modify displayText, " text", display$ gr.render! Set the variable " curVal" to hold the numerical value! of the text currently displayed on screen: curVal = val(display$)! All of the operator keys perform the same way. They set! the variable " operator$" to the operation the key performs,! and then they run the " setEval" subroutine: elseif q$ = " +" operator$ = " +" gosub setEval elseif q$ = " -" operator$ = " -" gosub setEval elseif q$ = " *" operator$ = " *" gosub setEval elseif q$ = " /" operator$ = " /" gosub setEval! The equal key performs all the following actions: elseif q$ = " ="! Check to see if the displayFlag variable is in the! appropriate state (the total is not being displayed): if displayFlag = 0! If the user tries to divide by zero, pop up an error! message and exit the current loop: if operator$ = " /" & curVal = 0.0 popup " Division by 0 is not allowed.", 0, 0, 1 goto getInput endif! Perform the appropriate math evaluation, based upon! the current state of the operator$ variable (set! above, whenever the user clicks on an operator key): if operator$ = " +" curVal = curVal + prevVal elseif operator$ = " -" curVal = prevVal - curVal elseif operator$ = " *" curVal = curVal * prevVal elseif operator$ = " /" curVal = prevVal / curVal endif! Convert the updated " curVal" value to a string, and! update the display: display$ = str$(curVal) gr.modify displayText, " text", display$ gr.render! Set the displayFlag variable to indicate that a! total is currently being displayed on the screen: displayFlag = 1 endif endif endif next i! Now go back and wait for more user input, endlessly: goto getInput! This is the subroutine run when any of the operator keys are pressed: setEval:! Save the value currently on string in the variable " prevVal",! to be used later: prevVal = curVal! Clear the display text, and update the screen: display$ = " " gr.modify displayText, " text", display$ gr.render return! This last bit of code ends the program gracefully when the user clicks! the Android back button: onError: cls end

Here's the whole program without comments:

prevVal = 0 curVal = 0 displayFlag = 0 gr.open 255, 248, 248, 248gr.color 255, 0, 0, 0, 0gr.orientation 1gr.text.bold 1 gr.rect displayBox, 10, 10, 300, 50gr.text.draw displayText, 20, 35, " 0" array.load buttonData$[], ~ " 1", " 30", " 60", " 2", " 90", " 60", " 3", " 150", " 60", ~ " 4", " 30", " 120", " 5", " 90", " 120", " 6", " 150", " 120", ~ " 7", " 30", " 180", " 8", " 90", " 180", " 9", " 150", " 180", ~ " 0", " 30", " 240", " +", " 90", " 240", " -", " 150", " 240", ~ " *", " 30", " 300", " /", " 90", " 300", " =", " 150", " 300" array.length numButtons, buttonData$[] dim rectNames[numButtons]dim textNames[numbuttons]bs = 20 % button sizexshift = 65 % this moves all buttons over a given # of pixelsyshift = 40 % this moves all buttons down a given # of pixelsfor i = 1 to numButtons step 3 lPos = val(buttonData$[i+1]) + xshift tPos = val(buttonData$[i+2]) + yshift gr.color 255, 0, 0, 255, 1 gr.rect rectNames[i], lPos-bs, tPos-bs-10, lPos+bs+10, tPos+bs gr.color 255, 255, 255, 255, 1 gr.text.draw textNames[i], lPos, tPos, buttonData$[i]next i gr.color 255, 0, 0, 0, 0gr.render getInput: do gr.touch touched, x, y until touched for i = 1 to numbuttons step 3 lPos = val(buttonData$[i+1]) + xshift tPos = val(buttonData$[i+2]) + yshift if x > (lpos-bs) & x < (lpos+bs+10) & ~ y > (tpos-bs-10) & y < (tpos+bs) q$ = buttonData$[i] if q$ = " 1" | q$ = " 2" | q$ = " 3" | q$ = " 4" | q$ = " 5" |~ q$ = " 6" | q$ = " 7" | q$ = " 8" | q$ = " 9" | q$ = " 0" if displayFlag = 1 gr.modify displayText, " text", display$ gr.render displayFlag = 0 endif if display$ = " 0" display$ = " " gr.modify displayText, " text", display$ gr.render endif display$ = display$ + buttonData$[i] gr.modify displayText, " text", display$ gr.render curVal = val(display$) elseif q$ = " +" operator$ = " +" gosub setEval elseif q$ = " -" operator$ = " -" gosub setEval elseif q$ = " *" operator$ = " *" gosub setEval elseif q$ = " /" operator$ = " /" gosub setEval elseif q$ = " =" if displayFlag = 0 if operator$ = " /" & curVal = 0.0 popup " Division by 0 is not allowed.", 0, 0, 1 goto getInput endif if operator$ = " +" curVal = curVal + prevVal elseif operator$ = " -" curVal = prevVal - curVal elseif operator$ = " *" curVal = curVal * prevVal elseif operator$ = " /" curVal = prevVal / curVal endif display$ = str$(curVal) gr.modify displayText, " text", display$ gr.render displayFlag = 1 endif endif endif next igoto getInputsetEval: prevVal = curVal display$ = " " gr.modify displayText, " text", display$ gr.renderreturnonError: cls end

Snake

Ski


Поделиться с друзьями:

mylektsii.su - Мои Лекции - 2015-2024 год. (0.007 сек.)Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав Пожаловаться на материал