Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\'Okay so I m working on a fraction calculator and I have some mishaps. First my

ID: 3567317 • Letter: #

Question

'Okay so I m working on a fraction calculator and I have some mishaps. First my program is not suppose to take fractions that are 2/0 because you cant do that. It doesnt exist. I got my program to do that but it gets stuck because when I enter something like 1/5 it prints 0 denominator to screen and 1/5 doesnt have a zero denominator duh also my program is suppose to changes things like 0/2 to 1/2 when you multilplay divide, subtract, add etc it does that but my denominators stay zero when multiplied. Please use c because that is what I'm using and use gcc to compile because that is what Im using too so here is my code

#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 '/':
if(n2 == 0) { n2 = 1;
printf("Give me a dollar ");
}
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);{

do {
printf("0 denominator! Input fraction in the form a/b: ");
scanf("%d/%d", nump, danp);

} while(*nump == 0);

} /*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()*/

Thanks in advance and changing my denominators to zero when performing operations I want it to print give me a dollar thats why that printf is in the multifrac function and if your answer is right ill give you top points and a top rating

Explanation / Answer

#include <iostream>

using namespace std;


struct fraction
{
   int num;
   int den;
};

void functadd(fraction a, fraction b);


void functsubtr(fraction a, fraction b);


void functmulti(fraction a, fraction b);

void functdivide(fraction a, fraction b);


int main()
{
   int ans;
   fraction number[2];
   char c;   
  
   char oprtr;   
  
   fraction result;
     
   fraction reduced;   
  
   bool n = true;
  
   bool showans;   
  
   char cont;

   do
   {
   showans = true;

   cout << " Enter Values in this form 2/3+94/3: ";
   cin >> number[0].num >> c >> number[0].den >>
   oprtr >>
   number[1].num >> c >> number[1].den

   switch(oprtr)
   {
   case '+':
   return ans;
   break;

   case '-':
   return ans;
   break;

   case '*':
   return ans;
   break;

   case '/':
   return ans;
   break;

   default:
     
   cout << "You have used an invalid operator. ";
   cout << "Please use any one of following operators: +, -, *, / ";
   showans = false;
   break;
   }


   if(showans)
   {
     
   cout << "Answer = " << result.num << "/" << result.den;

   do{
   cout << " ";
   cout << "Continue? y/n ";
   // Accept user input
   cin >> cont;
   } while((cont != 'y')&&(cont != 'Y')&&(cont != 'n')&&(cont != 'N'));

     
   if((cont == 'n') || (cont == 'N'))
   {
   n = false;
   }

   }
   } while(n);
  

   return 0;

}

void functadd(fraction a,fraction b);
{

  
   fraction ans;   
  
   ans.num = (a.num * b.den) + (b.num * a.den);
   ans.den = a.den * b.den;
   result = functadd(number[0], number[1]);
   return result;
}

void functsubtr(fraction a, fraction b);
{

   fraction ans;   
   ans.num = (a.num * b.den) - (b.num * a.den);
   ans.den = a.den * b.den;
   result = functsubtr(number[0], number[1]);
   return ans;
}


void functmulti(fraction a, fraction b);
{
   fraction ans;   
   ans.num = a.num * b.num;
   ans.den = a.den * b.den;
   result = functmulti(number[0], number[1]);
   ;
}


void functdivide(fraction a, fraction b);
{
   fraction ans;   
   ans.num = a.num * b.den;
   ans.den = a.den * b.num;
   result = functdivide(number[0], number[1])
   return ans;
}