double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

Similar documents
1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

cpp1.dvi

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

1.ppt

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

Condition DAQ condition condition 2 3 XML key value

C 資料 電脳梁山泊烏賊塾 ファイルの入出力 C++ のバイナリファイル入出力 初めに 此処では Visual Studio 2017 を起動し 新しいプロジェクトで Visual C++ の Windows デスクトップを選択し Windows コンソールアプリケーションを作成する

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

r08.dvi

A/B (2010/10/08) Ver kurino/2010/soft/soft.html A/B

cpp4.dvi

超初心者用

programmingII2019-v01

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

ohp08.dvi

untitled

P02.ppt

r07.dvi

ohp07.dvi

untitled

‚æ4›ñ

ohp03.dvi

PowerPoint Presentation

プログラミング基礎

P05.ppt

K227 Java 2

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

Cプログラミング - 第8回 構造体と再帰

r03.dvi

C による数値計算法入門 ( 第 2 版 ) 新装版 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 新装版 1 刷発行時のものです.

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

C言語によるアルゴリズムとデータ構造

: : : TSTank 2

lexex.dvi

untitled

PowerPoint Presentation

Microsoft Word - C.....u.K...doc

cpp2.dvi

Java (5) 1 Lesson 3: x 2 +4x +5 f(x) =x 2 +4x +5 x f(10) x Java , 3.0,..., 10.0, 1.0, 2.0,... flow rate (m**3/s) "flow

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

pptx

exec.dvi

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

design_pattern.key

Microsoft PowerPoint - lec10.ppt

Prog1_15th

‚æ2›ñ C„¾„ê‡Ìš|

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

Java (7) Lesson = (1) 1 m 3 /s m 2 5 m 2 4 m 2 1 m 3 m 1 m 0.5 m 3 /ms 0.3 m 3 /ms 0.6 m 3 /ms 1 1 3

データ構造とアルゴリズム論

C 言語経験者のための C++ 入門

untitled

C 資料 電脳梁山泊烏賊塾 ファイルの入出力 C++ のファイル入出力 初めに 此処では Visual Studio 2017 を起動し 新しいプロジェクトで Visual C++ の Windows デスクトップを選択し Windows コンソールアプリケーションを作成する 使用クラ

ex01.dvi

memo

