A string parser, that is . . . if you were to use a simple split() function on the input after canonicalizing to remove whitespace, and then strtol() the individual strings, you'd have a more robust input parser. (I may eventually put my str_split() function up here, but they're really a dime a dozen . . . examine the PHP source code for a nice, though painfully-slow, implementation.)
Then, as far as improper fractions go, try solving them like you would on paper (see below).
By the way, I've never tried to write a fraction-reducing algorithm before, and now I don't think I will. I like yours! It's one for the personal snippet-file . . .
I've got good news, and I've got bad news: The universe is merely a figment of my imagination. Now are you ready for the bad news?
// first find the _actual_ fractional part . . . int n = num / denom;
num -= (n * denom);
// . . . reduce that . . .
reduce_fraction(&num,&denom)
// . . . and output it as a mixed number or improper fraction if(want_improper_output){
num += (n * denom);
improper_fraction_out(num,denom); } else{
mixed_number_out(n,num,denom); }
When trying to compile your program I got errors stating that num1 and num2 were not declared. The fix is of course to put two lines at the beginning of main():
int num1; int num2;
Also, throwing in some error checking on the input might be a good idea in case someone decides they want to enter in something other than whole numbers.
Another suggestion would be to ask for the numerator and the demonater seperate of each other, making it a little more intuitive and less likely for the user to make a mistake.
Just my two cents; otherwise this is a nifty little app with potential.
Then, as far as improper fractions go, try solving them like you would on paper (see below).
By the way, I've never tried to write a fraction-reducing algorithm before, and now I don't think I will. I like yours! It's one for the personal snippet-file . . .
I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?
// string parser
input = squash_whitespace(input);
strings = split('/',input);
num = strings[0];
denom = strings[1];
// Dealing with improper fractions
(assuming integer arithmetic . . . )
// first find the _actual_ fractional part . . .
int n = num / denom;
num -= (n * denom);
// . . . reduce that . . .
reduce_fraction(&num,&denom)
// . . . and output it as a mixed number or improper fraction
if(want_improper_output) {
num += (n * denom);
improper_fraction_out(num,denom);
}
else {
mixed_number_out(n,num,denom);
}
int num1;
int num2;
Also, throwing in some error checking on the input might be a good idea in case someone decides they want to enter in something other than whole numbers.
Another suggestion would be to ask for the numerator and the demonater seperate of each other, making it a little more intuitive and less likely for the user to make a mistake.
Just my two cents; otherwise this is a nifty little app with potential.