开发者

Now I can't get the numerator to output any number. When I do this I get 0/whatever denominator. What am I missing?

开发者 https://www.devze.com 2023-02-08 14:55 出处:网络
//This program evaluates an expression of two fractions added or subtracted #include <iostream> #include <cmath>
//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;
}


in function Crossmultiplication, your values for DenominatorA and DenominatorB are not properly initialized. Try to change it into something like

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

and change the function call accordingly.

Also, your return statement should be outside the switch construction. As it is now, it will never return anything. Alternatively, you could get rid of the NewNumerator variable altogether and write it like this:

    switch (Operation) {
        case '+': return NumeratorA*DenominatorB + NumeratorB*DenominatorA;
        case '-': return NumeratorA*DenominatorB - NumeratorB*DenominatorA;
    }


You have a typo:

int CrossMuliplication (int NewNumeratorA,int NewNumeratorB);
int CrossMultiplication(int, int);
0

精彩评论

暂无评论...
验证码 换一张
取 消