UVa : 11878 (Homework Checker)



// http://uva.onlinejudge.org/external/118/11878.html
// Runtime : 0.020s
// Tag: Ad-hoc, String processing, ASCII 

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

int main ()
{
    char input [100];
    int cnt = 0;

    while ( gets (input) ) {
        int a, b, c;
        char dummy [100];
        strcpy (dummy, input);
        char *pch;
        pch = strtok (input, "+-=");
        a = atoi (pch);
        pch = strtok (NULL, "+-=");
        b = atoi (pch);
        pch = strtok (NULL, "+-=");

        if ( strcmp (pch, "?") == 0 ) continue;
        c = atoi (pch);

        int correct;

        if ( strchr (dummy, '+') ) correct = a + b;
        else correct = a - b;

        if ( correct == c ) cnt++;
    }

    printf ("%d\n", cnt);

    return 0;
}

One thought on “UVa : 11878 (Homework Checker)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s