2012年1月13日金曜日

[C/C++] 新・明解C++入門編 4-1 「算術型」

書籍「新・明解C++入門編 (明解シリーズ)」の中に収められているプログラム等を実際にコンパイル,実行してみた際のメモです.

List4-1
#include <climits>
#include <iostream>

using namespace std;

int main()
{
  cout << "Values that can be represented by integer type of this processing system. \n";

  cout << "char       :         " << CHAR_MIN   << " - "  << CHAR_MAX   << '\n';
  cout << "signed char:         " << SCHAR_MIN  << " - "  << SCHAR_MAX  << '\n';
  cout << "unsigned char:       " << 0          << " - "  << UCHAR_MAX  << '\n';

  cout << "short int:           " << SHRT_MIN   << " - "  << SHRT_MAX   << '\n';
  cout << "int:                 " << INT_MIN    << " - "  << INT_MAX    << '\n';
  cout << "long int:            " << LONG_MIN   << " - "  << LONG_MAX   << '\n';

  cout << "unsigned short int:  " << 0          << " - "  << USHRT_MAX  << '\n';
  cout << "unsigned int:        " << 0          << " - "  << UINT_MAX   << '\n';
  cout << "unsigned long int:   " << 0          << " - "  << ULONG_MAX  << '\n';

}
コンパイル,実行例:
$ g++ List4-1.cpp
$ ./a.out
Values that can be represented by integer type of this processing system. 
char       :         -128 - 127
signed char:         -128 - 127
unsigned char:       0 - 255
short int:           -32768 - 32767
int:                 -2147483648 - 2147483647
long int:            -9223372036854775808 - 9223372036854775807
unsigned short int:  0 - 65535
unsigned int:        0 - 4294967295
unsigned long int:   0 - 18446744073709551615

List4-2
#include <climits>
#include <iostream>

using namespace std;

int main()
{
  cout  << "The simple character type of this processing system is "
        << (CHAR_MIN ? "signed" : "unsigned") << " character type. \n";

}
コンパイル,実行例:
$ g++ List4-2.cpp
$ ./a.out
The simple character type of this processing system is signed character type. 

List4-3
#include <climits>
#include <iostream>

using namespace std;

int main()
{
  cout  << "The simple character type of this processing system is "
        << CHAR_BIT << " bits. \n";

}
コンパイル,実行例:
$ g++ List4-3.cpp
o$ ./a.out
The simple character type of this processing system is 8 bits. 

List4-4

#include <climits>

#include <iostream>



using namespace std;



int main()
{
  cout  << "The simple character type of this processing system is "
        << CHAR_BIT << " bits. \n";
}
コンパイル,実行例:
$ g++ List4-4.cpp
$ ./a.out
Please input a letter.: Z
The character code of character 'Z' is 90. 

List4-5

#include <cctype>

#include <climits>

#include <iostream>



using namespace std;


int main()
{
  cout << "Characters and character codes of this processing system\n";

  for (char i = 0; ; i++){
    switch (i) {
      case '\a' : cout << "\\a"; break;
      case '\b' : cout << "\\b"; break;
      case '\f' : cout << "\\f"; break;
      case '\n' : cout << "\\n"; break;
      case '\r' : cout << "\\r"; break;
      case '\t' : cout << "\\t"; break;
      case '\v' : cout << "\\v"; break;
      default   : cout << ' ' << (isprint(i) ? i : ' ');
    }

    // Casting to integer type is displayed in hexadecimal number.
    cout << ' ' << hex << int(i) << '\n';
    if (i == CHAR_MAX) break;
  }
}
コンパイル,実行例:
$ g++ List4-5.cpp
$ ./a.out
Characters and character codes of this processing system
   0
   1
   2
   3
   4
   5
   6
