简易计算器程序中数字和小数点输入的处理思路

2016年03月10日 其他资源 9条评论 阅读7202次

简易计算器程序中数字和小数点输入的处理思路

简易计算器程序设计思路及流程图,上一篇文章已经说了一些了,今天记录一下关于小数的处理。

  回顾上一篇文章简易计算器程序设计思路及流程图


本次说的数字和小数点输入的处理思路,化繁为简,分为两个分支事件响应。

Case1 . 小数点(.)的处理事件

关键在于判断当前输入数中是否已经包含了小数点

取得屏幕输入的字符串,使用indexOf方法得到小数点(.)的位置

  • 得到小于0的返回值,则说明现在已经输入的数字中还没有小数点,直接在元字符串后拼接一个小数点(.)即可
  • 得到大于等于0的返回值,则说明现在已经输入的数字中已经小数点了,直接返回,无需再做多余的处理。

Case2. 数字(0-9)的处理事件

还是先判断当前已输入数中是否包含小数点取得屏幕输入的字符串,使用indexOf方法得到小数点(.)的位置

  • 得到小于0的返回值,则说明没有小数点,结果为:原数据 * 10 + 新输入数据
  • 得到大于等于0的返回值,则说明已经有小数点了,结果为:原数据 + 新输入数据 / (小数点后的位数 + 1) 其中小数点后的位数可以通过元字符串长度和indexOf返回值得到

简易计算器中关于小数点输入的测试用例

  1. 输入: 1.5

    期望得到结果为1.5

  2. 输入:.5

    期望得到结果为0.5

  3. 输入:1......5

    期望得到结果为1.5

  4. 输入:......5

    期望得到结果为0.5

  5. 输入:1...-...5

    期望得到结果为-1.5

  6. 输入:15..

    期望得到结果为15

分享本文至:

WRITTEN BY

avatar
本文标签:calculatordecimalpoint
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

