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


    编程环境: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
    ", 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"
    ", name);
        printf("your name: "%20s"
    ", name);
        printf("your name: "%-20s"
    ", name);
    
        int width = strlen(name) + 3;
        printf("your name: |%*s| 
    ", 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 .
    ", number);
        printf("The input is %+.3f or %.3E .
    ", 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.
    ", name, height);
        printf("your name: %s, your hight: %.2f m.
    ", 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 
    ", download_time);
        }else
        {
            printf("Waitting... 
    ");
        }
    
        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|
    ", last_name);
        printf("%*d|", width_name, sum_name);
        printf("%*d|
    ", width_last_name, sum_last_name);
    
        printf("|%s", name);
        printf("|%s
    ", last_name);
        printf("|%-*d", width_name, sum_name);
        printf("|%-*d
    ", width_last_name, sum_last_name);
        return 0;
    }
  • 相关阅读:
    一道经典的线程间通信的编程题
    Windows Live Writer代码插件整理
    Squeeze Excitation Module 对网络的改进分析
    IGC(Interleaved Group Convolutions)
    探秘移动网络模型
    sparse_softmax_cross_entropy_with_logits
    四行公式推完神经网络BP
    视觉跟踪:MDnet
    tensorflow API _ 6 (tf.gfile)
    tensorflow API _ 4 (Logging with tensorflow)
  • 原文地址:https://www.cnblogs.com/yiweshen/p/12814507.html
Copyright © 2020-2023  润新知