How to Make a Calculator

Материал из Поле цифровой дидактики

This tutorial explains how to make a calculator. Шаблон:Note

What is a calculator?

A calculator is a (usually small) machine used to calculate math problems.<ref>wikipedia:Calculator</ref>

What will be needed:

Variables:

  • Answerto!
  • Answerto!p2
  • Answerto^
  • Answerto^p2
  • Answers:
  • CurrentEquation:
  • Number:e
  • MTEPNumber
  • MTTPO
  • Numberbefore!
  • Numberbefore!p2
  • Pi
  • TTPO
  • TTPONum1
  • TTPONum2

Lists:

  • Answers1
  • Answers2
  • Answers3
  • Answers4
  • Calculating
  • Equations
  • Key
  • Makingtheequation

Custom blocks:

  • Calculate
  • MaketheEquation
  • Key

Code

In the Stage

The code in the Stage is the following:

whenclickedsetCurrentEquation:tosetAnswers:toshowlistEquations:showlistKey:showvariableAnswer:showvariableCurrentEquation:Key
defineKeydeleteallofKey:addUse"*"formultiplication.toKey:addUse"/"fordivision.toKey:addUse"-"forsubtraction.toKey:addUse"+"foraddition.toKey:addToeasilygetthepercentofanumber,putthepercentwithoutthepercentsign,toKey:addthenpressthecheckmark,thenput"%of",thenpressthecheckmark,andthenputthenumberyouwanttoKey:addthepercentof.toKey:adddonotusecommas.toKey:addRememberthatyouwillhavetoputinanumberthenpressthecheckmarkthentoKey:addputasymbolthenpressthecheckmarkandthenputanumber.toKey:addYoucanusedecimalsasonenumber.HereisanexampleofaequationwithatoKey:addnumberthathasadecimal:5395.58320349(clicksthecheckmark)*toKey:add(clicksthecheckmark)69483(clicksthecheckmark)thenyouwillreceivethetoKey:addanswertotheequation,theanswertothatoneis374901307.7toKey:addYoucanonlymakeequationsthatare3clicksofthecheckmark.toKey:addSo,ifyourequationislongerthenjustbreakdowntheequationintotoKey:addsmallersections.toKey:add"squarerootof"willcalculatethesquarerootofanumber.toKey:add"sinof"willcalculatethesinofanumber.toKey:add"cosof"willcalculatethecosofanumber.toKey:add"tanof"willcalculatethetanofanumber.toKey:add"asinof"willcalculatetheasinofanumber.toKey:add"acosof"willcalculatetheacosofanumber.toKey:add"atanof"willcalculatetheatanofanumber.toKey:add"absof"willcalculatetheabsofanumber.toKey:add"floorof"willcalculatethefloorofanumber.toKey:add"ceilingof"willcalculatetheceilingofanumber.toKey:add"lnof"willcalculatethelnofanumber.toKey:add"logof"willcalculatethelogofanumber.toKey:add"mod"willcalculatethemod.toKey:add"round"willroundanumbertoawholenumber.toKey:

The key could be put in the Instructions page of the project instead, if it is going to be shared.

In the Sprite

After the code contained in the stage, the next part of the code will be part of a Sprite. Once the sprite is made, then the code is as follows:

whenclickedgotox:0y:-140setdragmodenotdraggableshowdeleteallofCalculatingdeleteallofMakingtheEquation

Set Pi and Euler's number

whenclickedsetPito3.14159265358979323846264338327950288419716939937510ThisisPi.setNumber:eto2.71828182845904523536028747135266249775724709369995ThisisEuler'snumber.

whenthisspriteclickedhideMaketheEquation

Make the Equation

defineMaketheEquationsetMTEPNumber:to1repeatuntillengthofMakingtheEquation=3orMakingtheEquationcontains!?askjoinNewEquation:(pjoinMTEPNumber:)andwaitaddanswertoMakingtheEquation:changeMTEPNumberby1endiflengthofMakingtheEquation=3orMakingtheEquationcontains!?thenshowCalculateend

Calculating

