Home » Category » C & C++

C & C++: Faust's problem

200| Mon, 12 May 2008 14:54:00 GMT| martin_ambuhl| 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 & Tags: faust, c++

URL: http://programming.itags.org/c-c++/199412/
 
«« Prev - Next »» 0 helpful answers below.

C & C++ Hot Answers

C & C++ New questions

C & C++ Related Categories