这道题 想了很久想用数组写 。。但是VS问题太多了,一直报错内存写入错误。
用string ,但是输入检测还是有问题的。不知道什么时候结束,一直循环检测输入。具体到后面讲。
题目描述
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
输入
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
输出
请计算A+B的结果,并以正常形式输出,每组数据占一行。
样例输入
-234,567,890 123,456,789
1,234 2,345,678
样例输出
-111111101
2346912
头文件
#include "targetver.h"
#include <stdio.h>#include <tchar.h>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <string>
using namespace std;
源文件
// ProblemB.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int main()
{
string str1, str2;
int n = 0;
while (cin>>str1>>str2 ) {
int a = 0, b = 0;
int len1 = str1.length();
int len2 = str2.length();
for (int i = 0; i < len1; i++) {
if (str1[i] >= '0' && str1[i] <= '9') {
a = str1[i] + a * 10-'0';
}
}
if (str1[0] == '-') {
a = -a;
}
for (int i = 0; i < len2; i++) {
if (str2[i] >= '0' && str2[i] <= '9') {
b = str2[i] + b * 10-'0';
}
}
if (str2[0] == '-') {
b = -b;
}
cout << a << " " << b << endl;
}
system("pause");
return 0;
}
结果:
遇到的问题:
1.用char num1[100],num2[100]; 然后while(scanf("%s%s",num1,num2) !=EOF) 报异常,目前没办法自动退出循环 ,手动ctrl+c;
2.int len1 = str1.length(); 没加括号报错。。 自己是初学嘛 下次记得。