数学上添加两个字符串? [英] Adding two strings mathematically?

查看:96
本文介绍了数学上添加两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在论坛中四处逛逛,但仍然找不到解决我问题的答案。
我有两个字符串,实际上只是一个数字数组。例如(我只选择随机数

i was looking around the forums and i still couldnt find my answer to my problem. I got two strings, that are just really an array of numbers. for example(i just choose random numbers

    string input1="12345678909876543212";
    string input2="12345";

我想将这两个字符串加在一起,但要像整数一样处理它们。正在创建一个类,在该类中我可以添加比(long long int)大的数字,因此它可以超过最大的long long int变量。

I want to add these two string together but act them like there integers. My goal is creating a class where i can add bigger numbers than (long long int) so it can exceed the largest long long int variable.

因此,我毫无疑问地重读了字符串,所以现在有

So i revese the string with no problem, so now there

  input1="21234567890987654321" 
  input2="54321"

然后我尝试添加,例如将input1 [0] + input2 [0](2 + 5)添加到新字符串中,将其称为newString [0]等于(7);但是我找不到在时间上转换当前数字以便将其添加到新字符串的好方法吗?有人可以帮忙。我厌倦了atoi,stof ,他们似乎根本不适合我
我可以通过任何方式使此功能起作用
我不在乎上课但是,我只是想找到一种方法,以数学方式添加这两个字符串,但仍保持newString的字符串格式。谢谢您,谁能为我解决这个问题

then i tried adding, let's say input1[0]+input2[0] (2+5) to a new string lets call it newString[0] where that would equal (7); but i cant find a good way to temporally convert the current number in the string so i can add it to the new string? can anyone help. I get sick and tired of atoi,stof,stod. they don't seem to work at all for me. Any way i can make this function work. I don't care about making the class yet, i just care about finding a way to add those two strings mathematically but still maintaining the newString's string format. Thank you for whoever can figure this out for me

推荐答案

好的,所以,假设您唯一的问题是逻辑,而不是逻辑类设计的东西,我想出了这样的逻辑

Okay, so, assuming your only problem is with the logic, not the class design thing, I came up with this logic


  • 用0填充输入,检查长度,匹配长度

  • 像普通加法一样添加,跟踪进位

  • 最终从结果中删除前导零

因此使用 std :: transform 在反向迭代器上具有lambda函数:-

So using std::transform with a lambda function on reverse iterators :-

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);
}

请参见 此处

编辑 您能否评论您在这里做什么

                +--------+--------------+------------+-------> 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> ,请参阅ideone发布链接。

std::transform is present in header <algorithm>, see the ideone posted link.

这篇关于数学上添加两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