我无法获得正确的输出 [英] I can't get the right output
问题描述
#include <stdio.h>
void getInput(int* pNum1, int* pNum2);
void calc(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder,
double* pHalfNum1, double* pHalfNum2, double* pFraction, int* pAlgebraic);
int intOps(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder);
double doubleOps(int num1, int num2, double* pHalfNum1, double* pHalfNum2,
double* fraction);
int algebra(int num1, int num2);
void display(int num1, int num2, int sum, int quotient, int remainder,
double halfNum1, double halfNum2, double fraction, int algebraic);
int main(void)
{
int num1, num2;
int sum, quotient, remainder;
double halfNum1, halfNum2, fraction;
int algebraic;
getInput(&num1, &num2);
calc(num1, num2, &sum, "ient, &remainder, &halfNum1, &halfNum2,
&fraction, &algebraic);
display(num1, num2, sum, quotient, remainder,halfNum1, halfNum2,
fraction, algebraic);
return 0;
}
void getInput(int* pNum1, int* pNum2)
{
printf("\nPlease enter two integers : ");
scanf("%d%d", pNum1, pNum2);
}
void calc(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder,
double* pHalfNum1, double* pHalfNum2, double* pFraction, int* pAlgebraic)
{
intOps(num1, num2, pSum, pQuotient, pRemainder);
doubleOps(num1, num2, pHalfNum1, pHalfNum2, pFraction);
algebra(num1, num2);
}
int intOps(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder)
{
*pSum = num1 + num2;
*pQuotient = num1 / num2;
*pRemainder = num1 % num2;
}
double doubleOps(int num1, int num2, double* pHalfNum1, double* pHalfNum2,
double* pFraction)
{
*pHalfNum1 = num1 / 2.0;
*pHalfNum2 = num2 / 2.0;
*pFraction = num1 / (double)num2;
}
int algebra(int num1, int num2)
{
int algebraic;
int x, y;
x = num1;
y = num2;
algebraic = (2 * x) + (4 * y) + (x * y) - (x / y);
return algebraic;
}
void display(int num1, int num2, int sum, int quotient, int remainder,
double halfNum1, double halfNum2, double fraction, int algebraic)
{
printf("\n%20s%20s", "Description", "Data");
printf("\n%20s%20s", "-----------", "----");
printf("\n%20s%20d", "Input 1", num1);
printf("\n%20s%20d", "Input 2", num2);
printf("\n%20s%20d", "Sum", sum);
printf("\n%20s%20.1lf", "Half of input 1", halfNum1);
printf("\n%20s%20.1lf", "Half of input 2", halfNum2);
printf("\n%20s%20d", "Quotient", quotient);
printf("\n%20s%20d", "Remainder", remainder);
printf("\n%20s%20.4lf", "Fraction", fraction);
printf("\n%20s%20d", "Algebra", algebraic);
printf("\n\n");
}
</stdio.h>
这是我的代码.除代数方法外,它可以编译并正确运行.那总是返回0的结果.我在做什么错?
This is my code. It compiles and runs correctly except for the algebra method. That always returns a result of 0. What am i doing wrong?
推荐答案
您不将algebra
的结果存储在任何地方
我想您想将algebra(num1, num2);
行更改为*pAlgebraic = algebra(num1, num2);
you do not store the result ofalgebra
anywhere
I guess you want to change the linealgebra(num1, num2);
to*pAlgebraic = algebra(num1, num2);
您在calc
中的其他两个函数通过接受您答案的指针传递变量:
Your two other functions incalc
are passing in variables by pointer that accept your answers:
<br />
int intOps(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder)<br />
{<br />
*pSum = num1 + num2;<br />
*pQuotient = num1 / num2;<br />
*pRemainder = num1 % num2;<br />
}<br />
<br />
double doubleOps(int num1, int num2, double* pHalfNum1, double* pHalfNum2,<br />
double* pFraction)<br />
{<br />
*pHalfNum1 = num1 / 2.0;<br />
*pHalfNum2 = num2 / 2.0;<br />
*pFraction = num1 / (double)num2;<br />
}<br />
这就是他们存储答案以打印出来的方式.
您的Algegra函数接受值,计算结果并适当地返回结果
That is how they are storing the answers to print out.
your Algegra function takes in the values, calculates the result, and returns the result appropriately
<br />
int algebra(int num1, int num2)<br />
{<br />
...<br />
return algebraic;<br />
}<br />
但是,当您在函数calc
中调用它时,结果不会存储到预期的字段中.
代数(num1,num2);
就像第一个答案所建议的那样,您将需要将呼叫更改为:
*pAlgebraic = algebra(num1, num2);
在进行更改以更正此问题之前,请在algebra
函数中放置另一个printf实例进行测试,您将看到正确的答案:
printf("\ n%20s%20d",代数内部的结果",代数);
此致
保罗
However, when you call it in the function calc
, the result is not stored off into the intended field.
algebra(num1, num2);
Like the first answer suggests, you will want to change the call to this:
*pAlgebraic = algebra(num1, num2);
Before you make the change to correct this, place another instance of your printf to test with, in the algebra
function, and you will see the correct answer:
printf("\n%20s%20d", "result inside of Algebra", algebraic);
Regards,
Paul
这篇关于我无法获得正确的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!