简体   繁体   English

24小时到12小时转换器时钟C(寻找小时和分钟之间的角度)

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

As there will be 2 angles the small and large between both hands, such that large + small = 360. You have to return the smaller one as a result.因为两只手之间会有小和大的 2 个角度,因此大 + 小 = 360。因此,您必须返回较小的那个。 time given is in 24 hrs format we have to convert into 12 hrs.给出的时间是 24 小时格式,我们必须转换为 12 小时。 For example If time is 00:30 then the time in 12 hrs format will be 12:30 and the angle between hour hand and minute hand is 165 degrees.例如,如果时间是 00:30,那么 12 小时格式的时间将是 12:30,时针和分针之间的角度是 165 度。 but I am unable to code in 24 hrs format to 12hrs format its only working for 12 hrs format.但我无法将 24 小时格式编码为 12 小时格式,它仅适用于 12 小时格式。 if I am giving input as 00:30 the angle is coming as 525. and for if I put 12:20 its working fine.. Can someone please help me what changes I had to made.如果我在 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;                   
}

I think this does the job:我认为这可以完成工作:

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

That code was in clk17.c , compiled to program clk17 .该代码在clk17.c中,编译为程序clk17 Sample outputs, generated from a file data containing:示例输出,从包含以下内容的文件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

The shell script (command) and the output: 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