第 1 章画面 出力 入力 C++ C++ C++ C++ C++ C++ C++ C++ #include using C++ C++ C++ main C++ C++ C++ int double char C++ C++ C++ string C++ C++ C++
21 1-1 C++ 歴史 C++ C++ 歴史 CC with classes Fig.1-1 C C++ Simula 67 Fig.1-1C++ C++
3 C++ 1-1 標準 C 標準 C++
41 1-2 画面 表示 画面 出力 List 1-1 List 1-1 chap1/list11.cpp // cout << "++\n"; cout << "\n"; 実行結果 ++ " p.v /, \, #,,, <, >, (, ), ", ; C++ p.11 Table 1-1 \ source program source file source 原始
5 list11.cpp.cpp.c.cc.c 処理系 C++ Microsoft Visual C++ GNU C++ 実行 Fig.1-2 実行 1 bitbinary digit 1 1 1 2 1 1, 1, 2,, 9 1 1 1-2 //... 実行 11111111 11111111111 1111111111 11111111111 11111111 Fig.1-2 診断 diagnostic message
61 ( 注釈 ) // // // 注釈 comment /* */ /* /* */ */ */ /* <iostream> Fig.1-3 <iostream> <string> header< > iostream string #include include
7 //... cout cin <iostream> 1-2 Fig.1-3#include 4 4 4 4 <iostream> iostream iostream.h iostream.hpp std 名前空間 利用 #include using std 名前空間 name space 4 4 std standard cout std::cout 9 1-1 <iostream> 1-2 List 1-1p.4 using cout std::cout
81 画面 出力 cout << "++\n"; cout << "\n"; stream Fig.1-4 cout 標準出力 standard output stream 4 4 4 4 < << 挿入子 inserter < < < cout << "ABC"; ABC cout Fig.1-4 iostream 入出力 input-output streamcout cont count 文字列 "++\n" "ABC" " 文字列 string literal "" " cout " 8
9改行 \n 4 4 4 \ n \ 拡張表記 3 main 関数 Fig.1-5 1-2 main main cout << "++\n"; cout << "\n"; Fig.1-5main main 関数 main function main 文 statement 4 4 C++ main 6 文 main ; 1-3 ;
101 List 1-2 List 1-2 // \n 実行結果 ++ cout << "++\n\n"; \n ABC ABC chap1/list12.cpp 連続 出力 List 1-3 List 1-3 // << chap1/list13.cpp 実行結果 警報改行 cout << "\a" << "\n"; cout << 警報 \a cout
11記号文字 読 方 Table 1-1 MS-Windows \ List 1-3 cout << " a" << " n"; Table 1-1 1-2 + - * / \ JIS %., : ; ' " ( ) [ ] < >?! & ~ JIS - - ^ # _ = 注意!!
121 自由形式記述 List 1-4 List 1-1 /* List 1-4 */ using namespace std; int main( cout << "++\n"; << "\n" ; cout ) chap1/list14.cpp 読 正 実行結果 ++ 自由形式 free formatted intmaincout, <<, //, /*, */ 空白類文字 ma in " "" " " cout << " ++\n"; cout << "" "++\n";
13List 1-5 List 1-5 // chap1/list15.cpp 実行結果 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz cout << "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // "abcdefghijklmnopqrstuvwxyz\n"; // 1-2 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz\n" white space 2 3 "ABCD" "EFGH" "IJKL" 1 "ABCDEFGHIJKL" # #include preprocessing directive #include #define #if \ #include <iostream> #include \ <iostream> \ 4 4
141 List 1-1main Fig.1-6, 4, 8, 12, for (int i = 1; i <= 9; i++) for (int j = 1; j <= 9; j++) cout << setw(3) << i * j; cout << '\n'; 3 List 3-13p.106 Fig.1-6 1-4 1-5
15bug debug C++ 1-2 Column 1-1 List 1C-1 // // cout << "++\n"; cout << "\n"; chap1/list1c1.c 実行結果 2 // // /* */ List 1C-2 // /* cout << "++\n"; cout << "\n"; */ chap1/list1c2.c 実行結果 #if Column 11-7 p.415
161 1-3 変数 演算結果 出力 List 1-6 18 63 List 1-6 // 1863 cout << "1863" << 18 + 63 << "\n"; chap1/list16.cpp 実行結果 186381 整数 18 63 整数 integer literal 18 1 8 "18" 2 1 8 4 演算結果 出力 Fig.1-7 cout "1863" "\n" \n 4 4 4 4 18 + 63 81 cout << "1863" << 18 + 63 << "\n"; 1863 81 Fig.1-7
171-3 変数 18 63 変数 宣言 宣言 declaration x 宣言文 declaration statement int x; // xint int x Fig.1-8 x 3.5 int 型 type int x int int 4, int x, y; // intxy int x ; int x int Fig.1-8
181 x y 63 18 List 1-7 List 1-7 // xy int x; int y; x = 63; y = 18; // xint // yint // x63 // y18 ㆒ chap1/list17.cpp 実行結果 x63 y18 81 4 cout << "x" << x << "\n"; // x cout << "y" << y << "\n"; // y cout << "" << x + y << "\n"; // xy cout << "" << (x + y) / 2 << "\n"; // xy ㆓ 叅 1 int x, y; 代入演算子 ㆒ = 代入演算子 assignment operator Fig.1-9 x 63 y 18 x = 63; y = 18; 63 18 x y Fig.1-9 x 63 y 18 4 4 p.22
191-3 変数 値 表示 ㆓ x Fig.1-10 4 4 4 cout x x 4 63 x cout << "x" << x << "\n"; x 63 63 x Fig.1-10 算術演算子 演算 化叅 x y x + y (x + y) / 2 x + y ( ) ( ) Fig.1-11 a x + y 2 / b( ) x + y / 2 x y / 2 Table 2-10p.76 a xy ( x + y ) / 2 y b x 2 x + y / 2 Fig.1-11( ) / 63 18 4.5 4 1-6 List 1-7 x y int 1-7 int
201 変数 初期化 ㆒ List 1-8 List 1-8 // xy int x; // xint int y; // yint chap1/list18.cpp 実行結果一例 x6936 y2358 9294 4647 cout << "x" << x << "\n"; // x cout << "y" << y << "\n"; // y cout << "" << x + y << "\n"; // xy cout << "" << (x + y) / 2 << "\n"; // xy x y 6 p.226 初期化 伴 宣言 List 1-9 x y 63 18 初期化 initialize Fig.1-12 = 初期化子 initializer C++ = = 63 = 63 4 initializer clause int x = 63 ; Fig.1-12
211-3 List 1-9 // xy int x = 63; // xint63 int y = 18; // yint18 chap1/list19.cpp 実行結果 x63 y18 81 4 cout << "x" << x << "\n"; // x cout << "y" << y << "\n"; // y cout << "" << x + y << "\n"; // xy cout << "" << (x + y) / 2 << "\n"; // xy 初期化 代入 List 1-7 4 4 4 4 4 Fig.1-13 10 = = a int x = 63; b x = 63; 63 int 63 x x Fig.1-13 1-8 int
221 1-4 入力 入力 List 1-10 cin 標準入力 standard input stream cin >> 抽出子 extractor cin Fig.1-14 int List 4-1p.129 cin >> x; 123 1 2 3 x cin Fig.1-14 演算子 - * % + - 演算子 operator operand x y x + y + x y Fig.1-15 +, -, *, /, % Table 1-2 Table 1-3 算術演算子 arithmetic operator
23List 1-10 // int x; int y; // // cout << "xy\n"; cout << "x"; // x cin >> x; // x cout << "y"; // y cin >> y; // y chap1/list11.cpp 実行例 xy x7 Ÿ y5 Ÿ x + y12 x - y2 x * y35 x / y1 x % y2 1-4 cout << "x + y" << x + y << "\n"; cout << "x - y" << x - y << "\n"; cout << "x * y" << x * y << "\n"; cout << "x / y" << x / y << "\n"; cout << "x % y" << x % y << "\n"; // x + y // x - y // x * y // x / y // x % y 2 項演算子 binary operator 単項演算子 unary operator 3 項演算子 ternary operator x + y Fig.1-15 Table 1-2additive operator x + y x - y x y x y Table 1-3multiplicative operator x * y x / y x % y x y x y x, y x y x, y
241 連続 読込 << cout >> cin List 1-11 List 1-11 // int x; int y; // // cout << "xy\n"; chap1/list111.cpp 実行例 xy xy7 5 Ÿ x + y12 x - y2 x * y35 x / y1 x % y2 cout << "xy"; cin >> x >> y; // xy // xy cout << "x + y" << x + y << "\n"; cout << "x - y" << x - y << "\n"; cout << "x * y" << x * y << "\n"; cout << "x / y" << x / y << "\n"; cout << "x % y" << x % y << "\n"; // x + y // x - y // x * y // x / y // x % y x y >> >> 7 5 7 x 5 y 7 5 Ÿ 7 7 5 5 7 Ÿ 5 Ÿ / % Column 1-2p.27
25単項 算術演算子 List 1-12 List 1-12 // int a; cout << ""; cin >> a; chap1/list112.cpp 実行例㆒ 7 Ÿ 7-7 実行例 // -15 Ÿ -1515 // // a 1-4 int b = -a; // ab cout << a << "" << b << "\n"; ㆒ ㆓ b ㆒ main b -a - - + +a a ㆓ cout << +a << "" << b << "\n"; + - Table 1-4 Table 1-4 +x x -x x
261 実数値 読込 int double List 1-13 List 1-13 // double x; double y; // // cout << "xy\n"; chap1/list113.cpp 実行例 xy x7.5 Ÿ y5.25 Ÿ x + y12.75 x - y2.25 x * y39.375 x / y1.42857 cout << "x"; cin >> x; cout << "y"; cin >> y; // x // x // y // y cout << "x + y" << x + y << "\n"; cout << "x - y" << x - y << "\n"; cout << "x * y" << x * y << "\n"; cout << "x / y" << x / y << "\n"; // x + y // x - y // x * y // x / y 5. 5 5. 5. Table 1-3 % % % cout << "x % y" << x % y << "\n"; // int double List 2-16p.63 4
27 1-9 1-10 1 1 1-11 7 Ÿ 7 7 Ÿ 117 1-3 x7.5 Ÿ y5.25 Ÿ 12.75 6.375 1-4 1-12 7.8 Ÿ 3.2 Ÿ 12.48 Column 1-2 除算 演算結果 / % x / y x % y x = 22 y = 5 4 2 / x = -22 y = -5 x = -22 y = 5 x = 22 y = -5 x / y x % y 4-2 5 3-4 -2-5 3-4 2-5 -3 x y y (x / y) * y + x % y x
281 定値 List 1-14 // double r; List 1-14 cout << ""; cin >> r; // // // cout << "" << 2 * 3.14 * r << "\n"; cout << "" << 3.14 * r * r << "\n"; chap1/list114.cpp 実行例 7.2 Ÿ 45.216 162.778 // // r 2 r r 2 3.14 floating-point literal 3.14 3.1415926535 3.1416 3.1416 3.14 3.14 3.1416 3.14 4 4 4 4 const object List 1-15 const PI 3.1416 4
29// const double PI = 3.1416; double r; List 1-15 cout << ""; cin >> r; // // // // cout << "" << 2 * PI * r << "\n"; cout << "" << PI * r * r << "\n"; chap1/list115.cpp 実行例 7.2 Ÿ 45.239 162.861 // // 1-4 PI 3.1416 PI 3.14159 3.1416 3.14159 PI const const double PI = 3.1416; const double PI; PI = 3.1416; const cv cv const volatile
301 乱数 生成 List 1-16 List 1-16 // 9 #include <ctime> #include <cstdlib> ㆒ srand(time(null)); // chap1/list116.cpp 実行例 6 int lucky = rand() % 1; // 9 叅 cout << "" << lucky << "\n"; ㆓ 9 乱数 ㆒㆓ 叅 ㆓ 叅 叅 rand() 1 1 lucky 9 1-13 1 1 9 1-9 -1 2 1 99 1-14 5 1 95 15 1 Ÿ 5 13
31 ㆒ ㆓ 叅 Column rand RAND_MAX <cstdlib> RAND_MAX 32,767 #include <cstdlib> // int x = rand(); // RAND_MAX int y = rand(); // RAND_MAX cout << "x" << x << "y" << y << "\n"; 1-4 Column 1-3 乱数 生成 x y x y x y 1 2 3 16,838 5,758 1,113 17,515 31,51 5,627 rand rand srand srand(5) srand(23) srand rand 5 22,715 22,43 16,275 21,417 4,96 9, srand srand #include <ctime> #include <cstdlib> // srand(time(null)); // int x = rand(); // RAND_MAX int y = rand(); // RAND_MAX cout << "x" << x << "y" << y << "\n"; time time_t time p.404 Column 11-4 rand 4 4
321 文字 読込 List 1-17 // char c; // List 1-17 cout << ""; cin >> c; cout << "" << c << "\n"; // // // chap1/list117.cpp 実行例 X Ÿ X char >> c char 4 文字列 読込 List 1-18 List 1-18 // #include <string> string name; // cout << ""; cin >> name; cout << "" << name << "\n"; 実行例㆓ Ÿ // // // chap1/list118.cpp 実行例㆒ Ÿ string <string>
33 >> "" name List 1-19 chap1/list119.cpp // #include <string> 実行例㆒ Ÿ 実行例㆓ Ÿ string name; // cout << ""; // getline(cin, name); // List 1-19 cout << "" << name << "\n"; // 1-4 string std::string cout p.7 getline(cin, ) List 1-20 string List 1-20 // #include <string> string s1 = "ABC"; string s2 = "XYZ"; s1 = "FBI"; // // // cout << "s1" << s1 << "\n"; cout << "s2" << s2 << "\n"; // // chap1/list12.cpp 実行結果 s1fbi s2xyz s1 "ABC" "FBI"
341 C++ C Simula 67 C++ #include C++ main ; <iostream> cout << cin >> int double char string string <string> "" \n \a const + *
35( ) / % / % rand /* */ // x + y x - y x * y x / y x % y +x -x #include /* */ chap1/summary.cpp #include <ctime> #include <cstdlib> #include <string> using int a; // aint a = 1; // int b = 5; // RAND_MAX srand(time(null)); // int lucky = rand() % 1; // 9 cout << "" << lucky << "\n"; cout << "" << lucky / 2 << "\n"; cout << "" << lucky % 2 << "\n"; // const double PI = 3.14; double r; 実行例 cout << ""; cin >> r; cout << "" << r << "" << (PI * r * r) << "\n"; 5 3.14 "" 5 2 1 4.5 Ÿ 4.563.585 Shibata BohYoh Ÿ Shibata string name; // cout << ""; // cin >> name; // cout << "\a" "" << name << "\n";