gpt4 book ai didi

c - 循环和函数 :Retirment lab for class

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:32 25 4
gpt4 key购买 nike

我无法弄清楚我的代码有什么问题。有两个问题是年份不会从 0 1 2 等它变成 1 1 或 0 0,还有第二个问题是赢得利息像我预期的那样加起来。我不知道它出了什么问题,所以我希望能有第二双眼睛看到它,所以提前谢谢。这是源代码和我输出的一部分。

#include <stdio.h>
#define TARGET_VALUE 2000000
#define RATE .10




int main(void)
{

int year;
double balance, target, finalbalance, deposit, rate, interest;

printf("Enter starting balance ($): ");
scanf("%lf", &balance);
printf("Enter amount deposited every year ($): ");
scanf("%lf", &deposit);
printf("Enter estimated annual interest rate (\%%): ");
scanf("%lf", &rate);
printf("Enter target balance ($): ");
scanf("%lf", &target);

year = 0;
interest = 0;

finalbalance = balance + deposit + interest;

printf("\nYear Deposit Interest Balance");
printf("\n---- ------- -------- -------");
printf("\n%d %.2lf %.2lf %.2lf", year, deposit, interest,
finalbalance);
do {
finalbalance = finalbalance + deposit + interest;
printf("\n%d %.2lf %.2lf %.2lf", year, deposit, interest,
finalbalance);
year += 1;
interest = (finalbalance + deposit) * RATE;
} while (finalbalance <= target);

printf("\nIn year %d, balance %.2lf reaches target %.2lf", year, balance, target);
}



Enter starting balance ($):250000
Enter amount deposited every year($):20000
Enter estimated annual interest rate(%):10
Enter target balance ($): 2000000

year deposit interest balance
0 20000.0 0.00 270000.00
0 20000.0 0.00 290000.00
1 20000.0 31000.00 341000.00
2 20000.0 36100.00 397100.00

这是我的老师给出的预期输出,但我的代码没有匹配。

Enter starting balance ($): 250000.0
Enter amount deposited every year ($): 20000.0
Enter estimated annual interest rate (%): 10.0
Enter target balance ($): 2000000.0
Year Deposit Interest Balance
---- ------- -------- -------
0 250000.00 0.00 250000.00
1 20000.00 27000.00 297000.00
2 20000.00 31700.00 348700.00
3 20000.00 36870.00 405570.00
4 20000.00 42557.00 468127.00
. . .
. . .
In year ??, balance ????? reaches target 2000000.00

最佳答案

切换循环中发生的一些事情。打印前递增年份。这些宏是不必要的,您的版本忽略了输入利率。通常最好将 \n 换行符放在 printf() 格式字符串的末尾。将输入利率除以 100。balancefinalbalance 是多余的。在第 0 年,存款是初始余额。最重要的是,您可以通过为 printf() 指定宽度来使一切变得漂亮。

#include <stdio.h>

int main(void)
{

int year;
double balance, target, deposit, rate, interest;

printf("Enter starting balance ($): ");
scanf("%lf", &balance);
printf("Enter amount deposited every year ($): ");
scanf("%lf", &deposit);
printf("Enter estimated annual interest rate (%%): ");
scanf("%lf", &rate);
rate = rate * 0.01;
printf("Enter target balance ($): ");
scanf("%lf", &target);

year = 0;
interest = 0;

printf("Year Deposit Interest Balance\n");
printf("---- --------- ---------- ----------\n");

static const char fmt_str[] = "%4d %10.2lf %10.2lf %10.2lf\n";
printf(fmt_str, year, balance, interest, balance);

// we haven't checked if target was reached with the initial deposit so use
// while instead of do...while
while (balance < target)
{
year += 1;
interest = (balance + deposit) * rate;
balance = balance + deposit + interest;
printf(fmt_str, year, balance, interest, balance);
}

printf("In year %3d, balance %.2lf reaches target %.2lf\n", year, balance, target);
return 0;

}

输出:

Enter starting balance ($): 250000.0
Enter amount deposited every year ($): 20000.0
Enter estimated annual interest rate (%): 10.0
Enter target balance ($): 2000000.0
Year Deposit Interest Balance
---- --------- ---------- ----------
0 250000.00 0.00 250000.00
1 297000.00 27000.00 297000.00
2 348700.00 31700.00 348700.00
3 405570.00 36870.00 405570.00
4 468127.00 42557.00 468127.00
5 536939.70 48812.70 536939.70
6 612633.67 55693.97 612633.67
7 695897.04 63263.37 695897.04
8 787486.74 71589.70 787486.74
9 888235.41 80748.67 888235.41
10 999058.96 90823.54 999058.96
11 1120964.85 101905.90 1120964.85
12 1255061.34 114096.49 1255061.34
13 1402567.47 127506.13 1402567.47
14 1564824.22 142256.75 1564824.22
15 1743306.64 158482.42 1743306.64
16 1939637.30 176330.66 1939637.30
17 2155601.03 195963.73 2155601.03
In year 17, balance 2155601.03 reaches target 2000000.00

关于c - 循环和函数 :Retirment lab for class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28710239/

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