第2章--基本概率

1、写一个程序,包含三个函数分别位于不同的文件中,第一个函数increment接受一个整型参数并且返回这个参数加一后的值,这个函数位于文件increment.c中,第二个函数叫做negate,同样接受一个整型参数并且返回它的相反数,最后一个函数是main函数,需要调用其他两个函数并且用10,0,-10的值测试并打印出来。

#include <stdio.h>

int increment(int input);
int negate(int input);

void main(void)
{
	printf("10 increment: %d\n", increment(10));
	printf("0 increment: %d\n", increment(0));
	printf("-10 increment: %d\n", increment(-10));
	printf("10 negate: %d\n", negate(10));
	printf("0 negate: %d\n", negate(0));
	printf("-10 negate: %d\n", negate(-10));
}

int increment(int input)
{
	return input + 1;
}

int negate(int input)
{
	return -input;
}

2、写一个程序从标准输入中读取c源程序并且确定花括号是否正确的配对,提示:你不用担心括号出现在注释中,字符串中或者字符常量中。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	int check = 0;
	char ch;

	printf("Input your code:");

	while (((ch = getchar()) != EOF) && (ch != '\n'))
	{
		if ((ch == '}') && (check == 0))
		{
			printf("Error! '}' before '{'\n");
			return -1;
		}

		if (ch == '{')
		{
			check++;
		}		
		if (ch == '}')
		{
			check--;
		}
	}

	if (check == 0)
	{
		printf("Correct!\n");
		return 0;
	}
	if (check > 0)
	{
		printf("Error! '{' has %d more\n", check);
		return -2;
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值