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