string input1="12345678909876543212";
string input2="12345";
我想将这两个字符串加在一起,但就像整数一样。 我的目标是创建一个类,我可以添加比(long long int)更大的数字,因此它可以超过最大的long long int变量。
所以我没有问题地发现字符串,所以现在
input1="21234567890987654321"
input2="54321"
然后我尝试添加,假设input1 [0] + input2 [0](2 + 5)到一个新字符串让我们称之为newString [0],其中它将等于(7);但我找不到一个好方法来暂时转换字符串中的当前数字,以便我可以将其添加到新字符串?有谁可以帮忙。我厌倦了atoi,stof,stod。他们似乎根本不适合我。 任何方式我都可以使这个功能工作。 我不关心上课,我只是想找到一种方法来数学地添加这两个字符串,但仍然保持newString的字符串格式。谢谢你能为我解决这个问题的人
答案 0 :(得分:5)
好的,所以,假设你唯一的问题是逻辑,而不是类设计,我想出了这个逻辑
因此在反向迭代器上使用带有lambda函数的std::transform
: -
char carry = 0;
std::transform(input1.rbegin(),input1.rend(),input2.rbegin(),
result.rbegin(),[&carry]( char x, char y){
char z = (x-'0')+(y-'0') + carry;
if (z > 9)
{
carry = 1;
z -= 10;
}
else
{
carry = 0;
}
return z + '0';
});
//And finally the last carry
result[0] = carry + '0';
//Remove the leading zero
n = result.find_first_not_of("0");
if (n != string::npos)
{
result = result.substr(n);
}
请参阅Here
修改 “你能评论你在这做什么”
+--------+--------------+------------+-------> Reverse Iterator
| | | |
std::transform( | input1.rbegin(), input1.rend(),input2.rbegin(),
result.rbegin(), [&carry]( char x, char y){
//This starts a lambda function
char z = (x-'0')+(y-'0') + carry; // x,y have ASCII value of each digit
// Substracr ASCII of 0 i.e. 48 to get the "original" number
// Add them up
if (z > 9) //If result greater than 9, you have a carry
{
carry = 1; // store carry for proceeding sums
z -= 10; // Obviously
}
else
{
carry = 0; //Else no carry was generated
}
return z + '0'; // Now you have "correct" number, make it a char, add 48
});
标题std::transform
中存在 <algorithm>
,请参阅发布的链接。
答案 1 :(得分:3)
这是一个添加两个数字表示为字符串的解决方案。
#include<iostream>
using namespace std;
string add(string a, string b)
{
int al=a.size()-1;
int bl=b.size()-1;
int carry=0;
string result="";
while(al>=0 && bl>=0)
{
int temp = (int)(a[al] - '0') + (int)(b[bl] - '0') + carry ;
carry = 0;
if(temp > 9 )
{
carry=1;
temp=temp-10;
}
result+=char(temp + '0');
al--;
bl--;
}
while(al>=0)
{
int temp = (int)(a[al] - '0') + carry ;
carry = 0;
if(temp>9)
{
carry=1;
temp=temp%10;
}
result+=char(temp + '0');
al--;
}
while(bl>=0)
{
int temp = (int)(b[bl] - '0') + carry ;
carry = 0;
if(temp>9)
{
carry=1;
temp=temp%10;
}
result+=char(temp + '0');
bl--;
}
if(carry)
result+="1";
string addition="";
for(int i=result.size()-1;i>=0;i--)
addition+=result[i]; // reversing the answer
return addition;
}
string trim(string a) // for removing leading 0s
{
string res="";
int i=0;
while(a[i]=='0')
i++;
for(;i<a.size();i++)
res+=a[i];
return res;
}
int main()
{
string a;
string b;
cin>>a>>b;
cout<<trim(add(a,b))<<endl;
}
答案 2 :(得分:0)
我不是一个非常狡猾的C ++,但我们不能这样做吗?
int i = stoi( input1[0]);
int j = stoi( input2[0]);
int x = i+j;
答案 3 :(得分:0)
您可以通过从中减去'0'将char转换为int:
char sumdigit = (input1[0]-'0') + (input2[0]-'0') + '0';
答案 4 :(得分:0)
atoi()
转换为int而言, input[0]
会更好:
int temp = atoi(input.substr(0,1).c_str());
然后使用stringstream转换回字符串:
stringstream convert;
convert << temp;
string newString = convert.str();
答案 5 :(得分:0)
这是一个解决方案,但这远非明智,甚至都不好笑。
GCC 4.7.3:g ++ -Wall -Wextra -std = c ++ 0x dumb-big-num.cpp
#include <algorithm>
#include <cctype>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
// dumb big num
// unsigned integer
class DBN {
public:
DBN() : num("0") {}
explicit DBN(const std::string& s) : num(s) {
for (const auto& c : num) {
if (!std::isdigit(c)) { throw std::invalid_argument("DBN::DBN"); } }
std::reverse(std::begin(num), std::end(num)); }
DBN operator+(const DBN& rhs) const {
DBN tmp(*this);
return tmp += rhs; }
DBN& operator+=(const DBN& rhs) {
std::string r;
const int m = std::min(num.size(), rhs.num.size());
int c = 0;
for (int i = 0; i < m; ++i) {
int s = (num[i] - '0') + (rhs.num[i] - '0') + c;
c = s / 10;
s %= 10;
r += static_cast<char>('0' + s); }
const std::string& ref = num.size() < rhs.num.size() ? rhs.num : num;
for (int i = m; i < ref.size(); ++i) {
int s = (ref[i] - '0') + c;
c = s / 10;
s %= 10;
r += static_cast<char>('0' + s); }
if (0 < c) { r += '1'; }
num = r;
return *this; }
friend std::ostream& operator<<(std::ostream& os, const DBN& rhs);
friend std::istream& operator>>(std::istream& os, DBN& rhs);
private:
std::string num;
};
std::ostream& operator<<(std::ostream& os, const DBN& rhs) {
std::string s(rhs.num);
std::reverse(std::begin(s), std::end(s));
return os << s;
}
std::istream& operator>>(std::istream& is, DBN& rhs) {
std::stringstream ss;
char c;
while (is && std::isspace(is.peek())) { is.ignore(); }
while (is) {
if (!std::isdigit(is.peek())) { break; }
is >> c;
ss << c; }
DBN n(ss.str());
rhs = n;
return is;
}
int main() {
DBN a, b, t;
while (std::cin >> a >> b) {
std::cout << a + b << "\n";
(t += a) += b;
}
std::cout << t << "\n";
}
答案 6 :(得分:0)
这是一个简单的C ++代码
string Sum(string a, string b)
{
if(a.size() < b.size())
swap(a, b);
int j = a.size()-1;
for(int i=b.size()-1; i>=0; i--, j--)
a[j]+=(b[i]-'0');
for(int i=a.size()-1; i>0; i--)
if(a[i] > '9')
{
int d = a[i]-'0';
a[i-1] = ((a[i-1]-'0') + d/10) + '0';
a[i] = (d%10)+'0';
}
if(a[0] > '9')
{
string k;
k+=a[0];
a[0] = ((a[0]-'0')%10)+'0';
k[0] = ((k[0]-'0')/10)+'0';
a = k+a;
}
return a;
}
答案 7 :(得分:0)
引自C - Adding the numbers in 2 strings together if a different length 回答,我写了一个更易读的代码:
void test_str_str_add(){
char s1[] = "123";
char s2[] = "456";
char s3[10] = {'\0'};
str_add(s1, s2, s3);
std::cout<<s3<<std::endl;
char s4[] = "456789";
char s5[10] = {'\0'};
str_add(s1, s4, s5);
std::cout<<s5<<std::endl;
char s7[] = "99999";
char s8[] = "21";
char s9[10] = {'\0'};
str_add(s7, s8, s9);
std::cout<<s9<<std::endl;
}
测试代码如下:
@PUT
@Path("/process")
@Consumes(MediaType.APPLICATION_JSON)
public Response processEreturn(List<Long> ereturns){
EntityManager em = Utils.initEntityManager();
Query q = em.createQuery("UPDATE ereturn e SET e.processedByShipper = true WHERE e.id = 111713");
q.executeUpdate();
return Response.ok().build();
}
输出:
579
456912
100020