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

相關文章
相關標籤/搜索