簡體   English   中英

24小時到12小時轉換器時鍾C(尋找小時和分鍾之間的角度)

[英]24 hrs to 12 hrs converter clock C(Finding angle between hour and min)

因為兩只手之間會有小和大的 2 個角度,因此大 + 小 = 360。因此,您必須返回較小的那個。 給出的時間是 24 小時格式,我們必須轉換為 12 小時。 例如,如果時間是 00:30,那么 12 小時格式的時間將是 12:30,時針和分針之間的角度是 165 度。 但我無法將 24 小時格式編碼為 12 小時格式,它僅適用於 12 小時格式。 如果我在 00:30 輸入輸入,則角度為 525。如果我輸入 12:20,它的工作正常。有人可以幫我做些什么改變。

#include <stdio.h> 

float time_angle(int XX, int YY){   
    float result = 0.5*((60 * XX) + YY)-(6*YY);
    if (XX > 23 || YY > 59)
    {
        return 1;
    }
    if (XX >= 12)
    {
        

        if (XX > 12)
        {
            
            XX -= 12;
        }
        

    }
    
    return result;                 
}

int main()                       
{
    int XX, YY;                 
    scanf("%d", &XX);           
    scanf("%d", &YY);           
    // printf("%d:%d",XX,YY);
    int result = 360-time_angle(XX, YY);  
    printf("%d",result);               
    return 0;                   
}

我認為這可以完成工作:

#include <stdio.h>
#include <stdlib.h>

static void get_time(int *hh, int *mm)
{
    if (scanf("%2d%*[ :.]%2d", hh, mm) != 2)
    {
        fprintf(stderr, "failed to read time\n");
        exit(EXIT_FAILURE);
    }
    if (*hh < 0 || *hh > 24 || *mm < 0 || *mm > 59 || (*hh == 24 && *mm != 0))
    {
        fprintf(stderr, "invalid time value %.2d:%.2d\n", *hh, *mm);
        exit(EXIT_FAILURE);
    }
}

/*
** The hour hand of a clock moves 30 degrees per hour.
** It moves 0.5 degree per minute.
** Alternatively, the hour hand moves 0.5 (60 * hours + minutes).
** The minute hand of a clock moves 6 degrees per minute.
*/

int main(void)
{
    int hh;
    int mm;
    get_time(&hh, &mm);
    int rh = hh % 12;
    double hh_angle = 30.0 * rh + 0.5 * mm;
    double mm_angle = 6.0 * mm;
    double result;
    if (hh_angle > mm_angle)
        result = hh_angle - mm_angle;
    else
        result = mm_angle - hh_angle;
    if (result > 180.0)
        result = 360.0 - result;
    printf("Angle between hands of clock at %.2d:%.2d = %.2f\n", hh, mm, result);
    return 0;
}

該代碼在clk17.c中,編譯為程序clk17 示例輸出,從包含以下內容的文件data生成:

1:5
1:6
1:7
1:8
2:10
2:11
2:12
3:16
3:17
4:21
4:22
4:23
5:27
5:28
6:32
6:33
7:37
7:38
7:39
8:43
8:44
9:48
9:49
9:50
10:54
10:55
11:58
11:59
12:00
14:37
24:00

24:01
13:61

shell 腳本(命令)和輸出:

$ while read data; do echo $data | clk17; done < data
Angle between hands of clock at 01:05 = 2.50
Angle between hands of clock at 01:06 = 3.00
Angle between hands of clock at 01:07 = 8.50
Angle between hands of clock at 01:08 = 14.00
Angle between hands of clock at 02:10 = 5.00
Angle between hands of clock at 02:11 = 0.50
Angle between hands of clock at 02:12 = 6.00
Angle between hands of clock at 03:16 = 2.00
Angle between hands of clock at 03:17 = 3.50
Angle between hands of clock at 04:21 = 4.50
Angle between hands of clock at 04:22 = 1.00
Angle between hands of clock at 04:23 = 6.50
Angle between hands of clock at 05:27 = 1.50
Angle between hands of clock at 05:28 = 4.00
Angle between hands of clock at 06:32 = 4.00
Angle between hands of clock at 06:33 = 1.50
Angle between hands of clock at 07:37 = 6.50
Angle between hands of clock at 07:38 = 1.00
Angle between hands of clock at 07:39 = 4.50
Angle between hands of clock at 08:43 = 3.50
Angle between hands of clock at 08:44 = 2.00
Angle between hands of clock at 09:48 = 6.00
Angle between hands of clock at 09:49 = 0.50
Angle between hands of clock at 09:50 = 5.00
Angle between hands of clock at 10:54 = 3.00
Angle between hands of clock at 10:55 = 2.50
Angle between hands of clock at 11:58 = 11.00
Angle between hands of clock at 11:59 = 5.50
Angle between hands of clock at 12:00 = 0.00
Angle between hands of clock at 14:37 = 143.50
Angle between hands of clock at 24:00 = 0.00
failed to read time
invalid time value 24:01
invalid time value 13:61
$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM