zoukankan      html  css  js  c++  java
  • 第三章 数据决定程序结构

      本章主要讲解一个合适的数据结构对一个程序的重要性。一个好的数据类型能够节省开发时间也能够节省内存的消耗。

      文末总结一下四点:

      1.使用数组重新编写重复代码。在现在的变成中主要用到的是list、map之类的数据结构来代替数组。

      2.封装复杂结构。抽象bean。

      3.尽可能使用高级工具。超文本、名字-值对、电子表格、数据库、编程语言等都是特定问题领域中的强大的工具。由于本书出版的年份较早,这里提到的基本都是日常开发需要用到的。比较新的工具redis在现在工程中刚接触到。也是基于键-值对的形式存储数据。

      4.从数据得出程序的结构。本章的主题就是:通过使用恰当的数据结构来替代复杂的代码,从数据可以得出程序的结果就。万变不离其宗:在动手编写代码之前,优秀的程序员会彻底理解输入、输出和中间数据结构,并围绕这些结构创建程序。

    习题:

    1.优化以下代码

     1 if (income<=2200)
     2     tax=0;
     3 else if (income<2700)
     4     tax= 0.14*(income-2200);
     5 else if (income <=3200)
     6     tax = 70 + 0.15*(income - 2700);
     7 else if(income <=3200)
     8     tax = 145 + 0.16 * (income - 3200);
     9 else if(income <=4700)
    10     tax = 225 = 0.17 * (income - 3700);
    11     ...
    12 else 
    13     tax = 53090 + 0.70 * (income - 102200);

     作答:虽然给出一个结果,但是依然存在问题。

    1.这里面一共有25个if判断,所以利率的增量在之后是大于0.01的,所以按照这个写法是不正确。可以考虑用数组代替这些增量,并做循环。

    1  int number = (income-2200)%500;
    2  for(int intI=0;intI<=number+1;intI++){
    3      moneyRate=0.14+0.1*intI;
    4      redis=(income-2200)-500*intI;
    5      tax=tax+moneyRate*redis;
    6
    
    
    优化后
    1  list moneyRates= new ArrayList(0.14,0.15,0.16,0.17,0.18,....,0.70);
    2  int number = (income-2200)%500+1;
    3  for(int intI=0;intI<moneyRates.length && intI<=number ; intI++){
    4   redis=(income-2200)-500*intI;
    5   tax=tax+moneyRates[intI]*redis;
    6  }
    
    
    
     
    
    
    
    
    
    
    
    
     
  • 相关阅读:
    扫描指定ip的端口(C#)
    C# 调用Delphi dll
    MuleSoft系列(五)-使用Anypoint Studio从RAML文件创建RESTful API接口
    MuleSoft系列(四)-用Anypoint Studio创建一个Mule应用程序
    MuleSoft系列(三)- 使用API设计器通过RAML定义API
    MuleSoft系列(二)- 使用Flow Designer创建Mule应用程序
    MuleSoft系列(一)
    Docusign系列(四)
    Docusign系列(三)
    Docusign系列(二)
  • 原文地址:https://www.cnblogs.com/nayt/p/5521892.html
Copyright © 2011-2022 走看看