• 【C Primer Plus】编程练习第五章


    1、

    #include <stdio.h>
    #include <string>
    #define H_PER_M 60
    int main()
    {
        int min, hour;
        printf("请输入分钟数:");
        scanf("%d", &min);
        getchar();
        while (min > 0) {
            hour = min / H_PER_M;
            min = min % H_PER_M;
            printf("总共%d小时,%d分钟
    ", hour, min);
            printf("请继续输入
    ");
            scanf("%d", &min);
            getchar();
        }
        printf("结束");
        return 0;
    }

     2、

    #include <stdio.h>
    #include <string>
    #define H_PER_M 60
    int main()
    {
        int a,i;
        printf("请输入一个整数:");
        scanf("%d", &i);
        getchar();
        a = i + 10;
        printf("%d", i); //这是为了让*号只存在于两个数之间
        while (i<a) {
            printf("*");
            i++;
            printf("%d", i);
            
        }
        getchar();
        return 0;
    }

    3、

    #include <stdio.h>
    #include <string>
    int main()
    {
        int weeks,days,day;
        printf("请输入工作天数:");
        scanf("%d", &days);
        getchar();
        weeks = days/7;
        day = days % 7;
        printf("%d days are %d weeks, %d days",days,weeks,day);
        getchar();
        return 0;
    }

     4、

    #include <stdio.h>
    #include <string>
    #define FEET (12*2.54)
    #define INCH 2.54
    int main()
    {
        float cm = 0;
        int feet = 0;
        float inch = 0;
        printf("Enter a height in centimeters:");
        scanf("%f", &cm);
        getchar();
        while (cm > 0)
        {
            feet = cm / FEET;
            inch = (cm - feet*FEET) / INCH;
            printf("%0.1f cm = %d feet, %0.1f inches
    ", cm, feet, inch);
            printf("Enter a height in centimeters (<=0 to quit) :");
            scanf("%f", &cm);
            getchar();
        }
        printf("bye");
        getchar();
        return 0;
    }

     

      

     5、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        int count, sum, day;
        count = 0;
        sum = 0;
        printf("请输入工作天数:");
        scanf("%d",&day);
        getchar();
        while (day > 0) {
            while (count++ < day)
                sum = sum + count;
            printf("sum = %d
    ", sum);
            sum = 0;
            count = 0;
            printf("请输入工作天数:");
            scanf("%d", &day);
            getchar();
        }
        
        getchar();
        return 0;
    }

     6、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        int count, sum, day,count_2;
        count = 0;
        sum = 0;
        printf("请输入工作天数:");
        scanf("%d",&day);
        getchar();
        while (day > 0) {
            while (count++ < day)
            {
                count_2 = count*count;  //用一个数保存count的平方
                sum = sum + count_2;
            }
            printf("sum = %d
    ", sum);
            sum = 0;
            count = 0;
            printf("请输入工作天数:");
            scanf("%d", &day);
            getchar();
        }
        
        getchar();
        return 0;
    }

    7、

    #include <stdio.h>
    #include <string>
    double da_3(double data);
    int main()
    {
        double data,a;
        while (1)
        {
            printf("请输入数组:");
            scanf("%lf", &data);
            getchar();
            a = da_3(data);
            printf("结果为%lf
    ", a);
        }
        getchar();
        return 0;
    }
    
    double da_3(double data) {
        double a = 0;
        a = data*data*data;
        return a;
    }

     8、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        int a, b,c;
        printf("This program computes moduli.
    ");
        printf("Enter an integer to serve as the second operand:");
        scanf("%d", &a);
        getchar();
        printf("Now enter the first operand:");
        scanf("%d", &b);
        getchar();
        while (b > 0) {
            c = b%a;
            printf("%d %% %d is %d
    ", b, a, c);
            printf("Enter next number for first operand(<= 0 to quit):");
            scanf("%d", &b);
            getchar();
        }
        printf("Done");
        getchar();
        return 0;
    }

     9、

    #include <stdio.h>
    #include <string>
    double wendu, st, kt,judge,hs;
    void Temperatures(double heat);
    
    int main()
    {
        printf("请输入华氏温度:");
        while (scanf("%lf", &wendu)) //输入时float 用 %f, double 用 %lf, 这是约定(规定),在这里浪费了好多时间,请注意
        {
            getchar();
            Temperatures(wendu);        
            printf("请输入华氏温度:");
        }
        getchar();
        return 0;
    }
    
    void Temperatures(double heat) {
        const double htos = 32;
        const double stok = 273.16;
        hs = heat;
        st = 5.0 / 9.0*(hs - htos);
        kt = st + stok;
        printf("华氏温度为:%.2f
    ", hs);
        printf("摄氏温度为:%.2f
    ", st);
        printf("开氏温度为:%.2f
    ", kt);
    }

     

  • 相关阅读:
    Redis批量删除key的小技巧,你知道吗?
    Spring条件注解@Conditional
    Spring Boot 2.X 如何快速集成单元测试?
    idea git提交时候提示 --author 'java_suisui' is not 'Name ' and matches no existing author
    Spring Boot 2.X 如何添加拦截器?
    SpringMVC+Mybatis 如何配置多个数据源并切换?
    Spring Boot 2.X 如何优雅的解决跨域问题?
    基于SSL实现MySQL的加密主从复制
    mysql -- mysql基于ssl的主从复制
    MySQL DB 主从复制之SSL
  • 原文地址:https://www.cnblogs.com/roscangjie/p/11796219.html
Copyright © 2020-2023  润新知