【C Primer Plus】编程练习第四章

1、

#include <stdio.h>

int main()
{
    char a[10] = { 0 };
    char b[10] = { 0 };
    printf("请输入您的姓名:");
    scanf("%s,%s", a, b);
    getchar();
    printf("欢迎您%s,%s",a, b);
    getchar();
    return 0;
}

2、

#include <stdio.h>
#include <string>
int main()
{
    char a[100] = { 0 };
    printf("请输入您的姓名:");
    scanf("%s",a);
    getchar();
    int len = strlen(a);
    printf("%d
", len);
    printf(""%s"
",a);
    printf(""%20s"
", a);
    printf(""%-20s"
", a);
    printf(""%*s"",len+3, a); //如果后面只有一个参数,*没有意义,如果有两个参数,*代表打印数据长度,本句相当于 printf(""%10s"",a)
    getchar();
    return 0;
}

 3、

#include <stdio.h>
#include <string>
int main()
{
    float f=0;
    printf("请输入一个浮点数:");
    scanf("%f",&f);
    getchar();
    printf("The input is %0.1f or %0.1e", f, f);
    
    getchar();
    return 0;
}

 4、

#include <stdio.h>
#include <string>
int main()
{
    char name[20] = { 0 };
    float f=0;
    printf("请输入你的姓名和身高:");
    scanf("%s %f", name,&f); //这里要注意如果采用逗号来分隔的话,会导致&f并不会接收到数据,只有空格才能表示字符串输入结束
    getchar();
    printf("%s,you are %f feet tall", name,f);
    getchar();
    return 0;
}

 5、

#include <stdio.h>
#include <string>
int main()
{
    float speed, file,time,speeds;
    printf("请输入下载速度和文件大小:");
    scanf("%f,%f", &speed, &file);
    getchar();
    speeds = speed / 8;  //题中给出的为bit,要化为byte
    time = file / speeds;
    printf("At %0.2f megabits per second,a file of %0.2f megabytes
",speed,file);
    printf("downloads in %0.2f seconds", time);
    getchar();
    return 0;
}

 

 6、

#include <stdio.h>
#include <string>
int main()
{
    char xin[10] = { 0 };
    char min[10] = { 0 };
    printf("请输入您的名:");
    scanf("%s", min);
    getchar();
    printf("请输入您的姓:");
    scanf("%s", xin);
    getchar();
    int len_x, len_m;
    len_x = strlen(xin);
    len_m = strlen(min);
    printf("%s,%s
", min, xin);
    printf("%*d,%*d
", len_m, len_m, len_x, len_x);
    printf("%s,%s
", min, xin);
    printf("%-*d,%-*d", len_m, len_m, len_x, len_x);
    getchar();
    return 0;
}

 7、

#include <stdio.h>
#include <string>
int main()
{
    double a = 1.0 / 3.0;
    float b = 1.0 / 3.0;
    printf("%0.6lf,%0.6lf
", a, b);
    printf("%0.12lf,%0.12lf
", a, b);
    printf("%0.18lf,%0.18lf
", a, b);
    printf("%d,%d
", FLT_DIG, DBL_DIG);  //这两个宏在float.h头文件下面,用来说明double、float两种数据类型有效数字的位数,注意不是小数点后面的有效位数,而是所有位数。
    getchar();
    return 0;
}

 8、

#include <stdio.h>
#include <string>
#define GALLON 3.785
#define MILE 1.609*100
int main()
{
    float gallon = 0;
    float mile = 0;
    double gal_mil = 0;
    double l_m=0;
    printf("请输入里程和耗油量:");
    scanf("%f,%f", &mile, &gallon);
    getchar();
    gal_mil = mile / gallon;
    printf("消耗每加仑行驶英里数为:%0.1lf
", gal_mil);
    l_m = (gallon*GALLON) / (MILE*mile);
    printf("把它化为升每一百公里:%0.1lf", l_m);
    getchar();
    return 0;
}

 

原文地址:https://www.cnblogs.com/roscangjie/p/11792167.html