C++/Data Type/Double
Содержание
- 1 Calculates the area of a circle based on a radius inputted by the user.
- 2 Circumference and area of a circle with radius 2.5
- 3 Create a table of square roots and squares.
- 4 Define, input and output double
- 5 double value cout format
- 6 Do while loop with double value type
- 7 Get sin, cos and tan value of an angle in double
- 8 Get the square root, power and log value of a double
- 9 long double: Formatting includes precision, width, alignment, and format of large numbers.
- 10 long double: Formatting includes precision, width and fixed with default of right justification when printing.
- 11 Read double value from keyboard and save it to another value
- 12 Using atof: convert string to double
- 13 Using strtod: convert string to double
Calculates the area of a circle based on a radius inputted by the user.
<source lang="cpp">
- include <iostream>
- include <cmath>
using namespace std; int main(void) {
double radius, area; cout << "Enter radius of circle: "; cin >> radius; area = 3.14159 * pow(radius, 2); cout << "The area is " << area << endl; return 0;
}
</source>
Circumference and area of a circle with radius 2.5
<source lang="cpp">
- include <iostream>
using namespace std; const double pi = 3.141593; int main() {
double area, circuit, radius = 1.5; area = pi * radius * radius; circuit = 2 * pi * radius; cout << "\nTo Evaluate a Circle\n" << endl; cout << "Radius: " << radius << endl << "Circumference: " << circuit << endl << "Area: " << area << endl; return 0;
}
</source>
Create a table of square roots and squares.
<source lang="cpp">
- include <iostream>
- include <cmath>
using namespace std; int main() {
double x; cout.precision(4); cout << " x sqrt(x) x^2\n\n"; for(x = 2.0; x <= 20.0; x++) { cout.width(7); cout << x << " "; cout.width(7); cout << sqrt(x) << " "; cout.width(7); cout << x*x << "\n"; } return 0;
}
</source>
Define, input and output double
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
double hours, wage; cout << "Enter hours worked: "; cin >> hours; cout << "Enter wage per hour: "; cin >> wage; cout << "Pay is: $" << wage * hours; return 0;
}
</source>
double value cout format
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
double x = 12.0; cout.precision(2); // Precision 2 cout << " By default: " << x << endl; cout << " showpoint: " << showpoint << x << endl; cout << " fixed: " << fixed << x << endl; cout << " scientific: " << scientific << x << endl; return 0;
}
</source>
Do while loop with double value type
<source lang="cpp">
- include <iostream>
using namespace std; int main() {
double feet; do { cout << "Enter feet (0 to quit): "; cin >> feet; cout << feet * 12 << " inches\n"; } while (feet != 0.0); return 0;
}
</source>
Get sin, cos and tan value of an angle in double
<source lang="cpp">
- include <iostream>
- include <cmath>
using namespace std; int main(){
double angle, dsine,dcos,dtan; cout << "Please enter an angle in radians \n"; cin >> angle; dcos =cos(angle); dsine =sin(angle); dtan =tan(angle); cout << " Your angles trigonometric functions are \n"; cout << " Sine is " << dsine << "\n"; cout << " Cosine is " << dcos << "\n"; cout << " Tangent is " << dtan << "\n"; return 0;
}
</source>
Get the square root, power and log value of a double
<source lang="cpp">
- include <iostream>
- include <cmath>
using namespace std; int main() {
double number, dsqrt,dpow,dlog; cout << "Please enter a number \n"; cin >> number; dsqrt =sqrt(number); dpow =pow(number,5); dlog =log(number); cout << " Math Example\n"; cout << " Square Root is " << dsqrt << "\n"; cout << " Raised to the fifth power is " << dpow << "\n"; cout << " Log is " << dlog << "\n"; return 0;
}
</source>
long double: Formatting includes precision, width, alignment, and format of large numbers.
<source lang="cpp">
- include <iostream>
- include <iomanip>
using namespace std; int main(){
long double number,factorial; number=1.0; factorial=1.0; cout.precision(0); // no decimal point cout.setf(ios::left); // left justify numbers cout.setf(ios::fixed); // use fixed format for(int i=0;i<25;i++) { factorial*=number; number=number+1.0; cout.width(30); // width of 30 characters cout << factorial << endl; } return (0);
}
</source>
long double: Formatting includes precision, width and fixed with default of right justification when printing.
<source lang="cpp">
- include <iostream>
- include <iomanip>
using namespace std; main( ) {
long double number,factorial; number=1.0; factorial=1.0; cout.precision(0); // no decimal place cout.setf(ios::fixed); // use fixed format for(int i=0;i<25;i++) { factorial*=number; number=number+1.0; cout.width(30); // width of 30 characters cout << factorial << endl; } return (0);
}
</source>
Read double value from keyboard and save it to another value
<source lang="cpp">
- include <iomanip>
- include <ios>
- include <iostream>
- include <string>
using std::cin; using std::setprecision; using std::cout; using std::string; using std::endl; using std::streamsize; int main() {
cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; cout << "Enter all your homework grades, " "followed by end-of-file: "; int count = 0; double sum = 0; double x; while (cin >> x) { ++count; sum += x; } streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << setprecision(prec) << endl; return 0;
}
</source>
Using atof: convert string to double
<source lang="cpp">
- include <iostream>
- include <stdlib.h>
using namespace std; int main() {
double d = atof( "99.0" ); cout << "The string \"99.0\" converted to double is " << d << "\nThe converted value divided by 2 is " << d / 2.0 << endl; return 0;
}
</source>
Using strtod: convert string to double
<source lang="cpp">
- include <iostream>
- include <stdlib.h>
using namespace std; int main() {
double d; char *string = "51.2% are admitted", *stringPtr; d = strtod( string, &stringPtr ); cout << "The string \"" << string << "\" is converted to the\ndouble value " << d << " and the string \"" << stringPtr << "\"" << endl; return 0;
}
</source>