c和指針第一章編程練習代碼

1.略

2.第二段代碼更符合題目的要求(答案)

#include<stdio.h>
int main()
{
	int i = 1 ;
	char a[1000] ;
	
	while ( gets( a ) != NULL )
	{
		printf ( "%d:%s\n", i, a ) ;
		i++ ;
	}
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int line = 0 ;	//行號
	int col = 1 ;	//
	int ch ;	//儲存字符 
	
	while ( (ch = getchar()) != EOF)	//讀取字符並逐個處理 
	{
		if ( col == 1)	//如果位於一行的起始位置,打印行號 
		{
			line += 1 ;
			col = 0 ;
			printf("%d: ", line ) ;
		}
		putchar( ch ) ;		//打印字符 
		if ( ch == '\n')	//對行尾進行檢查 
			col = 1 ;
	}
	return EXIT_SUCCESS ;
 }
3.
#include<stdio.h>

int main()
{
	char ch ;
	signed char checksum = -1 ;
	
	while ( (ch = getchar()) != EOF)	//讀取字符並逐個處理 
	{
		putchar( ch ) ;
		checksum += ch ;
		if ( ch == '\n')
		{
			printf ( "%d\n",checksum ) ;
			checksum = -1 ;
		}
	}
	return 0 ;
}
4.一篇博客裏找的,寫的很細(wrw112358寫的點擊打開鏈接
#include<stdio.h>
#include<string.h>
#define MAX 1000

int main(void)
{
	int  i ;
	char ch ;
	char len[MAX] ;	
	char temp[MAX] ;	
	
	while ( (ch = getchar()) != EOF)	//讀取字符並逐個處理  EOF:windows用Ctrl+z,LINUX用Ctrl+d 
	{
		for ( i = 0; ch != '\n'; i++)	//將輸入的字符由getchar一個個的返回出來,並儲存在temp中 
		{
			temp[i] = ch ;
			ch = getchar() ;
		}
		
		if ( strlen ( len ) < strlen ( temp ) )	//比較字符數組len和temp的字符長度 
			strcpy ( len, temp ) ;		//將temp中的字符串複製到len中 
	}
	printf ( "%s\n %d\n", len, strlen (len) ) ;
	return 0 ;
}
5.
#include<stdio.h>
#include<stdlib.h> 
#include<string.h>
#define MAX_COLS 20	//所能處理的最大列號
#define MAX_INPUT 1000	//每個數入行的最大長度

int read_columns_number ( int columns[], int max );
void rearrange ( char *output, char const *input, int n_columns, int const columns[] ) ;
 
int main ( void )
{
	int  n_columns ;		//進行處理的列標號(列標的長度) 
	int  columns[MAX_COLS] ;	//需要處理的列數
	char input[MAX_INPUT] ;		//容納輸入行的數組
	char output[MAX_INPUT] ;	//容納輸出行的數組
	
 	n_columns = read_columns_number ( columns, MAX_COLS ) ;	//讀取該列串列標號
	
	while ( gets ( input ) != NULL )	//讀取,處理和打印剩餘的輸入行 
	{
		printf ( "Original input : %s\n", input ) ;
		rearrange ( output, input, n_columns, columns ) ;
		printf ( "Rearranged input : %s\n", output ) ;  
	}
	return EXIT_SUCCESS ;
}

int read_columns_number ( int columns[], int max )	//讀取列標號,如果超出規定範圍則不予理會 
{
	int num = 0 ;
	int ch ;
	
	while ( num < max && scanf ( "%d", &columns[num] ) == 1 && columns[num] >= 0 )	//取得列標號,如果讀取的數小於0則停止 
		num += 1 ; 

	if ( num % 2 != 0 )	//確認已讀取的標號爲偶數個 
	{
		puts ( "Last columns number is paired." ) ;
		exit ( EXIT_FAILURE ) ; 
	 }
	
	 while ( ( ch = getchar() ) != EOF && ch != '\n' )	//丟棄該行中包含最後一個數字的那部分內容
		;
	 return num ; 
 } 

void rearrange ( char *output, char const *input, int n_columns, int const columns[] )//處理輸入行,將指定列的字符連接在一起,輸出行以NUL結尾
{
	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 ;
//		printf("%d\n",output);
	}
	output[output_col] = '\0' ;
 }
