【问题标题】:Converting char array to integer c programming将char数组转换为整数c编程
【发布时间】:2017-10-08 21:32:18
【问题描述】:

问题:用户以 24 小时格式输入时间。该阵列还以 24 小时格式列出了不同的时间。我想将用户输入的数字与最接近用户输入的时间的时间进行比较。任何最接近用户输入时间的数字都将显示在屏幕上。我一直在我的公司工作中通宵工作,并试图了解这背后的编程。谁能帮我?顺便说一句,我刚开始这个 C 编程课程。如果你看到任何看起来像 C# 代码的东西,那是因为我还没有学到足够的东西。到目前为止,这是我所拥有的:

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

int main(void) {

    int hours, mins; //24 hour time to be entered by user

    int bestTime = scanf("%d:,%d", &hours, &mins);  //best time for user departure

    char* depart [8] = {"8:00", "9:43", "11:19", "12:47", "14:00", "15:45", "19:00", "\0"}; //times available for departure

    int counter; //count number of times to loop
    printf("Please Enter The Best Time For Your Flight In 24 Hour Format.\n");
    scanf("%d:,%d", &hours, &mins);

    for(counter = 0; counter <  ; counter++){
        if(bestTime < depart[counter]){
            bestTime = depart[counter];
        }
    }

    return bestTime;
}

【问题讨论】:

  • scanf("%d:,%d",...闻起来很腥...
  • int bestTime = scanf....查看scanf()的返回值是什么...
  • 为了比较,我会将时间转换为总秒数(小时乘以 3600,分钟乘以 60 相加)。然后你在 2 个整数值之间进行比较,并且可以很容易地计算它们之间的差异,从而找到最近的一个
  • @AjayBrahmakshatriya 确实如此,但使用整数比 HH:MM 更容易,例如,用户输入 13:00,可能有 12:58 和 13:03 的时间,或者用户输入 15:00 和时间是 09:12 和 21:11.. 进行更复杂的搜索,整数版本是一个简单的减法,包含在 abs
  • for(counter = 0; counter &lt; ; counter++){&lt; 之后显然缺少一些字符。

标签: c arrays compare min


【解决方案1】:

您可以创建一个函数,将格式为“hh:mm”的字符串从“00:00”转换为分钟。然后用它来做所有的比较。

函数的一个实现可以是

unsigned int str_to_mins(const char* t) {
    return (unsigned int) (
        ((t[0] - '0') * 10 + (t[1] - '0')) * 60 
        + (t[3] - '0') *10 + (t[4] - '0')
    );
}

在你所有的字符串上调用这个函数。他们会给你无符号整数。使用它来找到最接近输入的数字。 (以类似的方式将您的输入也转换为这种格式)。

为了清楚起见,将您的输入扫描为

char input[10];
scanf("%s", input);

【讨论】:

    【解决方案2】:

    想法:

    • 从用户那里获取时间后,将所有内容转换为分钟。
    • 获取出发数组的每个元素,使用 atoi() 将其转换为整数。
    • 将出发时间转换为分钟,并将它们放入整数数组中。
    • 现在将用户输入的总分钟数与数组中的每个总分钟数进行比较。这很简单;你比较 离场总数减去的绝对值 输入总计。最低的绝对值是最接近的。为此,我 写了一个函数,返回内部最近元素的索引 数组。
    • 我会显示与您的初始出发字符串数组中最接近的等效值。
    • 注意:我假设您有 7 个出发时间。
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
    
    int findClosest(int * , int);
    
    int main(void) {
    
        int hours,mins,total; //24 hour time to be entered by user
        char computationHours[3],computationMins[3];
        int computationHoursInt,computationMinsInt,computationTotal;
    
        int bestTime; //best time for user departure
        int index;
        char * depart[8] = {
            "8:00",
            "9:43",
            "11:19",
            "12:47",
            "14:00",
            "15:45",
            "19:00",
            "\0"
        }; //times available for departure
        int departures[7];
        int counter; //count number of times to loop
        printf("Please Enter The Best Time For Your Flight In 24 Hour Format.\n");
        scanf("%d:%d",  & hours,  & mins);
        total = (hours * 60) + mins;
        for (counter = 0; counter < 7; counter++) {
    
            if (strlen(depart[counter]) == 4) {
                //Like 8:00
                strncpy(computationHours, depart[counter], 2);
                strncpy(computationMins, depart[counter] + 2, 2);
                computationHoursInt = atoi(computationHours);
                computationMinsInt = atoi(computationMins);
                computationTotal = (computationHoursInt * 60) + computationMinsInt;
            } else {
                //Like 19:00
                strncpy(computationHours, depart[counter], 2);
                strncpy(computationMins, depart[counter] + 3, 2);
                computationHoursInt = atoi(computationHours);
                computationMinsInt = atoi(computationMins);
            }
            computationTotal = (computationHoursInt * 60) + computationMinsInt;
            departures[counter] = computationTotal;
        }
    
        index = findClosest(departures, total);
        printf("Closest timing is: %s", depart[index]);
        return 0;
    }
    
    int findClosest(int values[], int n) {
        int dist = abs(values[0] - n),closest;
        int size = 7;
        for (int i = 1; i < size; ++i) {
            if (abs(values[i] - n) < dist) {
                dist = abs(values[i] - n);
                closest = i;
            }
        }
        return closest;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 2011-08-28
      相关资源
      最近更新 更多