《C Primer Plus》(第6版)编程练习——第4章

第1题

#include <stdio.h>
int main(void)
{
	char fname[40];
	char lname[40];
	
	printf("Enter your first name: ");
	scanf("%s", fname);
	printf("Enter your last name: ");
	scanf("%s", lname);
	printf("%s, %s\n", fname, lname);
	
	return 0;
}

第2题

#include <stdio.h>
#include <string.h>
int main(void)
{
	char name[40];
	int width;

	printf("Enter your name: ");
	scanf("%s", name);
	width = strlen(name);
	printf("\"%s\"\n", name);
	printf("\"%20s\"\n", name);
	printf("\"%-20s\"\n", name);
	printf("%*s\n", width + 3, name);
	
	return 0;
}

第3题

#include <stdio.h>
int main(void)
{
	float num;

	printf("Enter a floating-point number: ");
	scanf("%f", &num);
	printf("The input is %.1f or %.1e.\n", num, num);
	printf("The input is %+.3f or %.3E.\n", num, num);
	
	return 0;
}

第4题

#include <stdio.h>
int main(void)
{
	float height;
	char name[40];

	printf("Enter your height in inches: ");
	scanf("%f", &height);
	printf("Enter your name: ");
	scanf("%s", name);
	printf("%s, you are %.3f feet tall\n", name, height / 12.0);
	
	return 0;
}

第5题

#include <stdio.h>
#define BYTE_TO_BIT 8
int main(void)
{
	float speed, filesize, time;

	printf("Enter the downloading speed (Mb/s): ");
	scanf("%f", &speed);
	printf("Enter the filesize (MB): ");
	scanf("%f", &filesize);
	time = BYTE_TO_BIT * filesize / speed;
	printf("At %.2f megabits per second, a file of %.2f megabytes\n", speed, filesize);
	printf("downloads in %.2f seconds.\n", time);
	
	return 0;
}

第6题

#include <stdio.h>
#include <string.h>
int main(void)
{
	char fname[40];
	char lname[40];
	int fwidth;
	int lwidth;

	printf("Enter your first name: ");
	scanf("%s", fname);
	printf("Enter your last name: ");
	scanf("%s", lname);
	fwidth = strlen(fname);
	lwidth = strlen(lname);
	printf("%s %s\n", fname, lname);
	printf("%*d %*d\n", fwidth, fwidth, lwidth, lwidth);
	printf("%s %s\n", fname, lname);
	printf("%-*d %-*d\n", fwidth, fwidth, lwidth, lwidth);

	return 0;
}

第7题

#include <stdio.h>
#include <float.h>
int main(void)
{
	float ot_f = 1.0 / 3.0;
	double ot_d = 1.0 / 3.0;

	printf(" float values: ");
	printf("%.6f %.12f %.16f\n", ot_f, ot_f, ot_f);
	printf("double values: ");
	printf("%.6f %.12f %.16f\n", ot_d, ot_d, ot_d);
	printf("FLT_DIG: %d\n", FLT_DIG);
	printf("DBL_DIG: %d\n", DBL_DIG);

	return 0;
}

第8题

#include <stdio.h>
#define GALLON_TO_LITRE 3.785
#define MILE_TO_KM 1.609
int main(void)
{
	double miles;
	double gallons;
	double usa;
	double european;
	
	printf("Enter your mileages travelled in miles: ");
	scanf("%lf", &miles);
	printf("Enter your petrol comsuption in gallons: ");
	scanf("%lf", &gallons);
	usa = miles / gallons;
	european = gallons * GALLON_TO_LITRE / (100 * miles * MILE_TO_KM);
	printf("Miles per gallon: %.1f\n", usa);
	printf("Litres per 100 kilometers: %.1f\n", european);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值