Okay so im doing a fractional calculator in c and the when the user enters somet
ID: 3566344 • Letter: O
Question
Okay so im doing a fractional calculator in c and the when the user enters something like 12/16 is divided by -0/2 my program is suppose change that 0 into a 1.
Here is the sample output
Input fraction in the form a/b: 12/16
Input operator(+,-,*,/): /
Input fraction in the form a/b: -0/2
0 denominator changed to 1!
That is what it needs look like. In the output 12/16 will be divided by 1/2 instead of 0 which will equal 3/2. 0 has change to 1 when it is entered without the negative sign as well. Do not use a compiler like cywgin use gcc because that is what I'm using. So here is the code of my program and to whomever fixes it for me I will give you top points and the best rating I can give.
Here is my code and thanks in advance to the super awesome person who fixes it
/*
* lab9p2.c
* Juan Gastelum
* Simon Thompson
* 10/29/14
* This program is a fractional calculator
*/
#include <stdio.h>
void scan_frac(int *nump, int *danp);
char get_op(void);
void add_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp);
void mult_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp);
void reduce_frac(int *np, int *dp);
int find_gcd(int n1, int n2);
void display_eq(int n1, int d1, char op, int n2, int d2, int nans, int dans);
int main(void)
{
int n1, n2, d1, d2, nans, dans, n, d, gcd; /*interger variable declarations*/
char op, x; /*character variable declarations*/
printf("/*** FRACTIONAL CALCULATOR ***/ "); /*Program Title*/
do
{
scan_frac(&n1, &d1); /*gets the first fraction the user enters*/
op = get_op(); /*gets operators (+,-,*,/)*/
scan_frac(&n2, &d2); /*calls scan_frac() again*/
switch(op) /*switch calls 4 functions depending on op*/
{
case '+':
add_frac(n1, d1, n2, d2, &nans, &dans);
break;
case '-':
add_frac(n1, d1, -n2, d2, &nans, &dans);
break;
case '*':
mult_frac(n1, d1, n2, d2, &nans, &dans);
break;
case '/':
mult_frac(n1, d1, d2, n2, &nans, &dans);
break;
}
reduce_frac(&nans, &dans); /*reduces fraction*/
display_eq(n1, d1, op, n2, d2, nans, dans); /*displays fractions, operator, and answer*/
printf("Do another calculation (y/n)? "); /*asks user if they want to rerun program*/
scanf(" %c", &x);
printf(" ");
}while ((x == 'y') || (x == 'Y'));
return(0);
}
/*function scans numerators and denominators */
void scan_frac(int *nump, int *danp)
{
printf("Input fraction in the form a/b: ");
scanf("%d/%d", nump, danp);
} /*close scan_frac*/
/*function gets the operator for fractions (+, -, *, /)*/
char get_op(void)
{
char op, junk;
do {
printf("Input operator(+,-,*,/): ");
scanf(" %c%c", &op, &junk);
if ((op != '+') && (op != '-') && (op != '*') && (op != '/')){
printf("Try again. ");}
}while((op != '+') && (op != '-') && (op != '*') && (op != '/'));
return(op);
}/*closes get_op()*/
/*function is used to add or subtract*/
void add_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp)
{
*dansp = d1 * d2;
*nansp = (n1 * d2) + (n2 * d1);
} /*closes add_frac function*/
/*function finds the greatest common factor*/
int find_gcd(int n1, int n2)
{
int rem, gcd;
while (n2 != 0) {
rem = n1 % n2;
n1 = n2;
n2 = rem;
}
gcd = n1;
return (gcd);
}/*closes find_gcd()*/
/*function calculates the multiplcation, and the division*/
void mult_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp)
{
*nansp = n1 * n2;
*dansp = d1 * d2;
} /*closes mult_frac()*/
/*reduces fraction*/
void reduce_frac(int *np, int *dp)
{
int gcd, nans, dans;
gcd = find_gcd(*np, *dp);
*np = *np / gcd;
*dp = *dp / gcd;
if (((*dp < 0) && (*np < 0)) || ((*dp < 0) && (*np > 0))){
*np *= -1;
*dp *= -1;}
else
*np = *np;
*dp = *dp;
}/*closes reduce_frac()*/
/*function displays the output*/
void display_eq(int n1, int d1, char op, int n2, int d2, int nans, int dans)
{
printf(" %3d%8d%9d ", n1, n2, nans);
printf("---- %c ---- = ----", op);
printf(" %3d%8d%9d ", d1, d2, dans);
printf(" ");
} /*closes display_eq()*/
Explanation / Answer
/*
* lab9p2.c
* Juan Gastelum
* Simon Thompson
* 10/29/14
* This program is a fractional calculator
*/
#include <stdio.h>
void scan_frac(int *nump, int *danp);
char get_op(void);
void add_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp);
void mult_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp);
void reduce_frac(int *np, int *dp);
int find_gcd(int n1, int n2);
void display_eq(int n1, int d1, char op, int n2, int d2, int nans, int dans);
int main(void)
{
int n1, n2, d1, d2, nans, dans, n, d, gcd; /*interger variable declarations*/
char op, x; /*character variable declarations*/
printf("/*** FRACTIONAL CALCULATOR ***/ "); /*Program Title*/
do
{
scan_frac(&n1, &d1); /*gets the first fraction the user enters*/
op = get_op(); /*gets operators (+,-,*,/)*/
scan_frac(&n2, &d2); /*calls scan_frac() again*/
switch(op) /*switch calls 4 functions depending on op*/
{
case '+':
add_frac(n1, d1, n2, d2, &nans, &dans);
break;
case '-':
add_frac(n1, d1, -n2, d2, &nans, &dans);
break;
case '*':
mult_frac(n1, d1, n2, d2, &nans, &dans);
break;
case '/':
mult_frac(n1, d1, d2, n2, &nans, &dans);
break;
}
reduce_frac(&nans, &dans); /*reduces fraction*/
display_eq(n1, d1, op, n2, d2, nans, dans); /*displays fractions, operator, and answer*/
printf("Do another calculation (y/n)? "); /*asks user if they want to rerun program*/
scanf(" %c", &x);
printf(" ");
}while ((x == 'y') || (x == 'Y'));
return(0);
}
/*function scans numerators and denominators */
void scan_frac(int *nump, int *danp)
{
printf("Input fraction in the form a/b: ");
scanf("%d/%d", nump, danp);
if(*nump == 0){
*nump = 1;
}
} /*close scan_frac*/
/*function gets the operator for fractions (+, -, *, /)*/
char get_op(void)
{
char op, junk;
do {
printf("Input operator(+,-,*,/): ");
scanf(" %c%c", &op, &junk);
if ((op != '+') && (op != '-') && (op != '*') && (op != '/')){
printf("Try again. ");}
}while((op != '+') && (op != '-') && (op != '*') && (op != '/'));
return(op);
}/*closes get_op()*/
/*function is used to add or subtract*/
void add_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp)
{
*dansp = d1 * d2;
*nansp = (n1 * d2) + (n2 * d1);
} /*closes add_frac function*/
/*function finds the greatest common factor*/
int find_gcd(int n1, int n2)
{
int rem, gcd;
while (n2 != 0) {
rem = n1 % n2;
n1 = n2;
n2 = rem;
}
gcd = n1;
return (gcd);
}/*closes find_gcd()*/
/*function calculates the multiplcation, and the division*/
void mult_frac(int n1, int d1, int n2, int d2, int *nansp, int *dansp)
{
*nansp = n1 * n2;
*dansp = d1 * d2;
} /*closes mult_frac()*/
/*reduces fraction*/
void reduce_frac(int *np, int *dp)
{
int gcd, nans, dans;
gcd = find_gcd(*np, *dp);
*np = *np / gcd;
*dp = *dp / gcd;
if (((*dp < 0) && (*np < 0)) || ((*dp < 0) && (*np > 0))){
*np *= -1;
*dp *= -1;}
else
*np = *np;
*dp = *dp;
}/*closes reduce_frac()*/
/*function displays the output*/
void display_eq(int n1, int d1, char op, int n2, int d2, int nans, int dans)
{
printf(" %3d%8d%9d ", n1, n2, nans);
printf("---- %c ---- = ----", op);
printf(" %3d%8d%9d ", d1, d2, dans);
printf(" ");
} /*closes display_eq()*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.