9 条评论

  1. avatar batemanafrica

    <a href="http://www.nhhairdesign.com/nike-roshe-two-marron-obuvb">nike roshe two marron</a> <a href="http://www.struysthuis.com/mujeres-nike-free-flyknit-3.0-rojo-azul-chaussuresc">mujeres nike free flyknit 3.0 rojo azul</a> <a href="http://www.golfcavedave.com/nike-air-huarache-iii-gul-rosa-kurpesg">nike air huarache iii gul rosa</a> <a href="http://www.yfhfoundation.com/nike-shox-basketbtutti-scarpe-for-vendita-schuhea">nike shox basketbtutti scarpe for vendita</a> <a href="http://www.exoticbg.com/nike-roshe-run-wolf-gr%C3%A5-atomic-orange-uk-butyf">nike roshe run wolf gr氓 atomic orange uk</a> <a href="http://www.sushantnayak.com/air-jordan-retro-6-negro-infrarojo-pre-order-kurpesd">air jordan retro 6 negro infrarojo pre order</a>
    batemanafrica http://www.batemanafrica.com/

  2. avatar jordan 6 svart gr氓

    <a href="http://www.bergentoolsuk.com/ray-ban-orange-mirror-aviator-sunglasses-near-me-sunglassesc">ray ban orange mirror aviator sunglasses near me</a> <a href="http://www.dscclinic.com/baltimore-ravens-salary-cap-hats">baltimore ravens salary cap</a> <a href="http://www.dynamitagefdt.com/nike-flyknit-trainer-oreo-quicktime-sportsa">nike flyknit trainer oreo quicktime</a> <a href="http://www.threnodies.com/nike-free-run-3-in-hommesthe-verte-schuhee">nike free run 3 in hommesthe verte</a> <a href="http://www.ukcitymedia.com/penn-state-stocking-cap-hatr">penn state stocking cap</a> <a href="http://www.bradsnapshot.com/adidas-soccer-cleats-purple-skot">adidas soccer cleats purple</a>
    jordan 6 svart gr氓 http://www.xfangc.com/jordan-6-svart-gr%C3%A5-kurpesb

  3. avatar casbloom

    <a href="http://www.filmkonulari.com/nike-huarache-high-p%C3%BArpura-negro-butya">nike huarache high p煤rpura negro</a> <a href="http://www.fornhamhall.com/jordan-ultra-fly-bl%C3%A5-butya">jordan ultra fly bl氓</a> <a href="http://www.aktiif.com/adidas-zx-630-fresh-vert-jaune-obuve">adidas zx 630 fresh vert jaune</a> <a href="http://www.bertankavuncu.com/zx-flux-alle-rot-scarped">zx flux alle rot</a> <a href="http://www.budskab.com/air-jordan-14-svart-schuhef">air jordan 14 svart</a> <a href="http://www.chocolatecin.com/nike-kobe-12-rouge-vert-scarpef">nike kobe 12 rouge vert</a>
    casbloom http://www.casbloom.com/

  4. avatar ztsurfboards

    <a href="http://www.profancyshop.com/michael-kors-blue-leather-purse-mkh">michael kors blue leather purse</a> <a href="http://www.lmcgroep.com/nike-air-max-jr-varsity-r%C3%B8d-hvit-obuvi">nike air max jr varsity r酶d hvit</a> <a href="http://www.nowaitnails.com/nike-air-griffey-max-1-viola-nero-scarpej">nike air griffey max 1 viola nero</a> <a href="http://www.xmrujo.com/womens-nike-roshe-run-hyp-blue-green-shoesh">womens nike roshe run hyp blue green</a> <a href="http://www.sevenguard.com/hombres-nike-huarache-ultra-rosado-verde-zapatosk">hombres nike huarache ultra rosado verde</a> <a href="http://www.escortsue.com/mujeres-air-jordan-retro-3-amarillo-p%C3%BArpura-zapatosp">mujeres air jordan retro 3 amarillo p煤rpura</a>
    ztsurfboards http://www.ztsurfboards.com/

  5. avatar northstarcamp

    <a href="http://www.nordseepflege.net/maschio-nike-free-flyknit-3.0-oro-arancia-schuhea">maschio nike free flyknit 3.0 oro arancia</a> <a href="http://www.northsidecarservice.net/nike-flyknit-lunar-2-oro-viola-chaussuresa">nike flyknit lunar 2 oro viola</a> <a href="http://www.northstarcamp.net/air-max-1-r%C3%B8d-sort-gr%C3%A5-obuva">air max 1 r酶d sort gr氓</a> <a href="http://www.noviservice.net/air-max-90-hyperfuse-solar-rojo-comprar-butyc">air max 90 hyperfuse solar rojo comprar</a> <a href="http://www.nuovaideaviaggi.net/kvinners-nike-free-6.0-bl%C3%A5-gr%C3%A5-scarpeb">kvinners nike free 6.0 bl氓 gr氓</a> <a href="http://www.objetpub.net/nike-air-max-90-sneakerboot-gr%C3%A5-orange-kurpesf">nike air max 90 sneakerboot gr氓 orange</a>
    northstarcamp http://www.northstarcamp.net/

  6. avatar airdirectairways

    <a href="http://www.abetterwaymotors.net/nike-air-max-excellerate-oransje-schuhec">nike air max excellerate oransje</a> <a href="http://www.accessabath.net/nike-zoom-pegasus-31-lilla-himmelen-bl%C3%A5-butyg">nike zoom pegasus 31 lilla himmelen bl氓</a> <a href="http://www.acostadesign.net/nike-air-max-90-cool-gris-for-vente-scarpec">nike air max 90 cool gris for vente</a> <a href="http://www.agentorangelegacy.net/nike-air-max-shake-evolve-mennns-basketball-sko-online-chaussuresf">nike air max shake evolve mennns basketball sko online</a> <a href="http://www.aikenfamilypharmacy.net/le-coq-sportif-zapatos-negro-gris-butya">le coq sportif zapatos negro gris</a> <a href="http://www.ajimi.net/nike-flynit-air-max-braun-silber-schuhea">nike flynit air max braun silber</a>
    airdirectairways http://www.airdirectairways.net/

  7. avatar priverussia

    <a href="http://www.divyangmart.com/adidas-harden-vol-1-silver-blue-skof">adidas harden vol 1 silver blue</a> <a href="http://www.ezellhvac.com/air-jordan-5-white-red-quilts-skob">air jordan 5 white red quilts</a> <a href="http://www.oldoyopark.com/jordan-elephant-print-hats-uk-hata">jordan elephant print hats uk</a> <a href="http://www.racquetlady.com/red-orange-womens-nike-air-max-1-premium-shoes-runninge">red orange womens nike air max 1 premium shoes</a> <a href="http://www.ankaralokanta.com/nike-air-max-thea-rojo-bronze-schuhea">nike air max thea rojo bronze</a> <a href="http://www.mengenmetal.com/air-jordan-retro-9-b%C3%B8rn-gr%C3%A5-s%C3%B8lv-scarpec">air jordan retro 9 b酶rn gr氓 s酶lv</a>
    priverussia http://www.priverussia.com/

  8. avatar herschel dad hat

    <a href="http://www.prestonacuk.com/cheap-nike-free-5-womens-yellow-shoesa">cheap nike free 5 womens yellow</a> <a href="http://www.westonmaster.com/femmes-air-jordan-retro-14-gris-jaune-kurpesa">femmes air jordan retro 14 gris jaune</a> <a href="http://www.tadejvaljavec.com/nike-roshe-nm-woven-rainbow-sportsd">nike roshe nm woven rainbow</a> <a href="http://www.igbohoohaa.com/nike-air-max-90-mens-gold-grey-shoesc">nike air max 90 mens gold grey</a> <a href="http://www.baccoating.com/nike-lebron-soldier-10-oro-marr%C3%B3n-schuhef">nike lebron soldier 10 oro marr贸n</a> <a href="http://www.expochacra.com/lebron-13-triple-negro-man-kurpesd">lebron 13 triple negro man</a>
    [url=http://www.szbbjdn.com/herschel-dad-hat-hatr]herschel dad hat[/url]

  9. avatar lauraundleon

    <a href="http://www.albainmaz.com/adidas-zx-flux-slip-on-noir-blanc-schuhea">adidas zx flux slip on noir blanc</a> <a href="http://www.seemyvcard.com/herre-asics-gel-noosa-tri-9-lyser%C3%B8d-hvid-scarpeb">herre asics gel noosa tri 9 lyser酶d hvid</a> <a href="http://www.joegauntlett.com/womens-nike-air-max-bw-white-netherlands-shoeso">womens nike air max bw white netherlands</a> <a href="http://www.rpnailsalon.com/kyrie-2-velvet-rouge-key-kurpesb">kyrie 2 velvet rouge key</a> <a href="http://www.hrubinlaser.com/buy-basketball-boots-online-zapatost">buy basketball boots online</a> <a href="http://www.pruvitallday.com/white-leather-converse-shoesz">white leather converse</a>
    lauraundleon http://www.lauraundleon.com/

欢迎留言




用户登录

sitemap