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

Similar documents
1.ppt

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

超初心者用

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

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

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

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

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

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

‚æ4›ñ

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

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

/* 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¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

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

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

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

DVIOUT

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

Microsoft Word - no15.docx

P05.ppt

ohp03.dvi

演習課題No12

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

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

PowerPoint Presentation

Prog1_15th

橡Pro PDF

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

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

2 Excel =sum( ) =average( ) B15:D20 : $E$26 E26 $ =A26*$E$26 $ $E26 E$26 E$26 $G34 $ E26 F4

DVIOUT

main

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

r03.dvi

program.dvi

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

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

新・明解C言語で学ぶアルゴリズムとデータ構造

C C UNIX C ( ) 4 1 HTML 1

joho07-1.ppt

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

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

O(N) ( ) log 2 N

pg1

文書1

r07.dvi

ohp07.dvi

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

comment.dvi

r08.dvi

1 4 2 EP) (EP) (EP)

untitled

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

1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

第117期報告書

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

±é½¬£²¡§£Í£Ð£É½éÊâ

Microsoft PowerPoint - C言語の復習(配布用).ppt [互換モード]

ex01.dvi

プログラミング基礎

WinHPC ppt

PowerPoint プレゼンテーション

A common.h include #include <stdio.h> #include <time.h> #define MAXN int A[MAXN], n; double start,end; void inputdata(

( ) ( ) 30 ( ) 27 [1] p LIFO(last in first out, ) (push) (pup) 1

ex01.dvi

Microsoft PowerPoint - 説明2_演算と型(C_guide2)【2015新教材対応確認済み】.pptx

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè11²ó – ½ÉÂꣲ¤Î²òÀ⡤±é½¬£² –

untitled

untitled

untitled

gengo1-11

Cプログラミング1(再) 第2回

.,.,..,? 2.,.?.,...,...,.,.,.,.,,..,..,,.,,.,.,..,..,....,.,.,.,?,...,,.... Dr.Hener, i

プログラミング基礎

DA100データアクイジションユニット通信インタフェースユーザーズマニュアル

配列, 関数, 構造体

2008 DS T050049

ポインタ変数

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

printf("5つの整数を入力して下さい \n"); /* データ入力 */ for( /*** 02 ***/ ){ printf("%dつ目の入力 :",i+1); scanf("%d", /*** 03 ***/ ); sum=dat[0]; /* 合計値の初期設定 */ n_max= 0

目 目 用方 用 用 方

Microsoft PowerPoint - lec10.ppt

kiso2-09.key

PowerPoint プレゼンテーション - 物理学情報処理演習

Taro-再帰関数Ⅱ(公開版).jtd

C 言語講座 Vol 年 5 月 29 日 CISC

PowerPoint Presentation

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

Microsoft PowerPoint - 11th.ppt [互換モード]

untitled

17 1. strucr Potter ( ) Harry Potter and the Philosopher s Stone 1997 English Title : Harry Potter and the Philosopher s Stone : : 1997 #include<stdio

第3回 配列とリスト

新・明解C言語 実践編

XMPによる並列化実装2

Transcription:

C 8 ( ) C 1 / 30

n! a n+2 = a n+1 + a n ( ) C 2 / 30

#include <stdioh> struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a, b; struct student c={"frank", 90; ( ) C 3 / 30

#include <stdioh> #include <stringh> /*strcpy */ struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a, b; strcpy(aname,"frank"); amath = 90; aphys = 83; ( ) C 4 / 30

= #include <stdioh> #include <stringh> /*strcpy */ struct student{ char name[20]; int math; int phys; ; int main(void){ b=a; return 0; Name:%sY=n",bname); Math:%dY=n",bmath); Physics:%dY=n",bphys); struct student a,b; strcpy(aname, "Frank"); amath = 90; aphys = 83; Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); Name:Frank Math:90 Physics:83 Name:Frank Math:90 Physics:83 ( ) C 5 / 30

struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a, *pa; aname amath aphys ( ) C 6 / 30

(&) struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a, *pa; pa = &a; pa aname amath aphys ( ) C 6 / 30

(->) -> (* ) #include <stdioh> #include <stringh> struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a,*pa; strcpy(aname, "Frank"); amath = 90; aphys = 83; pa=&a; strcpy(pa->name, "Thomas"); pa->phys = 92; return 0; Name:%sY=n",pa->name); Math:%dY=n",pa->math); Physics:%dY=n",pa->phys); Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); ( ) C 7 / 30

(->) -> (* ) #include <stdioh> #include <stringh> struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a,*pa; strcpy(aname, "Frank"); amath = 90; aphys = 83; Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); pa=&a; strcpy(pa->name, "Thomas"); pa->phys = 92; return 0; Name:Frank Math:90 Physics:83 Name:Thomas Math:90 Physics:92 Name:%sY=n",pa->name); Math:%dY=n",pa->math); Physics:%dY=n",pa->phys); ( ) C 7 / 30

#include <stdioh> #include <stringh> struct student{ char name[20]; int math; int phys; ; int main(void){ struct student a,*pa; strcpy(aname, "Frank"); amath = 90; aphys = 83; return 0; Name:%sY=n",pa->name); Math:%dY=n",pa->math); Physics:%dY=n",pa->phys); Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); pa=&a; strcpy(pa->name, "Thomas"); pa->phys = 92; ( ) C 8 / 30

#include <stdioh> #include <stringh> struct student{ char name[20]; int math; int phys; ; int main(void){ return 0; Name:%sY=n",pa->name); Math:%dY=n",pa->math); Physics:%dY=n",pa->phys); Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); struct student a,*pa; strcpy(aname, "Frank"); amath = 90; aphys = 83; Name:%sY=n",aname); Math:%dY=n",amath); Physics:%dY=n",aphys); pa=&a; strcpy(pa->name, "Thomas"); pa->phys = 92; Name:Frank Math:90 Physics:83 Name:Thomas Math:90 Physics:92 Name:Thomas Math:90 Physics:92 ( ) C 8 / 30

#include <stdioh> struct student{ /**/ int main(void){ struct student S1 = {"Frank",90,83; ; char name[20]; int math; int phys; double ave; Average(&S1); Name =%sy=n",s1name); Math =%dy=n",s1math); Phys =%dy=n",s1phys); Ave =%2fY=n",S1ave); void Average(struct student *std){ /* */ int sum; sum = std->math + std->phys; std->ave = (double) sum/2; return 0; Name =Frank Math =90 Phys =83 Ave =8650 ( ) C 9 / 30

Average struct student Average(struct student temp){ tempave = (double) (tempmath + tempphys)/2; return temp; int main(void){ S1=Average(S1); ( ) C 10 / 30

struct [ ]; struct student Std[20]; for(i=0;i<n;i++){ Std[i]ave = (double)(std[i]math+std[i]phys)/2; ( ) C 11 / 30

081c 20 20 100 119 N N*29+83)%100, (N*13+58)%100 20 void InputData(struct student *X) /*student */ 100 83 58 705 101 12 71 415 118 5 92 485 119 34 5 195 485 515 500 ( ) C 12 / 30

3 struct student Std[20]; Std struct student Std[N]; int i; for(i=0;i<n;i++){ Std[i]No = 100+i; InputDATA(&Std[i]); Std[i]ave = (Std[i]math+Std[i]phys)/20; ( ) C 13 / 30

3 struct student Std[20]; Std struct student Std[N]; int i; struct student Std[N], *p_std; int i; p_std = Std; for(i=0;i<n;i++){ Std[i]No = 100+i; InputDATA(&Std[i]); Std[i]ave = (Std[i]math+Std[i]phys)/20; for(i=0;i<n;i++){ p_std->no = 100+i; InputDATA(p_Std); p_std->ave = (p_std->math+p_std->phys)/20; p_std ++; ( ) C 13 / 30

struct student Std[N], *p_std; int i; p_std = Std; struct student Std[N], *p_std; int i; p_std = Std; for(i=0;i<n;i++){ p_std->no = 100+i; InputDATA(p_Std); p_std->ave = (p_std->math+p_std->phys)/20; p_std ++; for(i=0;i<n;i++){ (p_std+i)->no = 100+i; InputDATA(p_Std+i); (p_std+i)->ave = ((p_std+i)->math+(p_std+i)->phys)/20; ( ) C 14 / 30

( ) C 15 / 30

1! = 1 2! = 1! 2 3! = 2! 3 = (1! 2) 3 n! = (n 1)! n = (n 2)! (n 1) n = #include <stdioh> int factorial( int n ){ int m; if( n==0 n==1 ){ 1"); return 1; else{ %d *(",n); m = n * factorial( n-1 ); )"); return m; int main(void){ N= 5; factorial(n); return 0; ( ) C 16 / 30

2 f (0) = 0 f (1) = 1 f (2) = f (1) + f (0) = 1 f (3) = f (2) + f (1) = 2 f (n) = f (n 1) + f (n 2) int fibonacci( int n ){ if( n==0 ){ return 0; else if( n == 1){ return 1; else { return fibonacci(n-1)+fibonacci(n-2); ( ) C 17 / 30

( ) C 18 / 30

int factorial( int n ){ int m; if( n==0 n==1 ){ 1"); return 1; else{ %d *(",n); m = n * factorial( n-1 ); )"); return m; int factorial( int n ){ int i, m=1; 1"); for( i=2; i<=n; i++){ *%d",i ); m = m *i; return m; ( ) C 19 / 30

int fibonacci( int n ){ if( n==0 ){ return 0; else if( n == 1){ return 1; else { return fibonacci(n-1)+fibonacci(n-2); int fibonacci( int n ){ int i, fn, fn1=1, fn2=0; if( n==0 ){ return 0; else if( n == 1){ return 1; else { for( i=2; i<=n;i++){ fn=fn1+fn2; fn2=fn1; fn1=fn; return fn; ( ) C 20 / 30

:082c n r n C r combination n C r nc r = n 1 C r 1 + n 1 C r, n C 0 = n C n = 1, n C 1 = n Input n : 20 Input r : 3 20 C 3 = 1140 ( ) C 21 / 30

3 A C A B C A B C 2 n 1 ( ) C 22 / 30

( ) C 23 / 30

( ) C 23 / 30

3 0 A B C 4 A B C 1 A B C 5 A B C 2 A B C 6 A B C 3 A B C 7 A B C ( ) C 24 / 30

n n 1 A B A C n 1 3 1 A B A C 3 1 C ( ) C 25 / 30

n 1 A B B A C A B B B A A ( ) C 26 / 30

void Hanoi(int n, char *from, char *work, char *dest) from dest) n n from work dest ( ) C 27 / 30

main N (from) A (dest) C (work) B Hanoi(N,"A","B","C"); from dest Move a disk from %s to %s",from,dest); n 1 (from) (work) Hanoi(n-1,from, dest, work); ( ) C 28 / 30

: 083c How many disks? 3 Move the disc from A to C Move the disc from A to B Move the disc from C to B Move the disc from A to C Move the disc from B to A Move the disc from B to C Move the disc from A to C ( ) C 29 / 30

#include <stdioh> void Hanoi(int n,char *from,char *work,char *dest){ if(n>=2) Hanoi(,,, ); Move the disc from %s to %sy=n",from,dest); if(n>=2) Hanoi(,,, ); int main(void){ int N; How many disks? "); scanf("%d",&n); Hanoi(N,"A","B","C"); return 0; ( ) C 30 / 30