C++/Data Type/Double

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Calculates the area of a circle based on a radius inputted by the user.

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


Circumference and area of a circle with radius 2.5

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


Create a table of square roots and squares.

 

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


Define, input and output double

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


double value cout format

 

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


Do while loop with double value type

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


Get sin, cos and tan value of an angle in double

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


Get the square root, power and log value of a double

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


long double: Formatting includes precision, width, alignment, and format of large numbers.

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


long double: Formatting includes precision, width and fixed with default of right justification when printing.

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


Read double value from keyboard and save it to another value

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


Using atof: convert string to double

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


Using strtod: convert string to double

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