DEV Community

Discussion on: Daily Challenge #249 - Incremental Changes

Collapse
 
aminnairi profile image
Amin

C

Assumed we always work with unsigned numbers for simplicity.

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

bool isUnsignedInteger(const char * string);

int main(int argumentsLength, char ** arguments) {
    if (argumentsLength != 4) {
        puts("Expected three arguments.");

        return EXIT_FAILURE;
    }

    if (!isUnsignedInteger(arguments[1])) {
        puts("Expected an unsigned number for the first argument.");

        return EXIT_FAILURE;
    }

    if (!isUnsignedInteger(arguments[2])) {
        puts("Expected an unsigned number for the second argument.");

        return EXIT_FAILURE;
    }

    if (!isUnsignedInteger(arguments[3])) {
        puts("Expected an unsigned number for the third argument.");

        return EXIT_FAILURE;
    }

    printf("%lld\n", atoll(arguments[1]) + atoll(arguments[2]) * atoll(arguments[3]));

    return EXIT_SUCCESS; 
}

bool isUnsignedInteger(const char * string) {
    for (const char * character = string; *character; character++) {
        if (!isdigit(*character)) {
            return false;
        }
    }

    return true;
}
$ gcc -Wall -Werror -Wpedantic -std=c11 -O3 main.c -o main
$ ./main 2 5 10
52
$ ./main 17 3 6
35
$ ./main 100 5 50
350
$ ./main 14 20 4
94