C和指针代码解析

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_cols 20
#define Max_input 1000
int read_column_numbers(int colums[],int Max){

	int num =0;
	int ch ;

	while(num<Max&&scanf("%d",&colums[num])==1&&colums[num]>=0){
		num+=1;
	}

	/*
	**读取列号,如果读取的数小于0就停止读入。问题是scanf()==1?为什么要这样(解释:scanf function的返回值如果输出的对应相应的类型,则返回1)
	code:
	int a ;
	printf("%d",scanf("%d",&a));
	*/

	/*
	**关于scanf()函数,其中对字符串的读取
	char str[10];
	scanf("%s",str)和gets(str)之间关于空格的读取要注意
	*/

	if((num%2)!=0){
		puts("Last column number is not paired");//自动换行
		exit(EXIT_FAILURE);
	}
	
	/**
	**丢弃该行中包含的最后一个数字的那一部分
	*/
	while((ch=getchar())!=EOF&&ch!='\n')
#if 0
		printf("%d",ch);
#endif
	;
	/*
	**	input:1 2 4 5 aaaa
		output:97 97 97 97 
	*/
	return num ;
}


/*
**处理输入行,将指定列的字符连接在一起,输出以NULL结尾
*/

void rearrange(char *output , char const *input ,int n_columns ,int const columns[]){
/*
**关于const 限定符的知识,参照<<C专家编程前一章节>>,其中要注意的是现在讨论的是ANSI C和C++中的const是有区别的
*/
	int col ;//columns数组的下标
	int output_col ;//输出列数的计数器,为下一段的数据拷贝建立一个哨兵
	int len ;//对应的字符串的长度

	len = strlen ( input );
	output_col = 0 ;
	
	/*
	**处理每对列号
	*/

	for(col = 0 ; col < n_columns ; col+=2 ){
		int n_chars = columns[col+1]-columns[col] + 1 ;

		/*
		**如果输入行结束或输出行数组已满,就结束任务
		*/
		if(columns[col] > len || output_col == Max_input-1)
			break;

		/*
		**如果输出的空间不够,只复制可以容纳的数据
		*/
		
		if(output_col + n_chars > Max_input -1 )
			n_chars = Max_input - output_col - 1 ;

		/*
		**复制相关的数据到output中
		*/

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

	}
	*(output+output_col)='\0';
	
}

int main(){
	
	int n_colums;//需要进行处理的列数
	int columns[Max_cols];//需要处理的列的列号

	char input[Max_input];//输入
	char output[Max_input];//输出

	n_colums = read_column_numbers(columns,Max_cols);
	while( gets(input)!=NULL){
		printf("Original input :%s\n",input);
		rearrange(output, input , n_colums ,columns);
		printf("Reaarrange line:%s\n",output);
	}
	return EXIT_SUCCESS;

}
 

编译运行生成exe文件,在exe文件后面加上任意字符串,运行出现"Program too big too fit in memory"问题,启用vc6.0底下工具栏的编译运行功能仍出现这样的问题,只有在“组建”底下用编译运行方可,可能是编译器的bug问题,具体的再研究。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值