Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C, 5 card reader

Name: Tim 2012-12-31 8:41

Looking for some to write a c program for me and possibly e-mail it to me, willing to pay and all and it must not be copied from somewhere else.

It is a program that reads one line of text input contain 2 characters for cards and a total of 5 cards. It will then post an output with a description of each card, the hand the 5 cards make and the amount of points that hand makes. It must also show an error if the input is not supported by the program.

I could write it myself but have other coursework to do and I'm quite simply lazy. Willing to pay.

Name: The Sussman 2012-12-31 15:44

Implementing 'printinfo' is left as an exercise for the reader.

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

static int read(char *s, size_t n, FILE *stream)
{
    int c;

    n--;
    while ((c = getc(stream)) != EOF && !isspace(c))
        if (n) {
            *s++ = c;
            n--;
        }
    *s = '\0';
    return c;
}

static int valid(const char *str)
{
    const char *s1 = "HSCD";
    const char *s2 = "A23456789JQK";

    return strchr(s1, str[0]) && strchr(s2, str[1]) && !str[2];
}

int main(void)
{
    char cards[5][3];
    char card[sizeof cards[0]];
    size_t i = 0;
    int c;

    do {
        if ((c = read(card, sizeof card, stdin)) == EOF)
            return 0;
        if (!card[0])
            continue;
        if (!valid(card)) {
            fprintf(stderr, "invalid card: %s\n", card);
            continue;
        }
        if (i >= sizeof cards / sizeof cards[0]) {
            fprintf(stderr, "excess card given: %s\n", card);
            continue;
        }
        memcpy(cards[i++], card, sizeof card);
    } while (c != '\n');
    if (i < sizeof cards / sizeof cards[0]) {
        fprintf(stderr, "too few cards given\n");
        return main();
    }
    for (i = 0; i < sizeof cards / sizeof cards[0]; i++)
        printinfo(cards[i]);
    return main();
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List