C Primer Plus 第6版 第四章 编程练习参考答案

编译环境VS Code+WSL GCC 源码请到文末下载

/*第1题*************************/
#include<stdio.h>
int main()
{
    char xing[10],ming[20];
    printf("请输入你的姓:");
    scanf("%s",xing);
    printf("请输入你的名:");
    scanf("%s",ming);
    printf("你的名是:%s,你的姓是:%s",ming,xing);
    getchar();
    return 0;
}

/*第2题*************************/
#include<stdio.h>
#include<string.h>
int main()
{
    char name_x[20],name_m[20];
    int name_x_lenth,name_m_lenth;

    printf("请输入你的姓:");
    scanf("%s",name_x);
    printf("请输入你的名:");
    scanf("%s",name_m);

    name_x_lenth = strlen(name_x);
    name_m_lenth = strlen(name_m);

    printf("\"%s,%s\"\n",name_x,name_m);
    printf("\"%20s,%s\"\n",name_x,name_m);
    printf("\"%-s,%-20s\"\n",name_x,name_m);
    printf("\"%*s,%s\"\n",name_m_lenth + name_x_lenth +3,name_x,name_m);
    getchar();
    return 0;
}
/*第3题*************************/
#include<stdio.h>
int main()
{
    double input_val;
    printf("请输入一个浮点数:");
    scanf("%lf",&input_val);
    printf("The input is %0.1f or %.1e\n",input_val,input_val);
    printf("The input is %+0.3f or %.3E\n",input_val,input_val);

    getchar();
    return 0;
}
/*第4题*************************/
#include<stdio.h>
int main()
{
    double stature;
    char name[30];
    printf("请输入你的升高(英寸):");
    scanf("%lf",&stature);
    printf("请输入你的姓名:");
    scanf("%s",name);
    printf("%s, you are %.3f feet tall\n",name,stature);

    getchar();
    return 0;
}
/*第5题*************************/
#include<stdio.h>
int main()
{
    float user_dload_speed,user_file_size;
    printf("请输入网速(MB/s):");
    scanf("%f",&user_dload_speed);
    printf("请输入下载文件的大小(MB):");
    scanf("%f",&user_file_size);
    printf("%.2fMB/s 下载%.2fMB 文件需要%.2f秒\n",
    user_dload_speed,user_file_size,user_file_size / user_dload_speed);
    getchar();
    return 0;
}
/*第6题*************************/
#include<stdio.h>
#include<string.h>
int main()
{
    char name_x[20],name_m[20];
    int name_x_lenth,name_m_lenth;

    printf("请输入你的姓:");
    scanf("%s",name_x);
    printf("请输入你的名:");
    scanf("%s",name_m);

    name_x_lenth = strlen(name_x);
    name_m_lenth = strlen(name_m);

    printf("%s %s\n",name_x,name_m);
    printf("%*d %*d\n",name_x_lenth,name_x_lenth,name_m_lenth,name_m_lenth);

    printf("%s %s\n",name_x,name_m);
    printf("%d %*d\n",name_x_lenth,name_x_lenth,name_m_lenth);
    getchar();
    return 0;
}
/*第7题*************************/
#include<stdio.h>
#include<float.h>
int main()
{
    float num_1 = 1.0/3.0;
    double  num_2 = 1.0/3.0;
    long long flt_dig_val = FLT_DIG,dbl_dig_val = DBL_DIG;
    printf("%.6f %.12f %.16f\n",num_1,num_1,num_1);
    printf("%.6f %.12f %.16f\n",num_2,num_2,num_2);
    printf("FLT_DIG:%lld DBL_DIG:%lld\n",flt_dig_val,dbl_dig_val);
    getchar();
    return 0;
}
/*第8题*************************/
#include<stdio.h>
int main()
{
    const float GALLON_TO_LITERS = 3.785;
    const float MILE_TO_KM       = 1.609;
    float user_mile,user_gas,user_consumes;
    printf("请输入你的旅行里程(英里):");
    scanf("%f",&user_mile);
    printf("请输入消耗的汽油量(加仑):");
    scanf("%f",&user_gas);
    user_consumes = (user_gas * GALLON_TO_LITERS) / (user_mile * MILE_TO_KM) * 100.0;
    printf("你的油耗是:%.1f升 / 100公里\n",user_consumes);
    getchar();
    return 0;
}
posted @ 2022-07-16 20:34  PYPYN  阅读(46)  评论(0编辑  收藏  举报