table-driven methods

本文介绍了表驱动设计方法,包括直接访问和阶梯访问两种方式,并通过具体示例展示了如何利用表驱动简化条件判断逻辑,提高代码的可维护性和复用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

            已经看到第18章了,今天还重温了下c写了个小程序。一直没项目,只有看书写博客了。

  • 什么是table-driven

首先看一个例子:

image

这是一段java代码,作用很明显判断字符类型。当然可以用函数封装起来,也可以用table来查询。

只需要一句:

image

前提是这张表要提前做好。顺便说一句,inputChar虽然是一个字符,但是放在数组下标里是作为ASCII

码来解释。如charTypeTable['a'] 等同于charTypeTable[97].这是一个很简单的table-driven methods的例子。

总的来说有三种table lookup的方法

  • direct access
  • indexed access
  • stair-step acces

我对indexed理解不深,就讲下剩下两种。

  • direct access

所谓直接读取就是将数据直接放进表中,上面的例子就是一个direct access的例子。还有一个比较好的例子,

就是计算月份天数,这个在K&R里有,我就不粘代码了。新手算一个月有多少天的话,肯定用if来算:

clip_image002

等等一直算到12月,但是如果用一个全局变量保存着12个月的天数,看一个VB的例子:

image

在计算的话又只有一句代码:

image

如果要计算闰年的话,就把一维数组改为二维就好了,再加个判断闰年的函数。是不是很方便了。

  • stair-step acces

上面这个应用还有点简单,看一个数据是区间的情况下如何运用这种方法。一个非常古老的例子,如果

学生成绩大于等于90就是a,大于等于75小于90是B.依次类推:

clip_image002[7]

这是一个以前练习c语言时候的联系题,那个时候全部都用if和else来做。看了代码大全我用c重新写了一个函数。代码如下:

 

 

  1.     double rangeLimit[] = {50.065.075.090.0100.0};
  2.     char   grade[100] = {'F''D''C''B''A'};      
  3. char GetLevel(double *rangeLimit, char *grade, double score)
  4. {
  5.     int level;
  6.     int maxGrade;
  7.     level = 0;
  8.     //maxGrade = ARRAY_LENGTH(grade); 
  9.      maxGrade = strlen(grade) - 1;
  10.     while ((score>=rangeLimit[level]) && (score<=rangeLimit[maxGrade]))
  11.     {
  12.         level++;
  13.     }
  14.     return grade[level];
  15. }

首先用两个数组一个保存分数的上线,一个保存分数等级,Level作为索引变量。这样就避免了繁琐的判断,这里使用的一个思路是将分段的数据保存起上限,然后再进行比较。尤其是分段较多时,或者改动分数范围,对代码的修改仅仅是对数组的修改,同时提高了复用。

  • .数组退化成指针

可以看到上面的代码中我注释了一行,本来我是用一个宏来获得数组长度,如下:

#define ARRAY_LENGTH( x ) (sizeof(x)/sizeof(x[0]))

后来经调试发现,结果总是4,而且不管我数组长度是多少!这是我才想起来数组传参时会退化成指针!这点林锐的《高质量c编程指南》讲的很详细。这里也就发现了面向对象的好处,直接定义一个string类,然后再调用length方法即可。不用指针这么容易出错。

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>员工信息</title> <script type="text/javascript" th:src="@{/js/vue.global.js}"></script> </head> <body> <table id="dataTable" border="1" cellspacing="0" cellpadding="0" style="text-align:center"> <tr> <th colspan="5">员工信息</th> </tr> <tr> <th>编号</th> <th>姓名</th> <th>邮箱</th> <th>性别</th> <th>操作</th> </tr> <tr th:each="employee : ${employeeList}"> <td th:text="${employee.id}"></td> <td th:text="${employee.lastName}"></td> <td th:text="${employee.email}"></td> <td th:text="${employee.gender}"></td> <td> <a @click="deleteEmployee" th:href="@{'/employee/' + ${employee.id}}">删除</a> <a href="">修改</a> </td> </tr> </table> <form id="deleteForm" method="post"> <input type="hidden" name="_method" value="delete"> </form> <script type="text/javascript"> var vue = new Vue({ el: "#dataTable", methods: { deleteEmployee: function (event) { var deleteForm = document.getElementById("deleteForm"); deleteForm.action = event.target.href; deleteForm.submit(); event.preventDefault(); } } }); </script> </body> </html> //删除员工信息 @RequestMapping(value = "/employee/{id}",method = RequestMethod.DELETE) public String deleteEmployee(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/employee"; } <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置编码过滤器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置 处理请求方式put,delete的HiddenHttpmethodFilter--> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置SpringMVC的前端控制器DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启组件扫描--> <context:component-scan base-package="com.atguigu.rest"/> <!--配置thymeleaf视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> <!--配置视图控制器--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!--开放对静态资源的访问呢--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!--开启mvc注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans> 报错 405 找不到方法
最新发布
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值