題目分析:
使用A,B兩個字符串存儲輸入的兩組數據,首先剔除掉非數字符號,求得數字的值。然後判斷正負號,若有負號,則必在串首,判斷後得出數據的值,進行計算。
源代碼:
[cpp]
#include <iostream>
#include <string>
using namespace std;
int main()
{
string A,B;
while (cin>>A>>B)
{
long m = 0, n = 0;
for (int i = 0; i < A.length(); i++)
{
if (A[i] >= '0' && A[i] <= '9')
{
//求出字符對應的數值
m = m * 10 + A[i] - '0';
}
}
for (int i = 0; i < B.length(); i++)
{
if (B[i] >= '0' && B[i] <= '9')
{
//求出字符對應的數值
n = n * 10 + B[i] - '0';
}
}
if (A[0] == '-')
{
m = 0 - m;
}
if (B[0] == '-')
{
n = 0 - n;
}
cout<<m + n<<endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string A,B;
while (cin>>A>>B)
{
long m = 0, n = 0;
for (int i = 0; i < A.length(); i++)
{
if (A[i] >= '0' && A[i] <= '9')
{
//求出字符對應的數值
m = m * 10 + A[i] - '0';
}
}
for (int i = 0; i < B.length(); i++)
{
if (B[i] >= '0' && B[i] <= '9')
{
//求出字符對應的數值
n = n * 10 + B[i] - '0';
}
}
if (A[0] == '-')
{
m = 0 - m;
}
if (B[0] == '-')
{
n = 0 - n;
}
cout<<m + n<<endl;
}
return 0;
}