C primer plus 4.8
程序员文章站
2023-12-22 22:23:10
...
C primer plus 4.8
4.8.1
#include<stdio.h>
int main(void)
{
char first_name[20];
char last_name[20];
//
printf("请输入您的名:");
scanf("%s",first_name);
printf("请输入您的姓:");
scanf("%s",last_name);
//
printf("%s,%s",first_name,last_name);
//
return 0;
}
4.8.2
#include<stdio.h>
#include<string.h>
int main(void)
{
char name[20];
//
printf("请输入您的名字:");
scanf("%s",name);
//
printf("\"%s\"\n",name);
printf("\"%20s\"\n",name);
printf("\"%-20s\"\n",name);
printf("%*s\n",strlen(name)+3,name);
//
return 0;
}
4.8.3
#include<stdio.h>
int main(void)
{
float num;
//
printf("请输入一个浮点数:");
scanf("%f",&num);
//
printf("a.The input is %.1f or %.1e.\n",num);
printf("b.The input is %+.3f or %.3e.\n",num);
//
return 0;
}
4.8.4
#include<stdio.h>
int main(void)
{
float high;
char name[20];
//
printf("请输入您的身高(厘米):");
scanf("%f",&high);
printf("请输入您的姓名:");
scanf("%s",name);
//
printf("%s,you are %.3f meter tall",name,high/100);
//
return 0;
}
4.8.5
#include<stdio.h>
int main(void)
{
float speed;
float size;
float time;
//
printf("请输入下载速度(Mb/s):");
scanf("%f",&speed);
printf("请输入文件大小(Mb):");
scanf("%f",&size);
//
time=size/speed;
//
printf("At %.2f megabits per second,a file of %.2f megabytes downloads in %.2f seconds.",speed,size,time);
//
return 0;
}
4.8.6
#include<stdio.h>
#include<string.h>
int main(void)
{
char first_name[20];
char last_name[20];
//
printf("请输入您的名:");
scanf("%s",first_name);
printf("请输入您的姓:");
scanf("%s",last_name);
//
printf("%s%s\n%*d%*d\n\n",first_name,last_name,strlen(first_name),strlen(first_name),strlen(last_name),strlen(last_name));
printf("%s%s\n%-d%*d\n",first_name,last_name,strlen(first_name),strlen(first_name),strlen(last_name),strlen(last_name));
//
return 0;
}
4.8.7
#include<stdio.h>
#include<float.h>
int main(void)
{
double A;
float B;
//
A=1.0/3.0;
B=1.0/3.0;
//
printf("%.3f\n%.3f\n%d\n%d\n\n",A,B,FLT_DIG,DBL_DIG);
printf("%.6f\n%.6f\n%d\n%d\n\n",A,B,FLT_DIG,DBL_DIG);
printf("%.12f\n%.12f\n%d\n%d\n\n",A,B,FLT_DIG,DBL_DIG);
//
return 0;
}
4.8.8
#include<stdio.h>
#define num1 3.785
#define num2 1.609
int main(void)
{
float speed_mile_gallon;
float speed_litre_kilometre;
//
printf("请输入(英里/加仑):");
scanf("%f",&speed_mile_gallon);
//
speed_litre_kilometre=(1/speed_mile_gallon)*num1/num2;
//
printf("(升/公里):%.1f",speed_litre_kilometre) ;
//
return 0;
}
完