K
KiMcHeE
Hi! I'm having a real difficult time trying to figure out what is
wrong with my program code. I'm trying to make a "calculator" in the C
language and I only know basic stuff. Here's my code:
#include <stdio.h>
/* global variables */
int e, f;
/* function declarations */
int reduceResultRational(int e, int f); /* change the global result
rational number to its reduced format */
void printRational(int e, int f); /* print a rational number */
void rAdd(int a, int b, int c, int d); /* do rational number addition
*/
void rSubtract(int a, int b, int c, int d); /* do rational number
subtraction */
void rMultiply(int a, int b, int c, int d); /* do rational number
multiplication */
void rDivide(int a, int b, int c, int d); /* do rational number
division */
/* function definition */
int reduceResultRational(int e, int f)
{
int a, b, tmp, r;
a = e;
b = f;
if (b > a)
{
tmp = b;
b = a;
a = tmp;
}
r = a % b;
while (r != 0)
{
a = b;
b = r;
r = a % b;
}
return b;
}
void printRational(int e, int f)
{
if( f < 0 )
{
e *= -1;
f *= -1;
printf("The current result is: %d / %d\n", e, f);
}
else if( e == 0 )
{
printf("The current result is: 0\n");
}
else if( f == 1)
{
printf("The current result is: %d\n", e);
}
else if( e == f)
{
printf("The current result is: 1\n");
}
else
{
int a, b, c, d;
a = e;
b = f;
c = a;
d = b;
c = (a / reduceResultRational(e, f));
d = (b / reduceResultRational(e, f));
printf("The current result is: %d / %d\n", c, d);
}
}
void rAdd(int a, int b, int c, int d)
{
e = (a*d) + (b*c);
f = b*d;
}
void rSubtract(int a, int b, int c, int d)
{
e = (a*d) - (b*c);
f = (b*d);
}
void rMultiply(int a, int b, int c, int d)
{
e = (a*c);
f = (b*d);
}
void rDivide(int a, int b, int c, int d)
{
e = (a*d);
f = (b*c);
}
int main()
{
int c, d, menu = 0; /* local variables */
printf("Welcome to my rational number calculator!\n");
printf("Please input a rational number.\n");
printf("Numerator: ");
scanf("%d", &e);
printf("Denominator: ");
scanf("%d", &f);
while(menu != 5)
{
printf("--------------------------------------------\n");
printRational(e, f);
printf("1. Add the current result with another rational
number;\n");
printf("2. Subtract the current result from another
rational number;\n");
printf("3. Multiply the current result with another
rational number;\n");
printf("4. Divide the current result by another
rational number;\n");
printf("5. Quit\n");
printf("\n");
printf("Please enter a selection: ");
scanf("%d", &menu);
switch( menu )
{
case 1:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rAdd(e, f, c, d);
break;
case 2:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rSubtract(e, f, c, d);
break;
case 3:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rMultiply(e, f, c, d);
break;
case 4:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rDivide(e, f, c, d);
break;
case 5:
default:
break;
}
}
return 0;
}
As you know in math, the numerator is negative to signify a fraction is
negative, not the denominator. On one of the outputs, the result is
outputted as the denominator as the negative number. But in my code, I
specified that if the denominator were to be negative, multiply the
numerator by -1 and the denominator by -1 to switch the negative signs.
Here's what is being outputted:
The current result is: 4 / 5
1. Add the current result with another rational number;
2. Subtract the current result from another rational number;
3. Multiply the current result with another rational number;
4. Divide the current result by another rational number;
5. Quit
Please enter a selection: 3
Please input a rational number.
Numerator: -9
Denominator: 14
--------------------------------------------
The current result is: 18 / -35
1. Add the current result with another rational number;
2. Subtract the current result from another rational number;
3. Multiply the current result with another rational number;
4. Divide the current result by another rational number;
5. Quit
if anyone can figure out what the error is in my code, PLEASE tell me?
I already wasted 3 hours trying to figure it out and I havent made any
progress.
wrong with my program code. I'm trying to make a "calculator" in the C
language and I only know basic stuff. Here's my code:
#include <stdio.h>
/* global variables */
int e, f;
/* function declarations */
int reduceResultRational(int e, int f); /* change the global result
rational number to its reduced format */
void printRational(int e, int f); /* print a rational number */
void rAdd(int a, int b, int c, int d); /* do rational number addition
*/
void rSubtract(int a, int b, int c, int d); /* do rational number
subtraction */
void rMultiply(int a, int b, int c, int d); /* do rational number
multiplication */
void rDivide(int a, int b, int c, int d); /* do rational number
division */
/* function definition */
int reduceResultRational(int e, int f)
{
int a, b, tmp, r;
a = e;
b = f;
if (b > a)
{
tmp = b;
b = a;
a = tmp;
}
r = a % b;
while (r != 0)
{
a = b;
b = r;
r = a % b;
}
return b;
}
void printRational(int e, int f)
{
if( f < 0 )
{
e *= -1;
f *= -1;
printf("The current result is: %d / %d\n", e, f);
}
else if( e == 0 )
{
printf("The current result is: 0\n");
}
else if( f == 1)
{
printf("The current result is: %d\n", e);
}
else if( e == f)
{
printf("The current result is: 1\n");
}
else
{
int a, b, c, d;
a = e;
b = f;
c = a;
d = b;
c = (a / reduceResultRational(e, f));
d = (b / reduceResultRational(e, f));
printf("The current result is: %d / %d\n", c, d);
}
}
void rAdd(int a, int b, int c, int d)
{
e = (a*d) + (b*c);
f = b*d;
}
void rSubtract(int a, int b, int c, int d)
{
e = (a*d) - (b*c);
f = (b*d);
}
void rMultiply(int a, int b, int c, int d)
{
e = (a*c);
f = (b*d);
}
void rDivide(int a, int b, int c, int d)
{
e = (a*d);
f = (b*c);
}
int main()
{
int c, d, menu = 0; /* local variables */
printf("Welcome to my rational number calculator!\n");
printf("Please input a rational number.\n");
printf("Numerator: ");
scanf("%d", &e);
printf("Denominator: ");
scanf("%d", &f);
while(menu != 5)
{
printf("--------------------------------------------\n");
printRational(e, f);
printf("1. Add the current result with another rational
number;\n");
printf("2. Subtract the current result from another
rational number;\n");
printf("3. Multiply the current result with another
rational number;\n");
printf("4. Divide the current result by another
rational number;\n");
printf("5. Quit\n");
printf("\n");
printf("Please enter a selection: ");
scanf("%d", &menu);
switch( menu )
{
case 1:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rAdd(e, f, c, d);
break;
case 2:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rSubtract(e, f, c, d);
break;
case 3:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rMultiply(e, f, c, d);
break;
case 4:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);
rDivide(e, f, c, d);
break;
case 5:
default:
break;
}
}
return 0;
}
As you know in math, the numerator is negative to signify a fraction is
negative, not the denominator. On one of the outputs, the result is
outputted as the denominator as the negative number. But in my code, I
specified that if the denominator were to be negative, multiply the
numerator by -1 and the denominator by -1 to switch the negative signs.
Here's what is being outputted:
The current result is: 4 / 5
1. Add the current result with another rational number;
2. Subtract the current result from another rational number;
3. Multiply the current result with another rational number;
4. Divide the current result by another rational number;
5. Quit
Please enter a selection: 3
Please input a rational number.
Numerator: -9
Denominator: 14
--------------------------------------------
The current result is: 18 / -35
1. Add the current result with another rational number;
2. Subtract the current result from another rational number;
3. Multiply the current result with another rational number;
4. Divide the current result by another rational number;
5. Quit
if anyone can figure out what the error is in my code, PLEASE tell me?
I already wasted 3 hours trying to figure it out and I havent made any
progress.