Hi, mary9734,
you have copied&pasted a lot of good code, but... are you sure it’s enough? :-)
Do you really want to calculate the factorial of a double?
Also: it seems you are up to a math exercise, but I’m not aware of any method to reckon the number of digits of a double only by a mathematical procedure - maybe there is, of course, since I’m terrible at math. The alternative should be to turn it into a string and get the string lenght().
In your requirement there’s written “...with correct format”. Is it possible there’s something you haven’t catch from the exercise?
Anyway, this is an example of your code ‘reorganised’ in my style, but without any solution for the two mentioned issues. For the number of digits, I just used the base 10 logarithm, which only works for the integer part of a number.
It’s up to you to decide if you want to get to the result your way or fix my code (I suggest the first).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// Input: You will input from a file a number (double)
// Output: Display on the screen the properties of the number similar
// to the example below with correct format.
// Example:
// Input: double number
// Output: The original value is
// The square is
// The square root is
// The factorial is
// Digits
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
class Number
{
private:
double num;
public:
Number(); // the default value should be 0
void setNum(double); // sets the number value
double getNumber(); // allows 'main()' to use/return the value
double square(); // Squares the value
double sRoot(); // Square Roots the value
long long int factorial(); // Returns the factorial value of the integer
int digits(); // add up all of the #s represented
void properties(); // Shows all properties of a number
friend std::istream& operator>>(std::istream& is, Number& number);
};
Number::Number() { num=0; }
void Number::setNum(double g) { num=g; }
double Number::getNumber() { return num; }
double Number::square() { return std::pow(num, 2.0); }
double Number::sRoot() { return std::sqrt(num); }
long long int Number::factorial()
{
if(num > 20) { // reasonable dimension
return 0;
}
long long int fact = 1;
for(int i{1}; i<=num; i++) {
fact *= i;
}
return fact;
}
int Number::digits() { return int(log10(num)) + 1; }
void Number::properties()
{
std::cout << "\nThe original value is: " << num << std::endl;
std::cout << "The square is: " << square() << std::endl;
std::cout << "The squareroot is: " << sRoot() << std::endl;
std::cout << "The factorial is: ";
long long int tmp = factorial();
0 != tmp ? std::cout << tmp << '\n'
: std::cout << "too big to be calculated!!\n";
std::cout << "Digits: " << digits() << std::endl;
}
std::istream& operator>>(std::istream& is, Number& number)
{
is >> number.num;
return is;
}
void pressEnter();
int main()
{
std::ifstream x_file("na.txt");
if (!x_file) {
std::cout << "Error! failed to open file. \n";
} else {
while(x_file) {
Number g;
x_file >> g;
if(g.getNumber() == 0.0) {
break;
}
g.properties();
}
}
pressEnter();
return 0;
}
void pressEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
| |