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. Java_动态加载类(英文)

    It is possible to load and reload classes at runtime in Java, though it is not as straightforward as ...

  2. Html5_禁止Html5在手机上屏幕页面缩放

    最近测试html5页面,发现默认都允许用户缩放页面,或者在屏幕双击放大或缩小.即相当于这样设置 <meta name="viewport" content="wid ...

  3. 每日英語2013.09.09(Email常用單字)

    carbon copy 副本抄送 forward 轉交;轉寄(+to) Please check the file carried by this email for the details.細節請看 ...

  4. Flex 页面空白或Error #2032

    日前用flex.arcgis做了一个地图显示的页面,本机调试没题目,公布后放到用户办事器上(win2003,ie6)ie6显示页面空白,换搜狗浏览器显示Error #2032,只显示进度条,客户端用i ...

  5. 20145330孙文馨 《Java程序设计》第二周学习总结

    20145330孙文馨第二周学习总结 第二周相比于第一周对java语言有了深一点的了解,也意识到多敲代码才是学习计算机语言的最好方法. 教材内容总结 类型.变量与运算符 *基本类型 整数(short. ...

  6. JAVA操作MongoDB数据库

    1. 首先,下载MongoDB对Java支持的驱动包 驱动包下载地址:https://github.com/mongodb/mongo-java-driver/downloads 2.Java操作Mo ...

  7. [转]AS3的垃圾回收

    GC和内存泄露无关 垃圾回收,这次是一个被无数人讨论过的传统话题. Action Script使用的是和Java相似的内存管理机制,并不会即时回收废弃对象的内存,而是在特定时间统一执行一次GC(Gab ...

  8. java web(四)文件上传与下载

     一.文件上传原理 1.在TCP/IP中,最早出现的文件上传机制是FTP ,它是将文件由客户端发送到服务器的标准机制:但是在jsp使用过程中不能使用FTP方法上传文件,这是由jsp运行机制所决定. 通 ...

  9. JS(javascript) 将网站加入收藏夹

    | 浏览:688 | 更新:2014-09-20 19:39 1 2 3 分步阅读 将网站网址加入收藏夹,方便下次访问! 工具/原料 网址: 电脑. 方法/步骤   //创建加入收藏夹JS函数 < ...

  10. SPFA导读及介绍(转载)

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...