华为2014年校园招聘机试题(2)

试题二:

通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:
1、操作数为正整数,不需要考虑计算结果溢出的情况。
2、若输入算式格式错误,输出结果为“0”。

要求实现函数:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例
输入:“4 + 7” 输出:“11”
输入:“4 – 7” 输出:“-3”
输入:“9 ++ 7” 输出:“0” 注:格式错误
/*
	通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
	输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

	补充说明:
	1、操作数为正整数,不需要考虑计算结果溢出的情况。
	2、若输入算式格式错误,输出结果为“0”。

	要求实现函数:
	void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

	【输入】 pInputStr:  输入字符串
	lInputLen:  输入字符串长度
	【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

	【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

	示例
	输入:“4 + 7”  输出:“11”
	输入:“4 – 7”  输出:“-3”
	输入:“9 ++ 7”  输出:“0” 注:格式错误
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	int a, b, sum;
	char str1[12], str2[12], ope;							//str1代表第一个操作数, str2代表第二个操作数, ope代表运算符
	//处理第一个操作数
	const char *pos = strchr(pInputStr, ' ');
	const char *ope_pos;
	int len = pos - pInputStr, len2;					//len代表第一个操作数的长度, len2代表第二个操作数的长度
//printf("len = %d\n", len);
	strncpy_s(str1, _countof(str1), pInputStr, len);	//将第一个操作数字符串取出来,赋给str1
//printf("str1 = %s\n", str1);
	a = atoi(str1);										//将第一个操作数转化为正整数
//printf("a = %d\n", a);
	//处理符号
	pos += 1;
	ope_pos = strchr(pos, ' ');							//确定第二个' '出现的指针位置
	if (!ope_pos)
	{
		printf("0\t注:格式错误!\n");
		return;
	}
	len2 = ope_pos - pos;								
//printf("len = %d\n", len);
	if (len2 > 1)										//若果运算符错误,程序将退出
	{
		printf("0:格式错误\n");
		return;
	}
	ope = pInputStr[len + len2];						//获取第二个操作数
//printf("ope = %c\n", ope);

	//处理第二个操作数
	strncpy_s(str2, _countof(str2), ope_pos + 1, lInputLen - len - len2);
//printf("str2 = %s\n", str2);
	b = atoi(str2);
//printf("b = %d\n", b);

	//运算处理
	switch (ope)
	{
	case '+': sum = a + b;	break;
	case '-': sum = a - b;	break; 
	default:
		printf("0(注:格式错误!)");
		break;
	}

//printf("sum = %d\n", sum);

	//将整数转化为字符串
	sprintf_s(pOutputStr,lInputLen, "%d", sum);
	printf("%s\n", pOutputStr);
}

int main()
{
	char in[21], out[21];
	int len = 0;
	while (1)
	{
		printf("请输入字符串,格式为:\"操作数1 运算符 操作数2\",运算符和操作数之间用一个空格键隔开【仅用于测试】:\n");
		gets_s(in, 20);
		len = strlen(in);
//printf("in = %s, len = %d.\n", in, len);
		arithmetic(in, len, out);
	}
	system("pause");
	return 0;
}

运行结果:


以下方法来自于<http://www.acmerblog.com/2014-huawei-interview-5936.html>转载请注明出处!
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
	char buffer[200];
	int num1, num2;
	int i = 0, j = 0;

	while (pInputStr[i] >= '0' && pInputStr[i] < '9')
	{
		buffer[j++] = pInputStr[i];
		i++;
	}

	//检测输入格式是否正确, i应该指向第一个空格
	if (i == 0 || pInputStr[i] != ' ')
	{
		pOutputStr[0] = '0';
		pOutputStr[1] = 0;
		return;
	}
	i++;//i指向操作符
	char tmp;
	if (pInputStr[i] == '+' || pInputStr[i] == '-')
	{
		tmp = pInputStr[i];
		i++;//i应该指向运算符之后的空格
	}
	else
	{
		pOutputStr[0] = '0';
		pOutputStr[1] = 0;
		return;
	}
	if (pInputStr[i] != ' ')
	{
		pOutputStr[0] = '0';
		pOutputStr[1] = 0;
		return;
	}
	i++;//i指向第二个操作数
	buffer[j] = 0;
	num1 = atoi(buffer);
	//开始获得第二个操作数
	j = 0;
	while (pInputStr[i] >= '0' && pInputStr[i] <= '9')
	{
		buffer[j++] = pInputStr[i];
		i++;
	}
	//判断结尾处,是否是合法的
	if (pInputStr[i] != 0)
	{
		pOutputStr[0] = '0';
		pOutputStr[1] = 0;
		return;
	}
	num2 = atoi(buffer);
	//计算结果
	pOutputStr = (char *)malloc(sizeof(char)*lInputLen);
	if (tmp == '+') _itoa_s(num1 + num2, pOutputStr, lInputLen, 10);
	else if (tmp == '-') _itoa_s(num1 - num2, pOutputStr, lInputLen, 10);
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值