【问题标题】:A program to multiply real numbers (fractions) without the multiplication operator using loops使用循环不使用乘法运算符将实数(分数)相乘的程序
【发布时间】:2018-11-22 20:21:54
【问题描述】:

我在想我可以创建一个不使用 (*) 运算符的数字相乘程序;然而,唯一的问题是当涉及到零和一之间的数字或将分数相乘时。

谁能给我一个关于如何编辑我的代码以更有效地实现这个目标的提示?例如,如何使用带有分数的 for 循环?

#include <iostream>
#include <string>
using namespace std; 

int main()
{
    double num1, num2, m=0; int count=0; 
    cout<<"enter num1 then num2: "; 
    cin >> num1>> num2; 
    if (num1==0 ||num2==0) {m=0; cout<<m<<endl; return 0;  }

    if (num1>=num2)
    {while(count<num2)
{
    m +=num1; 
    count++; 
}
cout<<m; 
}
else if (num1<num2)
{
 for (double i=0; i<num1; i++)
    {
        m +=num2; 
    }
    cout<<m; 
}

  return 0; 
}

【问题讨论】:

标签: c++ loops operators multiplication


【解决方案1】:

我想这是一个练习,假设您已经有一个整数 * 整数乘法工作,那么简而言之,您可以执行以下操作:

假设您需要 N 个有效数字,然后将每个数字乘以 10^(N/2)(您已经知道如何与整数相乘),然后将两个数字截断为整数,将整数相乘,然后将结果除以10^N。

【讨论】:

    【解决方案2】:

    如果您想要两个乘以两个分数,您可以将提名者、分母相乘,然后通过将结果元素除以 gcd 来简化结果。 gcd可以通过欧几里德算法计算。如果您想将两个不能用作分数的浮点数相乘,您可以先将它们中的每一个转换为一个紧分数。对于给定的精度 1/N,您可以将每个浮点数乘以 N,将其四舍五入并得到分子,N 是分母。

    #include <iostream>
    #include <string>
    #include <tuple>
    
    //  Euclide algorithm
    int gcd (int a, int b) {
        if (a < b) std::swap (a, b);
        while (b > 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
        //  multiplication float * int
    double mult_float_int (double a, int b) {
        double res = 0;
        for (int i = 0; i < b; i++)
            res += a;
        return res;
    }
    
    //  double -> fraction
    std::pair<int,int> float2frac (double x, int N) {
        int a = mult_float_int (x, N);
        int g = gcd (a, N);
        if (g == 0) g = 1;
        return std::make_pair (a/g, N/g);
    }
    
    //  multiplication a * b
    int mult_int (int a, int b) {
        int res = 0;
        if (a < b) std::swap (a, b);
        for (int i = 0; i < b; i++)
            res += a;
        return res;
    }
    
    //  multiplication a0/b0 * a1/b1
    std::pair<int,int> mult_frac (int a0, int b0, int a1, int b1) {
        int a2 = mult_int (a0, a1);
        int b2 = mult_int (b0, b1);
        int g = gcd (a2, b2);
        if (g == 0) g = 1;
        return std::make_pair (a2/g, b2/g);
    }
    
    int main()
    {
        const int N = 100000; 
        int a0, b0, a1, b1, a2, b2; 
        double x, y;
        std::cout << "enter x : "; 
        std::cin >> x; 
        std::cout << "enter y : "; 
        std::cin >> y; 
    
        std::tie (a0, b0) = float2frac (x, N);
        std::tie (a1, b1) = float2frac (y, N);
        std::tie (a2, b2) = mult_frac (a0, b0, a1, b1);
    
        std::cout << x << " * " << y << " = " << a2 << "/" << b2 << "\n";
        std::cout << "Error = " << x*y - double(a2)/b2 << "\n";
    
      return 0; 
    }
    

    演示:

        Process started (PID=7400) >>>
        enter x : 1.25
        enter y : 3.27
        1.25 * 3.27 = 327/80
        Error = 2.21177e-017
    

    注意:user463035818 首先调用了通过乘以 N 来近似浮点数的经典方法。用分数近似的一种理论上更好的方法是使用连分数,例如参见Fractions instead of decimals。然而,在没有乘法或除法的情况下做到这一点是一个挑战......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      相关资源
      最近更新 更多