《C与指针》第一章练习

时间:2022-06-21 14:14:44

本章例程

程序1.1 重排字符

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MAX_COLS 20
#define MAX_INPUT 1000 int read_column_numbers(int columns[], int max);
void rearrange(char * output, char const * input,
int n_columns, int columns[]); int main(void)
{
int n_columns;
int columns[MAX_COLS];
char input[MAX_INPUT];
char output[MAX_INPUT]; n_columns = read_column_numbers(columns, MAX_COLS); while(gets(input) != NULL){
printf("Original input: %s\n", input);
rearrange(output, input, n_columns, columns);
printf("Rearrange output: %s\n", output);
} return EXIT_SUCCESS;
} int read_column_numbers(int columns[], int max)
{
int num = ;
int ch; while(num < max && (scanf("%d", &columns[num])) ==
&& columns[num] >= )
num += ; if(num % != ){
puts("last column number is not paired.");
return EXIT_FAILURE;
} while((ch = getchar()) != EOF && ch != '\n')
;
return num;
} void rearrange(char * output, char const * input,
int n_columns, int columns[])
{
int col;
int output_col;
int len; len = strlen(input);
output_col = ; for(col = ; col < n_columns; col += ){
int nchars = columns[col+] - columns[col] + ; if(columns[col] >= len || output_col >= MAX_INPUT - )
break;
if(output_col + nchars > MAX_INPUT - )
nchars = MAX_INPUT - - output_col; strncpy(output + output_col, input + columns[col], nchars);
output_col += nchars;
}
output[output_col] = '\0';
}

本章问题

1.C is a free-form languague,which means that there are no rules regarding how programs must look.Yet the sample program followed specific spacing rules.why do you think this is?

(C是一种*形式的语言,这意味着没有规则规定它应该怎样编程,但是本章的例程仍然遵循了一定的空白规则,对此你有何看法?)

answer : To make the program easier to read, which in turn makes it easier to maintain later.

(使得程序更容易阅读,便于以后的维护)

2.What is the advantage of putting declaration, such as function prototypes,in header files and then using #include to bring the declarations into the source files where they are needed?

