10.23

1、
编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间。使用#define或const建立一个表示60的符号常量或const变量。经过while循环让用户重复输入值,直到用户输入小于或等于0的值才中止循环。
#include<stdio.h>
const int HOUR=60;
int main(void)
{
    int x,minute,hour;
    printf("请输入分钟数(输入0或负数中止循环):\n");
    scanf("%d",&x);
    hour=x/HOUR;
    minute=x%HOUR;
    printf("%d分钟是%d小时%d分钟.",x,hour,minute);
    while(x>0)
    {
        printf("\n\n请输入分钟数(输入0或负数中止循环):\n");
        scanf("%d",&x);
        hour=x/HOUR;
        minute=x%HOUR;
        printf("%d分钟是%d小时%d分钟.",x,hour,minute);
        }
    return 0;
 }

2、
编写一个程序,提示用户输入一个整数,而后打印从该数到比该数大 10的全部整数(例如,用户输入5,则打印5~15的全部整数,包括5和 15)。要求打印的各值之间用一个空格、制表符或换行符分开。
#include<stdio.h>
int main(void)
{
    int a,b;
    printf("请输入一个数:\n");
    scanf("%d",&a);
    b=a+10;
    while(a<=b)
    {
        printf("%d ",a);
        a++;
    }
    return 0;
}

3、编写一个程序,提示用户输入天数,而后将其转换成周数和天数。例 如,用户输入18,则转换成2周4天。如下面的格式显示结果:

18 days are 2 weeks, 4 days.

经过while循环让用户重复输入天数,当用户输入一个非正值时(如0 或-20),循环结束。
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int d;
    printf("请输入天数:");
    while(~scanf("%d", &d) && d > 0)
    {
        printf("%d days are %d weeks, %d days.\n", d, d / 7, d % 7);
        printf("input again:");
    }
    return 0;
}

4、编写一个程序,提示用户输入一个身高(单位:厘米),并分别以厘 米和英寸为单位显示该值,容许有小数部分。程序应该能让用户重复输入身 高,直到用户输入一个非正值。其输出示例以下:

Enter a height in centimeters: 182 
182.0 cm = 5 feet, 11.7 inches 
Enter a height in centimeters (<=0 to quit): 168.7 
168.0 cm = 5 feet, 6.4 inches
Enter a height in centimeters (<=0 to quit): 0 
bye
#define CM TO FEET 0.032808
#define CM TO INCT 0.3937008
#define FEET TO INCH 12
int main(void)
{
    float height;
    int feet;
    float inch;
    printf("Enter a height in centimeters: ");
    scanf("%f", &height);
    while (height > 0)
    {
        feet = height * CM TO FEET;
        inch = (height * CM TO INCT) - (FEET TO INCH*feet);
        printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inch);
        printf("Enter a height in centimeters (<=0 to quit): ");
        scanf("%f", &height);
    }
    printf("bye!");
    return 0;
}

5、编写一个程序,提示用户输入一个double类型的数,并打印该数的立方值。本身设计一个函数计算并打印立方值。main()函数要把用户输入的值传递给该函数。
#include<stdio.h>
#include<math.h>
double cube(double x);
int main(void)
{
    double x,n;
    printf("请输入一个double类型的值:\n");
    scanf("%lf",&x);
    cube(x);
    return 0;
 }
double cube(double n)
{
    double a;
    a=pow(n,3);
    printf("%.2lf的立方是%.2lf",n,a);
 }

6、
编写一个程序,显示求模运算的结果。把用户输入的第1个整数做为求模运算符的第2个运算对象,该数在运算过程当中保持不变。用户后面输入的数是第1个运算对象。当用户输入一个非正值时,程序结束。其输出示例 以下:
This program computes moduli. 
Enter an integer to serve as the second operand: 256 
Now enter the first operand: 438 
438 % 256 is 182 
Enter next number for first operand (<= 0 to quit): 1234567 
1234567 % 256 is 135 
Enter next number for first operand (<= 0 to quit): 0 
Done
#include <stdio.h>
int main(void)
{
    int first_op;
    int sec_op;
    int result ;
    printf("This program computes moduli. \n");
    printf("Enter an integer to serve as the second operand:");
    scanf("%d", &sec_op);
    printf("Now enter the first operand:");
    scanf("%d", &first_op);
    while (first_op > 0)
    {
        result = first_op % sec_op;
        printf("%d %% %d is %d \n", first_op, sec_op, result);
        printf("Enter next number for first operand (<= 0 to quit):");
        scanf("%d", &first_op);
    }
    return 0;
}

7、编写一个程序,要求用户输入一个华氏温度。程序应读取double类型 的值做为温度值,并把该值做为参数传递给一个用户自定义的函数Temperatures()。该函数计算摄氏温度和开氏温度,并以小数点后面两位数字 的精度显示3种温度。要使用不一样的温标来表示这3个温度值。下面是华氏温 度转摄氏温度的公式:
摄氏温度 = 5.0 / 9.0 * (华氏温度 - 32.0) 
开氏温标经常使用于科学研究,0表示绝对零,表明最低的温度。下面是摄 氏温度转开氏温度的公式: 
开氏温度 = 摄氏温度 + 273.16 
Temperatures()函数中用const建立温度转换中使用的变量。在main()函数 中使用一个循环让用户重复输入温度,当用户输入 q 或其余非数字时,循环 结束。scanf()函数返回读取数据的数量,因此若是读取数字则返回1,若是 读取q则不返回1。可使用==运算符将scanf()的返回值和1做比较,测试两 值是否相等。
#include <stdio.h>
void temperatures(double n)
{
    const double a = 5.0;
    const double b = 9.0;
    const double sub = 32.0;
    const double add = 273.16;
    double f, t;
    t = a / b * ( n - sub );
    f = t + add;
    printf("h = %0.2f, t = %0.2f, f = %0.2f.\n",n,t,f);
}
int main()
{
    double h;
    printf("Please type a temperature:");
    while( scanf("%lf", &h) == 1 )
    {
        temperatures(h);
        printf("Please type a temperature:");
    }
    printf("Done");
    return 0;
}

相关文章
相关标签/搜索