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

一、编写一个程序,将用分钟表示的时间转换成以小时和分钟表示的时间。使用#define或const来建立一个表明60的符号常量。使用while循环来容许用户重复键入值,而且当键入一个小于等于0的时间终止循环。编程

#include 
#define HOUR 60

int main()
{
    int minutes;
    printf("Please enter the minutes:");
    scanf("%d",&minutes);
    while(minutes>0)
    {
        printf("%d hour,%d minutes.\n",minutes/HOUR,minutes%HOUR);
        printf("Please enter the minutes again:");
        scanf("%d",&minutes);
    }
    return 0;
}

二、编写一个程序,此程序要求输入一个整数,而后打印从输入(包含)到比输入大10(包含)的全部整数值(也就说,若是输入是5,那么输出就从5到15)。要求在各个输出之间用空格、制表符或换行符分开。函数

#include 
int main(void)
{
    int num,i;
    i=0;
    printf("Please input the number.\n");
    scanf("%d",&num);
    while(i++<11)
    {
        printf("%d ",num++);     
    }
    return 0;
}

三、编写一个程序,该程序要求用户输入天数,而后将该值转换为周数和天数。例如,该程序将把18天转换为2周4天。用下面的格式显示结果:ui

18 days are 2 weeks,4 days.设计

#include 
#define DAYS_W 7

int main(void)
{
    int days;
    printf("Please input the days:\n");
    scanf("%d",&days);
    while(days>0)
    {
         printf("%d days are %d weeks,%d days.\n",days,days/DAYS_W,days%DAYS_W);
         printf("Please input the days:\n");
         scanf("%d",&days);
    }

    return 0;
}

四、编写一个程序,让用户按厘米输入高度值,而后程序按厘米和英尺英寸显示这个高度值。容许厘米和英尺英寸的值出现小数部分。程序容许用户输入,直到用户输入一个非正的数值。程序运行示例以下:code

Enter a height in centemeters:182input

182.0 cm = 5 feet,11.7 inchesit

Enter a height in centimeters  (<=0 to quit): 168io

168.0 cm = 5 feet ,6.1 inchesclass

Enter a height in centimeters  (<=0 to quit): 0变量

bye

#include 
#define INCH 2.54    //1 INCH = 2.54 CM

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

五、改写用来找到前20个整数以后的程序addemup.c(程序清单5.13)(若是你愿意,能够把addemup.c程序看做是一个计算若是您第一天获得$1,次日获得$2,以此类推,您在20天内会赚多少钱的程序)。修改该程序,目的是您能交互地告诉程序,计算将进行到哪里。也就是说,有一个读入的变量来代替20.

#include
int main(void)
{
    int count,sam,max;
    count=0;
    sam=0;
    printf("Input the max:\n");
    scanf("%d",&max);
    while(count++

六、如今修改编程练习5中的程序,使它可以计算整数平方的和。C没有平方函数,可是您能够利用n的平方是n*n的事实。

#include int main(void) { int count,sam,max; count=0; sam=0; printf("Input the max:\n"); scanf("%d",&max); while(count++#include int main(void) { int count,sam,max; count=0; sam=0; printf("Input the max:\n"); scanf("%d",&max); while(count++

 七、编写一个程序,该程序要求输入一个float型值,并打印该值的立方数。使用您本身设计的函数来计算该值的立方数并将其立方数打印出来。main()函数把输入的值传递给该函数。

#include<stdio.h>
float cube(float n);
int main(void)
{
    float num;
    printf("Input a float:\n");
    scanf("%f",&num);
    printf("The cube of %f is %f.\n ",num,cube(num));
    return 0;
}
float cube(float n)
{
    return(n*n*n);
}

八、编写一个程序,该程序要求用户输入一个华氏温度。程序以double类型读取温度值,并将它做为一个参数传递给用户提供的函数Temperatures()。该函数将计算相应的摄氏温度和绝对温度,并以小数点右边有两位数字的精度显示这三种温度。它应该用每一个值所表明的温度刻度来标识这3个值。下面是将华氏温度转换成摄氏温度的方程:

Celsius = 1.8*Fahrenheit + 32.0;

一般用在科学上的绝对温度的刻度是0表明绝对零,是可能温度的下界。下面是将摄氏温度转换为绝对温度的方程:

kelvin = Celsius + 273.16

Temperatures()函数使用const来建立表明该转换里的3个常量的符号。main()函数将使用一个循环来容许用户重复的输入温度,当用户输入Q或其余非数字值时,循环结束。

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

int main(void)
{
    double fahrenheit;
    printf("Please input a fahrenheit:\n");
    while(scanf("%lf",&fahrenheit)==1)
    {
        Temperatures(fahrenheit);
        printf("Please input a fahrenheit:\n");
    }
    printf("End.\n");
    return 0;
}
void Temperatures(double num)
{
     const double a=1.8,b=32.0,c=273.16;
     printf("Fahrenheit = %lf\t",num);
     printf("Celsius = %lf\t",a * num + b);
     printf("Kelvin = %lf\n",a * num + b + c);

}
相关文章
相关标签/搜索