defineCalculatesetCurrentEquation:toMakingtheEquationifMakingtheEquationcontainsans?thenreplaceitemitem#ofansinMakingtheEquationofMakingtheEquationwithAnswers:endifMakingtheEquationcontainsPi?orMakingtheEquationcontainsπ?thenreplaceitemitem#ofPiinMakingtheEquationoritem#ofπinMakingtheEquationofMakingtheEquationwithPiendifMakingtheEquationcontainse?orMakingtheEquationcontainsE?thenreplaceitemitem#ofeinMakingtheEquationoritem#ofEinMakingtheEquationofMakingtheEquationwithNumber:eendifMakingtheEquationcontains*?thenadditemitem#of*inMakingtheEquation-1ofMakingtheEquation*itemitem#of*inMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontains/?thenadditemitem#of/inMakingtheEquation-1ofMakingtheEquation/itemitem#of/inMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontains-?thenadditemitem#of-inMakingtheEquation-1ofMakingtheEquation-itemitem#of-inMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontains+?thenadditemitem#of+inMakingtheEquation-1ofMakingtheEquation+itemitem#of+inMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontains%of?thenadditemitem#of%ofinMakingtheEquation-1ofMakingtheEquation*itemitem#of%ofinMakingtheEquation+1ofMakingtheEquation/100toCalculatingendifMakingtheEquationcontains^?thensetTTPONum1toitemitem#of^inMakingtheEquation-1ofMakingtheEquationsetTTPONum2toitemitem#of^inMakingtheEquation+1ofMakingtheEquationsetMTTPOtoTTPONum2setTTPOtoTTPONum1ifTTPONum2=0thensetAnswerto^p2to1elserepeatuntilMTTPO=1setAnswerto^toTTPO*TTPONum1setAnswerto^p2toAnswerto^setTTPOtoAnswerto^changeMTTPOby0-1endendaddAnswerto^p2toCalculatingendifMakingtheEquationcontains!?thensetNumberbefore!toitemitem#of!inMakingtheEquation-1ofMakingtheEquationsetNumberbefore!p2toitemitem#of!inMakingtheEquation-1ofMakingtheEquationrepeatuntilNumberbefore!=1setAnswerto!toNumberbefore!p2*Numberbefore!-1setAnswerto!p2toNumberbefore!changeNumberbefore!by0-1changeNumberbefore!p2byAnswerto!endaddAnswerto!p2toCalculatingendifMakingtheEquationcontainssquarerootof?thenaddsqrtofitemitem#ofsquarerootofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainssinof?thenaddsinofitemitem#ofsinofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainscosof?thenaddcosofitemitem#ofcosofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainstanof?thenaddtanofitemitem#oftanofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsasinof?thenaddasinofitemitem#ofasinofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsacosof?thenaddacosofitemitem#ofacosofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsatanof?thenaddatanofitemitem#ofatanofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsabsof?thenaddabsofitemitem#ofabsofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsfloorof?thenaddfloorofitemitem#offloorofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsceilingof?thenaddceilingofitemitem#ofceilingofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainslnof?thenaddlnofitemitem#oflnofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainslogof?thenaddlogofitemitem#oflogofinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsmod?thenadditemitem#ofmodinMakingtheEquation-1ofMakingtheEquationmoditemitem#ofmodinMakingtheEquation+1ofMakingtheEquationtoCalculatingendifMakingtheEquationcontainsround?thenaddrounditemitem#ofroundinMakingtheEquation+1ofMakingtheEquationtoCalculatingendsetAnswer:toCalculatingaddjoinCurrentEquation:join=Answers:toEquations:ifnotAnswers1:=200000thenaddAnswers:toAnswers1:endifnotAnswers2:=200000andAnswers1:=200000thenaddAnswers:toAnswers2:endifnotAnswers3:=200000andAnswers1:=200000thenaddAnswers:toAnswers3:endifnotAnswers4:=200000andAnswers1:=200000thenaddAnswers:toAnswers4:enddeleteallofCalculatingdeleteallofMakingtheEquation

References