Provide a complete solution to the following problem using the C++ language in a SINGLE file with a .cpp file extension. READ THE ENTIRE QUESTION (AND THE EXAMPLE) CAREFULLY BEFORE DEVELOPING YOUR SOLUTION!
Design the code for a class called Pi, which will be used to encapsulate the value of pi stored as a string. (example ONLY) "3.141592654" | |_ _ _ _| whole number portion_| |_ _ _ _ _fractional portion (9 digits) The Pi class has the following publicly available features: When a Pi object is created/instantiated, it may be passed a string (maximum of 21 characters) that will contain a valid value of pi (i.e. "3.141592654", or "3.14159", or "3.14", etc). The number of decimal digits to display must be calculated by counting the number of characters to the right of the decimal point . the string contains . A Pi object may also be created without passing any parameters, in which case the value of pi would be stored as "3.14", and the number of digits to display would be 2 . There is a function getPi that returns the value of pi as a double. NOTE: You must convert the value of pi (stored as a string) to its equivalent double value . HINT: Starting with a value of 3, loop through the string starting at the index AFTER the . and compute the fractional portion by dividing by 10, 100, 1000. etc. It is possible to use the << operator with a Pi object on the left and an integer n on the right. This operator displays the value of the Pi string to n decimal places. If n is greater than the number of digits in the string value of Pi, then zero's are displayed after the digits. If n is negative, then all of the digits in the Pi string are displayed . (eg.) The following program would display the output shown in the // comments on the right side of the program (below): int main( ) { Pi n1("3.141592654"); Pi n2; double value; // display n1 << -1 // 3.141592654 n1 << 6; // 3.141592 n1 << 0; // 3 n2 << 2; // 3.14 n2 << 5; // 3.14000 printf("n1 pi:%.5lf n2 pi:%.6lf\n", n1.getPi( ), n2.getPi( )); // displays: n1 pi: 3.14159 n2 pi: 3.140000 return 0; }
Code to paste
#include <iostream>
#include <math.h>
using namespace std;
class Pi{
// Member variables
string pi;
int numberOfDecimal;
public:
// default constructor
Pi(){
pi = "3.14";
numberOfDecimal =
2;
}
// constructor with argument
Pi(string value){
pi = value;
if(value.length() >
2){
numberOfDecimal = value.length() - 2;
}else{
numberOfDecimal = 2;
}
}
// converting pi to decimal
double getPi(){
string temp = "3";
// pi without .
if(numberOfDecimal >
0){
for(int i = 2; i < pi.length();i++){
temp += pi[i];
}
}
return stod(temp) /
pow(10,numberOfDecimal);
}
// display value of pi with n decimal
Places
void operator<<(int decimalPlaces) {
if(decimalPlaces <
0){
cout << pi << endl;
}else if(decimalPlaces
== 0){
cout << pi[0] << endl;
}else{
if(decimalPlaces <= numberOfDecimal){
cout << pi.substr(0,decimalPlaces + 2) << endl;
}else{
cout << pi;
for(int i = 0; i < decimalPlaces - numberOfDecimal; i++){
cout << "0";
}
cout << endl;
}
}
}
};
int main()
{
Pi n1("3.141592654");
Pi n2;
double value;
// display
n1 <<
-1;
// 3.141592654
n1 <<
6;
// 3.141592
n1 <<
0;
// 3
n2 <<
2;
// 3.14
n2 <<
5;
// 3.14000
printf("n1 pi:%.5lf n2 pi:%.6lf\n",
n1.getPi(), n2.getPi());
//
displays: n1 pi:
3.14159 n2 pi: 3.140000
return 0;
}
Output screenshot
Get Answers For Free
Most questions answered within 1 hours.