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 >>

Similar documents
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

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 <<

超初心者用

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

1.ppt

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

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

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

cpp1.dvi

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

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

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

Condition DAQ condition condition 2 3 XML key value

ohp03.dvi

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

programmingII2019-v01

r03.dvi

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

r08.dvi

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

ohp08.dvi

pptx

cpp4.dvi

PowerPoint Presentation

‚æ4›ñ

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

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

untitled

P05.ppt

untitled

untitled

r07.dvi

ohp07.dvi

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) * *

P02.ppt

untitled

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

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

lexex.dvi

K227 Java 2

[ 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:

joho07-1.ppt

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

PowerPoint Presentation

WinHPC ppt

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

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

cpp2.dvi

exec.dvi

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

: : : TSTank 2

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

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

Prog1_15th

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

tuat1.dvi

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

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

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

Microsoft PowerPoint pptx

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

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

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

Microsoft PowerPoint - lec10.ppt

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 >=


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

導入基礎演習.ppt

新・明解C言語 ポインタ完全攻略

2017_08_ICN研究会_印刷用

卒 業 研 究 報 告.PDF

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

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

design_pattern.key

RHEA key

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

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 UNIX C ( ) 4 1 HTML 1

ex01.dvi

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

新版明解C言語 実践編

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

memo

Ruby Ruby ruby Ruby G: Ruby>ruby Ks sample1.rb G: Ruby> irb (interactive Ruby) G: Ruby>irb -Ks irb(main):001:0> print( ) 44=>

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

untitled

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

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

ex14.dvi

ex01.dvi

新版 明解C++入門編

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

Microsoft Word - no15.docx

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

PowerPoint プレゼンテーション

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

Transcription:

1 C++ 1.1 C C++ C++ C C C++ 1.1.1 C printf() scanf() C++ C hello world printf() 1-1 #include <stdio.h> printf( "hello world\n" ); C++ 1-2 std::cout << "hello world" << std::endl; C scanf() 1-3 #include <stdio.h> int a; scanf( "%d", &a ); printf( "a = %d\n", a ); C++ 1

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; 1-3 1-4 C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >> std::cin (stream) >> ( std::cin ) int a 1-4 printf() std::cout << ( std::cout ) a std::endl ( end of line ) C++ (std::cin, std::cout) ( << >> ) printf() scanf() 1-1 (10 ) 1-2 (double ) 2 std::cin, std::cout (10 ) 1.1.2 C fopen() fclose() C++ std::fstream 1-5 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.txt", std::ios::out ); fout << a << std::endl; fout.close(); 2

1-6 #include <fstream> std::fstream fout; int a; fout.open( "data.txt", std::ios::in ); fout >> a ; fout.close(); std::cout << a << std::endl; 1-5 1-6 fstream open() open() std::ios::out << open() std::ios::in >> 1-3 (10 ) 1-4 (10 ) 1.1.3 C C++ ( ) 1-7 int a; std::cin >> a; std::cout << a << std::endl; 3

1-8 int a; std::cin >> a; std::cout << a << std::endl; ( ) C++ ( ) 1-5 (10 ) 1-9 int a = 3; int a; std::cin >> a; std::cout << a << std::endl; std::cout << a << std::endl; 4

1.1.4 new delete C malloc() free() C++ new delete C int n 1-10 #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-11 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-11 new malloc() (sizeof(int)*n ) ( ) C++ JavaScript Ruby 1-6 int n n double new delete (10 ) 5

1.1.5 std::vector new delete C++ std::vector 1-11 std::vector 1-12 #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 ( ) ( ) std::vector (iterator) 1-7 std::vector ( 10 ) 1.1.6 std::string C char (BOF) C++ std::string C sprintf() 1-13 #include <stdio.h> #include <string.h> char a[256]; char b[256]; char c[256]; scanf( "%s", a ); 6

scanf( "%s", b ); sprintf( c, "%s%s", a, b ); printf( "%s\n", c ); std::string + 1-14 #include <string> 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 C strcmp() 1-15 #include <stdio.h> #include <string.h> char a[256]; char b[256]; scanf( "%s", a ); strcpy( b, " " ); if( strcmp( a, b ) == 0 ) printf( " \n" ); else printf( " \n" ); 7

std::string == 1-16 #include <string> std::string a; std::string b; std::cin >> a; b = " "; if( a == b ) std::cout << " \n"; else std::cout << " \n"; 1-8 int n n std::string std::vector (10 ) 1.1.7 ( ) C C++ ( ) 1-9 (10 ) 1-17 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; 8

1.1.8 C C++ 1-18 void test( int& z ) z += 1; int a = 1; test( a ); std::cout << a << std::endl; a test() 2 * ( ) ( ) C++ 1-10 test1() test2() test3() (10 ) 1-19 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 ); 9

