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

来源:互联网 发布:装修手机设计软件 编辑:程序博客网 时间:2024/05/16 15:48

试题二:

通过键盘输入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
原创粉丝点击