【问题标题】:Pointers, structures, and input指针、结构和输入
【发布时间】:2013-10-23 06:16:28
【问题描述】:

所以我制作了这个程序,它以以下格式向用户询问时间:
HH:MM:SS
而且我已经把它的代码写下来了..除了一件事。 假设用户输入如下内容:
12
此时,由于用户点击了“回车”,我希望程序显示错误。
所以,这是我的问题:如果用户输入12:42141(或其他类似的东西) 我希望程序停止而不是让 scanf 等待 3 个整数。
我该如何编码?

无论如何,这是实际的程序:

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

#define true 1
#define false 0

struct Time
{
    int hours;
    int minutes;
    int seconds;
};

void AddOneSecond(struct Time *timePtr);
int CheckForErrors(struct Time timeVars, char charOne, char charTwo);

int main()
{
    auto char firstColon;
    auto char secondColon;
    auto int secondsAdded;
    auto int errorCheck;
    struct Time userInput;

    // Menu prompt
    printf("Enter the start time in HH:MM:SS format: ");
    scanf("%d %c %d %c %d", &userInput.hours
                          , &firstColon
                          , &userInput.minutes
                          , &secondColon
                          , &userInput.seconds);

    // Check for errors
    errorCheck = CheckForErrors(userInput, firstColon, secondColon);

    // If the input format is correct, proceed with the program. If it's not,
    // display an error message and terminate the program.
    if (errorCheck)
    {
        printf("How many seconds would you like to add? ");
        scanf("%d", &secondsAdded);

        // Loop that calls the 'AddOneSecond' function 'secondsAdded' amount
        // of times.
        for (int i = 0; i < secondsAdded; i++)
        {
            AddOneSecond(&userInput);

            printf("%02d:%02d:%02d \n", userInput.hours
                                      , userInput.minutes
                                      , userInput.seconds);
        }
    }
    else
    {
        puts("Error reading input...");
        return 0;
    }

    return 0;
}

void AddOneSecond(struct Time *timePtr)
{
    timePtr->seconds++;

    if (timePtr->seconds == 60)
    {
        timePtr->seconds = 00;
        timePtr->minutes += 1;
    }

    if (timePtr->minutes == 60)
    {
        timePtr->minutes = 00;
        timePtr->hours += 1;
    }
}

int CheckForErrors(struct Time timeVars, char charOne, char charTwo)
{
    auto int isInputValid = true;

    if ((timeVars.hours < 0) || (timeVars.hours > 24))
    {
        isInputValid = false;
        puts("The 'hours' value is invalid.");
    }

    if ((timeVars.minutes < 0) || (timeVars.minutes > 59))
    {
        isInputValid = false;
        puts("The 'minutes' value is invalid.");
    }

    if ((timeVars.seconds < 0) || (timeVars.seconds > 59))
    {
        isInputValid = false;
        puts("The 'seconds' value is invalid.");
    }

    if ((':' != charOne) || (':' != charTwo))
    {
        isInputValid = false;
        puts("The 'colon' value is invalid.");
    }

    return isInputValid;
}

【问题讨论】:

    标签: c pointers structure scanf


    【解决方案1】:

    使用下面的 scanf 语句你可以得到输入,但是检查错误的输入变得更加复杂,你需要在输入的中间按回车键。

    scanf("%d %c %d %c %d", &userInput.hours
                              , &firstColon
                              , &userInput.minutes
                              , &secondColon
                              , &userInput.seconds);
    

    HH:MM:SS这种格式读取带有fgets()的字符串

    这里你可以检查长度,你可以检查这个字符串的0,1索引,3,4索引和6,7索引是不是数字?

    还可以在strchr() 的帮助下检查: 或检查字符串的2,5 个索引

    检查后,如果您想将小时转换为值

    hours=(str[0]-'0') * 10 +(str[1]-'0');   
    // use same for minutes and seconds
    

    您也可以使用strtol()atoi() 进行转换,strtol() 返回long int 如果您决定使用此功能,则需要更改小时、分钟、秒的类型。

    【讨论】:

    • 感谢您的回复!但我有个问题。这条线究竟是如何工作的? (str[0]-'0') * 10 +(str[1]-'0');
    • 这会将字符串的小时部分转换为小时的 int 值。例如,如果您输入 12:32:30 ,则 str[0] is '1' and '1'-'0' this will become 1。和str[1] is '2' and '2'-'0' will become 2,最后是str[0]-'0') * 10 +(str[1]-'0');,这将导致12
    • 好的,'0' 部分使它“减去”字符,这就是转换发生的地方?
    • 是的。确切地。如果从字符数字中减去字符零,则字符数字不是实际的整数数字,您将得到整数数字。 48 是 '0' 的 ASCII 值,57 是 '9' 的 ASCII 值,减去字符意味着减去它们的 ASCII 值
    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-01-16
    • 2020-04-15
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2013-02-15
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多