\a 7
\b 8
\t 9
\n a
\v b
\f c
\r d
   e
   f
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   1a
   1b
   1c
   1d
   1e
   1f
   20
 ! 21
 " 22
 # 23
 $ 24
 % 25
 & 26
 ' 27
 ( 28
 ) 29
 * 2a
 + 2b
 , 2c
 - 2d
 . 2e
 / 2f
 0 30
 1 31
 2 32
 3 33
 4 34
 5 35
 6 36
 7 37
 8 38
 9 39
 : 3a
 ; 3b
 < 3c
 = 3d
 > 3e
 ? 3f
 @ 40
 A 41
 B 42
 C 43
 D 44
 E 45
 F 46
 G 47
 H 48
 I 49
 J 4a
 K 4b
 L 4c
 M 4d
 N 4e
 O 4f
 P 50
 Q 51
 R 52
 S 53
 T 54
 U 55
 V 56
 W 57
 X 58
 Y 59
 Z 5a
 [ 5b
 \ 5c
 ] 5d
 ^ 5e
 _ 5f
 ` 60
 a 61
 b 62
 c 63
 d 64
 e 65
 f 66
 g 67
 h 68
 i 69
 j 6a
 k 6b
 l 6c
 m 6d
 n 6e
 o 6f
 p 70
 q 71
 r 72
 s 73
 t 74
 u 75
 v 76
 w 77
 x 78
 y 79
 z 7a
 { 7b
 | 7c
 } 7d
 ~ 7e
   7f

List4-6

#include <iostream>



using namespace std;



int main()

{
  char c;
  cout << "Char type size:              " << sizeof(char)   << '\n';
  cout << "Size of variable c:          " << sizeof(c)      << '\n';

  short h;
  cout << "The size of the short type:  " << sizeof(short)  << '\n';
  cout << "Size of variable h:          " << sizeof(h)      << '\n';

  int i;
  cout << "Size of int type:            " << sizeof(int)    << '\n';
  cout << "Size of variable i:          " << sizeof(i)      << '\n';

  long l;
  cout << "The size of a long type:     " << sizeof(long)   << '\n';
  cout << "Size of variable l:          " << sizeof(l)      << '\n'
}
コンパイル,実行例:
$ g++ List4-6.cpp
$ ./a.out
Char type size:              1
Size of variable c:          1
The size of the short type:  2
Size of variable h:          2
Size of int type:            4
Size of variable i:          4
The size of a long type:     8
Size of variable l:          8

List4-7
#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
  char c;
  short h;
  int i;
  long l;

  cout << "Type of variable c: " << typeid(c).name() << '\n';
  cout << "Type of variable h: " << typeid(h).name() << '\n';
  cout << "Type of variable i: " << typeid(i).name() << '\n';
  cout << "Type of variable l: " << typeid(l).name() << '\n';

  cout << "Type of character literal 'A': " << typeid('A').name() << '\n';
  cout << "Type of integer literal 100:   " << typeid(100).name() << '\n';
  cout << "Type of integer literal 100U:  " << typeid(100U).name() << '\n';
  cout << "Type of integer literal 100L:  " << typeid(100L).name() << '\n';
  cout << "Type of integer literal 100UL: " << typeid(100UL).name() << '\n'

}
コンパイル,実行例:
$ g++ List4-7.cpp
$ ./a.out
Type of variable c: c
Type of variable h: s
Type of variable i: i
Type of variable l: l
Type of character literal 'A': c
Type of integer literal 100:   i
Type of integer literal 100U:  j
Type of integer literal 100L:  l
Type of integer literal 100UL: m

List4-8
#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
  cout << true << ' ' << false << '\n';

  cout << boolalpha;
  cout << true << ' ' << false << '\n';

  cout << noboolalpha;
  cout << true << ' ' << false << '\n';

}
コンパイル,実行例:
$ ./a.out
1 0
true false
1 0

List4-9
#include <iostream>

using namespace std;

int main()
{
  int a, b;
  cout << "Integer a, b: ";
  cin >> a >> b;

  cout <<  boolalpha;

  cout << "a < b  = " << (a < b) << '\n';
  cout << "a <= b = " << (a <= b) << '\n';
  cout << "a > b  = " << (a > b) << '\n';
  cout << "a == b = " << (a == b) << '\n';
  cout << "a != b = " << (a != b) << '\n';
  cout << "!a     = " << (!a) << '\n';
  cout << "!b     = " << (!b) << '\n';

}
コンパイル,実行例:
$ g++ List4-9.cpp
$ ./a.out
Integer a, b: 0 9
a < b  = true
a <= b = true
a > b  = false
a == b = false
a != b = true
!a     = true
!b     = false

List4-10
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  float       a = 123456789.0;
  double      b = 12345678901234567890.0;
  long double c = 123456789012345678901234567890.0;

  cout << "a = " << setprecision(30) << a << '\n';
  cout << "b = " << setprecision(30) << b << '\n';
  cout << "c = " << setprecision(30) << c << '\n'

}
コンパイル,実行例:
$ g++ List4-10.cpp
$ ./a.out
a = 123456792
b = 12345678901234567168
c = 123456789012345677877719597056

0 件のコメント :

コメントを投稿