/*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20 /* max # of columns to process */
#define MAX_INPUT 1000 /* max len of input & output lines */
int read_column_numbers( int columns[], int max );
void rearrange( char *output, char const *input,int n_columns, int const columns[] );
int main( void )
{
int n_columns; /* # of columns to process 进行处理的列标号*/
int columns[MAX_COLS]; /* the columns to process 需要处理的列数*/
char input[MAX_INPUT]; /* array for input line 容纳输入的行的数组*/
char output[MAX_INPUT]; /* array for output line 容纳输出的行的数组*/
/*
** Read the list of column numbers
*/
n_columns = read_column_numbers( columns, MAX_COLS );
/*
** Read, process and print the remaining lines of input.
*/
while( gets( input ) != NULL ){
printf( "Original input : %s\n", input );
rearrange( output, input, n_columns, columns );
printf( "Rearranged line: %s\n", output );
}
return EXIT_SUCCESS;
}
/*
** Read the list of column numbers, ignoring any beyond the specified
** maximum.
*/
int read_column_numbers( int columns[], int max )
{
int num = 0;
int ch;
/*
** Get the numbers, stopping at eof or when a number is < 0.
*/
while( num < max && scanf( "%d", &columns[num] ) == 1
&& columns[num] >= 0 )
/*
**scanf()返回值:成功则返回参数数目, 失败则返回-1, 错误原因存于errno 中
*/
num += 1;
/*
** Make sure we have an even number of inputs, as they are
** supposed to be paired.
*/
if( num % 2 != 0 ){
puts( "Last column number is not paired." );
exit( EXIT_FAILURE );
}
/*
** Discard the rest of the line that contained the final
** number.
*/
while( (ch = getchar()) != EOF && ch != '\n' )
/*
**int getchar(void); #define EOF -1
*/ ;
printf("num=%d\n",num);
return num;
}
/*
** Process a line of input by concatenating the characters from
** the indicated columns. The output line is then NUL terminated.
*/
void rearrange( char *output, char const *input,int n_columns, int const columns[] )
{
int col; /* subscript for columns array */
int output_col; /* output column counter */
int len; /* length of input line */
len = strlen( input );
output_col = 0;
/*
** Process each pair of column numbers.
*/
for( col = 0; col < n_columns; col += 2 ){
printf("col=%d\n",col);
int nchars = columns[col + 1] - columns[col] + 1;
printf("columns[col+1]=%d\ncolumu[col]=%d\nnchars=%d\n",columns[col + 1],columns[col],nchars);
/*
** If the input line isn't this long or the output
** array is full, we're done.
*/
if( columns[col] >= len ||output_col == MAX_INPUT - 1 )
break;
/*
** If there isn't room in the output array, only copy
** what will fit.
*/
if( output_col + nchars > MAX_INPUT - 1 )
nchars = MAX_INPUT - output_col - 1;
/*
** Copy the relevant data.
*/
printf("input + columns[col]=%s\n",input + columns[col]);/*输入的字符串*/
strncpy( output + output_col, input + columns[col],nchars );
printf("input + columns[col]=%s\n",input + columns[col]);/*经过移位的字符串*/
/*
** char * strncpy(char *dest, const char *src, size_t n);
*/
output_col += nchars;
}
output[output_col] = '\0';/*循环之后输出的字符串将NUL字符作为终止符*/
printf("output[output_col]=%c\n",output[output_col-1]);/*将看见最后一个字符*/
}
/*****运行结果:********/
/*jiangxianxd@jiangxianxd:~/code/c_pointer/ch1$ ./rearrang
4 9 12 20 -1
num=4
jkfsdjkafjsdkjfaksdhfjasdhfjksdhafjkdhsjafdsfd
Original input : jkfsdjkafjsdkjfaksdhfjasdhfjksdhafjkdhsjafdsfd
col=0
columns[col+1]=9
columu[col]=4
nchars=6
input + columns[col]=djkafjsdkjfaksdhfjasdhfjksdhafjkdhsjafdsfd
input + columns[col]=djkafjsdkjfaksdhfjasdhfjksdhafjkdhsjafdsfd
col=2
columns[col+1]=20
columu[col]=12
nchars=9
input + columns[col]=kjfaksdhfjasdhfjksdhafjkdhsjafdsfd
input + columns[col]=kjfaksdhfjasdhfjksdhafjkdhsjafdsfd
output[output_col]=f
Rearranged line: djkafjkjfaksdhf*/
/****by jiangxianxd*******/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_COLS 20
#define MAX_INPUT 1000
int read_columns_num(int columns[],int max);
void rearrange(char *input, char *output, int columns[], int columns_col);
int main()
{
int columns[MAX_COLS];/*需要处理的列数,例如输入4,6 -1 columns[0]=4 columns[1]=6*/
int columns_col;//需要处理的列标号,4 6 -1 需要处理的列标号为2
char input[MAX_INPUT];
char output[MAX_COLS];
columns_col = read_columns_num(columns,MAX_COLS);
if(gets(input) != NULL){
printf("输入的字符串=%s\n",input);
rearrange(input, output, columns,columns_col);
printf("hello2\n");
printf("输出重新排列的字符串=%s\n",output);
}
else
printf("当前没有字符串输入\n");
return 0;
}
int read_columns_num(int columns[],int max)
{
int num=0;
int ch;/*注意ch = getchar()*/
while(num < max && scanf("%d",&columns[num]) == 1 && columns[num] >= 0 )
num = num + 1;
if(num % 2 != 0){
printf("输入的参数有问题\n");
return;
}
while((ch = getchar()) != EOF && ch != '\n')
;
printf("num=%d\n",num);
return num;
}
void rearrange(char *input, char *output, int columns[], int columns_col)
{
int col;
int output_col;
int len;
int num_char;
len = strlen (input);
output_col = 0;
printf("hello1\n");
for(col = 0;col < columns_col;col=col+2){
num_char = columns[col+1] - columns[col] +1;
if(columns[col] >= len || output_col == MAX_INPUT -1){
break;
}
/* else{
}*/
if(output_col+num_char > MAX_INPUT -1){
num_char = MAX_INPUT - output_col - 1;
}
/* else{
}*/
strncpy(output + output_col,input + columns[col],num_char);
output_col=output_col+num_char;
output[output_col] = '\0';
}
}
/****运行结果:*****/
/*jiangxianxd@jiangxianxd:~/code/c_pointer/ch1$ ./rearrangcopy
3 6 -1
num=2
sjfkdjskajfdskjfsdkjkfdk
输入的字符串=sjfkdjskajfdskjfsdkjkfdk
hello1
hello2
输出重新排列的字符串=kdjs*/