Question

In: Computer Science

Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem...

Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem requires that the digits be re-arranged.In this project, we will reverse the order of the digits in a number.

Assignment: Design, develop, and test an Object-Oriented C++program that reads a whole number from the user, removes the sign and all leading and trailing zeroes from that number, and then performs the following operations on that number: 1) reverses the digits, 2) sorts the digits in descending order, and 3) sums the digits.For example, if the input value is -123450, the reversed value is 54321, the sorted value is 54321, and the sum of the digits is 15. Similarly, for an input value of 5600, the reversed value is 65, the sorted value is 65, and the sum of digits is 11.Develop a user-defined Digits class to perform all these digit manipulations.

Discussion: Extracting the digits from a whole number can be performed in at least two ways: 1) repeated modulus 10 and division by 10 operations, and 2)conversion of the number to a printable string and then examining the characters in the string. Either method will work, and both are acceptable. How your Digits class functions internally must be transparent to the user, e.g.there must be no cases in which the inner workings of the Digits class produce unique results dependent on its inner workings. Consider the following

•The Digits class must have at least one instance variable for the original value, one for the reversed value, one for the sorted value, and one for the sum of digits. The class must also include a setter method for the original value, and getters for the reversed value, sorted value, and sum of digits. None of the digit manipulations are allowed to modify the original value. All the getters must return their respective values to the user as whole numbers(not as strings).

•Your program must allow the user to enter a whole number(as a number, not a string)from the console and see its reversed value, sorted value, and the sum of its digits, and then to repeat this process until the user enters a zero.Use a single instance of the Digits class to manipulate all input values.

•For each input value, display the input value, its reversed value, its sorted value, and the sum of its digits.Coding

•Each user-defined class must be defined within its own set of .h (,hpp in Xcode) and .cpp files.

•The program must accept any valid signed whole number as input.Do not accept any value with a decimal fraction or other extraneous characters, including dollar signs.

•Validate all inputs and do not proceed until a valid value has been entered.

*** Make the code as simple as possible. Make sure to include every requirements.

**There should be three files.

The first first file is Digit.cpp.

The second file is Digit.h.

The third file is main.cpp

Solutions

Expert Solution

main.cpp

#include<bits/stdc++.h>
#include "Digit.h"
#include "Digit.cpp"

using namespace std;

//function to return number without trailing zeros
int removeTrailingZeros(int num) {

        while (num % 10 == 0 && num != 0)
                num /= 10;

        return num;
}

int main()
{
        int s;
        Digits D;
        while (true) {
                cout << "Enter a number: ";
                cin >> s;
                if (s != 0) {
                        int num = removeTrailingZeros(abs(s));
                        D.setOriginal(num);
                        cout << "Reversed Value: " << D.getReversed() << endl;
                        cout << "Sorted Value: " << D.getSorted() << endl;
                        cout << "Sum of digits: " << D.getSum() << endl;
                } else {
                        break;
                }
        }
}

Digit.h

#include<bits/stdc++.h>

using namespace std;

class Digits;

Digit.cpp

#include<bits/stdc++.h>
using namespace std;
class Digits {
private:
        int original_value;
        int reversed_value;
        int sorted_value;
        int sum;

public:

        void Reverse() {
                int n = original_value, remainder;
                reversed_value = 0;
                while (n != 0) {
                        remainder = n % 10;
                        reversed_value = reversed_value * 10 + remainder;
                        n /= 10;
                }
        }

        void Sort() {
                int temp = original_value;

                std::vector<int> v;

                while (temp != 0) {
                        int r = temp % 10;
                        v.push_back(r);
                        temp = temp / 10;
                }

                // sorting the vector in decending order
                sort(v.begin(), v.end(), greater<int>());

                // calculating sorted number

                sorted_value = 0;

                for (int d : v)         {
                        sorted_value = sorted_value * 10;
                        sorted_value += d;
                }


        }

        void Sum() {
                int temp = original_value;
                sum = 0;
                while (temp != 0) {
                        sum += temp % 10;
                        temp /= 10;
                }
        }

