概述
第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, %sn", 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("%*sn", 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 talln", 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 megabytesn", 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 %sn", fname, lname);
printf("%*d %*dn", fwidth, fwidth, lwidth, lwidth);
printf("%s %sn", fname, lname);
printf("%-*d %-*dn", 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 %.16fn", ot_f, ot_f, ot_f);
printf("double values: ");
printf("%.6f %.12f %.16fn", ot_d, ot_d, ot_d);
printf("FLT_DIG: %dn", FLT_DIG);
printf("DBL_DIG: %dn", 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: %.1fn", usa);
printf("Litres per 100 kilometers: %.1fn", european);
return 0;
}
最后
以上就是个性机器猫为你收集整理的《C Primer Plus》(第6版)编程练习——第4章的全部内容,希望文章能够帮你解决《C Primer Plus》(第6版)编程练习——第4章所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复