1.10 外部变量和作用域
本节重点讲述了变量的作用域,这点很重要,要多揣摩,多理解!在以后的编写程序中慢慢体会。
但本节给出了几个比较难的题目,如果做不来,也没有关系,多积累,多练习;慢慢的就会了。
Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like unmatched parentheses, brackets and braces. Don’t forget about quotes, both single and double, escape
sequences, and comments. (This program is hard if you do it in full generality.)
但本节给出了几个比较难的题目,如果做不来,也没有关系,多积累,多练习;慢慢的就会了。
Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab
stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
解析:用blanks 和 tab 直接相互替换是文本编辑器最常用功能之一。如果这个简单的功能都没有,那肯定不是一个优秀的编辑器。算法:找到这个tab,算出这个tab距离下一个tab stop 还有多远,然后用空格填充即可。
程序见下一题。
Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either
a tab or a single blank would suffice to reach a tab stop, which should be given preference?
解析:用最少的tab和blank 去替换有很多blanks的句子;刚好和上题相反,那我们反向思维,还记得以前那个数单词的程序,那么在这里,我们把连续的blanks看做一个单词,但要记住其开始位!然后把开始位-tab结束之间的的blanks 替换为tab,当然如果只有1个空格就不用替换了。
#include <stdio.h> /* 系统头文件 */
#include <stdlib.h> /* 系统头文件 */
#define MAXLINE 1000
#define MINLINE 1
#define TAB_N 8
/* use '-' we can see it */
#define REP_CHAR '-'
#define TAB '\t'
// 函数声明
void TabTest(void);
int getline(char str[], int lim);
void detab(char str[],char strcopy[]);
void entab(char str[], char strcopy[]);
// 全局变量
char line[MAXLINE] = {0};
char linecopy[MAXLINE] = {0};
/* Print the longest input line */
int main()
{
int len;
TabTest();
while ((len = getline(line,MAXLINE)) > 0)
{
if (len > 1) /* at least one tab */
{
detab(line,linecopy);
printf("%s", linecopy);
entab(linecopy, line);
printf("%s", line);
}
}
system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */
return 0;
}
int getline(char str[], int lim)
{
int i;
int c;
for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n' ; ++i)
str[i] = c;
if( c == '\n')
{
str[i] = '\n';
++i;
}
str[i] = '\0';
return i;
}
/* test dos system tab stops */
void TabTest(void)
{
printf("|\t|\t|\t\n");
printf("||\t||\t||\t\n");
printf("|||\t|||\t|||\t\n");
printf("||||\t||||\t||||\t\n");
printf("123412341234123412341234\n");
}
/* repalce tab with REP_CHAR */
/*
0: 0-7 replace
1: 1-7 replace
2: 2-7 replace
3: 3-7 replace
4: 4-7 repalce
5: 5-7 repalce
6: 6-7 repalce
7: 7 repalce
*/
void detab(char str[],char strcopy[])
{
int i,j,k;
int temp;
for (i=0,k=0; str[i] != '\0'; ++i)
{
if (str[i] == '\t')
{
temp = k%TAB_N; /* think that why k?not i?*/
for(j = 0; j < TAB_N-temp; ++j)
{
strcopy[k] = REP_CHAR;
k++;
}
}
else
{
strcopy[k] = str[i];
k++;
}
}
strcopy[k] = '\0';
}
/*
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 ...
^ ^ // 7是
===================================== // 下面是字符,用'-'代替' '(blank)
x x x x x x x - // 不用替换,只有一个‘-’
x x x x x x - - // 替换
x x x x x - - - // 替换
x x x x - - - - // 替换
x x x - - - - - // 替换
x x - - - - - - // 替换
x - - - - - - - // 替换
- - - - - - - - // 替换
*/
void entab(char str[], char strcopy[])
{
int i,j;
int start = 0;
int state = 0;
for (i = 0, j = 0; str[i] != '\0'; i++,j++)
{
strcopy[j] = str[i];
if ( str[i] == REP_CHAR)
{
if (state == 0) /* first blank */
{
state = 1;
start = j; /* think why j,not i */
}
if ((i + 1) % TAB_N == 0 && state == 1)
{
if (start == i)
start++;
else
{
j = start;
strcopy[j] = TAB; // if
}
state = 0; /* clear state (must) */
}
}
else
{
state = 0;
}
}
strcopy[j] = '\0';
}Exercise 1-22. Write a program to ‘‘fold’’ long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input.
Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
解析:只能折叠句子,这个也是文本编辑器常用功能,类似与智能自动换行吧,dos显示窗口水平方向默认设定为80个字符,也就是说,如果我的句子超过了80个字符,就该考虑下换行了,但你有不能直接硬生生的换行把,把一个单词给它拆散,这种棒打鸳鸯的事,还是不做的好,所以叫智能换行。现在考虑下程序应该有多智能了?如果程序猿能吧所有的情况都考虑清楚,那它就非常智能了。那好,先定一个目标,每个新行第一个必须以非blank
& tab 开头。
Exercise 1-23. Write a program to remove all comments from a C program. Don’t forget to handle quoted strings and character constants properly. C comments don’t nest.
待更新...(想快点结束这章节,这些习题还是挺难的~~~可怜了笨笨的我~~~)
1913

被折叠的 条评论
为什么被折叠?



