C Primer Plus第四章课后练习答案

其他章节答案

/*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;
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撼沧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值