1086: 弟弟的作业
Description
你的弟弟刚做完了“100以内数的加减法”这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。
Input
输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
Output
输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
Sample Input
1+2=3
3-1=5
6+7=?
99-0=99
3-1=5
6+7=?
99-0=99
Sample Output
2
#include <iostream>
using
namespace
std;
bool
isEqual(
int
f,
char
op,
int
s,
int
r);
int
getResult(
const
string &expresion,
int
n);
int
getFirst(
const
string &expresion,
int
n);
int
getSecond(
const
string &expresion,
int
n,
int
sBegin);
int
main()
{
string expresion;
//接收表达式
int
f,
//操作数1
s,
//操作数2
r;
//计算结果
char
op;
//执行的操作类型 + 或者 -
int
len;
int
rightNum = 0;
int
i, nCount;
while
(cin >> expresion)
{
len = expresion.size();
if
(expresion[len-1] ==
'?'
)
continue
;
//*********************************处理操作数1***********************************
nCount = 0;
while
(expresion[nCount] !=
'+'
&& expresion[nCount] !=
'-'
)
{
nCount++;
}
f = getFirst(expresion, nCount);
//*********************************处理执行的操作 +或者-*************************
op = expresion[nCount];
//*********************************处理操作数2***********************************
int
sBegin = ++nCount;
//操作数2的起始
i = sBegin;
nCount = 0;
while
(expresion[i] !=
'='
)
{
nCount++;
i++;
}
s = getSecond(expresion, nCount, sBegin);
//*********************************处理计算结果***********************************
i = len-1;
nCount = 0;
while
(expresion[i] !=
'='
)
//&& expresion[i] != '-')//找到计算结果的位数,并判断正负
{
i --;
nCount ++;
}
r = getResult(expresion, nCount);
//*******************判断表达式是否正确*************************************
if
(isEqual(f, op, s, r))
{
rightNum++;
}
}
cout << rightNum << endl;
return
0;
}
//********************************************************************************
bool
isEqual(
int
f,
char
op,
int
s,
int
r)
{
int
tmp, t;
switch
(op)
{
case
'+'
:
tmp = f + s;
if
(tmp == r)
return
true
;
return
false
;
case
'-'
:
tmp = f - s;
if
(tmp == r)
return
true
;
return
false
;
}
}
//*********************************************************************************************
int
getResult(
const
string &expresion,
int
n)
{
int
len = expresion.size() - 1;
int
r;
switch
(n)
{
case
1:
r = expresion[len] -
'0'
;
break
;
case
2:
r = (expresion[len-1] -
'0'
) * 10 + (expresion[len] -
'0'
);
break
;
case
3:
r = (expresion[len-2] -
'0'
) * 100 + (expresion[len-1] -
'0'
) * 10 + (expresion[len] -
'0'
);
break
;
}
//cout << "r=" << r << endl;
return
r;
}
//*********************************************************************************************
int
getFirst(
const
string &expresion,
int
n)
{
int
f;
switch
(n)
{
case
1:
f = expresion[0] -
'0'
;
break
;
case
2:
f = (expresion[0] -
'0'
) * 10 + (expresion[1] -
'0'
);
break
;
case
3:
//操作数1若为3位,只能是100
f = 100;
break
;
}
//cout << "f=" << f << endl;
return
f;
}
//*********************************************************************************************
int
getSecond(
const
string &expresion,
int
n,
int
sBegin)
{
int
s;
switch
(n)
{
case
1:
s = expresion[sBegin] -
'0'
;
break
;
case
2:
s = (expresion[sBegin] -
'0'
) * 10 + (expresion[sBegin+1] -
'0'
);
break
;
case
3:
s = 100;
}
//cout << "s=" << s << endl;
return
s;
}
//*********************************************************************************************
/**************************************************************
Problem: 1086
User: 1006440533
Language: C++
Result: Accepted
Time:0 ms
Memory:1272 kb
****************************************************************/
最后更新:2017-04-02 15:14:57