A4P2

A4P2 is relatively a simple straightforward question. Here is the link to the question.

#include 
#include 
#include 

int
main(int argc, char *argv[])
{
    char c;
    short int failed = 0;
    int min = 0, sec = 0, total_min = 0, total_sec = 0,
        line = 0, wrong = 0, char_count = 0;
    printf("Enter a sequence of times, end with 0:00 or EOF.\n");

    while (wrong < 2)
    {
        /* MM:SS, This part checks the MM part. */
        c = getchar();
        if (!isdigit(c) && c != EOF)
        {
            printf("Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }
        else if (c == EOF)
        {
            break;
        }
        else
            ungetc(c, stdin);
        scanf("%d", &min);

        /* The : part. */
        c = getchar();
        if (c != ':')
        {
            fprintf(stderr,"Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }

        /* Now the SS part. */
        c = getchar();
        if (!isdigit(c))
        {
            fprintf(stderr, "Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }
        else
            ungetc(c, stdin);
        scanf("%d%n", &sec, &char_count);
        if (char_count > 2)
        {
            fprintf(stderr, "You've input more than 2 character's" 
                    " length in seconds.\n");
            char_count = 0;
        }

        if ((c = getchar()) == ':')
        {
            fprintf(stderr, "Make sure you do not have an hour!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }
        /* Check and verify the next lines */
        for (;;)
        {
            if (c == '\n')
                break;
            c = getchar();
        }

        /* Minutes validation part */
        if (sec > 60)
        {
            fprintf(stderr, "Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }

        if (sec == 0 && min == 0)
            break;


        total_sec += sec;
        total_min += min;
        line++;
        wrong = 0;
    }

    if (wrong > 1)
    {
        fprintf(stderr, "Three invalid song times in a row; I'm quitting!\n");
        return EXIT_FAILURE;
    }

    fprintf(stdout, "There were %d valid song times.\n", line);
    if (total_sec > 60)
    {
        total_min += total_sec / 60;
        total_sec = total_sec % 60;
    }
    fprintf(stdout, "The total times is %d:%.2d\n", total_min, total_sec);

    return EXIT_SUCCESS;
}

Please feel free to critisize the coding if you have a better solution.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.