In: Computer Science
Please post code in format that can be copied.
If possible please explain code but not necessary.
Directions:
1. Create a new project for the lab and copy stat.h, stat.cpp, and teststat.cpp into your workspace.
2. Now review the header file. Make sure you understand the design. Can you explain the difference between what the class represents versus how it plans to accomplish it?
3. Now edit the stat.cpp file. Notice that many of the member functions are missing. For all of the missing functions you will need to copy the function prototypes from the header file into the .cpp file. Remember to add Statistician:: before the name of each member function. (Not the friend functions or helper functions.)
4. Add the logic for 3 of the following methods (you may choose): • reset • mean • minimum • maximum • operator+ • operator==
5. You should now test all the functionality of the class (in the client/application code). Run the test code and determine if there are any errors in the code.
Stats.cpp
#include <iostream> // Provides istream classes
#include "stats.h"
using namespace std;
Statistician::Statistician( )
{
reset( );
}
void Statistician::next(double r)
{
total += r;
count++;
if ((count == 1) || (r < tinyest))
tinyest = r;
if ((count == 1) || (r > largest))
largest = r;
}
// implement reset
// implement mean
// implement minimum
// implement maximum
// implement operator +
// implement operator ==
istream& operator >>(istream& ins,
Statistician& target)
{
double user_input;
while (ins && (ins.peek( ) != ';'))
{
if (ins.peek( ) == ' ')
ins.ignore( );
else
{
ins >> user_input;
target.next(user_input);
}
}
ins.ignore( );
return ins;
}
ostream& operator <<(ostream& outs,
Statistician& target)
{
outs << "Values for statistician object: " <<
endl;
outs << "\ttotal: " << target.total <<
endl;
outs << "\tcount: " << target.count <<
endl;
outs << "\tsmallest: " << target.tinyest <<
endl;
outs << "\tlargest: " << target.largest << endl
<< endl;
return outs;
}
stats.h
#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>
using namespace std;
class Statistician
{
public:
// CONSTRUCTOR
Statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const { return count; }
double sum( ) const { return total; }
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend Statistician operator +
(const Statistician& s1, const Statistician& s2);
friend istream& operator >>
(istream&,
Statistician& s1);
friend ostream& operator
<<
(ostream&, Statistician&
s1);
private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tinyest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};
// NON-MEMBER functions for the Statistician class
bool operator ==(const Statistician& s1, const
Statistician& s2);
#endif
teststat.cpp
#include <iostream>
#include "stats.h"
using namespace std;
void menu (void)
{ cout<<endl<<endl;
cout<<"1. Add a number"<<endl;
cout<<"2. Reset the statistician"<<endl;
cout<<"3. Length"<<endl;
cout<<"4. Sum"<<endl;
cout<<"5. Mean"<<endl;
cout<<"6. Minimum"<<endl;
cout<<"7. Maximum"<<endl;
cout<<"8. Add"<<endl;
cout<<"9. Equality"<<endl;
cout<<"10. Print" << endl;
cout<<"11. Quit"<<endl<<endl;
cout<<"Enter selection>";
}
void add(Statistician& S)
{Statistician s2, s3;
s2.reset();
s3.reset();
//enter values for second stat
cout<<"Enter values for second stat, enter ; to
stop"<<endl;
cin>>s2;
s3 = S + s2;
cout<<"Values for added statistician are:"<<endl;
cout<<"Length: "<<s3.length( )<<endl;
cout<<"Sum: "<<s3.sum( )<<endl;
}
void equal(const Statistician &S)
{ Statistician s2;
s2.reset();
//enter values for second stat
cout<<"Enter values for second stat, enter ; to
stop"<<endl;
cin>>s2;
if (s2==S) cout<<"equal"<<endl;
else
cout<<"unequal"<<endl;
}
//Beginning of main program
int main (void)
{
int choice;
double num;
//declare a Statistician
Statistician s;
s.reset();
do {
//print menu
menu();
cin>>choice;
switch (choice) {
case 1 :
cout<<endl<<"Enter a number> ";
cin>>num;
s.next(num);
cout<<endl;
break;
case 2 : s.reset();
cout<<endl<<"Statistician
reset"<<endl;
break;
case 3:
cout<<endl<<"Length is
"<<s.length()<<endl;
break;
case 4:
cout<<endl<<"Sum is "<<s.sum()<<endl;
break;
case 5: if (s.length() >
0)
cout<<endl<<"Mean
is "<<s.mean()<<endl;
else
cout<<"Must add a
number first."<<endl;
break;
case 6: if (s.length()
>0)
cout<<endl<<"Minimum is
"<<s.minimum()<<endl;
else
cout<<"Must add a
number first."<<endl;
break;
case 7: if (s.length()
>0)
cout<<endl<<"Maximum is
"<<s.maximum()<<endl;
else
cout<<"Must add a
number first."<<endl;
break;
case 8: add(s);
break;
case 9: equal(s);
break;
case 10: cout << s;
break;
case 11:
cout<<endl<<"Thanks and goodbye."<<endl;
break;
default :
cout<<endl<<"Invalid command. Try
again."<<endl;
}
} while (choice != 11);
}
Answer 2:
Answer 4:
Stats.cpp
#include <iostream> // Provides istream classes
#include<string>
#include "stats.h"
using namespace std;
Statistician::Statistician( )
{
reset( );
}
void Statistician::reset( )
{
this->count=0;
this->total=0;
this->largest=0;
this->tinyest=0;
}
double Statistician::mean( ) const
{
return total/count;
}
double Statistician::minimum( ) const
{
return tinyest;
}
double Statistician::maximum( ) const
{
return largest;
}
void Statistician::next(double r)
{
total += r;
count++;
if ((count == 1) || (r < tinyest))
tinyest = r;
if ((count == 1) || (r > largest))
largest = r;
}
Statistician operator +(const Statistician& s1, const
Statistician& s2)
{
Statistician s3;
s3.count=s1.count+s2.count;
s3.total=s1.total+s2.total;
s3.tinyest=(s1.tinyest<s2.tinyest)?s1.tinyest:s2.tinyest;
s3.largest=(s1.largest<s2.largest)?s1.largest:s2.largest;
return s3;
}
bool operator ==(const Statistician& s1, const
Statistician& s2)
{
if(s1.length()==s2.length()&&s1.sum()==s2.sum()&&s1.minimum()==s2.minimum()&&s1.maximum()==s2.maximum())
return true;
else
return false;
}
// implement reset
// implement mean
// implement minimum
// implement maximum
// implement operator +
// implement operator ==
istream& operator >>(istream& ins,
Statistician& target)
{
string user_input;
while (ins && (ins.peek( ) != ';'))
{
if (ins.peek( ) == ' ')
ins.ignore( );
else
{
ins >> user_input;
if(user_input==";")
break;
target.next(stof(user_input.c_str()));
}
}
ins.ignore( );
return ins;
}
ostream& operator <<(ostream& outs,
Statistician& target)
{
outs << "Values for statistician object: " <<
endl;
outs << "\ttotal: " << target.total <<
endl;
outs << "\tcount: " << target.count <<
endl;
outs << "\tsmallest: " << target.tinyest <<
endl;
outs << "\tlargest: " << target.largest << endl
<< endl;
return outs;
}
Stats.h
#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>
using namespace std;
class Statistician
{
public:
// CONSTRUCTOR
Statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const { return count; }
double sum( ) const { return total; }
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend Statistician operator +
(const Statistician& s1, const Statistician& s2);
friend istream& operator >>
(istream&, Statistician& s1);
friend ostream& operator <<
(ostream&, Statistician& s1);
private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tinyest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};
// NON-MEMBER functions for the Statistician class
bool operator ==(const Statistician& s1, const
Statistician& s2);
#endif
testStat.cpp
#include <iostream>
#include "stats.h"
using namespace std;
void menu (void)
{ cout<<endl<<endl;
cout<<"1. Add a number"<<endl;
cout<<"2. Reset the statistician"<<endl;
cout<<"3. Length"<<endl;
cout<<"4. Sum"<<endl;
cout<<"5. Mean"<<endl;
cout<<"6. Minimum"<<endl;
cout<<"7. Maximum"<<endl;
cout<<"8. Add"<<endl;
cout<<"9. Equality"<<endl;
cout<<"10. Print" << endl;
cout<<"11. Quit"<<endl<<endl;
cout<<"Enter selection>";
}
void add(Statistician& S)
{Statistician s2, s3;
s2.reset();
s3.reset();
//enter values for second stat
cout<<"Enter values for second stat, enter ; to
stop"<<endl;
cin>>s2;
s3 = S + s2;
cout<<"Values for added statistician are:"<<endl;
cout<<"Length: "<<s3.length( )<<endl;
cout<<"Sum: "<<s3.sum( )<<endl;
}
void equal(const Statistician &S)
{ Statistician s2;
s2.reset();
//enter values for second stat
cout<<"Enter values for second stat, enter ; to
stop"<<endl;
cin>>s2;
if (s2==S) cout<<"equal"<<endl;
else cout<<"unequal"<<endl;
}
//Beginning of main program
int main (void)
{
int choice;
double num;
//declare a Statistician
Statistician s;
s.reset();
do {
//print menu
menu();
cin>>choice;
switch (choice) {
case 1 : cout<<endl<<"Enter a number> ";
cin>>num;
s.next(num);
cout<<endl;
break;
case 2 : s.reset();
cout<<endl<<"Statistician reset"<<endl;
break;
case 3: cout<<endl<<"Length is
"<<s.length()<<endl;
break;
case 4: cout<<endl<<"Sum is
"<<s.sum()<<endl;
break;
case 5: if (s.length() > 0)
cout<<endl<<"Mean is
"<<s.mean()<<endl;
else
cout<<"Must add a number first."<<endl;
break;
case 6: if (s.length() >0)
cout<<endl<<"Minimum is
"<<s.minimum()<<endl;
else
cout<<"Must add a number first."<<endl;
break;
case 7: if (s.length() >0)
cout<<endl<<"Maximum is
"<<s.maximum()<<endl;
else
cout<<"Must add a number first."<<endl;
break;
case 8: add(s);
break;
case 9: equal(s);
break;
case 10: cout << s;
break;
case 11: cout<<endl<<"Thanks and
goodbye."<<endl;
break;
default : cout<<endl<<"Invalid command. Try
again."<<endl;
}
} while (choice != 11);
}
Answer 5: