zoukankan      html  css  js  c++  java
  • 【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);
    }

     

  • 相关阅读:
    东芝开发板驱动OLED模块显示LOGO图片
    东芝MCU实现位带操作
    使用系统定时器SysTick实现精确延时微秒和毫秒函数
    VC++调试错误的解决方案
    #pragma once与 #ifndef的区别
    strcmp()字符串比较函数用法
    C、C++中的static和extern关键字
    error LNK1169 找到一个或多个多重定义的符号的解决方法
    vs2013编译obs源码
    Qt线程—QThread的使用--run和movetoThread的用法
  • 原文地址:https://www.cnblogs.com/roscangjie/p/11796219.html
Copyright © 2011-2022 走看看