天天看点

C和指针课后练习第二章

1.

//包含在increment.c中
int increment(int x) {
	return x + 1;
}

//包含在negate.c中
int negate(int x) {
	return 0 - x;
}

//主程序包含在main.c中
extern int increment(int x);
extern int negate(int x);

int main() {
	int a = 10,
		b = 0,
		c = -10;
	printf("source    : %d\t%d %d\n", a, b, c);
	printf("increment : %d\t%d %d\n", increment(a), increment(b), increment(c));
	printf("negate    : %d\t%d %d\n", negate(a), negate(b), negate(c));
	system("pause");
	return 0;
}
           

2.验证程序花括号是否成对,不考虑注释内部,字符串内部,常量形式的花括号

int main() {
	int ch,
		count = 0;	//计数
	while ((ch = getchar()) != '\\') {	//以反斜线为结束标志
		if (count < 0)		//计数小于0,已经多出现一个‘}’,肯定不成对
			break;
		if (ch == '{')		//左花括号,计数加一
			count++;
		if(ch == '}')		//右花括号,计数减一
			count--;
	}
	if (count != 0)
		printf("花括号并未成对出现!\n");
	else
		printf("所有花括号均正确!\n");
	system("pause");
	return 0;
}