C & C++: Faust's problem

  • martin_ambuhl / 200 / Wed, 24 Jun 2009 08:28:00 GMT / Comments (0)
  • > #include
    > #define d b%a
    > #define e a%b
    This is already a bad start.
    > void main()
    And this is worse: it's not even valid C. The rest of your program
    looks like very bad Basic. Try this on for size:

    #include
    #include

    int gcdx(int x, int y)
    {
    int rem = x % y;
    return !rem ? y : gcdx(y, rem);
    }

    int gcd(int x, int y)
    {
    if (x < 0)
    x = -x;
    if (y < 0)
    y = -y;
    if (x < y) {
    int t;
    t = x;
    x = y;
    y = t;
    }
    return gcdx(x, y);
    }

    int main()
    {
    int first, second, nread;
    char input[BUFSIZ];
    printf
    ("This program will figure out the greatest common\n"
    "divisor in two numbers you give.\n");
    printf("Please enter the numbers.\n");
    if (!fgets(input, sizeof input, stdin))
    exit(EXIT_FAILURE);
    if (2 != (nread = sscanf(input, "%d%d", &first, &second))) {
    if (1 != nread) {
    printf("Oh, jeez!\n");
    exit(EXIT_FAILURE);
    }
    printf("OK, that's one. Give me another one.\n");
    if (!fgets(input, sizeof input, stdin))
    exit(EXIT_FAILURE);
    if (1 != (nread = sscanf(input, "%d", &second))) {
    printf("You don't want to play? Ok.\n");
    exit(EXIT_FAILURE);
    }
    }
    printf("The GCD is %d\n", gcd(first, second));
    }
  • Keywords:

    faust, c++

  • http://programming.itags.org/c-c++/199412/«« Last Thread - Next Thread »»