c語言 複利(請幫我除錯)

題目:

複利率計算

不景氣的經濟環境下,銀行業為求生存常常喊出某些聳人聽聞的宣傳口號。其中常聽到的口號為『別家銀行一年計一次息,本銀行每月複利計息』,猶有甚者,甚至喊出諸如『別家銀行一年計一次息,本銀行每日複利計息』的口號以吸引客戶存款。假設年利率為2%,期初本金為100000元,存款期間為十年,請寫一程式執行以下計算;

1.計算出每年年底存款總額:年複利、月複利、日複利,皆須列出。

程式要求

1.你不可以使用任何內建數學函式

2.使用 for loop,你大約須使用3個for loop,一大loop包住兩小loop(巢狀loop)

3.在printf中使用 %15.0f來控制輸出。

Note: Proper indentation should be used.

Proper internal documentation should be included.

hint: 月複利的利率為 0.02 / 12,日複利的利率為 0.02 / 365

Sample output

Year Deposit (dayily) Deposit (monthly)Deposit (yearly)

1102020元(365)102018元(12)102000元(1)

2104081元(730)104078元(24)104040元(2)

3106184元(1095)106178元(36)106121元(3)

……………..

……………..

10122140元(3650)122120元(120)121899元(10)

-----------------------------------------------------------------------

我寫的:

#include <stdio.h>

int main(void)

{

int year=1,month=1,day=1;

float b=0.02,c=0.02/12,d=0.02/365,yearly=102000,monthly=102018,dayily=102020;

printf(" Year Deposit (dayily) Deposit (monthly) Deposit (yearly)\n");

for(year,yearly,b; year<=10 ; year++,yearly=yearly*b+yearly)

{

for(month,monthly,c;month<=12 ;month++,monthly=monthly*c+monthly)

for(day,dayily,d;day<=365 ;day++,dayily=dayily*d+dayily)

printf("%d %.0f %.0f %.0f\n",year,dayily,monthly,yearly);

}

getchar();

return 0;

}

---------------------------------------------------------------------------------------------

問題:

我寫的跑出來不對

請問要怎麼改

是for的{}錯了嗎?

for(year,yearly,b; year<=10 ; year++,yearly=yearly*b+yearly)

(這一句是指年利率)

跑了10次

for(month,monthly,c;month<=12 ;month++,monthly=monthly*c+monthly)

(這一句是指月利率)

跑了120次

for(day,dayily,d;day<=365 ;day++,dayily=dayily*d+dayily)

(這一句是指日利率)

跑了3650次

到了printf("%d %.0f %.0f %.0f\n",year,dayily,monthly,yearly);

當 年跑1次月跑12次日跑365次 時

輸出應該是 2 104081 104078 104040

2 Answers

Rating
  • ?
    Lv 4
    2 decades ago
    Favorite Answer

    你寫的不錯,快接近答案了.

    不過

    for(month....)

    for(day....)

    printf("%d %15.0f .....);

    前兩行要加 ; 否則會被算到 for loop 而一直壘加.

    另外不建議在for的括號內加入太多的運算式, 畢竟可讀性比你省打幾行字還重要.

    #include <stdio.h>

    int main(void)

    {

    int year, month, day;

    float b=0.02,c=0.02/12,d=0.02/365,yearly=100000,monthly=100000,dayily=100000;

    printf(" Year Deposit (dayily) Deposit (monthly) Deposit (yearly)\n");

    for(year = 1; year<=10 ; year++)

    {

    yearly += yearly*b;

    for(month = 1; month<=12 ;month++)

    monthly += monthly*c;

    for(day = 1; day<=365 ;day++)

    dayily += dayily*d;

    printf("%4d %15.0f %15.0f %15.0f\n",year,dayily,monthly,yearly);

    }

    getchar();

    return 0;

    }

  • Anonymous
    6 years ago

    到下面的網址看看吧

    ▶▶http://qoozoo09260.pixnet.net/blog

Still have questions? Get your answers by asking now.