其他章节答案
/*The answer for Unit 4*/
/*
//Project 1
#include <stdio.h>
int main(void)
{
char name[40],surname[40];
printf("Please enter your name and surname.\n");
scanf("%s%s",name,surname);
printf("%s,%s\n",name,surname);
return 0;
}
//Project 2
//这题不会
#include <stdio.h>
int main(void)
{
printf("Please enter your name and surname.\n");
scanf("%s%s",name,surname);
printf("\"%s %s\"\n",name,surname);
}
//Project 3
#include <stdio.h>
int main(void)
{
float f3;
scanf("%f",&f3);
printf("a.输入%.1f或%.1e;\n",f3,f3);
printf("b.输入%+.3f或%.3E;\n",f3,f3);
return 0;
}
//Project 4
#include <stdio.h>
int main(void)
{
float height;
char name[40];
printf("Enter your height:_____cm\b\b\b\b\b\b\b");
scanf("%f",&height);
printf("Enter your name: ");
scanf("%s",name);
printf("%s,you are %.2f m tall\n",name,height/100.0);
return 0;
}
//Project 5
#include <stdio.h>
int main(void)
{
float spead,file,time;
printf("Enter the spead of download:_____Mb/s\b\b\b\b\b\b\b\b\b");
scanf("%f",&spead);
printf("Enter the size of file:_____MB\b\b\b\b\b\b\b");
scanf("%f",&file);
time=file*8/spead;
printf("At %.2f megabits per second, a file of %.2f megabytes\ndownloads in %.2f seconds.\n"
,spead,file,time);
return 0;
}
*/
//Project 6
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[40],surname[40];
int str1,str2;
printf("Enter your name: ");
scanf("%s",name);
printf("Enter your surname: ");
scanf("%s",surname);
printf("%s %s\n",name,surname);
printf("%*d %*d\n",strlen(name),strlen(name),strlen(surname),strlen(surname));
//In the book's 75 pages, it said that %zd is also applicable to strlen(),but in fact it doesn't apply.Use %d directly!
printf("%s %s\n",name,surname);
printf("%-*d %-*d\n",strlen(name),strlen(name),strlen(surname),strlen(surname));
return 0;
}
//Project 7
#include <stdio.h>
#include <float.h>
//#define FLT_DIG 6
//#define DBL_DIG 15
//计算里存放浮点型数据,并不能精确表示。
//这两个宏在float.h头文件下面,用来说明double、float两种数据类型有效数字的位数,注意不是小数点后面的有效位数,而是所有位数。
//#define DBL_DIG 15 /* # of decimal digits of precision */
//#define FLT_DIG 6 /* # of decimal digits of precision */
//float能保证的有效位数最多是6~7位,完全能保证的是6位,double是15~16位,完全能保证的是15位。
int main(void)
{
double a = 1.0/3.0;
float b = 1.0/3.0;
printf("double values:%.6f,%.12f,%.16f;\nfloat values:%.6f,%.12f,%.16f;\n",a,b);
printf("FLT_DIG:%d,\nDBL_DIG:%d\n",FLT_DIG,DBL_DIG);
return 0;
}
//Project 8
#include <stdio.h>
#define GL 3.785
#define YQ 1.609
int main(void)
{
float juli,jialun,haoyou1,haoyou2;
printf("请输入行驶里程(英里):");
scanf("%f",&juli);
printf("请输入消耗汽油量(加仑):");
scanf("%f",&jialun);
haoyou1 = jialun/juli;
printf("燃料消耗:%.1f英里/加仑",haoyou1);
haoyou2 = (jialun*3.785)/(juli*1.609/100);
printf(" OR %.1f升/100公里\n",haoyou2);
return 0;
}
网友评论