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

C Primer Plus(第6版)—第4章编程练习

时间:2020-05-01 20:57:38      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:can   环境   对齐   练习   file   ubuntu   %s   引号   结构   

编程环境:VSCode + Remote-WSL,Ubuntu 20.04 LTS (GNU/Linux 4.4.0-18362-Microsoft x86_64)

/*
    编写程序,提示用户输入名和姓,然后以“名 姓”的格式打印出来
*/

#include<stdio.h>

int main(void){
    char name[10];
    char last_name[10]; 

    printf("What`s your name: ");
    scanf("%s",  name);

    printf("What`s your last name: ");
    scanf("%s", last_name);

    printf("%s.%s\n", last_name, name);

    return 0;
}
/*
    编写程序,提示用户输入名字,并执行以下操作:
    1. 打印名字,包括双引号
    2. 在宽度为 20 的字段右端打印名字,包括双引号
    3. 在宽度为 20 的字段左端打印名字,包括双引号
    4. 再比姓名宽度宽 3 的字段中打印名字
*/

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

    printf("Please input your name: ");
    scanf("%s", name);

    printf("your name: \"%s\"\n", name);
    printf("your name: \"%20s\"\n", name);
    printf("your name: \"%-20s\"\n", name);

    int width = strlen(name) + 3;
    printf("your name: |%*s| \n", width, name);

    return 0;
}

 理想是美好的,Debug 是残酷的。结果不重要,重要的是过程,耐心

/*
    编写程序,读取浮点数,
    首先以小数点记数法打印,然后以指数记数法打印
*/
#include<stdio.h>
int main(void){
    float number;

    printf("Please input float number: ");
    scanf("%f", &number);

    printf("The input is %.1f or %.1e .\n", number);
    printf("The input is %+.3f or %.3E .\n", number);

    return 0;
}
/*
    编写程序,提示用户输入身高(厘米)和姓名,然后显示
*/
#include<stdio.h>
int main(void){
    float height;
    char name[30];

    printf("Please input your name and hight(cm): ");
    scanf("%s %f", name, &height);

    float height_m = height / 100;
    printf("your name: %s, your hight: %.0f cm.\n", name, height);
    printf("your name: %s, your hight: %.2f m.\n", name, height_m);

    return 0;
}

实践出真知,注意琐碎的细节,逐步优化程序结构

/*
    编写程序,提示用户输入以兆位每秒(Mb/s)为单位的下载速度,
    以兆字节(MB)为单位的文件大小,程序计算文件的下载时间
*/
#include<stdio.h>
int main(void){
    float download_speed;
    float file_size;
    float download_time;

    printf("Please input file size(MB) and download speed(Mb/s): ");
    scanf("%f %f", &file_size, &download_speed);

    download_time = file_size / download_speed;
    if (download_speed != 0)
    {
        printf("download_time: %.0f \n", download_time);
    }else
    {
        printf("Waitting... \n");
    }

    return 0;
}
/*
    编写程序,提示用户输入名,再输入姓,在一行打印用户输入的名和姓
    下一行分别打印名和姓的字母数,先右对齐,再左对齐
*/
#include<stdio.h>
int main(void){
    char name[30];
    char last_name[30];
    int sum_name;
    int sum_last_name;
    int width_name;
    int width_last_name;

    printf("Please input your name: ");
    scanf("%s", name);
    printf("Please input your last name: ");
    scanf("%s", last_name);

    sum_name = strlen(name);
    sum_last_name = strlen(last_name);
    width_name = sum_name;
    width_last_name = sum_last_name;

    printf("%s|", name);
    printf("%s|\n", last_name);
    printf("%*d|", width_name, sum_name);
    printf("%*d|\n", width_last_name, sum_last_name);

    printf("|%s", name);
    printf("|%s\n", last_name);
    printf("|%-*d", width_name, sum_name);
    printf("|%-*d\n", width_last_name, sum_last_name);
    return 0;
}

 

C Primer Plus(第6版)—第4章编程练习

标签:can   环境   对齐   练习   file   ubuntu   %s   引号   结构   

原文地址:https://www.cnblogs.com/yiweshen/p/12814507.html

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