Fraction AddFractions(const Fraction& frac1, const Fraction& frac2)
{
Fraction frac3;
if (frac1.denom == frac2.denom)
{
frac3.num = frac1.num + frac2.num;
frac3.denom = frac1.denom;
}
else
{
frac3.denom = (frac1.denom * frac2.denom);
frac3.num = (frac1.num * frac2.denom) + (frac2.num * frac1.denom);
}
return frac3;
}
这是我的原型,有一个结构定义为Fraction
int num
和int denom
当我调用它只是给我回垃圾的功能时?我不知道它有什么问题。请帮忙。 =(
cout << "Please enter two Fractions" << endl;
cout << endl;
GetFraction(frac1);
cout << "Your fisrt fraction is: ";
PrintFraction(frac1);
cout << endl;
cout << endl;
GetFraction(frac2);
cout << "Your second fraction is: ";
PrintFraction(frac2);
cout << endl;
do
{
choice1 = MathMenu();
if (choice1 == ErrorOpt)
{
cout << "There was an error with what you have entered" << endl;
}
else if(choice1 != QuitOpt)
{
// do the math options
switch (choice1)
{
case AddOpt:
AddFractions(frac1, frac2);
break;
case SubtractOpt:
SubtractFractions(frac1, frac2);
break;
case MultiplyOpt:
MultiplyFractions(frac1, frac2);
break;
case DivideOpt:
DivideFractions(frac1, frac2);
break;
}
//print the results
PrintFraction(frac1);
PrintMathOption(choice1);
PrintFraction(frac2);
cout << " = ";
Standardize(frac3);
Reduce(frac3);
PrintFraction(frac3);
cout << endl;
Pause();
答案 0 :(得分:4)
您应该指定frac3
例如:
frac3 = AddFractions(frac1, frac2);
答案 1 :(得分:2)
从我的代码中可以看出来;在以下范围内没有变量frac3
:
Standardize(frac3);
Reduce(frac3);
PrintFraction(frac3);
你的数学函数正在被调用,但你没有获得回报。