8

在 C++ 中,当我计算 2/3 时,它会输出十进制值,我怎样才能得到原始格式(即 2/3)而不是 0.66666667

谢谢

4

13 回答 13

10

你不能。您需要编写一个专门用于保存有理数(即分数)的类。或者也许只是使用Boost Rational Number library

于 2011-01-27T16:37:04.420 回答
8

如果我理解正确,您有一个浮点数(afloatdouble类型变量),并且您希望将此值作为分数输出。

如果是这种情况,您需要进一步说明您的问题:

  • 根据定义,FP 数一个分数:FP 数由两个整数组成,一个尾数m和一个指数e(和一个符号,但这在这里无关紧要)。所以每个 FP 数实际上是一对(m,e),它所代表的值f是f=mb^e(其中b是固定整数基数,通常为 2)。因此,作为分数的自然表示只是m / b^(-e)e<0(如果e>=0f无论如何都是整数)。
  • 但是,您可能希望获得具有最小合理除数的分数。这是一个不同的问题。例如,您可以使用 Pari/GP 库中的bestappr函数。在您的情况下,您可能会使用bestappr(x, A),输入x,而A是您要尝试的最大分母。bestappr 将为您提供最接近x且分母仍小于A的分数。
于 2011-01-27T16:48:40.727 回答
6

编写自己的 Rational 类来计算除法

class Rational
{
public:
    int numerator, denominator;

    Rational(int num, int den=1){
        numerator = num;
        denominator=den;
    }
    Rational(Rational other){
        numerator = other.numerator;
        denominator = other.denominator;
    }
    double operator / (int divisor){
            denominator *= divisor;
            simplificate();
            return getrealformat();
    }
    Rational& operator / (int divisor){
            denominator *= divisor;
            simplificate();
            return this;
    }
    Rational& operator / (Rational &divisor){
            numerator *= divisor.numerator;
            denominator *= divisor.denominator;
            simplificate();
            return this;
    }
    double operator / (int divisor){
            denominator *= divisor;
            simplificate();
        return getrealformat();
    }
    double getrealformat(){
        return numerator/denominator;
    }
    simplificate(){
        int commondivisor = 1;
        for(int i=2;i<=min(abs(numerator), abs(denominator));i++)
            if( numerator%i == 0 && denominator%i == 0 )
                commondivisor = i;
        numerator /= commondivisor;
        denominator /= commondivisor;
    }
};

采用

Rational r1(45), r2(90), r3=r1/r2;
cout<<r3.numerator<<'/'<<r3.denominator;
cout<<r3.getrealformat();
于 2011-01-27T17:23:40.873 回答
4

我怎样才能得到原始格式(例如 2/3)而不是 0.66666667

仅通过使用自定义输出运算符包装诸如GMP库之类的东西非常困难。下面是关于 GMP 的更多信息:

什么是GMP?

GMP 是用于任意精度算术的免费库,可对有符号整数、有理数和浮点数进行操作。除了运行 GMP 的机器中的可用内存所暗示的精度外,对精度没有实际限制。GMP具有丰富的功能集,并且功能具有规则的接口。

GMP的主要目标应用是密码学应用和研究、互联网安全应用、代数系统、计算代数研究等。

GMP 经过精心设计,以尽可能快地处理小操作数和大操作数。速度是通过使用全字作为基本算术类型、使用快速算法、为许多 CPU 最常见的内部循环使用高度优化的汇编代码以及普遍强调速度来实现的。

GMP 比任何其他 bignum 库都快。GMP 的优势随着许多操作的操作数大小而增加,因为 GMP 使用渐近更快的算法。

第一个 GMP 版本于 1991 年发布。它不断开发和维护,大约每年发布一次新版本。

于 2011-01-27T16:38:22.410 回答
4

您必须将它们存储在某种带有两个整数字段的 Fraction 类中。当然,在将分数用于输出之前,您必须对其进行化简。

