1
//This program evaluates an expression of two fractions added or subtracted
#include <iostream>
#include <cmath>
using namespace std;
int CrossMultiplication(int, char, int);
int DenominatorConversion(int, int);
int main(){
    int NumeratorA;
    int NumeratorB;
    int DenominatorA;
    int DenominatorB;
    char Operation;
    char Slash;
    Slash = '/';
    cout << "This program evaluates an expression of two fractions added or subtracted.\n";
    cout << "The expression should look like this: A/B + C/D  where A, B, C, and D are ";
    cout << "integers, the + sign can be a - sign and spacing is not important.\n";
    cout << "\nEnter the expression: ";
    cin >> NumeratorA >> Slash >> DenominatorA >> Operation >> NumeratorB >> Slash >> DenominatorB;
    cout << NumeratorA  << Slash << DenominatorA << " " << Operation << " " << NumeratorB << Slash << DenominatorB << " " << "=" << " " << CrossMultiplication(NumeratorA,Operation,NumeratorB) << Slash << DenominatorConversion(DenominatorA,DenominatorB);
    cout << "\nThanks for using this program.";
    cin.get();
    cin.get();
    return 0;
}

int CrossMultiplication (int NumeratorA,char Operation, int NumeratorB){
    int NewNumerator;
    int DenominatorA;
    int DenominatorB;
    switch (Operation) {
        case '+': NewNumerator = NumeratorA*DenominatorB + NumeratorB*DenominatorA;
            break;
        case '-':NewNumerator = NumeratorA*DenominatorB - NumeratorB*DenominatorA;
            break;
    return NewNumerator;
    }
}

int DenominatorConversion(int DenominatorA, int DenominatorB){
    int NewDenominator;
    NewDenominator = DenominatorA*DenominatorB;
    return NewDenominator;
}
4

2 回答 2

1

你打错字了:</p>

int CrossMuliplication (int NewNumeratorA,int NewNumeratorB);
int CrossMultiplication(int, int);
于 2011-02-06T02:01:01.223 回答
1

在函数 Crossmultiplication 中,您的 DenominatorA 和 DenominatorB 的值未正确初始化。尝试将其更改为类似

int CrossMultiplication (int NumeratorA, int DenominatorA, char Operation, int NumeratorB, int Denominator B)

并相应地更改函数调用。

此外,您的 return 语句应该在 switch 构造之外。就像现在一样,它永远不会返回任何东西。或者,您可以完全摆脱 NewNumerator 变量并像这样编写它:

    switch (Operation) {
        case '+': return NumeratorA*DenominatorB + NumeratorB*DenominatorA;
        case '-': return NumeratorA*DenominatorB - NumeratorB*DenominatorA;
    }
于 2011-02-06T03:49:05.920 回答