1、重写折半查找,使得在循环内部只执行一次测试

传统的非递归式的折半查找的例子中,while循环语句内部共执行了两次测试,其实只要一次就足够(代价是将更多的测试在循环外执行)。重写该函数,使得在循环内部只执行一次测试,比较两种版本函数的运行时间。

/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
int binsearch(int x, int v[], int n)
{
int low, high, mid; low = 0;
high = n - 1;
mid = (low+high) / 2;
while(low <= high && x != v[mid])
{
if(x < v[mid])
high = mid - 1;
else
low = mid + 1;
mid = (low+high) / 2;
}
if(x == v[mid])
return mid; // found match
else
return -1; // no match
}

两种方案的执行时间几乎没有差异,我们并没有得到多大的性能改进,反而失掉了代码的可读性。

2、可视化转义字符与实际字符的双向转换

编写一个函数escape(s, t),将字符串t 复制到字符串s 中,并在复制的过程中将换行符、制表符等等不可显地字符分别转换为\n、\t等相应的可显示的转义字符序列。要求使用switch语句,再编写一个具有相反功能的函数,在复制过程中将转义字符序列转换为实际字符。

/* escape: expand newline and tab into visible sequences while copying the string t to s */
void escape(char s[], char t[])
{
int i, j; for(i = j = 0; t[i] != '\0'; i++)
{
switch(t[i])
{
case '\n': // newline
s[j++] = '\\';
s[j++] = 'n';
break;
case '\t': // tab
s[j++] = '\\';
s[j++] = 't';
break;
default: // all other chars
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}

unescape函数与escape函数很相似,如下:

/* unescape: convert escape sequences into real characters while copying the string t to s */
void unescape(char s[], char t[])
{
int i, j; for(i = j = 0; t[i] != '\0'; i++)
{
if(t[i] != '\\')
s[j++] = t[i];
else // it is a backslach
switch(t[++i])
{
case 'n': // real newline
s[j++] = '\n';
break;
case 't': // real tab
s[j++] = '\t';
break;
default: // all other chars
s[j++] = '\\';
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}

switch语句是允许嵌套的,因此unescape 函数也可以这样写:

/* unescape: convert escape sequences into real characters while copying the string t to s */
void unescape(char s[], char t[])
{
int i, j; for(i = j = 0; t[i] != '\0'; i++)
{
switch(t[i])
{
case '\\': // backslach
switch(t[++i])
{
case 'n': // real newline
s[j++] = '\n';
break;
case 't': // real tab
s[j++] = '\t';
break;
default: // all other chars
s[j++] = '\\';
s[j++] = t[i];
break;
}
break;
default: // not a backslach
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}

3、正如TCPL读书笔记&心得中第33条阐述的那样,我们先前编写的itoa 函数不能处理绝对值最大的负数,因为在整形32位二进制数字的表示中,最大的负数取负,在机器底层上得到的结果仍然为最大的负数(也就是相应正数溢出的结果),这里我们的想法是把取模操作的结果转换为正数,从而绕过无法把最大的负数转换为正数的问题。

#include <stdio.h>
#define abs(x) ((x) < 0 ? -(x) : (x)) // itoa: convert n to characters in s - modified
void itoa(int n, char s[])
{
int i, sign;
void reverse(char s[]); sign = n; // record sign
i = 0;
do{ // generate digits in reverse order
s[i++] = abs(n % 10) + '0'; // get next digit
}while((n /= 10) != 0); // delete it
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

当然了,还可以采取一种措施,就是先判断该数字是不是最大的负数,如果是就进行特殊处理,不过这样做比较浪费资源,要额外多进行一次比较操作。

4、编写函数,将字符串s1中的速记符号在s2中扩展为等价的完整列表

编写函数expand(s1, s2),将字符串s1 中类似于a-z 一类的速记符号在字符串s2 中扩展为等价的完整列表abc···xyz。该函数可以处理大小写字母和数字,并可以处理a-b-c、a-z0-9 与a-z 等类似的情况。作为前导和尾随的字符原样复制。

/* expand: expand shorthand notation in s1 into string s2 */
void expand(char s1[], char s2[])
{
char c; int i, j;
while((c = s1[i++]) != '\0') // fetch a char from s1[]
{
if(s1[i] == '-' && s1[i+1] >= c)
{
i++;
while(c < s1[i]) // expand shorthand
s2[j++] = c++;
}
else
s2[j++] = c; // copy the character
}
s2[j] = '\0';
}

5、编写函数itob(n, s, b),将整数n转换为以b为底的数,并将结果以字符的形式保存到字符串s中。例如,itob(n, s, 16)把整数n格式化成十六进制整数保存在s中。

思路:先按逆序生成b进制数的每一位数字,再用函数reverse(自行编写,非库函数)对字符串s中的字符做一次颠倒而得到最终的结果。

/* itob: convert n to characters in s - base b */
void itob(int n, char s[], int b)
{
int i, j, sign;
void reverse(char s[]); if((sign = n) < 0) // record sign
n = -n; // make n positive
i = 0;
do // generate digits in reverse order
{
j = n % b; // get next digit
s[i++] = (j <= 9) ? j+'0' : j+'a'-10;
}while((n /= b) > 0); // delete it
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

Control Flow的更多相关文章

  1. SSIS的 Data Flow 和 Control Flow

    Control Flow 和 Data Flow,是SSIS Design中主要用到的两个Tab,理解这两个Tab的作用,对设计更高效的package十分重要. 一,Control Flow 在Con ...

  2. Control Flow 如何处理 Error

    在Package的执行过程中,如果在Data Flow中出现Error,那么Data Flow component能够将错误行输出,只需要在组件的ErrorOutput中进行简单地配置,参考<D ...

  3. 关于Control flow

    1.一个package包含一个control flow并且一个或多个data flow. (这个项目叫做 Integration services project,提供了三种不同类型的control  ...

  4. Core Java Volume I — 3.8. Control Flow

    3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...

  5. SSIS ->> Control Flow And Data Flow

    In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...

  6. Control Flow in Async Programs

    Control Flow in Async Programs You can write and maintain asynchronous programs more easily by using ...

  7. A swift Tour(2) Control Flow

    Control Flow 用 if 和 switch 来做条件语句,并且用for-in,for,while,和do-while做循环,条件和循环的括号是可以不写的,但是body外面的括号是必须写的 l ...

  8. [译]Stairway to Integration Services Level 9 - Control Flow Task Errors

    介绍 在本文中,我们会实验 MaximumErrorCount和ForceExecutioResult 故障容差属性,并且还要学习Control Flow task errors, event han ...

  9. 《CS:APP》 chapter 8 Exceptional Control Flow 注意事项

    Exceptional Control Flow The program counter assumes a sequence of values                            ...

  10. Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises

    非常好的文章,讲javascript 的异步编程的. ------------------------------------------------------------------------- ...

随机推荐

  1. 【CodeVS】p1174 靶形数独

    题目描述 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z 博士 ...

  2. iOS上让按钮文本左对齐问题

    一,问题分析 1.在做历史记录视图的时候,由于让键盘退出后才能触发表格的 didselect 那个代理方法,也就是得点两下才触发,而表格中的按钮点一下就可以立即响应. 2.于是我就有了用按钮事件代替 ...

  3. base64编解码实现

    #include <stdio.h> #include <stdlib.h> #include <limits.h> #include <string.h&g ...

  4. 20145330《Java程序设计》第四次实验报告

    20145330<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...

  5. AE唯一值符号化的流程以及过程

    唯一值符号化的流程以及过程(转)   一.获取ServerStyle库中的符号       Style符号库在ArcGIS Engine开发中对应的是ServerStyle符号库,可以通过专门的转换程 ...

  6. 使用RESTClient插件进行数据模拟(GET,POST)提交

    1:在Firefox中下载RESTClient插件安装 2:安装好后的界面 (chrome://restclient/content/restclient.html) 3:选择GET/POST,输入U ...

  7. ECLIPSE下SVN的创建分支/合并/切换使用

    最近接项目要求,要在svn主干上创建分支,用分支来进行程序的bug修改,而主干上进行新功能的开发.分支上的bug修改完,发布后,可以合并到主干上.项目程序可以在主干和分支之间进行切换,来实现主干和分支 ...

  8. 用java页面下载图片

    try { //根据String形式创建一个URL对象 String filePath = materialProductWorks.getDownloadImageUrl(); URL url = ...

  9. How parse REST service JSON response

    1. get JSON responses and go to : http://json2csharp.com/ 2. write data contracts using C# All class ...

  10. Oracle基础及三层分页查询

    一.数据库表空间和数据文件 解析:一个数据库下可以开N个表空间,一个表空间可以包含N个数据文件.表空间是逻辑概念. 二.关于listener.ora位置 修改该界面上的数据,会影响指定路径的监听配置文 ...