以前在网上看大数相加的代码,基本上都是用字符数组写的。今天突然想试试用C++中的string能不能写,写了之后感觉比用字符串写略微简单,先上代码吧
大数相加的函数
void add(string &a, string &b, string &c)
{
int lena = a.length(), lenb = b.length();
int i, j;
c = "";
string t("");
char ch;
int up;
for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
{
ch = a[i] + b[j] - 2*'0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
if (i < 0)
{
for(; j >= 0; j--)
{
ch = b[j] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
else if ( j < 0)
{
for(; i >= 0; i--)
{
ch = a[i] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
if (up)
{
ch = up + '0';
t += ch;
}
for (i=t.length() - 1; i >= 0 ; i--)
{
c += t[i];
}
}
string比较方便的地方是,string中有很多成员函数,而且对各种运算符都进行了重载,用起来比较方便。
比如说,复制字符串直接使用=
就行了,而不用像C语言那样使用strcpy函数了。对string的操作符合我们的平时对普通数据的操作,用起来很方便,不用考虑数组溢出问题
string详解在这里http://blog.csdn.net/qq_21120027/article/details/47816273
用上面的函数直接就A了几道题
POJ 1503 Integer Inquiry http://poj.org/problem?id=1503
代码
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>
using namespace std;
string a, b, c;
void add(string &a, string &b, string &c)
{
int lena = a.length(), lenb = b.length();
int i, j;
c = "";
string t("");
char ch;
int up;
for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
{
ch = a[i] + b[j] - 2*'0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
if (i < 0)
{
for(; j >= 0; j--)
{
ch = b[j] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
else if ( j < 0)
{
for(; i >= 0; i--)
{
ch = a[i] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
if (up)
{
ch = up + '0';
t += ch;
}
for (i=t.length() - 1; i >= 0 ; i--)
{
c += t[i];
}
}
int main()
{
{
cin >> a;
if (a == "0")
{
cout << "0\n";
return 0;
}
while(1)
{
cin >> b;
if (b == "0")
{
break;
}
add(a, b, c);
a = c;
}
cout << a << endl;
}
return 0;
}
HDU 1002 A + B Problem II http://acm.hdu.edu.cn/showproblem.php?pid=1002
代码
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>
using namespace std;
string a, b, c;
void add(string &a, string &b, string &c)
{
int lena = a.length(), lenb = b.length();
int i, j;
c = "";
string t("");
char ch;
int up;
for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
{
ch = a[i] + b[j] - 2*'0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
if (i < 0)
{
for(; j >= 0; j--)
{
ch = b[j] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
else if ( j < 0)
{
for(; i >= 0; i--)
{
ch = a[i] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
if (up)
{
ch = up + '0';
t += ch;
}
for (i=t.length() - 1; i >= 0 ; i--)
{
c += t[i];
}
}
int main()
{
int t, i;
cin >> t;
for(i = 1; i <= t; i++)
{
a = b = c = "";
cin >> a >> b;
add(a, b, c);
printf("Case %d:\n", i);
cout << a << " + " << b << " = " << c << endl;
if (i != t)
{
cout << endl;
}
}
return 0;
}
HDU-1047-Integer Inquiry http://acm.hdu.edu.cn/showproblem.php?pid=1047
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
void add(string &a, string &b, string &c)
{
int lena = a.length(), lenb = b.length();
int i, j;
c = "";
string t("");
char ch;
int up;
for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
{
ch = a[i] + b[j] - 2*'0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
if (i < 0)
{
for(; j >= 0; j--)
{
ch = b[j] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
else if ( j < 0)
{
for(; i >= 0; i--)
{
ch = a[i] - '0' + up;
up = ch / 10;
ch %= 10;
ch += '0';
t += ch;
}
}
if (up)
{
ch = up + '0';
t += ch;
}
for (i=t.length() - 1; i >= 0 ; i--)
{
c += t[i];
}
}
int main()
{
int n, T;
cin >> T;
string a, b, c, t;
while(T--)
{
a = "0";
while(cin >> b)
{
if (b == "0") break;
add(a, b, c);
a = c;
}
cout << a << endl;
if(T > 0) cout << endl;
}
return 0;
}