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

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

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

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

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


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

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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

17 条评论

  1. <a href="http://www.cesafetyprod.com/mens-alshon-jeffery-elite-midnight-green-nike-jersey-nfl-philadelphia-eagles-17-home-usa-flag-fashion-nfls">mens alshon jeffery elite midnight green nike jersey nfl philadelphia eagles 17 home usa flag fashion</a> <a href="http://www.fhomestudio.com/mackage-leather-jacket-kijiji-mackager">mackage leather jacket kijiji</a> <a href="http://www.getwichquick.com/dam%C3%A4nner-ralph-lauren-city-polo-beige-khaki-polor">dam盲nner ralph lauren city polo beige khaki</a> <a href="http://www.juraamei.com/air-max-thea-olive-oil-runningg">air max thea olive oil</a> <a href="http://www.proximityhome.com/mackage-crossbody-qatar-riyal-to-peso-mackagee">mackage crossbody qatar riyal to peso</a> <a href="http://www.rwmconnects.com/youth-nike-baltimore-ravens-46-morgan-cox-white-vapor-untouchable-limited-player-nfl-jersey-nflz">youth nike baltimore ravens 46 morgan cox white vapor untouchable limited player nfl jersey</a>
    cohenhypnosis http://www.cohenhypnosis.com/

  2. <a href="http://www.tcdigicrops.com/louboutin-pointed-toe-flats-nutrition-louboutine">louboutin pointed toe flats nutrition</a> <a href="http://www.bytestalk.com/hermes-bag-green-kitchens-hermesc">hermes bag green kitchens</a> <a href="http://www.smartervin.com/nike-lebron-soldier-9-gul-lyser%C3%B8d-footg">nike lebron soldier 9 gul lyser酶d</a> <a href="http://www.nancredewater.com/parajumpers-vest-rest-login-parajumpersa">parajumpers vest rest login</a> <a href="http://www.synchromodaal.com/moncler-vest-looks-retro-monclera">moncler vest looks retro</a> <a href="http://www.asandoor.com/nike-lebron-soldier-10-r%C3%B8d-gr%C3%A5-chaussuresb">nike lebron soldier 10 r酶d gr氓</a>
    adracingworld http://www.adracingworld.com/

  3. <a href="http://www.flypiip.com/mens-asics-gel-noosa-tri-10-orange-purple-footc">mens asics gel noosa tri 10 orange purple</a> <a href="http://www.frissedenkers.com/tory-burch-boots-size-8-toryburchr">tory burch boots size 8</a> <a href="http://www.brbuyeragent.com/air-jordan-11-marr%C3%B3n-rojo-butyf">air jordan 11 marr贸n rojo</a> <a href="http://www.edenwins.com/jordan-6-infrarouge-champs-obuve">jordan 6 infrarouge champs</a> <a href="http://www.ceespeters.com/ua-clutchfit-prodigy-verde-rojo-obuvc">ua clutchfit prodigy verde rojo</a> <a href="http://www.fitwizhawaii.com/ralph-lauren-polo-shirts-nordstrom-hours-poloa">ralph lauren polo shirts nordstrom hours</a>
    cockaweek http://www.cockaweek.com/

  4. <a href="http://www.gslitsupport.com/adidas-springblade-drive-blu-ridge-chaussuresb">adidas springblade drive blu ridge</a> <a href="http://www.pondineerblog.com/nike-roshe-run-triangle-argent-marron-zapatosj">nike roshe run triangle argent marron</a> <a href="http://www.crossfitlwr.com/air-max-thea-gituttio-discharge-obuvf">air max thea gituttio discharge</a> <a href="http://www.raumpilotin.com/pandora-charms-uk-teacher-website-pandoraj">pandora charms uk teacher website</a> <a href="http://www.kinksradio.com/kvinders-nike-free-5.0-mid-gym-l%C3%B8b-trainers-trainersd">kvinders nike free 5.0 mid gym l酶b trainers</a> <a href="http://www.ntfservice.com/nike-air-max-95-leder-orange-blau-chaussuresm">nike air max 95 leder orange blau</a>
    bateslzw http://www.bateslzw.com/

  5. <a href="http://www.sunchaseapts.com/free-run-2015-skos">free run 2015</a> <a href="http://www.financewarm.com/nike-sb-stefan-janoski-max-nero-marrone-kurpese">nike sb stefan janoski max nero marrone</a> <a href="http://www.baktindo.com/elite-nike-black-mens-ben-roethlisberger-home-jersey-nfl-7-pittsburgh-steelers-drift-fashion-nflt">elite nike black mens ben roethlisberger home jersey nfl 7 pittsburgh steelers drift fashion</a> <a href="http://www.iranqnap.com/hermes-h-belt-sizes-hermesr">hermes h belt sizes</a> <a href="http://www.classiccarslb.com/air-jordan-1-retro-infrarojo-zephyr-chaussuresd">air jordan 1 retro infrarojo zephyr</a> <a href="http://www.dkslawn.com/bon-march%C3%A9-nike-free-tr2-hommes-argent-sportsd">bon march茅 nike free tr2 hommes argent</a>
    trouverhotel http://www.trouverhotel.com/

  6. <a href="http://www.vinagebarber.com/polo-ralph-lauren-tote-bag-singapore-poloa">polo ralph lauren tote bag singapore</a> <a href="http://www.claudefremont.com/fendi-bag-eyes-peeled-open-fendib">fendi bag eyes peeled open</a> <a href="http://www.katjehempel.com/nike-free-3.0-v4-bleu-gray-hommes-fonctionnement-chaussures-chaussuresd">nike free 3.0 v4 bleu gray hommes fonctionnement chaussures</a> <a href="http://www.mysiatravel.com/ray-ban-aviator-blue-gold-sunglassesr">ray ban aviator blue gold</a> <a href="http://www.luizabarroso.com/adidas-neo-dam%C3%A4nner-lila-shoesd">adidas neo dam盲nner lila</a> <a href="http://www.transonellc.com/yeezy-boost-350-v2-white-9.5-runninge">yeezy boost 350 v2 white 9.5</a>
    djdiscgolf http://www.djdiscgolf.com/

  7. avatar texomatruss

    <a href="http://www.texomatruss.com/canada-goose-kensington-parka-kopen-jumper-canadagooseb">canada goose kensington parka kopen jumper</a> <a href="http://www.askjms.com/kvinders-nike-kyrie-1-lilla-himmelbl%C3%A5-kurpesk">kvinders nike kyrie 1 lilla himmelbl氓</a> <a href="http://www.dublinraces.com/pandora-bracelet-spacers-uk-uk-pandoraf">pandora bracelet spacers uk uk</a> <a href="http://www.denteraserinc.com/louboutin-ankle-booties-camel-louboutine">louboutin ankle booties camel</a> <a href="http://www.bearclawfire.com/hermes-evelyne-gm-review-essay-hermesc">hermes evelyne gm review essay</a> <a href="http://www.broadbandgeek.com/ua-curry-3-argento-rosso-footm">ua curry 3 argento rosso</a>
    texomatruss http://www.texomatruss.com/

  8. avatar podemosvigo

    <a href="http://www.einsdigital.com/the-north-face-denali-classic-fleece-jacket-jacket-northfaceb">the north face denali classic fleece jacket jacket</a> <a href="http://www.ksmartcity.com/north-face-denali-vest-northfacer">north face denali vest</a> <a href="http://www.ralfwentz.com/kyrie-1-green-glow-jordans-foote">kyrie 1 green glow jordans</a> <a href="http://www.thermilax.com/adidas-herre-forum-mid-basketballe-sko-covers-kurpesf">adidas herre forum mid basketballe sko covers</a> <a href="http://www.watfashion.com/ralph-lauren-stripe-polo-gr%C3%BCn-beige-polow">ralph lauren stripe polo gr眉n beige</a> <a href="http://www.xhlvwang.com/hermes-wallets-2015-range-hermesf">hermes wallets 2015 range</a>
    podemosvigo http://www.podemosvigo.com/

  9. avatar paoniabud

    <a href="http://www.videobind.com/parajumpers-jakke-irene-uk-parajumpersa">parajumpers jakke irene uk</a> <a href="http://www.abracaxola.com/moncler-jacket-new-mexico-monclera">moncler jacket new mexico</a> <a href="http://www.katwords.com/nike-air-max2-cb-94-gr%C3%B8n-lyser%C3%B8d-chaussuresb">nike air max2 cb 94 gr酶n lyser酶d</a> <a href="http://www.amorerugs.com/nike-lebron-13-for-vendita-today-zapatosj">nike lebron 13 for vendita today</a> <a href="http://www.gadgetdaisuke.com/nike-shox-deliver-hvit-gr%C3%A5-nursery-obuvf">nike shox deliver hvit gr氓 nursery</a> <a href="http://www.celsonline.com/pandora-earrings-birthstone-august-design-pandoraj">pandora earrings birthstone august design</a>
    paoniabud http://www.paoniabud.com/

  10. avatar billkace

    <a href="http://www.abiliator.com/nike-free-gray-and-gr%C3%B8n-butyg">nike free gray and gr酶n</a> <a href="http://www.tapsevilla.com/parajumpers-man-right-hand-down-jacket-navy-yard-parajumpersb">parajumpers man right hand down jacket navy yard</a> <a href="http://www.sanduicheria.com/canada-goose-kensington-parka-hood-ut-canadagooseb">canada goose kensington parka hood ut</a> <a href="http://www.clavideo.com/nike-lunarglide-7-azul-urine-kurpesk">nike lunarglide 7 azul urine</a> <a href="http://www.usnbraces.com/pandora-birthstone-rings-aries-release-date-pandoraf">pandora birthstone rings aries release date</a> <a href="http://www.kinmountfair.com/christian-louboutin-sneakers-rantus-mat-zwart-louboutine">christian louboutin sneakers rantus mat zwart</a>
    billkace http://www.billkace.com/

  11. avatar meersaitig

    <a href="http://www.ceeditsesame.com/tory-burch-mini-wallet-robinson-toryburchr">tory burch mini wallet robinson</a> <a href="http://www.nikert.com/nike-free-3.0-v3-mujeres-p%C3%BArpura-footc">nike free 3.0 v3 mujeres p煤rpura</a> <a href="http://www.perlai.com/parajumpers-uk-stockists-parajumpersr">parajumpers uk stockists</a> <a href="http://www.webgiganten.com/hommes-asics-gel-kinsei-4-bleu-ciel-or-obuvc">hommes asics gel kinsei 4 bleu ciel or</a> <a href="http://www.aesqmed.com/air-max-90-oreo-janoski-max-runningd">air max 90 oreo janoski max</a> <a href="http://www.alloweducates.com/air-jordan-5-retro-negro-cool-gris-blanco-eyes-chaussurese">air jordan 5 retro negro cool gris blanco eyes</a>
    meersaitig http://www.meersaitig.com/

  12. avatar greatratesrus

    <a href="http://www.sonsofmalcolm.com/herre-ralph-lauren-pony-polo-sort-lyser%C3%B8d-poloa">herre ralph lauren pony polo sort lyser酶d</a> <a href="http://www.moppelmode.com/nike-air-max-tailwind-8-for-verkauf-arkansas-scarpek">nike air max tailwind 8 for verkauf arkansas</a> <a href="http://www.upgradeurlife.com/floral-dresses-for-cheap-dresse">floral dresses for cheap</a> <a href="http://www.edufom.com/new-balance-890-hvit-r%C3%B8d-footi">new balance 890 hvit r酶d</a> <a href="http://www.flicksman.com/nike-lunarglide-6-p%C3%BArpura-butyg">nike lunarglide 6 p煤rpura</a> <a href="http://www.texasjudaica.com/parajumpers-gobi-size-chart-templates-parajumpersb">parajumpers gobi size chart templates</a>
    greatratesrus http://www.greatratesrus.com/

  13. avatar plushthreads

    <a href="http://www.texomatruss.com/yeezy-boost-350-negro-chaussuresg">yeezy boost 350 negro</a> <a href="http://www.askjms.com/herre-ralph-lauren-mesh-polo-bl%C3%A5-gr%C3%B8n-poloa">herre ralph lauren mesh polo bl氓 gr酶n</a> <a href="http://www.dublinraces.com/air-yeezy-2-blu-gituttio-scarpee">air yeezy 2 blu gituttio</a> <a href="http://www.denteraserinc.com/vestido-floral-con-bolsillos-franceses-dresse">vestido floral con bolsillos franceses</a> <a href="http://www.bearclawfire.com/adidas-nitrocharge-dark-vert-hair-footc">adidas nitrocharge dark vert hair</a> <a href="http://www.broadbandgeek.com/nike-air-max-95-jacquard-s%C3%B8lv-gul-butym">nike air max 95 jacquard s酶lv gul</a>
    plushthreads http://www.plushthreads.com/

  14. avatar dumpthatdebt

    <a href="http://www.larrystoverjr.com/nike-air-max-correlate-mujeres-obuvb">nike air max correlate mujeres</a> <a href="http://www.gokartinfo.com/nike-flyknit-lunar-3-lyser%C3%B8d-knits-chaussuresc">nike flyknit lunar 3 lyser酶d knits</a> <a href="http://www.enemythunder.com/asics-gel-kayano-21-gr%C3%B8nn-hvit-kurpesg">asics gel kayano 21 gr酶nn hvit</a> <a href="http://www.lahtacentr.com/yeezy-boost-750-sort-gum-kaskus-schuhea">yeezy boost 750 sort gum kaskus</a> <a href="http://www.intspv.com/reekbok-life-series-argent-butyf">reekbok life series argent</a> <a href="http://www.itsmylogo.com/nike-air-huarache-light-bl%C3%A5-brun-kurpesd">nike air huarache light bl氓 brun</a>
    dumpthatdebt http://www.dumpthatdebt.com/

  15. avatar realchiyouth

    <a href="http://www.dvdlivedoor.com/air-jordan-jumpman-h-negro-oro-scarpea">air jordan jumpman h negro oro</a> <a href="http://www.exterminas.com/florida-state-seminoles-top-of-the-world-ncaa-ploom-adjustable-cap-hati">florida state seminoles top of the world ncaa ploom adjustable cap</a> <a href="http://www.filoliteafarm.com/nike-lunarglide-6-kvinders-gr%C3%A5-guld-trainersb">nike lunarglide 6 kvinders gr氓 guld</a> <a href="http://www.shalandaleigh.com/nike-air-max-90-fl%C3%A5den-bl%C3%A5-top-kurpesd">nike air max 90 fl氓den bl氓 top</a> <a href="http://www.skepticmoment.com/nike-air-max-90-hyp-frm-frau-grau-lila-schuhel">nike air max 90 hyp frm frau grau lila</a> <a href="http://www.kleankampsite.com/femmes-air-jordan-retro-2-vert-rouge-skof">femmes air jordan retro 2 vert rouge</a>
    realchiyouth http://www.realchiyouth.com/

  16. avatar trueaustinpg

    <a href="http://www.precisetrades.com/jordan-4-nero-nero-butyf">jordan 4 nero nero</a> <a href="http://www.teamoxia.com/youth-nike-dallas-cowboys-82-jason-witten-limited-black-2016-salute-to-service-nfl-jersey-nflx">youth nike dallas cowboys 82 jason witten limited black 2016 salute to service nfl jersey</a> <a href="http://www.dopeslimezz.com/mens-nike-pittsburgh-steelers-84-antonio-brown-elite-whitegold-nfl-jersey-nflw">mens nike pittsburgh steelers 84 antonio brown elite whitegold nfl jersey</a> <a href="http://www.easternmeter.com/nike-kaishi-bianca-and-nero-yeezy-chaussuresa">nike kaishi bianca and nero yeezy</a> <a href="http://www.floxshop.com/nike-kd-9-rose-dust-buster-butya">nike kd 9 rose dust buster</a> <a href="http://www.kinkyfresno.com/nike-air-max-90-svart-hvit-wolf-gr%C3%A5-butyc">nike air max 90 svart hvit wolf gr氓</a>
    [url=http://www.trueaustinpg.com/]trueaustinpg[/url]

  17. avatar butterbroad

    <a href="http://www.whatoiljobs.com/nike-air-max-tailwind-bianca-scarpei">nike air max tailwind bianca</a> <a href="http://www.mustberavi.com/hombres-nike-air-max-2017-amarillo-azul-zapatosh">hombres nike air max 2017 amarillo azul</a> <a href="http://www.dynamixawards.com/nike-mercurial-superfly-fg-sort-bl%C3%A5-skok">nike mercurial superfly fg sort bl氓</a> <a href="http://www.nerdalertnews.com/adidas-zx-flux-adv-x-gr%C3%A5-suede-gull-02-obuvi">adidas zx flux adv x gr氓 suede gull 02</a> <a href="http://www.dmvantalya.com/armada-azul-and-blanco-converse-zapatosl">armada azul and blanco converse</a> <a href="http://www.houshyaree.com/ray-ban-folding-wayfarer-sale-sunglassesh">ray ban folding wayfarer sale</a>
    butterbroad http://www.butterbroad.com/

欢迎留言




用户登录

sitemap