0

我需要编写一个程序,可以使用用户输入的运算符计算分数。我有不同的函数来减少分数并找到最大公分母(我不确定我是否正确设置它们),然后我有函数 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;
}
4

2 回答 2

1

1)实现一个有理数类,或者 2)使用 std::pairs<int, int>作为分子和分母会更好吗?然后你总是可以在 1 的情况下返回一个有理数,或者在 2 的情况下返回一对。

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

只是另一种方法....

于 2013-10-24T23:14:10.013 回答
0

查看您的函数声明,您省略了第一个参数 op。

void calculate(int num1, int den1, int& num3, int& den3, int num2, int den2);

然后你按照你的意图调用计算,

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 以确保它正常工作。

此外,您可以考虑使用 switch/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;
}
于 2013-10-24T23:16:01.900 回答