1. 程式人生 > >C Prime Plus(第6版) 第四章答案

C Prime Plus(第6版) 第四章答案

/*4.1答案*/
#include<stdio.h>
int main()
{
	char pre[20],af[20];
	printf("please input your name and your famliyname: ");
	scanf("%s%s",af,pre);	
	printf("%s,%s",af,pre);
	
} 
/*4.2答案*/
#include<stdio.h>
#include<string.h>
int main()
{
	char first_name[20],last_name[20];
	int first_length,last_length;
	printf("please input your first name:");
	scanf("%s",first_name);
	printf("please input your last name:");
	scanf("%s",last_name);
	first_length = strlen(first_name);
	last_length = strlen(last_name);
	printf("\"%s %s\"\n",first_name,last_name);
	printf("\"%+20s %+20s\"\n",first_name,last_name);
	printf("\"%-20s %-20s\"\n",first_name,last_name);
	printf("\"%*s %*s\"\n",first_length+3,first_name,last_length+3,last_name);
}
/*4.3 答案*/
#include<stdio.h>
int main()
{
	float number;
	printf("input a float number:");
	scanf("%f",&number);
	printf("a.The input is %.1f or %.1e\n",number,number);
	printf("b.The input is +%.3f or %.3E\n",number,number);
	
}
/*4,4/
#include<stdio.h>
int main()
{
	char name[20];
	float height;
	printf("please input your name and your height:");
	scanf("%s",name);
	scanf("%f",&height);
	printf("%s,you are %.3f feet tall",name,height);	
} 
/*4.5答案*/
#include<stdio.h>
int main()
{
	float Mb,size;
	printf("input:");
	scanf("%f%f",&Mb,&size);
	printf("At %.2f megabits per second, a file of %.2 megabytes\n",Mb,size);
	printf("downloads in %.2f seconds.",size/(Mb/8));
}
/*4.6答案*/
#include<stdio.h>
#include<string.h>
int main()
{
	char first_name[20],last_name[20];
	int first_length,last_length;
	scanf("%s %s",first_name,last_name);
	first_length = strlen(first_name);
	last_length = strlen(last_name);
	printf("%s %s\n%*d %*d\n",first_name,last_name,first_length,first_length,last_length,last_length);
	printf("%s %s\n%-*d %-*d\n",first_name,last_name,first_length,first_length,last_length,last_length);
	
}
/*4.7*/
#include<stdio.h>
#include<float.h>
int main()
{
	double a = 1.0/3.0;
	float b = 1.0/3.0;
	printf("double:%.6f float:%.6f\n",a,b);
	printf("double:%.12f float:%.12f\n",a,b);
	printf("double:%.16f float:%.16f\n",a,b);
//	printf("%f %f",FLT_DIG,DBI_DIG);
}
/*4.8*/

#include<stdio.h>
const float L = 3.785;
const float K = 1.609;
int main()
{
	float kilo,jk; 
	printf("kilometers and waste:");
	scanf("%f%f",&kilo,&jk);
	printf("%.1f\n",kilo/jk);
	kilo *= K;
	jk *= L;
	printf("%.1f",jk/(kilo/100)); 
}