It doesn\'t have to be fulfill all the conditions. I\'ve been trying to do this,
ID: 3764995 • Letter: I
Question
It doesn't have to be fulfill all the conditions. I've been trying to do this, but code keeps fail. Really need a help, thanks.
C programming fraction problem a. Daclare a data structure that will contain a rational number b. Write a functions that will add, subtract, multiply and divide rational numbers. In all functions, pass in three parameters, each pointing to a data structure of the type you declared in 'part a'. Use two of the paramaters for the operands, and the third for the result. c. Write a function that takes a pointer to your data structure as a parameter and returns the greatest common divisor of the numerator and denominator d. Use your function from part c to write a function that will reduce a fraction(rational number) to lowest terms. Pass in a pointer to the fraction and have the fraction modified by the func tions. e. Write a input and output functions so that a user can enter a fraction in the form [ ex: 2/3, -5/6, 4/-5, or -12/-33 ] and the output is in the same form. Have your input function check for errors in the input and return a value of -1 in case an error was sensed. The error might consist of a zero denominator as in 3/O, or an error in form as in 4a/5 or 4%5. Both the input function and the output func- tion should take a single pointer to your data structure from part as a parameter f. Combine your functions from parts b through e to write a program that will perform tational number arithmetic for the user. The user should be allowed and the program should output the answer in lowest terms. A sample dialogue is below. The user's input is in color to enter any number of problems, ex: FRACTION ARITHMETIC PROGRAM Enter your problem: 2/3 + 1/5 The answer us 13/15. Another problem (y/n)? y Enter your problem: 4/2 - 5/0 Illegal input Another problem (y/n)? y Enter your problem: 2/93/4 The answer is 1/6. Another problem (y/n)? n Goodbye and thank youExplanation / Answer
#include <stdio.h>
#include<string.h>
struct fraction
{
int num;
int den;
};
struct fraction add_fraction(struct fraction a, struct fraction b);
struct fraction subtract_fraction(struct fraction a, struct fraction b);
struct fraction multiply_fraction(struct fraction a, struct fraction b);
struct fraction devide_fraction(struct fraction a, struct fraction b);
int main()
{
int i=0;
char ch,op;
char buf[50];
struct fraction a,b,c;
while(i<4)
{
printf("enter your problem ");
scanf("%[^ ]s",&buf);
scanf("%c",&op);
removespaces(buf);
sscanf(buf,"%d/%d%c%d/%d",&a.num,&a.den,&op,&b.num,&b.den);
if(a.den==0 || b.den==0)
{
printf("illegal input ");
}
else
{
reduce(&a);
reduce(&b);
printf("a0=%d b0=%d a1=%d b1=%d c=%c ",a.num,a.den,b.num,b.den,op);
switch(op)
{
case '+': c=add_fraction(a,b);
break;
case '-': c=subtract_fraction(a,b);
break;
case '*': c=multiply_fraction(a,b);
break;
case '/': c=devide_fraction(a,b);
break;
default: printf("illegal input ");
break;
}
printf("Aswer is %d/%d ",c.num,c.den);
}
printf("want to solve another problem..?(y / n) ");
scanf("%c",&ch);
if(ch=='n')
break;
scanf("%c",&ch);
i++;
}
return 0;
}
void removespaces(char buf[])
{
int i=0,j=0;
for(i=0;buf[i];i++)
{
if(buf[i]!=' ')
buf[j++]=buf[i];
}
buf[j]='';
}
void reduce(struct fraction *a)
{
int gcd= find_gcd(abs(a->num),abs(a->den));
a->num/=gcd;
a->den/=gcd;
}
int find_gcd(int a, int b)
{
if(b== 0)
return a;
else
return find_gcd(b,a%b);
}
struct fraction add_fraction(struct fraction a, struct fraction b)
{
struct fraction temp;
temp.num = (a.num * b.den) + (a.den * b.num);
temp.den = (a.den * b.den);
reduce(&temp);
return temp;
}
struct fraction subtract_fraction(struct fraction a, struct fraction b)
{
struct fraction temp;
temp.num = (a.num * b.den) - (a.den * b.num);
temp.den = (a.den * b.den);
reduce(&temp);
return temp;
}
struct fraction multiply_fraction(struct fraction a, struct fraction b)
{
struct fraction temp;
temp.num = (a.num * b.num);
temp.den = (a.den * b.den);
reduce(&temp);
return temp;
}
struct fraction devide_fraction(struct fraction a, struct fraction b)
{
struct fraction temp;
temp.num = (a.num * b.den);
temp.den = (a.den * b.num);
reduce(&temp);
return temp;
}
output:
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.