【c語言程序設計精髓】第四周作業


1分數比較(4分)
題目內容:

利用人工方式比較分數大小的最常見的方法是:對分數進行通分后比較分子的大小。請編程模擬手工比較兩個分數的大小。首先輸入兩個分數分子分母的值,例如"11/13,17/19",比較分數大小后輸出相應的提示信息。例如,第一個分數11/13小於第二個分數17/19,則輸出"11/13<17/19"。

 

程序的運行結果示例1:

Input a/b, c/d:11/13,17/19↙

11/13<17/19

程序的運行結果示例2:

 

 

Input a/b, c/d:17/19,23/27↙

17/19>23/27

程序的運行結果示例3:

 

Input a/b, c/d:3/4,18/24↙

3/4=18/24

 

輸入提示信息:"Input a/b, c/d:"  (注意:逗號后面有一個空格)

輸入格式: "%d/%d,%d/%d"

輸出格式:

比較的結果是大於:"%d/%d>%d/%d\n"

比較的結果是小於:"%d/%d<%d/%d\n"

比較的結果是相等:"%d/%d=%d/%d\n"

為避免出現格式錯誤,請直接拷貝粘貼題目中給的格式字符串和提示信息到你的程序中。

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int a, b, c, d;
 5     int m, n;
 6     printf("Input a/b, c/d:");
 7     scanf("%d/%d,%d/%d", &a, &b, &c, &d);
 8     m = a * d;
 9     n = c * b;
10     if ( m > n ) 
11         printf("%d/%d>%d/%d", a, b, c, d);
12     else if ( m == n )
13         printf("%d/%d=%d/%d", a, b, c, d);
14     else
15         printf("%d/%d<%d/%d", a, b, c, d);
16     return 0;
17 }
 
2 存款利率計算器v2.0(4分)

題目內容:

 

 設capital是最初的存款總額(即本金),rate是整存整取的存款年利率,n 是儲蓄的年份,deposit是第n年年底賬號里的存款總額。已知如下兩種本利之和的計算方式:

  • 按復利方式計息的本利之和計算公式為:

               

  • 按普通計息方式計算本利之和的公式為:

              deposit  =  capital  * (1 + rate * n)

    編程從鍵盤輸入存錢的本金、存款期限以及年利率,然后再輸入按何種方式計息,最后再計算並輸出到期時能從銀行得到的本利之和,要求結果保留到小數點后4位。

提示:使用數學函數需要加入頭文件 <math.h>

程序的運行結果示例1:

 

Input rate, year, capital:0.0225,2,10000↙

Compound interest (Y/N)?Y

deposit = 10455.0625

 

程序的運行結果示例2:

 

Input rate, year, capital:0.0225,2,10000↙

Compound interest (Y/N)?n

deposit = 10450.0000

 

輸入提示信息:"Input rate, year, capital:"

輸入提示信息:"Compound interest (Y/N)?" 

本金、存款期限以及年利率的輸入格式: "%lf,%d,%lf"

是否選擇復利計算的輸入格式: " %c" (注意:%c的前面有一個空格。輸入的字符大小寫皆可,即Y或y,N或n皆可)

輸出格式:"deposit = %.4f\n"

為避免出現格式錯誤,請直接拷貝粘貼題目中給的格式字符串和提示信息到你的程序中。

時間限制:500ms內存限制:32000kb
 
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main()
 5 {
 6     int year;
 7     double rate;
 8     double capital;
 9     float deposit;
10     char c;
11     printf("Input rate, year, capital:");
12     scanf("%lf,%d,%lf", &rate, &year, &capital );
13     printf("Compound interest (Y/N)?");
14     scanf(" %c", &c);
15     if ( c == 'Y' || c == 'y' )
16         deposit = capital * pow((1+rate), year);
17     else
18         deposit = capital * (1 + rate * year);
19     printf("deposit = %.4f\n", deposit);
20 
21     return 0;
22 }

四條測試,通過了三條,有一條提示結果錯誤,原因未明。

 

3
存款利率計算器v3.0(9分)

題目內容:

設capital是最初的存款總額(即本金),rate是整存整取的存款年利率,n 是儲蓄的年份,deposit是第n年年底賬號里的存款總額。已知如下兩種本利之和的計算方式:

  • 按復利方式計息的本利之和計算公式為:deposit  =  capital * (1 + rate) n

  • 按普通計息方式計算本利之和的公式為:deposit  =  capital  * (1 + rate * n)

已知銀行整存整取不同期限存款的年息利率分別為:

存期1年,利率為 0.0225

存期2年,利率為 0.0243

存期3年,利率為 0.0270

存期5年,利率為 0.0288

存期8年,利率為 0.0300

若輸入其他年份,則輸出"Error year!"

 

編程從鍵盤輸入存錢的本金和存款期限,然后再輸入按何種方式計息,最后再計算並輸出到期時能從銀行得到的本利之和,要求結果保留到小數點后4位。

 

程序的運行結果示例1:

Input capital, year:10000,2↙

Compound interest (Y/N)?Y↙

rate = 0.0243, deposit = 10491.9049

 

程序的運行結果示例2:

 

Input capital, year:10000,2↙

Compound interest (Y/N)?n↙

rate = 0.0243, deposit = 10486.0000

 

程序的運行結果示例3:

Input capital, year:1000,4↙

Compound interest (Y/N)?y↙

Error year!

 

輸入提示信息:"Input capital, year:"

輸入提示信息:"Compound interest (Y/N)?"

存期輸入錯誤的提示信息: "Error year!\n"

本金存款期限的輸入格式: "%lf,%d"

是否選擇復利計算的輸入格式: " %c" (注意:%c的前面有一個空格。輸入的字符大小寫皆可,即Y或y,N或n皆可)

輸出格式:"rate = %.4f, deposit = %.4f\n"

為避免出現格式錯誤,請直接拷貝粘貼題目中給的格式字符串和提示信息到你的程序中。

時間限制:500ms內存限制:32000kb
 
 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main()
 5 {
 6     int year;
 7     double rate;
 8     double capital;
 9     float deposit;
10     char c;
11     printf("Input capital, year:");
12     scanf("%lf,%d", &capital, &year );
13     printf("Compound interest (Y/N)?");
14     scanf(" %c", &c);
15     switch(year) {
16         case 1: rate = 0.0225;    break;
17         case 2: rate = 0.0243;    break;
18         case 3: rate = 0.0270;    break;
19         case 5: rate = 0.0288;    break;
20         case 8: rate = 0.0300;    break;
21         default: rate = 0;         break;    
22     }
23     if (rate != 0 ) {
24         if ( c == 'Y' || c == 'y' )
25             deposit = capital * pow((1+rate), year);
26         else
27             deposit = capital * (1 + rate * year);
28         printf("rate = %.4f, deposit = %.4f\n", rate, deposit);
29     } else
30         printf("Error year!\n");
31         
32 
33     return 0;
34 }

9條測試,通過了6條,有3條提示結果錯誤。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM