使用字符串作为输入C ++的整数乘法

时间:2013-08-20 14:03:45

标签: c++ string

每当我将单个数字输入馈送到以下代码时,答案就是4位数。

#include <iostream>
#include <stdio.h>

using namespace std;

int makeEqual(string &s1, string &s2)
{
    int len1 = s1.length();
    int len2 = s2.length();

    //cout<<"Lenght 1 :: "<<len1<<endl;
    //cout<<"Lenght 2 :: "<<len2<<endl;

    if(len1>len2)
    {
        for(int i=0; i<(len1-len2); i++)
            s2='0'+s2;  
        //cout<<"Return value is :: "<<len1<<endl;
        return len1;
    }
    else if(len2>len1)
    {
        for(int i=0; i<len2-len1; i++)
            s1='0'+s1;
        //cout<<"Return value is :: "<<len2<<endl;
        return len2;
    }
    else
        return len1;
}
int singleMultiply(string s1, string s2)
{
    return (s1[0]-'0')*(s2[0]-'0');
}
long int multiply(string a, string b)
{
    int n=makeEqual(a,b);
    if(n==0) return 0;
    if(n==1) return (singleMultiply(a,b));
    return 0;
}
int main()
{
    cout<<singleMultiply("9","9");
    return 0;
}

输出为0020而不是20。 任何人都可以解释一下这背后的逻辑吗?

编辑:我包含了我写的所有代码。实际上我是编程的新手,我正在尝试为基础10上的Karatsuba算法编写代码。

2 个答案:

答案 0 :(得分:0)

您的函数实现仅限于单个数字,但您正在使用字符串(可能是多个数字。

如果您只想将单个数字相乘,请将您的签名更改为

int singleMultiply(char s1, char s2)
{
  return (s1-'0')*(s2-'0');
}

int main()
{
    cout << singleMultiply('5','4');
    return 0;
}

如果你想更通用:

int singleMultiply(string s1, string s2)
{
    // deprecated method, but still the easiest to understand
    //return atoi(s1.c_str()) * atoi(s2.c_str());
    // updated method
    long l1 = strtol(s1.c_str(), NULL, 10);
    long l2 = strtol(s2.c_str(), NULL, 10);
    return l1 * l2;
}

int main()
{
    cout << singleMultiply("50", "40");
    return 0;
}

编辑:编辑完成后,很明显你的工作量超出了你的需要。 atoi / strtol将为您执行整数转换(并且比您尝试的更好)。

我有时间杀人,所以我认为这就是你要做的事情(但是,我不知道你为什么要这样做):

#include <algorithm>
#include <iterator>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

std::string karatsuba(std::string lhs, std::string rhs)
{
    if (lhs.length() != rhs.length())
    {
        if (lhs.length() < rhs.length())
        {
            int diff = rhs.length() - lhs.length();
            lhs.insert(0, diff, '0');
        }
        else
        {
            int diff = lhs.length() - rhs.length();
            rhs.insert(0, diff, '0');
        }
    }

    if (lhs.length() == 1 && rhs.length() == 1)
    {
        long l = std::strtol(lhs.c_str(), NULL, 10);
        long r = std::strtol(rhs.c_str(), NULL, 10);
        long ret = l * r;
        std::stringstream ss;
        ss << ret;
        return ss.str();
    }

    int m = lhs.length();
    int midpoint = m / 2;
    std::string lowlhs(lhs.begin() + midpoint, lhs.end());
    std::string lowrhs(rhs.begin() + midpoint, rhs.end());
    std::string highlhs(lhs.begin(), lhs.begin()  + midpoint);
    std::string highrhs(rhs.begin(), rhs.begin()  + midpoint);

    long addLhs = std::strtol(lowlhs.c_str(), NULL, 10) + std::strtol(highlhs.c_str(), NULL, 10);
    long addRhs = std::strtol(lowrhs.c_str(), NULL, 10) + std::strtol(highrhs.c_str(), NULL, 10);
    std::stringstream ssL;
    std::stringstream ssR;
    ssL << addLhs;
    ssR << addRhs;

    std::string sZ0 = karatsuba(lowlhs, lowrhs);
    std::string sZ1 = karatsuba(ssL.str(), ssR.str());
    std::string sZ2 = karatsuba(highlhs, highrhs);

    long z0 = std::strtol(sZ0.c_str(), NULL, 10);
    long z1 = std::strtol(sZ1.c_str(), NULL, 10);
    long z2 = std::strtol(sZ2.c_str(), NULL, 10);
    long highOrder = static_cast<long>(std::pow(10.0, static_cast<double>(m)));
    long lowOrder = static_cast<long>(std::pow(10.0, static_cast<double>(m / 2)));
    long result = (z2 * highOrder) + ((z1 - z2 - z0) * lowOrder) + z0;
    std::stringstream ss;
    ss << result;
    return ss.str();
}

int main()
{
    std::string lhs = "20";
    std::string rhs = "45";
    std::string result = karatsuba(lhs, rhs);

    std::cout << "Multiplied:  " << lhs << " x " << rhs << " = " << result << std::endl;
    return 0;
}

答案 1 :(得分:0)

我怀疑你的cout在某个地方看不到setw(4) << setfill('0')

如您所述,这将产生0020

我建议尝试:

cout << setw(0) << setfill(' ') << singleMultiply("9","9");

以这种方式开始寻找问题:

int singleMultiply(string s1, string s2)
{
    cout << "Input 1 is " << s1[0] << endl;
    cout << "Input 2 is " << s2[0] << endl;

    cout << "Oper 1 is " << s1[0]-'0' << endl;
    cout << "Oper 2 is " << s2[0]-'0' << endl;

    cout << "Answer is " << (s1[0]-'0')*(s2[0]-'0') << endl;
    printf("Answer (via printf) is %d\n", (s1[0]-'0')*(s2[0]-'0'));

    return (s1[0]-'0')*(s2[0]-'0');
}