您可以开发自己的类或使用一些库,例如用于精确数学的库:CLN - 数字类库

于 2011-01-27T16:46:16.710 回答
3

这通常是不可能的:浮点数不精确,并且没有保留足够的信息来完全重建分数。

但是,您可以编写一个启发式地找到“最佳”近似值的函数,其中分子和分母较小的分数是首选,与浮点数具有几乎相同值的分数也是如此。

如果您完全控制代码,Oli 的想法会更好:首先不要丢弃信息。

于 2011-01-27T16:36:38.500 回答
2

您可以将所有分数的分子和分母存储为整数。整数在二进制中有精确的表示。

于 2011-01-27T16:40:37.837 回答
2

为了简化工作,我建议您尽可能坚持使用已知分母。

我正在使用一个应用程序,其中分数被限制为 2 的幂或使用 3(三分之二)的分母。

我使用近似值转换为这些分数(四舍五入到最接近的 1.0/24.0)。

如果没有一些限制,找到分母可能会很麻烦,并且会占用大量执行时间。

于 2011-01-27T18:36:11.610 回答
0

我是初学者,我使用的这种方式可能不是正确的方式

#include <iostream>

using namespace std;
int main ()
{
  double a;
  double b;
  double c;

  cout << "first number: ";
  cin >> a;
  cout << "second number: ";
  cin >> b;

  c = a/b;
  cout << "result is: " << c << endl;

  if (b != 0) {
    if (a > 0) {
      if (c - (int)c > 0 && c - (int)c < 1)
        cout << "fraction: " << a << "/" << b;
    } else {
      if (c - (int)c < 0 && c - (int)c < 1)
        cout << "fraction: " << a << "/" << b;
    }
  }

  return 0;
}
于 2013-06-10T07:12:18.340 回答
0

将这两个数字除以他们的 HCF 可能会有所帮助。

于 2014-06-09T08:52:26.263 回答
0
#include <iostream>
using namespace std;

int main() {
    int a,b,q,r;
    cin>>a>>b;//first number and second number
    q = a/b;
    r = a-q*b;
    cout<<q<<" "<<r<<" "<<"/"<<" "<<b<<"\n";
    return 0;
}

我刚刚得到 a/b 的商,然后得到 aq*b 的余数。如果有建议,请开放。

于 2015-10-06T21:46:13.143 回答
0

使用最大公约数概念。

如果我们用 gcd 将数字除以它们的数字,我们会得到这些数字的最小可能值。示例:-

#define si long long
int main() {
si int total=4;
si int count=2;
si int g= __gcd(count,total);
count/=g;
total/=g;
cout<<count<<"/"<<total<<endl;
}
for more reference check out this:-https://www.codechef.com/viewsolution/17873537
于 2018-03-18T03:11:56.980 回答
-1

这是一个将十进制数转换为分数的程序

#include<iostream>
using namespace std;

int main()
{

    float num, origNum, rem = 1;
    int den = 1, i, count=0, gcd=1;

    cout << "Enter any float number to convert it into mixed fraction: ";
    cin >> origNum;

    num = origNum - static_cast<int>(origNum);

    if (num > 0.1)
    {
        while ( (rem > 0.1) )
        {
            num = num * 10;
            rem = num - static_cast<int>(num);
            count++;
        }

        for (i = 1; i <= count; i++) // counter is for the calculation of denominator part of mixed fraction 
        {
            den = den * 10;
        }

        for (i = 2; i <= num|| i<=rem; i++)
        {
            if( (static_cast<int>(num) % i == 0) && (den % i == 0) )
            {
                gcd = i;
            }   
        }

        cout << (static_cast<int>(origNum)) << " and " << (static_cast<int>(num))/gcd << "/" << den/gcd;
    }
    else
        cout << (static_cast<int>(origNum));

    return 0;   
}
于 2015-02-22T20:17:49.013 回答