码迷,mamicode.com
首页 > 其他好文 > 详细

C Primer Plus 第4章 字符串和格式化输入/输出 编程练习

时间:2016-02-07 17:24:43      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

   1、

#include <stdio.h>
int main(void)
{
    char first_name[40];
    char last_name[40];

    printf("请输入您的名字: ");
    scanf("%s", last_name);
    printf("请输入您的姓氏: ");
    scanf("%s", first_name);
    printf("%s, %s", last_name, first_name);

    return 0;
}

   2、

#include <stdio.h>
#include <string.h>
int main(void)
{
    char name[40];

    printf("请输入您的名字: \n");
    scanf("%s", name);
    printf("\"%s\"\n", name);
    printf("\"%20s\"\n", name);
    printf("\"%-20s\"\n", name);
    printf("\"%*s\"\n", strlen(name) + 3, name);

    return 0;
}

   3、

#include <stdio.h>

int main(void)
{
    float number;

    printf("请输入一个浮点数: \n");
    scanf("%f", &number);
    printf("The input is %.1f or %.1e\n", number, number);
    printf("The input is %+.3f or %.3e\n", number, number);
    return 0;
}

   4、

#include <stdio.h>

int main(void)
{
    float feet;
    char name[40];

    printf("请输入您的身高: \n");
    scanf("%f", &feet);
    printf("请输入您的名字: \n");
    scanf("%s", name);
    printf("%s, you are %.3f m tall.\n", name, feet / 100);

    return 0;
}

   5、

#include <stdio.h>
#include <string.h>
int main(void)
{
    char lname[30];
    char fname[30];

    printf("请输入名字: \n");
    scanf("%s", lname);
    printf("请输入姓氏: \n");
    scanf("%s", fname);
    printf("%10s %10s\n", lname, fname);
    printf("%10d %10d\n", strlen(lname), strlen(fname));
    printf("%-10s %-10s\n", lname, fname);
    printf("%-10d %-10d\n", strlen(lname), strlen(fname));
    return 0;
}

   6、

#include <stdio.h>
#include <float.h>
int main(void)
{
    double n1 = 1.0 / 3.0;
    float n2 = 1.0 / 3.0;
    printf("n1 = %.4f\n", n1);
    printf("n1 = %.12f\n", n1);
    printf("n1 = %.16f\n", n1);
    printf("n2 = %.4f\n", n2);
    printf("n2 = %.12f\n", n2);
    printf("n2 = %.16f\n", n2);
    printf("FLT_DIG = %d, DBL_DIG = %d\n", FLT_DIG, DBL_DIG);
    return 0;
}

   7、

#include <stdio.h>
#define CAST1 3.785
#define CAST2 1.609
int main(void)
{
    float miles;
    float gallons;
    printf("请输入行驶的英里数: \n");
    scanf("%f", &miles);
    printf("请输入消耗汽油的加仑数: \n");
    scanf("%f", &gallons);
    printf("消耗每加仑汽油行驶的英里数: %.1f\n", miles / gallons);
    printf("每100公里的升数: %.1f\n", gallons * CAST1 / (miles * CAST2 / 100));

    return 0;
}

 

C Primer Plus 第4章 字符串和格式化输入/输出 编程练习

标签:

原文地址:http://www.cnblogs.com/yerenyuan/p/5184676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!