《C语言程序设计现代方法:第2版 修订版》第五章课后编程题,水平有限,仅供参考。 1
#include <stdio.h>
int main (void)
{
int number;
printf("Enter a number:");
scanf("%d",&number);
if (100<=number&&number<=999)
printf("The number %d have 3 digits",number);
else if(10<=number&&number<=99)
printf("The number %d have 2 digits",number);
else
printf("The number %d have 1 digit",number);
return 0;
}
2
#include <stdio.h>
int main (void)
{
int hour,time;
printf("Enter a 24-hour time:");
scanf("%d:%d",&hour,&time);
if(hour>12){
printf("Equivalent 12-hour time: %d:%d PM",hour-12,time);
}
else{
printf("Equivalent 12-hour time: %d:%d AM",hour,time);
}
return 0;
}
3
#include <stdio.h>
int main (void)
{
float commission,value,pre_price,num,oppose;
printf("Please enter price of each shar:");
scanf("%f",&pre_price);
printf("Please enter the number of your share:");
scanf("%f",&num);
value=pre_price*num;
if(num<2000){
oppose=0.33*num+0.03;
}
else if(num>2000){
oppose=0.33*num+0.02;
}
if(value<2500.00f)
commission=30.00f+.017f*value;
else if(value<6250.00f)
commission=56.00f+.0066f*value;
else if(value<20000.00f)
commission=76.00f+.0034f*value;
else if(value<50000.00f)
commission=100.00f+.0022f*value;
else if(value<500000.00f)
commission=155.00f+.0011f*value;
else
commission=255.00f+.0009f*value;
if(commission<39.00f)
commission=39.00f;
printf("Commission:$%.2f\n",commission);
printf("Opponent:$%.2f\n",oppose);
return 0;
}
4
#include <stdio.h>
int main (void)
{
float speed;
printf("Please enter the speed:");
scanf("%f",&speed);
if(speed<1){
printf("It's Calm.");
}
else if(speed>=1&&speed<=3){
printf("It's Light air.");
}
else if(speed>=4&&speed<=27){
printf("It's Breeze.");
}
else if(speed>=28&&speed<=47){
printf("It's Gale.");
}
else if(speed>=48&&speed<=63){
printf("It's Storm.");
}
else{
printf("It's Hurricane.");
}
return 0;
}
5
#include <stdio.h>
int main (void)
{
float earning,tax;
printf("Please enter the number of your earning:");
scanf("%f",&earning);
if(earning<=750){
tax=earning*0.01;
}
else if(earning>=750&&earning<=2250){
tax=7.5+(earning-750)*0.02;
}
else if(earning>=2250&&earning<=3750){
tax=37.5+(earning-2250)*0.03;
}
else if(earning>=3750&&earning<=5250){
tax=82.5+(earning-3750)*0.04;
}
else if(earning>=5250&&earning<=7000){
tax=142.5+(earning-5250)*0.05;
}
else{
tax=230+(earning-7000)*0.06;
}
printf("The tax you should pay is %f",tax);
return 0;
}
6
#include <stdio.h>
int main (void)
{
int i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,first_sum,second_sum,total;
printf("Enter the first 11 digits of a UPC: ");
scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",&i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8,&i9,&i10,&i11,&i12);
first_sum=i1+i3+i5+i7+i9+i11;
second_sum=i2+i4+i6+i8+i10;
total=3*first_sum+second_sum;
if((9-((total-1)%10))==i12){
printf("VALID");
}
else{
printf("NOT VALID");
}
return 0;
}
7
#include <stdio.h>
int main (void)
{
int num1,num2,num3,num4;
int min1,max1,min2,max2;
printf("Enter four integers:");
scanf("%d %d %d %d",&num1,&num2,&num3,&num4);
if(num1>num2){
max1=num1;
min1=num2;
}
else{
max1=num2;
min1=num1;
}
if(num3>num4){
max2=num3;
min2=num4;
}
else{
max2=num4;
min2=num3;
}
if(min1<min2)min2=min1;
if(max1<max2)max1=max2;
printf("Lagest:%d\n",max1);
printf("Smallest:%d",min2);
return 0;
}
8
#include <stdio.h>
int main (void)
{
int hour,time,compare;
printf("Enter a 24-hour time:");
scanf("%d:%d",&hour,&time);
compare=hour*60+time;
if(compare<=8*60+51.5){
printf("Cloest departure time is 8:00 a.m,arrivint at 10:16 a.m.");
}
else if(compare>=8*60+51.5&&compare<=9*60+43+48){
printf("Cloest departure time is 9:43 a.m,arrivint at 11:52 a.m.");
}
else if(compare>=9*60+43+48&&compare<=11*60+19+44){
printf("Cloest departure time is 11:19 a.m,arrivint at 13:31 p.m.");
}
else if(compare>=11*60+19+44&&compare<=12*60+47+36.5){
printf("Cloest departure time is 12:47 p.m,arrivint at 15:00 p.m.");
}
else if(compare>=12*60+47+36.5&&compare<=14*60+52.5){
printf("Cloest departure time is 14:00 p.m,arrivint at 16:08 a.m.");
}
else if(compare>=14*60+52.5&&compare<=15*60+45+97.5){
printf("Cloest departure time is 15:45 p.m,arrivint at 17:55 p.m.");
}
else if(compare>=15*60+45+97.5&&compare<=19*60+82.5){
printf("Cloest departure time is 19:00 p.m,arrivint at 21:20 p.m.");
}
else{
printf("Cloest departure time is 21:45 p.m,arrivint at 23:58 p.m.");
}
}
9
#include <stdio.h>
int main (void)
{
int day1,month1,year1,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(year2<year1){
printf("%d/%d/%d is earlier than %d/%d/%d",month2,day2,year2,month1,day1,year1);
}
else if(year2==year1){
if(month2<month1){
printf("%d/%d/%d is earlier than %d/%d/%d",month2,day2,year2,month1,day1,year1);
}
else if(month2==month1){
if(day2<day1){
printf("%d/%d/%d is earlier than %d/%d/%d",month2,day2,year2,month1,day1,year1);
}
else if (day2>day1){
printf("%d/%d/%d is earlier than %d/%d/%d",month1,day1,year1,month2,day2,year2);
}
}
else{
printf("%d/%d/%d is earlier than %d/%d/%d",month1,day1,year1,month2,day2,year2);
}
}
else{
printf("%d/%d/%d is earlier than %d/%d/%d",month1,day1,year1,month2,day2,year2);
}
return 0;
}
10
#include <stdio.h>
int main (void)
{
int number,grade;
printf("Enter numerical grade:");
scanf("%d",&number);
if(number>100||number<0){
printf("Error!");
}
else{
grade=number/10;
switch (grade){
case 10:printf("Letter grade: A");break;
case 9:printf("Letter grade: A");break;
case 8:printf("Letter grade: B");break;
case 7:printf("Letter grade: C");break;
case 6:printf("Letter grade: D");break;
default:printf("Letter grade: F");break;
}
}
return 0;
}
11
#include <stdio.h>
int main (void)
{
int number,tens,ones;
printf("Enter a two-digit number:");
scanf("%d",&number);
tens=number/10;
ones=number%10;
if(tens==1){
switch(ones){
case 1:printf("You entered the number:eleven.");break;
case 2:printf("You entered the number:twelve.");break;
case 3:printf("You entered the number:thirteen.");break;
case 4:printf("You entered the number:fourteen.");break;
case 5:printf("You entered the number:fifteen.");break;
case 6:printf("You entered the number:sixteen.");break;
case 7:printf("You entered the number:seventeen.");break;
case 8:printf("You entered the number:eighteen.");break;
case 9:printf("You entered the number:nineteen.");break;
}
}
else{
switch(tens){
case 2:printf("You entered the number twenty-");break;
case 3:printf("You entered the number thirty-");break;
case 4:printf("You entered the number fourty-");break;
case 5:printf("You entered the number fifty-");break;
case 6:printf("You entered the number sixty-");break;
case 7:printf("You entered the number seventy-");break;
case 8:printf("You entered the number eighty-");break;
case 9:printf("You entered the number ninety-");break;
}
switch(ones){
case 1:printf("one.");break;
case 2:printf("two.");break;
case 3:printf("three.");break;
case 4:printf("four.");break;
case 5:printf("five.");break;
case 6:printf("six.");break;
case 7:printf("seven.");break;
case 8:printf("eight.");break;
case 9:printf("nine.");break;
}
}
return 0;
}
|