C++/Console/cout setw setprecision — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:21, 25 мая 2010

Cout: precision 4

#include <iostream>
using namespace std;
int main()
{
  cout.precision(4);
  cout << 10.0/3.0 << "\n";
  return 0;
}


Demonstrate an I/O manipulator: setprecision(2) and setw(20)

 
#include <iostream> 
#include <iomanip> 
using namespace std; 
 
int main() 
{ 
  cout << setprecision(2) << 1000.243 << endl; 
  cout << setw(20) << "Hello there."; 
 
  return 0; 
}


Enters a character and outputs its octal, decimal, and hexadecimal code.

#include <iostream>
#include <iomanip> 
                   
#include <string>
using namespace std;
int main()
{
   int number = " ";
   cout << "The white space code is:" << number << "." << endl;
   char ch;
   string prompt = "Please enter a character followed by <return>: ";
   cout << prompt;
   cin >> ch;                                         
   number = ch;
   cout << "The character " << ch << " has code" << number << endl;
   cout << uppercase                                  // For hex-digits
        << "     octal  decimal  hexadecimal\n "
        << oct << setw(8) << number
        << dec << setw(8) << number
        << hex << setw(8) << number << endl;
   return 0;
}


Uses I/O manipulators to display the table of squares and square roots.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
  double x;
  cout << setprecision(4);
  cout << "      x   sqrt(x)     x^2\n\n";
  for(x = 2.0; x <= 20.0; x++) {
    cout << setw(7) << x << "  ";
    cout << setw(7) << sqrt(x) << "  ";
    cout << setw(7) << x*x << "\n";
  }
 
  return 0;
}