C++ 分数计算

C++ 分数计算,c++,function,C++,Function,我需要编写一个程序,可以使用用户输入的运算符计算分数。我有不同的函数来减少分数并找到最大公分母(我不确定是否正确设置了它们),然后我有函数calculate()来找到答案。我必须以分数形式而不是小数形式打印出最终答案 我遇到的问题是,我不知道如何将num3和den3从calculate()返回到主函数中。如果有人能帮忙,我将非常感激。多谢各位 以下是我目前的代码: /*Problem: Write a program to manipulate fractions. It allows for

我需要编写一个程序,可以使用用户输入的运算符计算分数。我有不同的函数来减少分数并找到最大公分母(我不确定是否正确设置了它们),然后我有函数calculate()来找到答案。我必须以分数形式而不是小数形式打印出最终答案

我遇到的问题是,我不知道如何将num3和den3从calculate()返回到主函数中。如果有人能帮忙,我将非常感激。多谢各位

以下是我目前的代码:

/*Problem: Write a program to manipulate fractions. It allows for the addition, subtraction, multiplication, or division of fractions.
    Inputs:
    Outputs:
*/

#include <iostream>
using namespace std;

void calculate(int num1, int den1, int& num3, int& den3, int num2, int den2);
void reduce(int& num, int& den);
int gcd(int a, int b);

int main(){
    int num1, num2, den1, den2, add, sub, mult, div;
    int op, calc;
    int num3, den3;

    cout << "Enter the numerator of the first fraction" << endl;
    cin >> num1;
    cout << "Enter the denominator of the first fraction" << endl;
    cin >> den1;
    cout << "Enter the numerator of the second fraction" << endl;
    cin >> num2;
    cout << "Enter the denominator of the second fraction" << endl;
    cin >> den2;
    cout << "Enter a 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
    cin >> op;
    calculate(op, num1, den1, num3, den3, num2, den2);
    cout << "The answer using option: " << op << endl;
    cout << "is " << num3 << " / " << den3 << endl;



    return 0;
}

void calculate(int op, int num1, int den1, int& num3, int& den3, int num2, int den2){
    if(op==1){
        num3 = num1 + num2;
        den3 = den1;
    }
    else if(op==2){
        num3 = num1 - num2;
        den3 = den1;
    }
    else if(op==3){
        num3 = num1 * num2;
        den3 = den1 * den2;
    }
    else if(op==4){
        num3 = num1 * den2;
        den3 = num2 * den1;
    }
}

void reduce(int& num, int& den){
    int reduced;
    reduced = gcd(num, den);
    num = num/reduced;
    den = den/reduced;

}

int gcd(int a, int b){
    int divisor=1, temp;

    while(b!= 0 || b>a){ 
        temp = a % b; 
        a = b; 
        b = temp = divisor; 
    }
    while(a!=0 || a>b){
        temp = b % a;
        b = a;
        a = temp = divisor;
    }

    return divisor;
}
/*问题:编写一个程序来处理分数。它允许分数的加、减、乘或除。
投入:
产出:
*/
#包括
使用名称空间std;
无效计算(整数m1、整数den1、整数和整数m3、整数和整数3、整数和整数m2、整数den2);
无效减少(整数和数字,整数和数字);
国际通用数据表(国际通用数据表a、国际通用数据表b);
int main(){
int num1、num2、den1、den2、add、sub、mult、div;
整数运算,计算;
int num3,den3;
coutnum1;
cout-den1;
coutnum2;
cout-den2;
cout-op;
计算(op,num1,den1,num3,den3,num2,den2);

cout最好是1)实现一个有理数类,还是2)使用std::pairs
作为分子和分母。然后,您可以在1的情况下返回一个有理数,或者在2的情况下返回一对

方法1允许你自然地处理奇点。如果分母为零,你可以抛出一个异常


另一种方法……

看看你的函数声明,你省略了第一个参数,op

void calculate(int num1, int den1, int& num3, int& den3, int num2, int den2);
然后按你的意愿调用calculate

calculate(op, num1, den1, num3, den3, num2, den2);
函数定义有op,所以您注意到声明和定义之间存在参数不匹配

