C語言程式設計現代方法第二版,第五章課後程式設計習題全部答案

皆編譯通過,但可能存在些遺漏,僅供參考

其餘章節還沒有學習,持續穩定緩慢更新中..

5.1

#include <stdio.h>
int main (void)
{
int num = 0;
int dig = -1;				// 初始化-1便於發現錯誤 
printf ("Enter a number: ");
scanf ("%d", &num);
if (num < 0) {				//題目並沒有說如果輸入是負數或五位數會怎麼樣,所以不更多考慮了 
}else if (num < 10) {
dig = 1;
}else if (num < 100) {
dig = 2;
}else if (num < 1000) {
dig = 3; 
}else if (num < 10000) {
dig = 4;
}
printf ("The number %d has %d digits", num, dig);
return 0;
} 

ps:加個 i f 即可判斷輸入是否合法,懶得加了

5.2

#include <stdio.h>
int main (void)
{
int hour, hour1, min;
printf ("Enter a 24-hour time: ");
scanf ("%d:%d", &hour, &min);
printf ("Equivalent 12-hour time: ");
if (hour > 12) {
hour1 = hour - 12;
printf ("%d:%.2d", hour1, min);
} 
else {
printf ("%d:%.2d", hour, min);
}
switch (hour) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:	case 10:  case 11:  case 12:	
printf ("AM");
break;
default: printf ("PM");
break;
}
return 0;
} 

ps: 這題感覺寫麻煩了,所以附上官方版

5.3

#include <stdio.h>
int main (void)
{
int num;
float price, value, commission1, commission2;
printf ("輸入購股數量:");
scanf ("%d", &num);
printf ("輸入每股單價:"); 
scanf ("%f", &price);
value = num * price;
//自己 
if (value < 2500.0f){
commission1 = 30.0f   0.017f * value;
} else if (value < 6250.0f) {
commission1 = 56.0f   0.0066f * value;
} else if (value < 20000.0f) {
commission1 = 76.0f   0.0034f * value;
} else if (value < 50000.0f) {
commission1 = 100.0f   0.0022f * value;
} else if (value < 500000.0f) {
commission1 = 155.0f   0.0011f * value;
} else {
commission1 = 255.0f   0.0009f * value;
}
if (commission1 < 39) commission1 = 39;
//競爭對手 
if (num < 2000){
commission2 = 33.03f * num; 
} else {
commission2 = 33.02f * num;
}
printf("你的佣金是: $%.2f ,競爭對手的佣金是: $%.2f", commission1, commission2);
return 0;
}

5.4

#include <stdio.h>
int main (void)
{
int speed;
printf ("Input the speed:");
scanf ("%d", &speed);
printf ("GRADE: ");
if (speed < 1){
printf ("Clam");
} else if (speed <= 3) {
printf ("Light air");
} else if (speed <= 27){
printf ("Breeze");
} else if (speed <= 47){
printf ("Gale");
} else if (speed <= 63){
printf ("Storm");
} else printf ("Hurricane");
return 0;
}

5.5

#include <stdio.h>
int main (void)
{
float income, tax;
printf ("Enter the income value: ");
scanf ("%f", &income);
if (income < 750.0f) {                     
tax = income * 0.01;
} else if (income < 2250.0f) {
tax = 7.5f   (income - 750.0f) * 0.02;
} else if (income < 3750.0f) {
tax = 37.5f   (income - 2250.0f) * 0.03;
} else if (income < 5250.0f) {
tax = 82.5f   (income - 3750.0f) * 0.04;
} else if (income < 7000.0f) {
tax = 142.5f   (income - 5250.0f) * 0.05;
} else tax = 230.0f   (income - 7000.0f) * 0.06;
printf ("The tax to be paid is %.4f", tax);
return 0;
} 

5.6

