C语言程序设计现代方法(第二版)Chapter 5

第五章

1.确定一个数的位数

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
 	int x, y;
 	
 	printf("Enter a number : ");
 	scanf("%d", &x);
 	
 	y = x;
 	
 	int i;
 	
 	while (x != 0)
 	{
 		x /= 10;
 		i ++;
	}
	
	printf("The number %d has %d digits", y, i);
	
	system("pause");
	return 0;
}

2.输入24小时制时间,然后显示12小时制时间

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
	int hour, min;
	
	printf("Enter a 24-hour time:");
	scanf("%d:%d", &hour, &min);
	
	printf("Equivalent 12-hour time: ");
	
	if (hour <= 12)
	{
		printf("%d:%d  AM", hour, min);
	} 
	else 
	{
    printf("%d:%d  PM", hour - 12, min);
    }
    
    system("pause");
	return 0;
}

4.测量风力

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int v;
	
	printf("Enter the v: ");
	scanf("%d", &v);
	
	if (v < 1) 
	printf("Calm");
	else if (v < 4) printf("Light air");
	else if (v < 28) printf("Breeze");
	else if (v < 48) printf("Gale");
	else if (v < 64) printf("Storm");
	else 
	printf("Hurricane");
	
	system("pause");
	return 0;
}

7.找出四个数中的最大值和最小值(未使用if语句)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int a, b, c, d;
	
	printf("Enter your integer: ");
	scanf("%d %d %d %d", &a, &b, &c, &d);
	
	int max;
	
	max = a >= b ? a : b;
	max = max >= c ? max : c;
	max = max >= d ? max : d;
	
	int min;
	
	min = a <= b ? a : b;
	min = min <= c ? min : c;
	min = min <= d ? min : d;
	
	printf("Largest : %d \n", max);
	printf("Smallest : %d \n", min);
	
    system("pause");
	return 0; 
}

8.航班显示

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int hour, min;
	
	printf("Enter a 24-hour time: ");
	scanf("%d:%d", &hour, &min);
	
	printf("Close departure time is ");
	if (hour < 8) printf("8:00 a.m., arriving at 10:16 a.m.");
	else if (hour < 9) printf("9:43 a.m., arriving at 11:52 a.m.");
	else if (hour < 11) printf("11:19 a.m., arriving at 1:31 p.m.");
	else if (hour < 12) printf("12:47 a.m., arriving at 3:00 p.m.");
	else if (hour < 15) printf("2:00 p.m., arriving at 4:08 p.m.");
	else if (hour < 16) printf("3:45 p.m., arriving at 5:55 p.m.");
	else if (hour < 20) printf("7:00  p.m., arriving at 9:20 p.m.");
	else 
	printf("9:45 p.m., arriving at 11:58 p.m.");
	
    system("pause");
	return 0;
}

9.比较更早日期

#include <stdio.h>
#include <stdlib.h)
 
int main(void)
{
	int mm1, dd1, yyyy1;
	int mm2, dd2, yyyy2;
	
	printf("Enter first date (mm/dd/yyyy): ");
	scanf("%d/%d/%d", &mm1, &dd1, &yyyy1);
	
	printf("Enter second date (mm/dd/yyyy): ");
	scanf("%d/%d/%d", &mm2, &dd2, &yyyy2);
	
	int max, min;
	
	if (yyyy1 == yyyy2)
	{
		if (mm1 == mm2)
		{
			if (dd1 > dd2)
			{
				max = dd1;
				min = dd2; 
				printf("%d/%d/%d is earlier than %d/%d/%d",
	            min, dd1, yyyy1, max, dd2, yyyy2);
			}
			else 
			    max = dd2;
			    min = dd1;
			    printf("%d/%d/%d is earlier than %d/%d/%d",
	            min, dd1, yyyy1, max, dd2, yyyy2);
		}
		else if (mm1 > mm2)
		{
			printf("%d/%d/%d is earlier than %d/%d/%d",
			mm2, dd2, yyyy2, mm1, dd1, yyyy1);
		}
		else 
		    printf("%d/%d/%d is earlier than %d/%d/%d",
	    	mm1, dd1, yyyy1, mm2, dd2, yyyy2);
	}
	else if (yyyy1 > yyyy2)
	{
		printf("%d/%d/%d is earlier than %d/%d/%d",
		mm2, dd2, yyyy2, mm1, dd1, yyyy1);
	}
	else
	    printf("%d/%d/%d is earlier than %d/%d/%d",
	  	mm1, dd1, yyyy1, mm2, dd2, yyyy2);
	
	system("pause");
	return 0;
}

10.转化成绩等级

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int a, b;
	
	printf("Enter your grade: ");
	scanf("%1d%1d", &a, &b);
	
	printf("Letter grade: ");
	switch (a)
	{
		case 9: printf("A"); break;
		case 8: printf("B"); break;
		case 7: printf("C"); break;
		case 6: printf("D"); break;
		case 5: case 4: case 3: case 2: case 1: case 0:
	    	printf("F"); break;
		default :
			printf("ERROW!"); break;
	}
	
	system("pause");
	return 0;
} 

11.用户输入两位数,显示其英文单词

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int a, b;
	
	printf("Enter a two digits number: ");
	scanf("%1d%1d", &a, &b);
	
	int sum;
	sum = a * 10 + b;
	
	printf("You enter the number: ");
	
	switch (sum)
	{
		case 11: printf("Eleven \n"); break;
		case 12: printf("Twelve \n"); break;
		case 13: printf("Thirteen \n"); break;
		case 14: printf("Fourteen \n"); break;
		case 15: printf("Fifteen \n"); break;
		case 16: printf("Sixteen \n"); break;
		case 17: printf("Seventh \n"); break;
		case 18: printf("Eighteen \n"); break;
		case 19: printf("Nineteen \n"); break;
	}
	
	switch (a)
	{
		case 2: printf("Twenty"); break;
		case 3: printf("Thirty"); break;
		case 4: printf("Forty"); break;
		case 5: printf("Fifty"); break;
		case 6: printf("Sixty"); break;
		case 7: printf("Seventy"); break;
		case 8: printf("Eighty"); break;
		case 9: printf("Ninety"); break;
	}
	
	if (a != 1)
	{
	    switch (b)
	    {
		    case 0: printf(" \n"); break;
		    case 1: printf("-one \n"); break;
		    case 2: printf("-two \n"); break;
		    case 3: printf("-three \n"); break;
		    case 4: printf("-four \n"); break;
		    case 5: printf("-five \n"); break;
		    case 6: printf("-six \n"); break;
		    case 7: printf("-seven \n"); break;
		    case 8: printf("-eight \n"); break;
		    case 9: printf("-nine \n"); break;
	    }
    }
	
	system("pause");
	return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的企鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值