I need help with this C++ program. There are three separate files i need, one is
ID: 3849340 • Letter: I
Question
I need help with this C++ program. There are three separate files i need, one is a header file, one is the corresponding c++ file with function definitions, and the other is the driver file that will test the program and its conditions. I'm attaching a sample driver file (that is incomplete, it doesn't test all conditions) and a sample run as well. Thanks!
You can take your time and post, then keep me updated in the comments whether it's incomplete so that you can update the answer.
Thanks for the help!
Info: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class should be in the header file, and the member functions definitions should be in another file 1. The class you create must allow for storage of rational numbers in a mixed number format (like 3 1/2 "three and The Mixed must allow for both positive and negative mixed number values. A zero in the denominator of the fraction part constitutes an illegal number and should not be allowed. You should create appropriate member data in your class. All member data must be private. 2. There should be two constructors. One constructor should take in three parameters, representing the integer part, the numerator, and the denominator (in that order), used to initialize the object. If the mixed number is to be a negative number, the negative should be passed on the first non-zero parameter, but on no others. If the data passed in is invalid (negatives not fitting the rule, or 0 denominator), then simply set the object to represent the value 0. Examples of declarations of objects: sets object to 3 4 5 Mixed ml (3, 4, 5) sets object to 4 1/2 Mixed m2 (-4, 1, 2); sets object to 3/5 integer part is 0 Mixed m3 (0, -3, 5) Mixed m 4 (-1, 2, bad parameter combination. Set object to 0 The other constructor should expect a single int parameter with a default value of 0 (so that it also acts as a default constructor). This constructor allows an integer to be passed in and represented as a Mixed object. This means that there is no fractional part. Example declarations: Mixed m5 (4) sets object to 4 (i.ee 4 and no fractional part) Mixed 6; sets object to 0 default Note that this last constructor will act as a "conversion constructor allowing automatic type conversions from type int to type Mixed.Explanation / Answer
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;
struct mixedNumber
{
int whole, num, denom;
void output();
};
void mixedNumber::output()
{
if(whole == 0)
if(num == 0)
cout<<0;
else
cout<<num<<"/"<<denom;
else
if(num == 0)
cout<<whole;
else
cout<<whole<<" "<<num<<"/"<<denom;
}
bool input(string &line);
void process(string line);
int gcd(int p, int q);
void reduce(mixedNumber &number);
mixedNumber add(mixedNumber x, mixedNumber y);
mixedNumber mult(mixedNumber x, mixedNumber y);
mixedNumber divide(mixedNumber x, mixedNumber y);
mixedNumber subtract(mixedNumber x, mixedNumber y);
char getOperator(string line, int &pos);
void getOperands(const string &line, mixedNumber &x, mixedNumber &y, int pos);
mixedNumber convertToMixedNumber(string line);
void trim(string &line);
int main()
{
string line;
while(input(line))
process(line);
return 0;
}
bool input(string &line)
{
cout<<"Input: ";
getline(cin,line);
return line != "";
/*
The above is identical, in result, to:
if(line != "")
return true;
else
return false;
which is also the same result of:
return line != "" ? true : false;
*/
}
void getOperands(const string &line, mixedNumber &x, mixedNumber &y, int pos)
{
string first, second;
first = line.substr(0, pos);
second = line.substr(pos+3);
x = convertToMixedNumber(first);
y = convertToMixedNumber(second);
}
void trim(string &line)
{
while(line[0] == ' ')
line.erase(0,1);
}
mixedNumber convertToMixedNumber(string line)
{
stringstream ss;
char junk;
mixedNumber answer;
trim(line);
int pos = line.find(' '),
pos2 = line.find('/');
if(pos < string::npos && pos2 < string::npos) //have a true mixed number!
{
ss << line;
ss>> answer.whole>>answer.num>>junk>>answer.denom;
}
else
if(pos2 >= string::npos)
{
ss<<line;
ss>>answer.whole;
answer.num = 0;
answer.denom = 1;
}
else
{
ss<<line;
ss>>answer.num>>junk>>answer.denom;
answer.whole = 0;
}
return answer;
}
char getOperator(string line, int &pos)
{
string choices[4] = {" + ", " - ", " * ", " / "};
for(int i = 0; i < 4; i++)
if( (pos = line.find(choices[i])) < string::npos)
break;
return pos < string::npos ? line[pos+1] : '';
}
void process(string line)
{
mixedNumber one, two, answer;
int pos;
char op = getOperator(line, pos);
if(op == '')
{
cout<<"No operator found! Please re-etner"<<endl;
return;
}
getOperands(line, one, two, pos);
switch(op)
{
case '+' : answer = add(one, two);
break;
case '-' : answer = subtract(one,two);
break;
case '*' : answer = mult(one,two);
break;
case '/' : answer = divide(one,two);
}
cout<<line<<" = ";
answer.output();
cout<<endl;
}
int gcd(int p, int q)
{
return q==0 ? p : gcd(q, p%q);
}
void reduce(mixedNumber &number)
{
int newNum = number.denom*number.whole + number.num,
newDenom = number.denom,
divisor = gcd(newNum,newDenom );
newNum /= divisor;
newDenom /= divisor;
number.whole = newNum/newDenom;
number.num = newNum %newDenom;
number.denom = newDenom;
}
mixedNumber add(mixedNumber x, mixedNumber y)
{
mixedNumber answer;
answer.whole = 0;
answer.num = y.denom*(x.denom*x.whole + x.num) + x.denom*(y.denom*y.whole + y.num);
answer.denom = x.denom * y.denom;
reduce(answer);
return answer;
}
mixedNumber mult(mixedNumber x, mixedNumber y)
{
mixedNumber answer;
answer.whole = 0;
answer.num = (x.denom*x.whole + x.num) * (y.denom*y.whole + y.num);
answer.denom = x.denom * y.denom;
reduce(answer);
return answer;
}
mixedNumber divide(mixedNumber x, mixedNumber y)
{
mixedNumber answer;
answer.whole = 0;
answer.num = (x.denom*x.whole + x.num) * y.denom ;
answer.denom = x.denom * (y.denom*y.whole + y.num);
reduce(answer);
return answer;
}
mixedNumber subtract(mixedNumber x, mixedNumber y)
{
mixedNumber answer;
answer.whole = 0;
answer.num = y.denom*(x.denom*x.whole - x.num) + x.denom*(y.denom*y.whole + y.num);
answer.denom = x.denom * y.denom;
reduce(answer);
return answer;
}