#include <stdio.h>
int main (void)
{
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, c;
int first_sum, second_sum, total;
int check;
printf ("Enter the 12 digits of a UPC: ");
scanf ("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&d, &i1, &i2, &i3, &i4, &i5, &j1, &j2, &j3, &j4, &j5, &c);
first_sum = d   i2   i4   j1   j3   j5;
second_sum = i1   i3   i5   j2   j4;
total = 3 * first_sum   second_sum;
check =  9 - ((total - 1) % 10);
if (check == c)
printf ("VALID");
else 
printf ("NOT VALID");
return 0;
}

5.7

#include <stdio.h>
int main (void)
{
int a, b, c, d;
int min1, min2, max1, max2;
printf ("Enter four integers: ");
scanf ("%d%d%d%d", &a, &b, &c, &d);
if (a < b) {
min1 = a;
max1 = b;
} 
else {
min1 = b;
max1 = a;	
}
if (c < d) {
min2 = c;
max2 = d;
}
else {
min2 = d;
max2 = c;
}
if (min1 > min2) min1 = min2;
if (max1 < max2) max1 = max2;
printf ("Largest: %d\n", max1);
printf ("Smallest: %d", min1);
return 0;
}

5.8

#include <stdio.h>
int main (void)
{
int hours, minutes;
int time;
printf ("Enter a 24-hour time:");
scanf ("%d:%d", &hours, &minutes);
time = hours * 60   minutes;
// 480 583 679 767 840 945 1140 1305 這是幾個起飛時間換算為分鐘的結果 
if (time < 480){
printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
} else if (time < 583) {
if ((time-480) < (583-time)) printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
else printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
} else if (time < 679) {
if ((time-583) < (679-time)) printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
else printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m");
} else if (time < 767) {
if ((time-679) < (767-time)) printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
else printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m");
} else if (time < 840) {
if ((time-767) < (840-time)) printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m.");
else printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
} else if (time < 945) {
if ((time-840) < (945-time)) printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
else printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
} else if (time < 1140) {
if ((time-945) < (1140-time)) printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
else printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
} else {
if ((time-1140) < (1305-time)) printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
else printf ("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
}
return 0;	
} 

5.9

#include <stdio.h>
// 根據題目, 這道題輸入的年份需要在同一個世紀, 否則會出錯 
int main (void) 
{
int day1, month1, year1;
int day2, month2, year2;
printf ("Enter first date (mm/dd/yy): ");
scanf ("%d/%d/%d", &month1, &day1, &year1);
printf ("Enter second date (mm/dd/yy): ");
scanf ("%d/%d/%d", &month2, &day2, &year2);
if (year1 < year2) {
printf ("%d/%d/%.2d is earlier than %d/%d/%.2d", month1, day1, year1, month2, day2, year2);	
} else if (year1 == year2) {
if (month1 < month2) {
printf ("%d/%d/%.2d is earlier than %d/%d/%.2d", month1, day1, year1, month2, day2, year2);	
} else if (month1 == month2) {
if (day1 < day2) {
printf ("%d/%d/%.2d is earlier than %d/%d/%.2d", month1, day1, year1, month2, day2, year2);	
} else if (day1 == day2) {
printf ("%d/%d/%.2d is equal than %d/%d/%.2d", month1, day1, year1, month2, day2, year2);		
} else {
printf ("%d/%d/%.2d is earlier than %d/%d/%.2d", month2, day2, year2, month1, day1, year1);	
}
} else {
printf ("%d/%d/%.2d is earlier than %d/%d/%.2d", month2, day2, year2, month1, day1, year1);	
}
} else {
printf ("%d/%d/%.2d is earlier than %d/%d/%.2d", month2, day2, year2, month1, day1, year1);	
}
return 0;
}

5.10 以下兩道題就加入了輸入資料是否合法的判斷

#include <stdio.h>
int main (void)
{
int num;
printf ("Enter numerical grade: ");
scanf ("%d", &num);
if (num < 0 || num >100) {
printf ("Illegal input!");
return 0;
}
switch (num/10) {
case 10:
case 9 :
printf ("Lstter grade: A");
break;
case 8 :
printf ("Lstter grade: B");
break;
case 7 :
printf ("Lstter grade: C");
break;
case 6 :
printf ("Lstter grade: D");
break;
default :
printf ("Lstter grade: F");
break;
}
return 0;	
} 

5.11

#include <stdio.h>
int main (void)
{
int num;
printf ("Enter a two-digit number: ");
scanf ("%d", &num);
if (num < 10 || num > 99) {
printf ("Illegal input!");
return 0;
}
printf("You entered the number ");
switch (num / 10) {
case 9:
printf ("ninety");
break;
case 8: 
printf ("eighty");
break;
case 7:
printf ("seventy");
break;
case 6:
printf ("sixty");
break;
case 5:
printf ("fifty");
break;
case 4:
printf ("fourty");
break;
case 3:
printf ("thirty");
break;
case 2:
printf ("twenty");
break;
case 1:
switch (num % 10) {
case 0: printf ("ten"); break;
case 1: printf ("eleven"); break;
case 2: printf ("twelve"); break;
case 3: printf ("thirteen"); break;
case 4: printf ("fourteen"); break;
case 5: printf ("fifteen"); break;
case 6: printf ("sixteen"); break;
case 7: printf ("seventeen"); break;
case 8: printf ("eighteen"); break;
case 9: printf ("nineteen"); break;
}
return 0;
}
switch (num % 10) {
case 9:
printf ("-nine");
break;
case 8: 
printf ("-eight");
break;
case 7:
printf ("-seven");
break;
case 6:
printf ("-six");
break;
case 5:
printf ("-five");
break;
case 4:
printf ("-four");
break;
case 3:
printf ("-three");
break;
case 2:
printf ("-two");
break;
case 1:
printf ("-one");
break;
case 0:
break;
}
return 0;
}