第五章 C语言程序设计

本文通过多个C语言编程实例,详细介绍了如何计算数字位数、转换24小时制时间、计算股票交易佣金、判断飓风等级、计算税收、验证商品条形码、找出最大最小值及最近航班时间等实用功能,旨在帮助读者理解和掌握C语言的条件判断和流程控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5.1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
	int a = 0, count = 0, x;
	printf("Enter a number: ");
	scanf("%d", &a);
	x = a;
	if (a == 0) {
		printf("The number 0 has 1 digits.\n");
	}
	else {
		while (a != 0) {
			a /= 10;
			printf("a==%d\n", a);
			count++;
		}
		printf("The number %d has %d digits.\n", x, count);
	}	
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
	int a;
	printf("Enter a number: ");
	scanf("%d", &a);

	if (a >= 0 && a <= 9) {
		printf("The number %d has 1 digits.\n", a);
	}
	else if (a > 9 && a < 100) /*a>=10&&a<=99*/ {
		printf("The number %d has 2 digits.\n", a);
	}
	else if (a < 99 & a < 1000) {
		printf("The number %d has 3 digits.\n", a);
	}

	return 0;
}

5.2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	int hour, minute;
	printf("Enter a 24-hour time: ");
	scanf("%d : %d", &hour, &minute);
	
	if (hour > 12) {
	 if (hour == 24) {
		printf("Equivalent 12-hour time: 0:%d AM\n", minute);
	 }
	 else {
		 hour -= 12;
		 printf("Equivalent 12-hour time: %d:%d PM\n", hour, minute);
	 }
	}
		
	else {
		printf("Equivalent 12-hour time: %d:%d AM\n", hour, minute);
	}
	return 0;
}

5.3
有点问题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	float number, per_share, value, commission, commission1;

	printf("Number of stocks entered and price per share: ");
	scanf("%f%f", &number, &per_share);
	value = number * per_share;
	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;
	}
	if (number < 2000) {
		commission1 = 33.03*number;
	}
	else {
		commission1 = number* 33.02;
	}
	printf("Commission: $%.2f\n", commission);
	printf("Your opponent :$%.2f\n", commission1);
	return 0;
}

5.4

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	int wind_velocity;
	printf("输入风速:");
	scanf("%d", &wind_velocity);
	if (wind_velocity < 1) { printf("Caim"); }
	else if (wind_velocity < 3) { printf("Light air"); }
	else if (wind_velocity < 28) { printf("Breeze"); }
	else if (wind_velocity < 48) { printf("Gale"); }
	else if (wind_velocity < 64) { printf("Storm"); }
	else if(wind_velocity >63){ printf("Hurricane"); }
	return 0;
}

5.5

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	float income, taxes;

	printf("Enter your income: ");
	scanf("%f", &income);

	if (income < 750) { taxes = income * 0.01; }
	else if(income<=2250){
		taxes = 7.50 + (income - 750.0) * 0.02;
	}
	else if (income <= 3750) {
		taxes = 37.50 + (income - 2250.0) * 0.03;
	}
	else if (income <= 5250) {
		taxes = 82.50 + (income - 3750.0) * 0.04;
	}
	else if (income <= 7000) {
		taxes = 142.50 + (income - 5250.0) * 0.05;
	}
	else {
		taxes = 230.00 + (income - 7000.0) * 0.06;
	}
	printf("taxes==%.2f\n", taxes);
	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

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	int a, b, c, d;
	int max1, min1, max2, min2, max, min;
	printf("Enter four integers: ");
	scanf("%d%d%d%d", &a, &b, &c, &d);

	if (a > b) {max1 = a; min1= b;}
	else {max1 = b; min1 = a;}

	if (c > d) {max2 = c; min2 = d;}
	else {max2= d; min2 = c;}

	if (max1 > max2) {max = max1; }
	else { max = max2; }

	if (min1 < min2) {min = min1; }
	else { min = min2; }

	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 4
void max_min(int a[], int n, int* max, int* min);
int main(void)
{
	int b[N], i, big, small;

	printf("Enter %d numbers: ", N);
	for (i = 0; i < N; i++) {
		scanf("%d", &b[i]);
	}

	max_min(b, N, &big, &small);

	printf("Largest: %d\n", big);
	printf("Smallest: %d\n", small);

	return 0;
}

void max_min(int a[], int n, int* max, int* min)
{
	int i;

	*max = *min = a[0];
	for (i = 1; i < n; i++) {
		if (a[i] > * max) { *max = a[i]; }
		else if (a[i] < *min) { *min = a[i]; }
	}
}

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

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	int year1, year2, month1, month2, day1, day2;
	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);

	int max=0 ,min = 0;

	if (year1 > year2) {
		printf("%d/%d/%d is earlier than %d/%d/%d", month2, day2, year2, \
			month1, day1, year1);
	}
	else if (year1 == year2) {
		if (month1 > month2) {
			printf("%d/%d/%d is earlier than %d/%d/%d", month2, day2, year2, \
				month1, day1, year1);
		}
		else if (month1 == month2) {
			if (day1 > day2) {
				printf("%d/%d/%d is earlier than %d/%d/%d", month2, day2, year2, \
					month1, day1, year1);
			}
			else if (day1 == day2) {
				printf("666");
			}
			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);
		}
	}
	else {
		printf("%d/%d/%d is earlier than %d/%d/%d", month1, day1, year1, \
			month2, day2, year2);
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值