华为2014年机试题【100以内正整数的加、减运算】-【C语言/C++】
2013-09-17 17:42
381 查看
雪影工作室版权所有,转载请注明【http://blog.csdn.net/lina791211】
2013-09-17 | 题目来源 http://blog.csdn.net/net_assassin/article/details/11660869
2013-09-17 | 答案来源 http://blog.csdn.net/net_assassin/article/details/11660869
华为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” 注:格式错误
原博主 : http://blog.csdn.net/net_assassin/article/details/11660869
一、灵感来源
2013-09-17 | 题目来源 http://blog.csdn.net/net_assassin/article/details/116608692013-09-17 | 答案来源 http://blog.csdn.net/net_assassin/article/details/11660869
二、题目
华为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” 注:格式错误
三、C/C++实现
#include <iostream> using namespace std; void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr) { const char *input = pInputStr; char *output = pOutputStr; int sum = 0; int operator1 = 0; int operator2 = 0; char *temp = new char[5]; char *ope = temp; while(*input != ' ') //获得操作数1 { sum = sum*10 + (*input++ - '0'); } input++; operator1 = sum; sum = 0; while(*input != ' ') { *temp++ = *input++; } input++; *temp = '\0'; if (strlen(ope) > 1 ) { *output++ = '0'; *output = '\0'; return; } while(*input != '\0') //获得操作数1 { sum = sum*10 + (*input++ - '0'); } operator2 = sum; sum = 0; switch (*ope) { case '+':itoa(operator1+operator2,pOutputStr,10); break; case '-':itoa(operator1-operator2,pOutputStr,10); break; default: *output++ = '0'; *output = '\0'; return; } } int main() { char input[] = "4 - 7"; char output[] = " "; arithmetic(input,strlen(input),output); cout<<output<<endl; return 0; }
四、备用
原博主 : http://blog.csdn.net/net_assassin/article/details/11660869
相关文章推荐
- 华为2014年机试题【100以内正整数的加、减运算】-【C语言/C++】
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为机试通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为2014年机试题【字符串过滤】-【C语言/C++】
- (华为机考题)通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 华为2014年机试题【字符串压缩】-【C语言/C++】
- 机试题:过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为机试——通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为2014年机试题【字符串过滤】-【C语言/C++】
- 华为2014年机试题【字符串压缩】-【C语言/C++】
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 通过键盘输入100 以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 补充说明: 1、操作数为
- 软件工程概论第一次课堂小测-------产生30个100以内的随机整数四则运算的小程序
- C/C++实现"输出100以内能被3整除且个位数为6的所有整数"
- 100以内整数的加减法运算
- 1----编程实现100以内两个随机整数的加法运算练习(rand函数)
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 软件工程实验一--编程随机生成30个四则运算,算数包括100以内的整数和真分数。