问题 B: A+B

这道题 想了很久想用数组写 。。但是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();  没加括号报错。。  自己是初学嘛  下次记得。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值