6.不知有沒有更好的寫法,if用的感覺很蛋疼
#include<stdio.h>
#include<stdlib.h> 
#include<string.h>
#define MAX_COLS 20	//所能處理的最大列號
#define MAX_INPUT 1000	//每個數入行的最大長度

int read_columns_number ( int columns[], int max );
void rearrange ( char *output, char const *input, int n_columns, int const columns[] ) ;
 
int main ( void )
{
	int  n_columns ;		//進行處理的列標號(列標的長度) 
	int  columns[MAX_COLS] ;	//需要處理的列數
	char input[MAX_INPUT] ;		//容納輸入行的數組
	char output[MAX_INPUT] ;	//容納輸出行的數組
	
 	n_columns = read_columns_number ( columns, MAX_COLS ) ;	//讀取該列串列標號
	
	while ( gets ( input ) != NULL )	//讀取,處理和打印剩餘的輸入行 
	{
		printf ( "Original input : %s\n", input ) ;
		rearrange ( output, input, n_columns, columns ) ;
		printf ( "Rearranged input : %s\n", output ) ;  
	}
	return EXIT_SUCCESS ;
}

int read_columns_number ( int columns[], int max )	//讀取列標號,如果超出規定範圍則不予理會 
{
	int num = 0 ;
	int ch ;
	
	while ( num < max && scanf ( "%d", &columns[num] ) == 1 && columns[num] >= 0 )	//取得列標號,如果讀取的數小於0則停止 
		num += 1 ; 
	
	while ( ( ch = getchar() ) != EOF && ch != '\n' )	//丟棄該行中包含最後一個數字的那部分內容
		;
	return num ; 
 } 

 void rearrange ( char *output, char const *input, int const n_columns, int const columns[] )//處理輸入行,將指定列的字符連接在一起,輸出行以NUL結尾
{
	int col ;		//columns數組的下標 
	int output_col ;	//輸出列計數器
	int len ;		//輸出行的長度
	
	len = strlen ( input ) ;
	output_col = 0 ;
	
	if ( n_columns % 2 == 0 )	//如果讀取的標號爲偶數個 
	{
	    for ( col = 0; col < n_columns; col += 2 )	//處理每對列標號
	    {
		int nchars = columns[col + 1] - columns[col] - 1 ;
		
		if ( columns[col] >= len )			//如果輸入行沒這麼長,跳過這個範圍 
				continue ;
		
		if ( output_col == MAX_INPUT - 1 )		//如果輸入行結束或輸出行已滿,就結束任務 
				break ;
		
		if ( output_col + nchars > MAX_INPUT - 1 )	//如果輸出行數據空間不夠,只複製可以容納的數據 
				nchars = MAX_INPUT - output_col - 1 ; 
			
		if ( columns[col] + nchars - 1 >= len )		//觀察輸入行中多少個字符在範圍內,如果他小於nchars,就對nchars的值進行調整 
				nchars = len - columns[col] + 1 ; 
			
		strncpy ( output + output_col, input + columns[col], nchars ) ;	//複製相關數據
		output_col += nchars ;
	    }
		output[output_col] = '\0' ;
	}
	else
	{
	    if ( columns[n_columns - 1] >= len )
		{
		    puts ( "Last columns number is paired." ) ;
		    exit ( EXIT_FAILURE ) ;
		}
	    else
		strcpy ( output, input + columns[n_columns - 1] ) ;
	    output[len - columns[n_columns - 1]] = '\n' ;
	}
	
 } 






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章