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' ;
	}
	
 } 






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值