test3( a3 ); std::cout << a1 << " " << a2 << " " << a3 << std::endl; 10

1.2 ( ) C++ int a,b int ( ) a b ( ) 1.2.1 C++ C C (SCORE) 1-20 #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++ 1-21 class SCORE int math; int english; ; SCORE sc; 11

sc.math = 80; sc.english = 70; std::cout << "math = " << sc.math << " english = " << sc.english << std::endl; 1-20 1-21 struct class int math; SCORE math, english SCORE math,english C math english mean() 1-22 #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() 1-23 class SCORE 12

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; 1-22 1-23 mean() math english sc. 1-23 mean() SCORE maen() sc.maen(). () 1-11 1-23 math english int max() 1.2.2 ( ) ( ) SCORE tokai,oyama 1-24 class SCORE int math; int english; ; double mean() return ( math + english )/2.0; 13

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 1-12 max() SCORE tokai,oyama, 3 1.2.3 (construncor) SCORE 1-25 class SCORE int math; int english; SCORE( int m, int e ) math = m; english = e; ; double mean() return ( math + english )/2.0; 14

SCORE tokai( 10, 20 ), oyama( 90, 80 ); std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; main() SCORE( int m, int e ) (destruntor) 1-26 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() 1-13 ( 10 ) 15

1-27 #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; 1.2.4 ( ) ( :inheritance) (FRUIT) 1-28 #include <string> class FRUIT std::string name; FRUIT( std::string n ) name = n; 16

; void show_name() std::cout << name << std::endl; (APPLE) (ORANGE) 1-29 class APPLE : public FRUIT ; APPLE() : FRUIT( " " ) class ORANGE : public FRUIT ; ORANGE() : FRUIT( " " ) APPLE apple; ORANGE orange; apple.show_name(); orange.show_name(); 1-29 APPLE ORANG : public FRUIT FRUIT : FRUIT( ) apple.show name() orange.show name() 1-30 class APPLE : public FRUIT 17

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

apple.show_name(); main() apple.show name() (FRUIT) show name() (APPLE) show name() 1-14 (DRINK) (name) (yen) (show name(),show yen()) 2 (10 ) 1.2.5 1-29 APPLE 1-29 main() 1-32 APPLE apple; apple.name = " "; apple.show_name(); C ( ) C++ public private protected 3 public public private 1-33 #include <string> class FRUIT private: 19

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

apple.name = " "; main() protected FRUIT name 1-35 #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 1-36 21

class FRUIT std::string name; FRUIT( std::string n ) name = n; ; name private 1-15 DRINK private main() private main() 1.2.6 ( ) 22

1.3 1.3.1 has a is a has a( ) is a( ) has a A B ( ) A has a B 1-37 class TIRE TIRE() ; class CAR TIRE front_right, front_left, rear_right, rear_left; ; CAR() 1-37 (CAR) 4 (TIRE) is a A B A ia a B 1-38 class CAR CAR() ; class TRUCK : public CAR TRUCK() ; 1-38 (TRUCK) (CAR) has a is a A B A B 1-39 23

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(); 1-39 (HUMAN) (CAR) (void drive car()) 1-16 1-39 has a (HUMAN) (CAR) void drive car() 24

1.3.2 PAD has a is a 1: 1 + public - private # protected (HUMAN) 1-40 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) 25

1.3.3 has a( ) has a 1-41 class PENCIL PENCIL() ; class HUMAN PENCIL pencil; ; HUMAN() 3: has a 1.3.4 is a( ) is a 1-42 class CAR CAR() ; class TRUCK : public CAR 26

; TRUCK() 4: is a 1.3.5 1-43 class PENCIL PENCIL() ; class HUMAN HUMAN() ; void use_pencil() PENCIL pencil; 27

5: 1.4 (1) ABC DEF 2 ( ) off ( ) (2) ( ) ( ) TV 3 TV TV ABC DEF 2 (is a) TV ABC DEF (has a) (3) TV (TV ) SCREEN ( ) CHANNEL ( ) 3 CHANNEL ABC (ABC ) DEF (DEF ) 2 TV SCREEN screen ABC abc DEF def 3 private void change channel() ( ) public off SCREEN void show text( std::string text ) ( ) public CHANNEL std::string name ( ) private std::string get name() ( ) public ABC DEF 28

TV -screen +change channel() -abc -def SCREEN +show text() CHANNEL -name +get name() ABC CHANNEL DEF CHANNEL (4) 6: TV (5) C++ 1-44 #include <string> class SCREEN SCREEN() void show_text( std::string text ) std::cout << text << std::endl; ; 29

//---------------------------------------- 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 SCREEN screen; 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" ) screen.show_text( abc.get_name() ); if( input == "1" ) screen.show_text( def.get_name() ); if( input == "off" ) break; 30

//---------------------------------------- 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 31

1.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 ) 32