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

1、编写一个程序,将用分钟表示的时间转换成以小时和分钟表示的时间。使用#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;
} 

2、编写一个程序,此程序要求输入一个整数,然后打印从输入(包含)到比输入大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;
}

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

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;
}

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

Enter a height in centemeters:182

182.0 cm = 5 feet,11.7 inches

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

168.0 cm = 5 feet ,6.1 inches

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;
}

5、改写用来找到前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++

6、现在修改编程练习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++

 7、编写一个程序,该程序要求输入一个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);
}

8、编写一个程序,该程序要求用户输入一个华氏温度。程序以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);

}

 

转载于:https://my.oschina.net/idreamo/blog/680766

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值