Question

Provide a complete solution to the following problem using the C++ language in a SINGLE file...

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;
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the...
Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the console calculator is created which perform multiply,divide,sub and add, operation and it accept all type of data (divide by 0 case as well ).Now the main motive is to create the library project from the console calculator project .Than we have to create a unit test project which test the functionality of added library.Make test like Test 1. multiply two positive number,Test 2. Add...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT