gpt4 book ai didi

c - 我的代码中的功能不起作用

转载 作者:行者123 更新时间:2023-11-30 21:08:13 25 4
gpt4 key购买 nike

我尝试运行该程序,但它运行不正确。问题出在函数上,但我不知 Prop 体在哪里。我首先声明了这些函数,然后尝试在 main 中调用它们。但是,我不确定情况是否如此。我认为问题出在函数定义上?但我不知道还能做什么。如果有人能看一下并向我指出,那就太好了。

#include <stdio.h>
#include <math.h>

float computeMonthlyPayment(float p, float YearRate, float YearTerm);
float computeMonthlyInterest(float p, float YearRate);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm);

int main()
{

float n, p, r, MonthlyPayment, YearRate, YearTerm;

printf("Enter the loan amount: ");
scanf("%f", &p);

if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}

printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);

if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}

printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);

if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}


float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);


return 0;
}


float computeMonthlyPayment(float p, float YearRate, float YearTerm)
{
float r = YearRate/12;
float n = YearTerm*12;
float MonthlyPayment = 0;

MonthlyPayment = (r*p)/1-((1+r)/n);

return MonthlyPayment;
}


float computeMonthlyInterest(float p, float YearRate)
{
float r = 0;
r = ((YearRate)/12)/12;

return r;

}


void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n)
{

printf("LOAN INFORMATION\n");
printf("-----------------------\n");
printf("Initial loan amount: %.2f\n", p);
printf("Annual interest rate: %.3f\n", YearRate);
printf("Monthly interest rate: %.3f\n", r);
printf("Term of loan (years): %f\n", YearTerm);
printf("Term of loan (months): %f\n", n);
printf("Monthly payment amount: %.2f\n", MonthlyPayment);

}

最佳答案

看一下 main() 中的第 48..50 行。

int main()
{

// ...

float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);

// ...
}

这些是函数的原型(prototype),而不是您实际调用它们的方式。您应该以与调用 printf()scanf() 相同的方式调用它们,通过向它们传递适当的参数。就目前而言,您只是在 main() 中重新声明函数,而不是实际调用它们。尝试这样的事情:

int main()
{

// ...

r = computeMonthlyInterest(p, YearRate); // r should be labeled better.
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);

// ...
}

但这仍然没有 100% 修复,因为 n 尚未初始化。虽然 printLoanInfo() 说明了它的用途(这也是我识别 r 的方式),但您忘记在 main() 中设置它。所以..

int main()
{

// ...

r = computeMonthlyInterest(p, YearRate);
n = YearTerm * 12; // You did this in computeMonthlyPayment(), but not here.
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);

// ...
}

因此,考虑到这些更改,您的 main() 可能应如下所示:

int main()
{

float n, p, r, MonthlyPayment, YearRate, YearTerm;

printf("Enter the loan amount: ");
scanf("%f", &p);

if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}

printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);

if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}

printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);

if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}


// Remove these lines:
// float computeMonthlyInterest(float p, float YearRate);
// float computeMonthlyPayment(float p, float YearRate, float YearTerm);
// void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);


// Add these lines:
r = computeMonthlyInterest(p, YearRate);
n = YearTerm * 12;
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);


return 0;
}

关于c - 我的代码中的功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com