分数计算器c ++

时间:2014-04-11 06:49:03

标签: c++

Iam试图找出一种方法来添加两个分数并根据输出得到最低形式或混合形式的结果分数,但问题是当我输入具有相同分母的两个分数时它只是弄乱了一切其他工作完美 这是我到目前为止的代码

#include<cmath>
#include<iostream>
using namespace std;
void reduce_fraction(int &numerator, int &denominator);
void addFractions(int numerator1Par, int denominator1Par, int numerator2Par, int              denominator2Par);
int main(){
    int numerator1 = 0;
    int denominator1 = 0;
    int numerator2 = 0;
    int denominator2 = 0;
    int unfactoredNumerator = 0;
    int resultDenominator = 0;
    int mixedNumberRemainder = 0;
    int mixedNumberWhole = 0;
    int reducedNumerator = 0;
    int reducedDenominator = 0;
    int result;
    int greatCommonDenominator;
    cout << "Enter first numerator:   ";
    cin >> numerator1;

    cout << "Enter first denominator: ";
    cin >> denominator1;
    cout << endl;


    cout << "Enter second numerator:   ";
    cin >> numerator2;

    cout << "Enter second denominator: ";
    cin >> denominator2;
    cout << endl;
    if (denominator1 == denominator2)
    {
            result = numerator1 + numerator2;
            if (unfactoredNumerator>resultDenominator){
                    int a = unfactoredNumerator / resultDenominator;
                    int b = unfactoredNumerator - (a*resultDenominator);
                    cout << numerator1 << "/" << denominator1 << " + " << numerator2 << "/" << denominator2 << " = " << a << " " << b << "/" << resultDenominator << endl;
            }
            else
            {
                    if (unfactoredNumerator == 0 && resultDenominator == 0)
                            greatCommonDenominator = 1;
                    else if (resultDenominator == 0)
                            greatCommonDenominator = unfactoredNumerator;
                    else if (unfactoredNumerator != 0) {
                            int a = unfactoredNumerator, b = resultDenominator;
                            while (a != b)
                                    if (a < b)
                                            b -= a;
                                    else
                                            a -= b;
                            greatCommonDenominator = b;
                    }
                    cout << numerator1 << "/" << denominator1 << " + " << numerator2 << "/" << denominator2 << " = " << (unfactoredNumerator / greatCommonDenominator) << "/" << (resultDenominator / greatCommonDenominator) << endl;
            }

    }
    else
    {
            addFractions(numerator1, denominator1, numerator2, denominator2);
    }
    system("pause>nul");
    return 0;
}

void reduce_fraction(int &numerator, int &denominator)
{
    for (int i = denominator * numerator; i > 1; i--) {
            if ((denominator % i == 0) && (numerator % i == 0))
            {
                    denominator /= i;
                    numerator /= i;
            }

    }
}
void addFractions(int numerator1Par, int denominator1Par, int numerator2Par, int denominator2Par)  {
    int unfactoredNumerator;
    int resultDenominator;
    int greatCommonDenominator;
    unfactoredNumerator = numerator1Par * denominator2Par + numerator2Par * denominator1Par;
    resultDenominator = denominator1Par * denominator2Par;
    if (unfactoredNumerator>resultDenominator){
            int a = unfactoredNumerator / resultDenominator;
            int b = unfactoredNumerator - (a*resultDenominator);
            cout << numerator1Par << "/" << denominator1Par << " + " << numerator2Par << "/" << denominator2Par << " = " << a << " " << b << "/" << resultDenominator << endl;
    }
    else
    {
            if (unfactoredNumerator == 0 && resultDenominator == 0)
                    greatCommonDenominator = 1;
            else if (resultDenominator == 0)
                    greatCommonDenominator = unfactoredNumerator;
            else if (unfactoredNumerator != 0) {
                    int a = unfactoredNumerator, b = resultDenominator;
                    while (a != b)
                            if (a < b)
                                    b -= a;
                            else
                                    a -= b;
                    greatCommonDenominator = b;
            }
            cout << numerator1Par << "/" << denominator1Par << " + " << numerator2Par << "/" << denominator2Par << " = " << (unfactoredNumerator / greatCommonDenominator) << "/" << (resultDenominator / greatCommonDenominator) << endl;
    }

}

1 个答案:

答案 0 :(得分:0)

如果分母相同,则您不会初始化 unfactoredNumerator resultDenominator ,因此它们始终为零。此外,在除以之前,您不会检查 resultDenominator 是否为零。 (你应该检查一下分子和分母的传入值是否为零,并相应地处理这些情况。)