[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

C , C++ C C++ C++ C cpprefjp - C++ 1 C CUI 2.1 donothing.cpp 1

データ構造とアルゴリズム論

A/B (2018/06/08) Ver kurino/2018/soft/soft.html A/B

2017_08_ICN研究会_印刷用

解きながら学ぶC++入門編


tuat1.dvi

C のコード例 (Z80 と同機能 ) int main(void) { int i,sum=0; for (i=1; i<=10; i++) sum=sum + i; printf ("sum=%d n",sum); 2

untitled

新版 明解C++入門編

2 T 1 N n T n α = T 1 nt n (1) α = 1 100% OpenMP MPI OpenMP OpenMP MPI (Message Passing Interface) MPI MPICH OpenMPI 1 OpenMP MPI MPI (trivial p

MultiWriter 5100F 活用マニュアル

ex14.dvi

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

:30 12:00 I. I VI II. III. IV. a d V. VI

P6dark P6dark µ-pic 2 µ-pic 2 3 µ-pic µ-pic µ-pic 3 µ-pic (10cm ) MPGC N3035-KA195 No. SN ASD (16ns[C]) (16nsC

新版明解C言語 実践編

ex01.dvi

C ( ) C ( ) C C C C C 1 Fortran Character*72 name Integer age Real income 3 1 C mandata mandata ( ) name age income mandata ( ) mandat

Taro-リストⅢ(公開版).jtd

Microsoft Word - no15.docx

Java演習(4) -- 変数と型 --

double float

解きながら学ぶC++入門編

Microsoft PowerPoint ppt

Intel® Compilers Professional Editions

8 / 0 1 i++ i 1 i-- i C !!! C 2

卒 業 研 究 報 告.PDF

SystemC言語概論

問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

joho07-1.ppt

memo

O(N) ( ) log 2 N

Java 3 p.2 3 Java : boolean Graphics draw3drect fill3drect C int C OK while (1) int boolean switch case C Calendar java.util.calendar A

XMPによる並列化実装2

Microsoft PowerPoint pptx


明解Java入門編

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

Transcription:

C++ 1 C C++ C++ C C C++ 1.1 C printf() scanf() C++ C 1-1 #include <stdio.h> int a; scanf( "%d", &a ); printf( "a = %d\n", a ); C++ 1-2 int a; std::cin >> a; std::cout << "a = " << a << std::endl; 1-1 1-2 C++( 1-2 ) stdio.h iostream iostream.h C++ include.h 1-2 scanf() std::cin >> std::cin (stream) >> ( std::cin ) int a 1-2 printf() std::cout << ( std::cout ) a std::endl ( end of line ) C++ (std::cin, std::cout) ( << >> ) printf() scanf() 1

1-1 1-2 double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.txt", std::ios::out ); fout << a << std::endl; fout.close(); 1-4 #include <fstream> std::fstream fin; int a; fin.open( "data.txt", std::ios::in ); fin >> a ; fin.close(); std::cout << a << std::endl; 1-3 1-4 fstream open() open() std::ios::out << open() 2

std::ios::in >> 1-3 1-4 1.3 new delete C malloc() free() C++ new delete C int n 1-5 #include <stdio.h> #include <string.h> int n, i; int *a; n = 4; a = ( int* ) malloc( sizeof( int ) * n ); for( i = 0; i < n ; ++i ) scanf( "%d", a+i ); for( i = 0; i < n ; ++i ) printf( "[%d]=%d\n", i, a[i] ); free( a ); C++ new, delete 1-6 int n = 4; int* a = new int[n]; for( int i = 0; i < n; ++i ) std::cin >> a[i]; for( int i = 0; i < n; ++i ) std::cout << "[" << i << "]=" << a[i] << std::endl; delete a; a = new int[n] int n delete a 1-6 3

new malloc() (sizeof(int)*n ) 1-5 int n n double new delete 1.4 std::vector new delete C++ std::vector 1-6 std::vector 1-7 #include <vector> int n = 4; std::vector< int > a; a.resize( n ); for( int i = 0; i < n; ++i ) std::cin >> a[i]; for( int i = 0; i < n; ++i ) std::cout << "[" << i << "]=" << a[i] << std::endl; vector std::vector< int > int std::vector a.resize( n ) std::vector delete 1-6 std::vector 1.5 std::string C char BOF( ) C++ std::string C++ std::string 1-8 #include <string> 4

std::string a; std::string b; std::string c; std::cin >> a; std::cin >> b; c = a + b; std::cout << c << std::endl; string 2 == 1-9 #include <string> std::string a; std::string b; std::cin >> a; b = " "; if( a == b ) std::cout << " \n"; else std::cout << " \n"; 1-7 std::string a, b, c a+b c 1.6 ( ) C C++ ( ) 1-8 1-10 5

double test( int a ) return a + 1; double test( double a ) return a - 1; int a = 1; double b = 1; std::cout << test( a ) << std::endl; std::cout << test( b ) << std::endl; 1.7 C C++ 1-9 test1() test2() test3() 1-11 void test1( int a ) a += 1; void test2( int* a ) *a += 1; void test3( int& a ) a += 1; int a1 = 1, a2 = 1, a3 = 1; test1( a1 ); test2( &a2 ); test3( a3 ); std::cout << a1 << " " << a2 << " " << a3 << std::endl; 6

2 ( ) C++ int a,b int ( ) a b ( ) 2.1 C++ C C (SCORE) 2-1 #include <stdio.h> struct SCORE int math; int english; ; struct SCORE sc; sc.math = 80; sc.english = 70; printf( "math = %d english = %d\n", sc.math, sc.english ); C++ 2-2 class SCORE int math; int english; ; SCORE sc; 7

sc.math = 80; sc.english = 70; std::cout << "math = " << sc.math << " english = " << sc.english << std::endl; 2-1 2-2 struct class int math; SCORE math, english SCORE math,english C math english mean() 2-3 #include <stdio.h> struct SCORE int math; int english; ; double mean( struct SCORE sc ) return ( sc.math + sc.english )/2.0; struct SCORE sc; sc.math = 80; sc.english = 70; printf( "math = %d english = %d\n", sc.math, sc.english ); printf( "mean = %lf\n", mean( sc ) ); C++ mean() 2-4 class SCORE 8

int math; int english; double mean() return ( math + english )/2.0; ; SCORE sc; sc.math = 80; sc.english = 70; std::cout << "math = " << sc.math << " english = " << sc.english << std::endl << "mean = " << sc.mean() << std::endl; 2-3 2-4 mean() math english sc. 2-4 mean() SCORE maen() sc.maen(). () 2-1 2-4 math english int max() 2.2 ( ) ( ) SCORE tokai,oyama 2-5 class SCORE int math; int english; ; double mean() return ( math + english )/2.0; 9

SCORE tokai,oyama; tokai.math = 10; tokai.english = 20; oyama.math = 90; oyama.english = 80; std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; tokai.mean() oyama.mean() tokai.mean() tokai math,english oyama.mean() oyama math,english 2-2 max() SCORE tokai,oyama, 3 2.3 (construncor) SCORE 2-6 class SCORE int math; int english; SCORE( int m, int e ) math = m; english = e; ; double mean() return ( math + english )/2.0; SCORE tokai( 10, 20 ), oyama( 90, 80 ); 10

std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; main() SCORE( int m, int e ) (destruntor) 2-7 class SCORE int math; int english; SCORE( int m, int e ) math = m; english = e; ~SCORE() std::cout << " \n"; ; double mean() return ( math + english )/2.0; SCORE tokai( 10, 20 ), oyama( 90, 80 ); std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; main() tokai,oyama SCORE() 2-3 11

2-8 #include <string> class FRUIT std::string name; FRUIT( std::string n ) name = n; std::cout << name << " \n"; ; ~FRUIT() std::cout << name << " \n"; FRUIT *fr = new FRUIT( "apple" ); delete fr; fr = new FRUIT( "orange" ); delete fr; 2.4 ( ) ( :inheritance) (FRUIT) 2-9 #include <string> class FRUIT std::string name; FRUIT( std::string n ) name = n; 12

; void show_name() std::cout << name << std::endl; (APPLE) (ORANGE) 2-10 class APPLE : public FRUIT ; APPLE() : FRUIT( " " ) class ORANGE : public FRUIT ; ORANGE() : FRUIT( " " ) APPLE apple; ORANGE orange; apple.show_name(); orange.show_name(); 2-10 APPLE ORANG : public FRUIT FRUIT : FRUIT( ) apple.show name() orange.show name() 2-11 class APPLE : public FRUIT 13

int size; APPLE() : FRUIT( " " ) size = 10; ; void show_size() std::cout << size << " cm" << std::endl; APPLE apple; apple.show_name(); apple.show_size(); 2-12 class APPLE : public FRUIT int size; APPLE() : FRUIT( " " ) size = 10; ; void show_name() std::cout << " " << name << " " << std::endl; APPLE apple; 14

apple.show_name(); main() apple.show name() (FRUIT) show name() (APPLE) show name() 2-4 (class DRINK) (std::string name) (int yen) (void show name(),void show yen()) 2 2.5 2-10 APPLE 2-10 main() 2-13 APPLE apple; apple.name = " "; apple.show_name(); C ( ) C++ public private protected 3 public public private 2-14 #include <string> class FRUIT private: 15

std::string name; FRUIT( std::string n ) name = n; ; FRUIT fruit( " " ); fruit.name = " "; private name main() protected 2-15 #include <string> class FRUIT protected: std::string name; FRUIT( std::string n ) name = n; ; class APPLE : public FRUIT APPLE() : FRUIT( " " ) ; APPLE apple; 16

apple.name = " "; main() protected FRUIT name 2-16 #include <string> class FRUIT protected: std::string name; FRUIT( std::string n ) name = n; ; class APPLE : public FRUIT APPLE() : FRUIT( " " ) ; void change_name() name = " "; APPLE apple; apple.change_name(); FRUIT APPLE name private private 2-17 17

class FRUIT std::string name; FRUIT( std::string n ) name = n; ; name private 2-5 DRINK private main() private main() 2.6 ( ) 18

3 3.1 has a is a has a( ) is a( ) has a A B ( ) A has a B 3-1 class TIRE TIRE() ; class CAR TIRE front_right, front_left, rear_right, rear_left; ; CAR() 3-1 (CAR) 4 (TIRE) is a A B A ia a B 3-2 class CAR CAR() ; class TRUCK : public CAR TRUCK() ; 3-2 (TRUCK) (CAR) has a is a A B A B 3-3 19

class CAR CAR() std::cout << " \n"; ~CAR() std::cout << " \n"; void drive() std::cout << " \n"; ; class HUMAN HUMAN() ; void drive_car() CAR car; car.drive(); HUMAN human; human.drive_car(); 3-3 (HUMAN) (CAR) (void drive car()) 3-1 3-3 has a (HUMAN) (CAR) void drive car() 3.2 PAD has a is a 1 + public - private # protected (HUMAN) 20

1: 3-4 class HUMAN std::string name; protected: void change_name( std::string new_name ) name = new_name; HUMAN() ; void show_name() std::cout << name << std::endl; 2: (HUMAN) 21

3.3 has a( ) has a 3-5 class PENCIL PENCIL() ; class HUMAN PENCIL pencil; ; HUMAN() 3: has a 3.4 is a( ) is a 3-6 class CAR CAR() ; class TRUCK : public CAR 22

; TRUCK() 4: is a 3.5 3-7 class PENCIL PENCIL() ; class HUMAN HUMAN() ; void use_pencil() PENCIL pencil; 23

5: 3-2 DRINK 24

4 (1) ABC DEF 2 ( ) off ( ) (2) ( ) ( ) TV 2 TV TV ABC DEF 2 (is a) TV ABC DEF (has a) (3) TV (TV ) CHANNEL ( ) 2 CHANNEL ABC (ABC ) DEF (DEF ) 2 TV ABC abc DEF def 2 private void change channel() ( ) public off CHANNEL std::string name ( ) private std::string get name() ( ) public ABC DEF TV -abc +change channel() -def CHANNEL -name +get name() ABC CHANNEL DEF CHANNEL (4) (5) C++ 25

6: TV 4-1 #include <string> //---------------------------------------- class CHANNEL std::string name; CHANNEL( std::string n ) name = n; std::string get_name() return name; ; //---------------------------------------- class ABC : public CHANNEL ABC() : CHANNEL( "ABC " ) ; //---------------------------------------- class DEF : public CHANNEL DEF() : CHANNEL( "DEF " ) ; //---------------------------------------- class TV 26

ABC abc; DEF def; TV() std::cout << " on\n"; ~TV() std::cout << " off\n"; void change_channel() for(;;) std::cout << " [ 0 or 1 or off ] : "; std::string input; std::cin >> input; ; if( input == "0" ) std::cout << abc.get_name() << std::endl; if( input == "1" ) std::cout << def.get_name() << std::endl; if( input == "off" ) break; //---------------------------------------- TV tv; tv.change_channel(); (6) on [ 0 or 1 or off ] : 0 ABC [ 0 or 1 or off ] : 1 DEF [ 0 or 1 or off ] : off off 4-1 TV GHI 27

5 100 (1) (4) OK C++ (1) (4) PC (1) (10 ) (2) (1) is a has a (20 ) (3) (2) (20 ) (4) (3) (20 ) (5) (4) C++ (20 ) (6) (10 ) 28