In: Computer Science
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
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: