C //练习 5-11 修改程序entab和decab(第1章练习中编写的函数),使它们接受一组作为参数的制表符停止位。如果启动程序时不带参数,则使用默认的制表符停止位设置。

这篇文章介绍了如何修改C程序中的entab和decab函数,使其接受一组制表符作为参数,以便在不同IDE环境下支持可变的制表符设置。作者提供了VisualStudio2010下的代码示例和注意事项。

C程序设计语言 (第二版) 练习 5-11

练习 5-11 修改程序entab和decab(第1章练习中编写的函数),使它们接受一组作为参数的制表符停止位。如果启动程序时不带参数,则使用默认的制表符停止位设置。

注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010

 

代码块:
entab
#include <stdio.h>
#include <stdlib.h>

#define DEFSTOP 8
#define MAXSTOPS 100

/* getstop: return tabstop if valid, -1 if invalid */
int getstop(char *cp){
	int n;

	for (n = 0; *cp != '\0'; cp++) {
		if (*cp < '0' || *cp > '9')
			return -1;
		n = 10 * n + *cp - '0';
	}
	return n;
}

/* entab: replace spaces by the least number of tabs/spaces to get the same spacing */
int main(int argc, char *argv[]){
	int c, n;
	unsigned char col, spc;
	unsigned char stop[MAXSTOPS];

	if( argc-- > MAXSTOPS )	
		return 2;	/* too many arguments */

	for(c = n = 0; n < argc; n++) {
		int temp;
		temp = getstop(*++argv);
		if(temp <= c)
			return 1;	/* tabstop is not a positive integer or not in ascending order */
		stop[n] = temp - c;
		c = temp;
	}

	if(n == 0)
		stop[n++] = DEFSTOP;

	stop[n] = 0;
	spc = col = n = 0;

	while( (c = getchar()) != '$') {
		/* print blanks */
		if(col == 0) {
			if(spc > 1 || (spc == 1 && (c == ' ' || c == '\t')))
				putchar('#');
			else if(spc == 1)
				putchar(' ');
		} else if(c != '\t' && c != ' ')
			while(spc--)
				putchar(' ');

		/* print character, if non-space; and count space */
		if(c != ' ') {
			putchar(c);
			spc = 0;
		}
		else
			(col == 0) ? (spc = 1) : spc++;
		/* count column and stops */
		switch(c) {
			case '\t':
				if(stop[n] && stop[1])
					++n;
				col = 0;
				break;
			case '\n':
				n = col = 0;
				break;
			default:
				if(col == stop[n] - 1) {
					col = 0;
					if(stop[n] && stop[1])
						++n;
				}
				else
					++col;
				break;
		}
	}

	system("pause");
	return 0;
}
detab
#include <stdio.h>
#include <stdlib.h>

#define DEFSTOP 8
#define MAXSTOPS 100

/* getstop: return tabstop if valid, -1 if invalid */
int getstop(char *cp){
	int n;

	for(n = 0; *cp != '\0'; cp++) {
		if(*cp < '0' || *cp > '9')
			return -1;

		n = 10 * n + *cp - '0';
	}
	return n;
}

/* detab: replace tabs with the proper number of blanks to space to the next tabstop */
int main(int argc, char *argv[]){
	int c, n;
	unsigned char col;	/* column after previous tabstop or beginning of line */
	unsigned char stop[MAXSTOPS];	/* list of tabstops */

	if(argc-- > MAXSTOPS)
		return 2;	/* too many arguments */

	for(c = n = 0; n < argc; n++) {
		int temp;
		temp = getstop(*++argv);

		if(temp <= c)
			return 1;	/* tabstop is not a positive integer or not in ascending order */

		stop[n] = temp - c;
		c = temp;
	}

	if(n == 0)
		stop[n++] = DEFSTOP;

	stop[n] = 0;
	col = n = 0;

	while( (c = getchar()) != '$') {
		switch(c) {
			case '\t':
				do {
					putchar('#');
				} while ( stop[n] > ++col);

				if(stop[n] && stop[1])
					n++;
				col = 0;
				break;
			case '\n':
				putchar(c);
				col = n = 0;
				break;

			default:
				putchar(c);
				if(col == stop[n] - 1) {
					col = 0;
					if(stop[n] && stop[1])
						n++;
				}
				else
					++col;
				break;
		}
	}

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值