Using C sharp windows form Create a struct called Rational for performing arithm
ID: 3847413 • Letter: U
Question
Using C sharp windows form
Create a struct called Rational for performing arithmetic with fractions. Write an app to test your class. Use integer variables to represent the private variables of the struct—the numerator and the denominator. Provide a constructor that enables an object of this struct to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public methods that perform each of the following operations (all calculation results should be stored in a reduced form): Add two Rational numbers. Subtract two Rational numbers. Multiply two Rational numbers.and Divide two Rational numbers.
Explanation / Answer
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 );
Rational addition( const Rational & ) const;
Rational subtraction( const Rational & ) const;
Rational multiplication( const Rational & ) const;
Rational division( const Rational & ) const;
void printRational () const;
private:
int numerator;
int denominator;
void reduce();
};
#endif
#include
#include "function.h"
using namespace std;
int main()
{
char s1;
int s2, s3, s4, s5;
Rational x;
while(cin >>s1>>s2>>s3>>s4>>s5)
{
if(cin.eof())
{
break;
}
Rational c(s2, s3), d(s4, s5);
if(s1 == '+')
{
x = c.addition( d );
x.printRational();
cout << ' ';
}
else if(s1 == '-')
{
x = c.subtraction( d );
x.printRational();
cout << ' ';
}
else if(s1 == '*')
{
x = c.multiplication( d );
x.printRational();
cout << ' ';
}
else if(s1 == '/')
{
x = c.division( d );
x.printRational();
cout << ' ';
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.