void calculate(int op, int num1, int den1, int& num3, int& den3, int num2, int den2)
处理有理数的规范方法是声明一个std::pairs,或者声明一个具有分子和分母的类

你的加法和减法代码中也有一个缺陷。你需要考虑如何做有理加/减(参见:)。下面是固定的。你可以仔细查看GCD以确保它工作正常。

也可以考虑使用开关/case语句来决定操作,并且可以轻松地将数字输入转换为字母操作码。 这是您的代码,修复了一些缺陷,并做了一些其他更改

#include <iostream>
using namespace std;

class ratn //rational
{
public:
    int num;  //numerator
    int denom;  //denominator

    friend std::ostream& operator<< ( std::ostream& stream, const ratn& r);
};

std::ostream& operator<< (std::ostream& stream, const ratn& r)
{
    stream << r.num << " / " << r.denom;
    return stream;
}

int gcd(int a, int b);
void reduce(ratn& r);
void calculate(int op, ratn a, ratn b, ratn& r );

int gcd(int a, int b)
{
    int divisor=1, temp;
    while(b!= 0 || b>a){
        temp = a % b;
        a = b;
        b = temp = divisor;
    }
    while(a!=0 || a>b){
        temp = b % a;
        b = a;
        a = temp = divisor;
    }
    return divisor;
}

void
reduce(ratn& r )
{
    int reduced;
    reduced = gcd(r.num, r.denom);
    r.num = r.num/reduced;
    r.denom = r.denom/reduced;
}

char operands[8] = { ' ','+','-','*','/','%','^' };
void
calculate(int op, ratn a, ratn b, ratn& r )
{
    switch(op)
    {
    case ('+'):
        r.num = a.num*b.denom + b.num*a.denom;
        r.denom = a.denom*b.denom;
        //reduce(r);
        break;
    case ('-'):
        r.num = a.num*b.denom - b.num*a.denom;
        r.denom = a.denom*b.denom;
        //reduce(r);
        break;
    case ('*'):
        r.num = a.num * b.num;
        r.denom = a.denom * b.denom;
        break;
    case ('/'):
        r.num = a.num * b.denom;
        r.denom = b.num * a.denom;
        break;
    case ('%'):
        //r.num = a.num * b.denom;
        //r.denom = b.num * a.denom;
        break;
    case ('^'):
        //r.num = a.num * b.denom;
        //r.denom = b.num * a.denom;
        break;
    default :
        break;
    }
}

int
main()
{
    int op;
    ratn a, b, r;
    cout << "Enter first fraction, numerator" << endl;
    cin >> a.num;
    cout << "Enter first fraction, denominator" << endl;
    cin >> a.denom;
    cout << "Enter second fraction, numerator" << endl;
    cin >> b.num;
    cout << "Enter second fraction, denominator" << endl;
    cin >> b.denom;
    cout << "Enter operator, 1=addition, 2=subtraction, 3=multiplication, 4=division" << endl;
    cin >> op;
    calculate(operands[op], a, b, r );
    cout << "Answer:" ;
    cout << a << " "<<operands[op]<<" " << b << " = " << r << endl;

    return 0;
}
#包括
使用名称空间std;
类ratn//rational
{
公众:
int num;//分子
int denom;//分母

friend std::ostream&Operator您不需要做任何特殊的事情。您已经在做您需要的一切,只需将新值分配给
num3
den3
。调用者将观察这些新值。这就是引用的目的。我已经解决了这个问题,但现在当我输入这些数字时,我使用减法得到了这个答案:输入第一个分数的分子。2输入第一个分数的分母。4输入第二个分数的分子。1输入第二个分数的分母。2输入1表示加法,2表示减法,3表示乘法,或4表示除法。2使用选项的答案:2为1/4按任意键继续…考虑当DENOM1和DENOM2不匹配时需要做什么。您需要找出两个分母都是因子的最小数目。当试图编译固定代码时,我得到了这个错误:“1 >项目\实验室72 2好的实验室72 2好的实验室72.2。CPP(16):错误C4716:“操作员已修复。操作员银行很忙,科特里尔先生。我非常感谢。