C Primer Plus 第5章 运算符、表达式和语句 编程练习

   1、

#include <stdio.h>
const int S_PER_M = 60;

int main(void)
{
    int min, hour, lmin;

    printf("请输入分钟数: \n");
    scanf("%d", &min);
    while(min > 0)
    {
        hour = min / S_PER_M;
        lmin = min % S_PER_M;
        printf("%d分钟 = %d小时, %d分钟\n", min, hour, lmin);
        scanf("%d", &min);
    }
    printf("Done!\n");
    return 0;
}

   2、

#include <stdio.h>

int main(void)
{
    int num, i = -1;

    printf("请输入一个数: \n");
    scanf("%d", &num);
    printf("输出为: \n");
    while(i++ < 10)
        printf("%d ", num + i);
    return 0;
}

   3、

#include <stdio.h>

int main(void)
{
    int days, week, ldays;

    printf("请输入天数: \n");
    scanf("%d", &days);
    while(days > 0)
    {
        week = days / 7;
        ldays = days % 7;
        printf("%d days are %d week, %d days.\n", days, week, ldays);
        printf("请输入天数: \n");
        scanf("%d", &days);
    }
    printf("Done!\n");
    return 0;
}

   4、

#include <stdio.h>
#define CAST1 0.3937008
#define CAST2 12
int main(void)
{
    float cm, inch;
    int feet;

    printf("Enter a height in centimeters: ");
    scanf("%f", &cm);
    while(cm > 0)
    {
        feet = (int)(cm * CAST1) / CAST2;
        inch = cm * CAST1 - feet * CAST2;
        printf("%.1f cm = %d feet, %.1f inches\n", cm, feet, inch);
        printf("Enter a height in centimeters (<= 0 to quit): ");
        scanf("%f", &cm);
    }
    printf("bye\n");
    return 0;
}

   5、

#include <stdio.h>

int main(void)
{
    int count, sum, number;
    count = 0;
    sum = 0;
    printf("请您输入一个数: \n");
    scanf("%d", &number);
    while(count++ < number)
        sum += count;
    printf("1 + 2 + 3 + ... + %d = %d\n", number, sum);
    return 0;
}

   6、

#include <stdio.h>

int main(void)
{
    int count, sum, number;
    count = 0;
    sum = 0;
    printf("请您输入一个数: \n");
    scanf("%d", &number);
    while(count++ < number)
        sum += count * count;
    printf("1~%d的平方之和为: %d\n", number, sum);
    return 0;
}

   7、

#include <stdio.h>
void print(float fl);

int main(void)
{
    float n;
    printf("请输入一个float类型的数: \n");
    scanf("%f", &n);
    print(n);
    return 0;
}

void print(float fl)
{
    printf("%.2f的立方为: %.2f\n", fl, fl * fl * fl);
}

   8、

#include <stdio.h>
void Temperatures(double db);

int main(void)
{
    double fahrenheit;
    printf("请输入一个华氏温度: \n");
    while(scanf("%lf", &fahrenheit))
    {
        Temperatures(fahrenheit);
        printf("请输入一个华氏温度: \n");
    }
    printf("bye\n");
    return 0;
}
void Temperatures(double db)
{
    const double cast1 = 1.8;
    const double cast2 = 32.0;
    const double cast3 = 273.16;
    double celsius, kelvin;
    celsius = cast1 * db + cast2;
    kelvin = celsius + cast3;
    printf("华氏温度%.2f\n------>摄氏温度为%.2f\n------>绝对温度为%.2f\n", db, celsius, kelvin);
}

 

posted @ 2016-02-08 09:54  叶十一少  阅读(291)  评论(0编辑  收藏  举报