(声明有什么优点?比如函数原型,在头文件使用#include声明并在源文件需要的时候包含它们)

answer : The declaration needs to be written only once,which makes it easier to maintain if modification(修改) are needed later,Also,writing it only once eliminates(消除) the chance that additional copies(副本) are written differently from each other.

(声明只需要写一次,能使代码在日后需要修改的时候更容易维护,只写一次可以消除多个副本不一致的情况)

3.What is the advantage of using #define to give names to literal(字面的) constants?

(用#define 给字面常量命名有什么好处?)

answer : It is easier to see what a named constant represents,if it is well named,than a literal constant,which merely(仅仅) displays its value.

(如果是好的命名常量比仅仅显示值的字面常量更容易看出常量代表的意义)

4.What format string would you use with printf in order to print a decimal integer,a string,and a floating-point value,in that order?Separate the values from one another with a space,and end the output with a newline character.

(用printf把整数,字符串,浮点数依次打印出来应该用什么格式,分别把它们用空格隔开,并且在结尾输出换行符)

answer :

"%d %s %g\n"

5.Write the scanf statement needed to read two integers,called quantity and price,followed by a string,which should be stored in a character array called department.

(写一个scanf语句,需要读取质量和价格两个整型参数,后面还要读取一个字符串,用一个字符数组在存储一个叫做部门的参数)

answer :

scanf("%d %d %s",&quantity, &price,department);

6.There are no checks made on the validity(有效性) of an array subscript(下标) in C,why do you think this obvious safety measure was omitted(省略) from the language?

(在C语言中对数组下标的有效性不做检查,你认为为什么这个显然的安全措施在语言中被省略了呢?)

answer : The programmer can put in subscript checks where they are needed;in places where the subscript is already known to be correct(for example,from having been checked earlier),there is no overhead expended(花费) in checking it again.But the real reason they are omitted is the fact that subscripts are implemented(工具) as pointer expressions,which are described in chapter 8.

(程序员可以自己在他们需要的地方对下标做检查,下标在适当的位置已经知道是对的,比如说过早的检查,不用在消耗开支去再次检查,不过真正省略下标检查的原因是因为下标是个指针表达式,将在第八章描述它)

7.The rearrange program described in the chapter contains the statement:

strncpy(output + output_col, input + columns[col], nchars);

the strcpy function takes only two arguments,so the number of characters it copies is determined by the string specified(指定) by the second argument,what would be the effect of replacing the strncpy function call with a call to strcpy in this program?

(strcpy 这个函数只需要两个参数,所以它实际复制的字符由第二个参数指定,在这个程序中,如果用strcpy替换strncpy会产生什么影响?)

answer : More characters would be copied than are actually needed;however,the output_col would be updated properly,so the next range of characters would be copied into the output array at the proper place,replacing any extra characters from the preceding(在之前) operation.the only potential(潜在的) problem is that the unbounded strcpy might copy more characters into the output array than it has room to hold,destroying some other variables.

(将会比实际需要复制的字符多,然而,由于output_col的值一直在更新,所以下个字符的范围仍然会在合适的地方,会替代前面操作中额外复制的字符,唯一潜在的问题是strcpy可能从output复制更多的字符而没有边界,超过数组本身的容量,会销毁其他的变量)

8.The rearrange program contains the statement

while(gets(input) != NULL )

what might go wrong with this code?

(下面的代码可能会产生什么问题?)

answer : When an array is passed as a function argument,the function has no way of knowing its size,Therefore,gets has no way to prevent(预防) a very long input line from overflowing the input array,the fgets function,which requires that the array size be passed as an argument,does not have this problem.

(当一个数组作为函数参数被通过时,这个函数不知道数组的长度,因此,gets没法预防一个非常长的输入行的溢出,而fgets这个函数,它的参数中要求有数组的长度,因此不会出现这种情况)

本章练习

1.The "Hello world!" program is often the first C program that a student of c writes.It prints hello world! followed by a newline on the standard output.This trivial(微不足道的) program is a good one to use when figuring out how to run the c compiler on your particular system.

(hello world!程序经常被作为学生用c写的第一个c语言程序,它打印出一行hello world!以换行符结尾的标准输出,这个微不足道的程序用来摸索特殊系统怎样编译c程序是很好的)

#include <stdio.h>

int main(void)
{
printf("hello world!");
return ;
}

2.Write a program that reads lines from the standard input.Each line is printed on the standard output preceded(先于) by its line number.Try to write the program so that it has no built-in limit on how long a line it can handle.

(写一个程序,从标准输入中读取行数,每一行在标准输出中先打印行号再打印出来,尝试让这个程序能够处理的一行不受限制)

#include <stdio.h>

int main(void)
{
int ch;
int line = ;
int at_beginning = ;
while((ch = getchar()) != EOF){
if(at_beginning == ){
at_beginning = ;
line += ;
printf("%d", line);
}
putchar(ch);
if(ch == '\n')
at_beginning = ;
}
return EXIT_SUCCESS;
}

3.Write a program that reads characters from the standard input and writes them to the standard output.It should also compute a checksum and write it out after the characters .The checksum is computed in a signed char variable that is initialized to -1.As each character is read from the standar input,it is added to the checksum.Any overflow from the checksum variable is ignored.When all of the character have been written,the checksum is then written as a decimal integer,which may be negative.Be sure to follow the checksum with a new-line.On computers that use ASCII,running your program on a file containning the words "hello world",followed by a newline should produce the following output:

(写一个程序从标准输入中读取字符之后在标准输出中写出,同时应该计算checksum并把它写在字符的后面,checksum用一个signed char变量计算并初始化为-1,每从标准输入读取一个字符,checksum的值就增加,checksum变量的值溢出部分忽略,当所有的字符被写入时,checksum作为一个整型数写出,它可能是个负数,确定在checksum的后面加个换行符,计算时使用ASCII码,运行你的程序包含hello world!后面的输出为)

Hello world!
#include <stdio.h>
#include <stdlib.h> int main(void)
{
char checksum = -;
int ch; while( (ch = getchar()) != EOF && ch != '\n'){
putchar(ch);
checksum += ch;
}
printf("\n%d\n",checksum); return ;
}

4.Write a program that reads input lines one by one until end of file is reached.determines the length of each input line,and then prints out only the longest line that was found,To simplify(简化) matters,you may assume that no input line will be longer than 1000 characters.

(写一个程序从标准输入中一行一行读取,直到遇到文件结尾符,计算每个输入行的长度,然后找到最长的一行并打印出来,为了使情况简单,你需要假定输入的行最大字符不超过1000)

 #include <stdio.h>
#include <string.h>
#include <stdlib.h> #define MAX 1000 int main(void)
{
int length = ;
char longest[MAX];
char input[MAX]; while( gets(input) != NULL ){
int len = strlen(input); if(len > MAX){
printf("input longer than 1000");
return -;
} if(length < len){
length = len;
strcpy(longest,input);
}
} if(length > )
printf("the longest string %s length is %d\n",longest,length); return ;
}

注意:我在第23行进行字符串的复制的时候犯了一个小错误,这牵涉到字符数组的初始化与赋值的问题,不能直接给字符数组赋值,如果直接写:

longest = input;

程序是通不过的,如果longest变量为char *类型,那么程序可能可以通过,不过longest会随着input的变化而变化,if条件句对它失效:

char * longest;
char input[];
longest = input;

5.The statement

if(columns[col] >= len ...)
break;

in the rearrange program stops copying ranges of characters as soon as range is encountered(遇到问题) that is past the end of the input line,This statement is correct only if the ranges are entered in increasing order,which may not be the case,modify the rearrange function so that it will work correctly even if the ranges are not entered in order.

(rearrange函数中的语句在输入行的长度少于列范围时就退出,这条语句只有在列范围是递增的情况下正确,然而事实并不一定如此,(比如3,4,1,2,-1 或者 3 5 6 4 -1)修改rearrange函数使得在任何情况下都正确)

 void rearrange(char * output, char const * input,
int n_columns, int columns[])
{
int col;
int output_col;
int len; len = strlen(input);
output_col = ; for(col = ; col < n_columns; col += ){
int nchars = columns[col+] - columns[col] + ; if(output_col >= MAX_INPUT - )
break; if(nchars <= 1){
18 printf("columns error,begin small than end!\n");
19 break;
20 } if(columns[col] >= len)
23 continue; if(output_col + nchars > MAX_INPUT - )
nchars = MAX_INPUT - - output_col; strncpy(output + output_col, input + columns[col], nchars);
output_col += nchars;
}
output[output_col] = '\0';
}

6.Modify the rearrange program to remove the restriction(限制) that an even(不变的) number of column values must be read initially(最初),If an odd(奇数) number of values are read,the last valued indicates the start of the final range of characters,characters from here to the end of the input string are copied to the output string.

(修改rearrange程序,去除最开始读取到column数组的数必须是奇数的限制,如果读取的为奇数个,最后一个值作为最后一组字符串的开始,到输入字符串的末尾被复制到输出字符串中)

//去掉read_column_numbers函数中的判断奇偶语句

void rearrange(char * output, char const * input,
int n_columns, int columns[])
{
int col;
int output_col;
int len; len = strlen(input);
output_col = ; for(col = ; col < n_columns; col += ){
int nchars; if(output_col >= MAX_INPUT - )
break;
if(columns[col] >= len)
continue; if(col+1 >= n_columns)
nchars = len - columns[col];
else
nchars = columns[col+1] - columns[col] + 1; if(nchars <= ){
printf("columns error,begin small than end!\n");
break;
} if(output_col + nchars > MAX_INPUT - )
nchars = MAX_INPUT - - output_col; strncpy(output + output_col, input + columns[col], nchars);
output_col += nchars;
}
output[output_col] = '\0';
}