        void setOriginal(int x) {

                original_value = x;
                Reverse();
                Sort();
                Sum();
        }

        int getOriginal() {
                return original_value;
        }
        int getReversed() {
                return reversed_value;
        }

        int getSum() {
                return sum;
        }

        int getSorted() {
                return sorted_value;
        }

};

OUTPUT:


Related Solutions

For each of these problems, please use first a mathematical formula to solve the problem. Second...
For each of these problems, please use first a mathematical formula to solve the problem. Second use Excel spreadsheet to also solve the problem. You are thinking about leasing a car. The purchase price of the car is $30,000. The residual value (the amount you could pay to keep the car at the end of the lease) is $15,000 at the end of 36 months. Assume the first lease payment is due one month after you get the car. The...
Task: Apply mathematical problem solving skills to a variety of problems at the college level. To...
Task: Apply mathematical problem solving skills to a variety of problems at the college level. To accomplish this task, the students will 1. Identify what they are given and what they need to find; 2. Identify the type of problem they have been given and the tools necessary to solve the problem; 3. Correctly apply the tools to the information given to set up the problem; 4. Perform mathematically correct calculations to determine a solution; 5. Interpret their results in...
problem that can be solved by one or more of gene manipulation techniques for example PCR
problem that can be solved by one or more of gene manipulation techniques for example PCR
one of the components of internal control is monitoring, what does this involve within an organization?...
one of the components of internal control is monitoring, what does this involve within an organization? what is COSO and why is it important?
Many developmental psychologists believe that a number of adjustment difficulties during adolescence involve problems with “control.”...
Many developmental psychologists believe that a number of adjustment difficulties during adolescence involve problems with “control.” Consider the following adjustment difficulties: eating disorders, depression, and suicide. Describe specifically the role of control or lack thereof in each of these adjustment difficulties, and explain why researchers and clinicians might be concerned about adolescents who suffer from each of these adjustment problems.
How would you perform a mathematical analysis on a least number of n bits problem? (For...
How would you perform a mathematical analysis on a least number of n bits problem? (For example, least number n bits that could be extracted from a database of bad phone numbers).
Cross- sectional studies are sometimes done with one respondent answering for the whole household. What problems...
Cross- sectional studies are sometimes done with one respondent answering for the whole household. What problems do you anticipate in such a survey? Use terms presented in the course materials to name, explain, and provide an example of at least 2 specific kinds of bias that may be present.
One hundred teachers attended a seminar on mathematical problem solving. The attitudes of representative sample of...
One hundred teachers attended a seminar on mathematical problem solving. The attitudes of representative sample of 12 of the teachers were measured before and after the seminar. A positive number for change in attitude indicates that a teacher's attitude toward math became more positive. The twelve change scores are as follows. 4; 8; −1; 1; 0; 4; −3; 2; −1; 5; 4; −2 1.) What is the standard deviation for this sample? (Round your answer to two decimal places.) 2.)...
One hundred teachers attended a seminar on mathematical problem solving. The attitudes of representative sample of...
One hundred teachers attended a seminar on mathematical problem solving. The attitudes of representative sample of 12 of the teachers were measured before and after the seminar. A positive number for change in attitude indicates that a teacher's attitude toward math became more positive. The twelve change scores are as follows. 4; 7; −1; 1; 0; 5; −2; 2; −1; 6; 5; −3 1. What is the mean change score? (Round your answer to two decimal places.) 2. What is...
One hundred teachers attended a seminar on mathematical problem solving. The attitudes of representative sample of...
One hundred teachers attended a seminar on mathematical problem solving. The attitudes of representative sample of 12 of the teachers were measured before and after the seminar. A positive number for change in attitude indicates that a teacher's attitude toward math became more positive. The twelve change scores are as follows. 4; 8; −1; 1; 0; 4; −3; 2; −1; 5; 4; −2 B) What is the standard deviation for this sample? (Round your answer to two decimal places.) C)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT