我試着運行該程序,但它運行不正常。問題在於功能,但我不知道確切的位置。我首先聲明瞭這些函數,然後我嘗試在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);
}
你需要比這個更具體* * *。說'問題出在功能上'有點像飛機機械師說的'問題出在飛機的某個地方'。當你說你的程序不起作用你是什麼意思?它編譯失敗嗎?如果是這樣,您需要提供錯誤的詳細信息。如果不是,則需要提供您正在提供的輸入的詳細信息和您期望的輸出以及實際輸出。更好的是,提供一個MVCE(http://stackoverflow.com/help/mcve) –
將來,請解釋你的程序的預期行爲是什麼以及它實際做了什麼;如果編譯失敗,那麼告訴我們你的編譯器給你什麼錯誤消息。儘管一旦你開始尋找它,問題就突顯出來,但情況並非總是如此。即使其他人在嘗試運行代碼時遇到問題,他們也無法確定是否遇到了同樣的問題。謝謝。 –