通过键盘输入100以内正整数的加、减运算式,算出结果
2014-04-27 20:30
579 查看
通过键盘输入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 "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
//判断是否是数字
bool isNum(char c){
if (c>='0'&&c<='9')
{
return true;
}
return false;
}
//判断是否是操作符
bool isOperator(char c){
if(c!='+'&&c!='-') {
return false;
}
return true;
}
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){
char *numOne = (char*)malloc(sizeof(char)*lInputLen-4);
char *numTwo = (char*)malloc(sizeof(char)*lInputLen-4);
char opt;
int oneIndex =0,twoIndex =0;
int i=0;
//判断输入格式是否正确
if (isNum(pInputStr[i]))
{
while (isNum(pInputStr[i]))
{
numOne[oneIndex]=pInputStr[i];
oneIndex++;
i++;
if (i==lInputLen)
{
cout<<"0, error";
return;
}
}
numOne[oneIndex]='\0';
//空格+操作符+空格+数字
if (i+3<lInputLen&&pInputStr[i]==' '&&isOperator(pInputStr[i+1])&&pInputStr[i+2]==' '&&isNum(pInputStr[i+3]))
{
opt=pInputStr[i+1];
i+=3;
while (isNum(pInputStr[i]))
{
numTwo[twoIndex]=pInputStr[i];
twoIndex++;
i++;
if (i==lInputLen)
{
numTwo[twoIndex]='\0';
break;
}
}
}else
{
cout<<"0, error";
return;
}
}else
{
cout<<"0, error";
return;
}
i=0;int j=0;
int one=0,two=0;
while (numOne[i]!='\0')
{
one+=(numOne[i]-'0')*pow(10,oneIndex-1-i);
i++;
}
while (numTwo[j]!='\0')
{
two+=(numTwo[j]-'0')*pow(10,twoIndex-1-j);
j++;
}
int result =0;
//运算
switch (opt)
{
case '+':
result=one+two;
break;
case '-':
result=one-two;
break;
default:
cout<<"0, error";
break;
}
cout<<one<<opt<<two<<'='<<result;
}
int _tmain(int argc, _TCHAR* argv[])
{
char pInputStr[] = "41 + 73";
long lInputLen = strlen(pInputStr);
char *pOutputStr = (char*)malloc(sizeof(char)*lInputLen);
arithmetic(pInputStr,lInputLen,pOutputStr);
system("PAUSE");
return 0;
}
输入字符串的格式为:“操作数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 "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
//判断是否是数字
bool isNum(char c){
if (c>='0'&&c<='9')
{
return true;
}
return false;
}
//判断是否是操作符
bool isOperator(char c){
if(c!='+'&&c!='-') {
return false;
}
return true;
}
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){
char *numOne = (char*)malloc(sizeof(char)*lInputLen-4);
char *numTwo = (char*)malloc(sizeof(char)*lInputLen-4);
char opt;
int oneIndex =0,twoIndex =0;
int i=0;
//判断输入格式是否正确
if (isNum(pInputStr[i]))
{
while (isNum(pInputStr[i]))
{
numOne[oneIndex]=pInputStr[i];
oneIndex++;
i++;
if (i==lInputLen)
{
cout<<"0, error";
return;
}
}
numOne[oneIndex]='\0';
//空格+操作符+空格+数字
if (i+3<lInputLen&&pInputStr[i]==' '&&isOperator(pInputStr[i+1])&&pInputStr[i+2]==' '&&isNum(pInputStr[i+3]))
{
opt=pInputStr[i+1];
i+=3;
while (isNum(pInputStr[i]))
{
numTwo[twoIndex]=pInputStr[i];
twoIndex++;
i++;
if (i==lInputLen)
{
numTwo[twoIndex]='\0';
break;
}
}
}else
{
cout<<"0, error";
return;
}
}else
{
cout<<"0, error";
return;
}
i=0;int j=0;
int one=0,two=0;
while (numOne[i]!='\0')
{
one+=(numOne[i]-'0')*pow(10,oneIndex-1-i);
i++;
}
while (numTwo[j]!='\0')
{
two+=(numTwo[j]-'0')*pow(10,twoIndex-1-j);
j++;
}
int result =0;
//运算
switch (opt)
{
case '+':
result=one+two;
break;
case '-':
result=one-two;
break;
default:
cout<<"0, error";
break;
}
cout<<one<<opt<<two<<'='<<result;
}
int _tmain(int argc, _TCHAR* argv[])
{
char pInputStr[] = "41 + 73";
long lInputLen = strlen(pInputStr);
char *pOutputStr = (char*)malloc(sizeof(char)*lInputLen);
arithmetic(pInputStr,lInputLen,pOutputStr);
system("PAUSE");
return 0;
}
相关文章推荐
- 华为机试通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 通过键盘输入100 以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- (华为机考题)通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 补充说明: 1、操作数为
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为机试——通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 机试题:过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 对三个整数排序,由键盘输入3个数存入num1,num2,num3,中使得结果为num1 <= num2 <= num3
- 判断是否为润年&&键盘输入一个10000以内的大于零的正整数,输出相应中文标识
- 输写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。
- 输入一个100以内的整数,判断与9的关系
- 输入一个100以内的整数,判断是一位数的数、两位数的数、还是100
- 人民币金额大写 在与财务相关的应用中,经常会用到人民币金额的大写,比如发票的打印程序。 本题的任务是:从键盘输入一个十亿以内的正整数(int类型),把它转换为人民币金额大写(不考虑用户输入错误
- 2、从键盘上输入两个整数,由用户回答它们的和,差,积,商和取余运算结果,并统计出正确答案的个数。
- 编写计算“两个整数的最大公约数”程序。 要求通过键盘输入两个整数、检查输入整数是否符合要求,并计算出其最大公约数。
- 产生不大于100的随机正整数,用户通过键盘对随机正整数进行猜测,猜大了给用户提示猜大,猜小提示猜小, 猜对了则提示“恭喜,猜对了”,结束游戏。
- 通过键盘输入两个整数、检查输入整数是否符合要求,并计算出其最大公约数。