</pre><p></p><pre name="code" class="cpp"><span style="font-family:Courier New;font-size:14px;">/* 1.1_example */
#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_colums, int const 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 line: %s\n",output);
}
return EXIT_SUCCESS;
}
/*
**读取列标号,如果超出规定范围则不予理会。
*/
int read_column_numbers(int columns[], int max)
{
int num = 0;
int ch;
/*
**取得列标号,如果所读取的数小于0则停止。
*/
while(num < max && scanf("%d", &columns[num]) == 1
&& columns[num] >= 0)
num += 1;
/*
**确认已经读取的标号为 偶数个,因为它们是以对的形式出现的。
*/
if(num % 2 != 0)
{
puts("Last column number is not paired.");
exit(EXIT_FAILURE);
}
/*
**丢弃该行中包含最后一个数字的那部分内容。
*/
while((ch = getchar()) != EOF && ch != '\n')
{
;
}
return num;
}
/*
**处理输入行,将指定列的字符连接在一起,输出行以NUL结尾。
*/
void rearrange(char *output, char const *input,
int n_columns, int const columns[])
{
int col; /*columns数组的下标*/
int output_col; /*输出列计数器*/
int len; /*输出行的长度*/
len = strlen(input);
output_col = 0;
/*
**处理每对列标号。
*/
for(col = 0; col < n_columns; col += 2)
{
int nchars = columns[col + 1] - columns[col] + 1;
/*
**如果输入行结束或输出行数组已满,就结束任务。
*/
if(columns[col] > len ||
output_col == MAX_INPUT - 1)
break;
/*
**如果输出行数据空间不够,只复制可以容纳的数据。
*/
if(output_col + nchars > MAX_INPUT - 1)
nchars = MAX_INPUT - output_col - 1;
/*
**复制相关的数据。
*/
strncpy(output + output_col, input + columns[col],
nchars);
output_col += nchars;
}
output[output_col] = '\0';
}</span>
1、gets() 函数丢弃换行符,并在改行的末尾存储一个NUL字节(一个NUL字节是指字节模式为全0的字节,类似'\0'这样的字符常量)
当gets函数被调用但事实上不存在输入行时,它就返回NULL值,表示他到达了输入的末尾(文件尾)
puts函数是gets 函数的输出版本,它把指定的字符串写到标准输出并在末尾添上一个换行符。
2、NUL是ASCII 字符集中'\0' 字符的名字,它的字节模式为全0. NULL 指一个其值为0 的指针。
它们都是整型值,其值也相同,所以它们可以互换使用。但表示的含义不同。
符号NULL 在头文件stdio.h 中定义。另一方面,并不存在预定义的符号NUL,需自行定义。
3、scanf() 函数的返回值是函数成功转换并存储于参数中的值的个数。
使用格式码(除了%c 之外)时,输入值之前的空白(空格、制表符、换行符等)会被跳过,
值后面的空白表示该值得结束。使用%s格式码输入字符串时,中间不能包含空白。
4、while( (ch = getchar()) != EOF && ch != '\n' )
首先,getchar函数从标准输入读取一个字符并返回它的值。如果输入中不再存在任何字符,
函数就会返回常量EOF(在stdio.h 中定义),用于提示文件的结尾。
int putchar(int c);
5、char * strncpy(char *dest, const char *src, size_t n);
如果源字符串的字符数少于第3个参数指定的复制数量,目标字符串中剩余的字节将用NUL字节填充。
strcpy, strcat 第一个字符串参数不能是字符串常量。
char * strchr(cosnt char *str, int c) 在字符串参数str 中搜索字符参数c第一次出现的位置,
搜索成功,返回指向这个位置的指针,搜索失败,返回NULL指针。
char * strstr(const char *haystack, const char *needle);
搜索第二个字符串在第一个字符串中第一次出现的位置。
<span style="font-family:Courier New;font-size:14px;">/* 习题1.8.2: */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int line;
int at_beginning;
int ch;
at_beginning = 1;
line = 0;
while( (ch = getchar()) != EOF)
{
if(at_beginning == 1)
{
at_beginning = 0;
line++;
printf("%d: ", line);
}
putchar(ch);
if(ch == '\n')
at_beginning = 1;
}
return EXIT_SUCCESS;
}</span>
本文深入解析了一个C++代码片段,重点在于如何通过读取列标号来重新排列输入行中的字符。通过实例演示,详细解释了`read_column_numbers`、`rearrange`函数的工作原理,包括列数读取、输入处理及输出生成。此外,还讨论了`gets`、`scanf`等函数的使用及其特性。

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



