九度OJ 1003 A+B
题目分析:
使用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;
}
补充:软件开发 , C++ ,