【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分钟\n", hour, min);
        printf("请继续输入\n");
        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\n", 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\n", 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\n", 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\n", 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.\n");
    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\n", 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\n", hs);
    printf("摄氏温度为:%.2f\n", st);
    printf("开氏温度为:%.2f\n", kt);
}

 

 

 

 

 

 

posted @ 2019-11-05 08:25  一颗蘋果  阅读(358)  评论(1编辑  收藏  举报