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

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

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

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

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

PowerPoint プレゼンテーション

memo

Microsoft PowerPoint - 説明3_if文switch文(C_guide3)【2015新教材対応確認済み】.pptx

新・明解C言語 実践編

(STL) STL 1 (deta structure) (algorithm) (deta structure) 2 STL STL (Standard Template Library) 2.1 STL STL ( ) vector<int> x; for(int i = 0; i < 10;

新版明解C言語 実践編

バイオプログラミング第 1 榊原康文 佐藤健吾 慶應義塾大学理工学部生命情報学科

cpp1.dvi

DVIOUT

1.ppt

C プログラミング演習 1( 再 ) 2 講義では C プログラミングの基本を学び 演習では やや実践的なプログラミングを通して学ぶ

C#の基本2 ~プログラムの制御構造~

プログラミング基礎

kiso2-06.key

r07.dvi

ohp07.dvi

untitled

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

Taro-2分探索木Ⅰ(公開版).jtd

untitled

Taro-プログラミングの基礎Ⅱ(公

Taro-ポインタ変数Ⅰ(公開版).j

情報工学実験 C コンパイラ第 2 回説明資料 (2017 年度 ) 担当 : 笹倉 佐藤

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

Taro-スタック(公開版).jtd

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

Z...QXD (Page 1)

.n.s.N.._...{.\1

cp-7. 配列

PowerPoint Presentation

ohp03.dvi

RX ファミリ用 C/C++ コンパイラ V.1.00 Release 02 ご使用上のお願い RX ファミリ用 C/C++ コンパイラの使用上の注意事項 4 件を連絡します #pragma option 使用時の 1 または 2 バイトの整数型の関数戻り値に関する注意事項 (RXC#012) 共用

プログラミングI第10回

gengo1-11

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

Taro-ファイル処理(公開版).jtd

Microsoft Word - C言語研修 C++編 3.doc

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

/*Source.cpp*/ #include<stdio.h> //printf はここでインクルードして初めて使えるようになる // ここで関数 average を定義 3 つの整数の平均値を返す double 型の関数です double average(int a,int b,int c){

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

Microsoft Word - Cプログラミング演習(12)

design_pattern.key

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

kiso2-03.key

Undestand の解析 Understand の C 言語で抽出できない依存関係について サンプルコードを用いて説明します 確認バージョン Understand 3.0 (Build 640) Understand 3.1 (Build 700) Understand 4.0 (Build 78

Transcription:

アセンブラ (Z80) の例 ORG 100H LD B,10 SUB A LOOP: ADD A,B DEC B JR NZ,LOOP LD (SUM),A HALT ORG 200H SUM: DEFS 1 END 1

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

ベクトルの C による表現 C 言語でのデータ抽象化への模索 3

ベクトル ( 配列を使わない ) #include <stdio.h> main() { int a1=2,a2=4,a3=6; //vector a=( 2, 4, 6); int b1=12,b2=14,b3=16;//vector b=(12,14,16); int c1=0,c2=0,c3=0; //vector c=( 0, 0,0); /* vector c=a+b */ c1=a1+b1; c2=a2+b2; c3=a3+b3; printf("a=>(%d,%d,%d) n + n",a1,a2,a3); printf("b=>(%d,%d,%d) n = n",b1,b2,b3); printf("c=>(%d,%d,%d)",c1,c2,c3); 4

#include <stdio.h> main() { // 配列を使う int a[]={2,4,6; //vector a=( 2, 4, 6); int b[]={12,14,16;//vector b=(12,14,16); int c[]={0,0,0; //vetcot c=( 0, 0,0); /* vector c=a+b */ c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2]; printf(" 配列は使うが ループは使わない n"); printf("a=>(%d,%d,%d) n + n",a[0],a[1],a[2]); printf("b=>(%d,%d,%d) n = n",b[0],b[1],b[2]); printf("c=>(%d,%d,%d)",c[0],c[1],c[2]); ベクトル ( 配列を使う ) /* 実行結果配列は使うが ループは使わない a=>(2,4,6) + b=>(12,14,16) = 5 c=>(14,18,22) */

ベクトル ( 配列 + ループ ) #include <stdio.h> main() { // 配列を使う & ループを使う int a[]={2,4,6; //vector a=( 2, 4, 6); int b[]={12,14,16;//vector b=(12,14,16); int c[]={0,0,0; //vetcot c=( 0, 0,0); int array_max=sizeof(a)/sizeof(a[0]); // 配列の繰り返しの大きさを求める printf("array_max=%d n",array_max); /* vector c=a+b */ int i; // vector の a(i) への位置づけ用 for (i=0;i<array_max;i++) c[i]=a[i]+b[i]; printf(" 配列は使う & ループを使う n"); printf("a=>("); for (i=0;i<array_max-1;i++) printf("%d,",a[i]); printf("%d",a[array_max-1]); printf(") n + n"); printf("b=>("); for (i=0;i<array_max-1;i++) printf("%d,",b[i]); printf("%d",b[array_max-1]); printf(") n + n"); printf("c=>("); for (i=0;i<array_max-1;i++) printf("%d,",c[i]); printf("%d) n",c[array_max-1]); /* 実行結果 array_max=3 配列は使う & ループを使う a=>(2,4,6) + b=>(12,14,16) + c=>(14,18,22) */ 6

#include <stdio.h> void display(char *vecid,int *vector,int a_max) { 結果の表示を関数化 int i; printf("%s=>(",vecid); for (i=0;i<a_max-1;i++) printf("%d,",vector[i]); printf("%d",vector[a_max-1]); printf(") n"); main() { // 配列を使う & ループを使う & 結果の表示を関数化 int a[]={2,4,6; //vector a=( 2, 4, 6); int b[]={12,14,16;//vector b=(12,14,16); int c[]={0,0,0; //vetcot c=( 0, 0,0); int array_max=sizeof(a)/sizeof(a[0]); // 配列の繰り返しの大きさを求める printf("array_max=%d n",array_max); /* vector c=a+b */ int i; // vectorのa(i) への位置づけ用 for (i=0;i<array_max;i++) c[i]=a[i]+b[i]; printf(" 配列は使う & ループを使う & 結果の表 示を関数化 n"); display("a",a,array_max); printf(" + n"); display("b",b,array_max); printf(" = n"); display("c",c,array_max); 7

実行結果 /* 実行結果 array_max=3 配列は使う & ループを使う & 結果の表示を関数化 a=>(2,4,6) + b=>(12,14,16) = c=>(14,18,22)*/ 8

ベクトルの加算を関数化 (1) #include <stdio.h> #define MAX 3 typedef struct vec{ int v[max]; vector; void display(char *vecid,vector vec,int a_max) { int i; printf("%s=>(",vecid); for (i=0;i<a_max-1;i++) printf("%d,",vec.v[i]); printf("%d",vec.v[a_max-1]); printf(") n"); vector *vectoradd(vector aa,vector bb,int a_max) { int i; vector *ans; ans=(vector *)malloc(sizeof(vector)); for (i=0;i<a_max;i++) ans->v[i]=aa.v[i]+bb.v[i]; return ans; 9

ベクトルの加算を関数化 (2) main() { // 配列を使う & ループを使う & 結果の表示を関数化 & ベクトルの加算を関数化 // &typedef を使い vector 型を定義 vector a={2,4,6; //vector a=( 2, 4, 6); vector b={12,14,16;//vector b=(12,14,16); vector c={0,0,0; //vetcot c=( 0, 0,0); int array_max=sizeof(a.v)/sizeof(a.v[0]); // 配列の繰り返しの大きさを求める printf("array_max=%d n",array_max); /* vector c=a+b */ int i; // vector の a(i) への位置づけ用 c=*vectoradd(a,b,array_max); printf( 配列は使う & ループを使う & 結果の表 示を関数化 & ベクトルの加算を関数化 &typedef を使い vector 型を定義 n"); display("a",a,array_max); printf(" + n"); display("b",b,array_max); printf(" = n"); display("c",c,array_max); 10

ベクトルの加算を関数化 (3) /* 実行結果 array_max=3 配列は使う & ループを使う & 結果の表示を関数化 & ベクトルの加算を関数化 & typedef を使い vector 型を定義 a=>(2,4,6) + b=>(12,14,16) = c=>(14,18,22) */ 11

#include <stdio.h> #define MAX 3 typedef struct vec{ int v[max]; vector; void display(char *vecid,vector vec,int a_max) { int i; printf("%s=>(",vecid); for (i=0;i<a_max-1;i++) printf("%d,",vec.v[i]); printf("%d",vec.v[a_max-1]); printf(") n"); vector *vectoradd(vector aa,vector bb,vector ans,int i) { if ( i!= 0 ) { i--; ans.v[i]=aa.v[i]+bb.v[i]; ans=*vectoradd(aa,bb,ans,i); else ; return &ans; 再帰でループを実現 (1) 12

main() { // 再帰でループを実現 vector a={2,4,6; //vector a=( 2, 4, 6); vector b={12,14,16;//vector b=(12,14,16); vector c={0,0,0; //vetcot c=( 0, 0,0); 再帰でループを実現 (2) int array_max=sizeof(a.v)/sizeof(a.v[0]); // 配列の繰り返しの大きさ printf("array_max=%d n",array_max); /* vector c=a+b */ c=*vectoradd(a,b,c,array_max); printf(" 配列は使う & n"); printf(" &<< 再帰でループを実現 >> n"); display("a",a,array_max); printf(" + n"); display("b",b,array_max); printf(" = n"); display("c",c,array_max); /* 実行結果 array_max=3 配列は使う & &<< 再帰でループを実現 >> a=>(2,4,6) + b=>(12,14,16) = c=>(14,18,22) */ 13

#include <stdio.h> // -------------------- Start of Abstract Data Type typedef struct vec{ int cell; struct vec *next; vector; 抽象データ型的 (1) void err2con(char* msg) { printf("error:%s:",msg); void display(char *vecid,vector **head) { vector *work=*head; int i; printf("%s=>(",vecid); while (work!= NULL){ if (work->next!= NULL) printf("%d,",work->cell); else printf("%d",work->cell); // ベクトルの最後の要素の後ろに, は付けない work=work->next; printf(") n"); 14

抽象データ型的 (2) vector *vectorconstructor(vector **head,int max,int value[]) { vector *work; int i; for (i=max-1;i>=0;i--) { if ((work = (vector *)malloc(sizeof(*work))) == NULL) { err2con("memory nothing"); return NULL; work->cell = value[i]; work->next = *head; *head = work; printf("cell=%d ",work->cell); return *head; 15

抽象データ型的 (3) void vectoradd(vector **aa,vector **bb,vector **ans) { vector *a=*aa; vector *b=*bb; vector *c=*ans; while (c!= NULL){ c->cell = a->cell + b->cell; a=a->next; b=b->next; c=c->next; vectordestructor(vector **head){ vector *work=*head; while (work!= NULL){ free(work); work=work->next; // -------------------- End of Abstract Data Type 16

main() { // 抽象データ型的抽象データ型的 (4) vector *ahead=null; vector *bhead=null; vector *chead=null; int a[]={2,4,6; int b[]={12,14,16; int c[]={0,0,0; // コンストラクタの呼び出しを擬似る vectorconstructor(&ahead,3,a); //vector a=( 2, 4, 6); vectorconstructor(&bhead,3,b); //vector b=(12,14,16); vectorconstructor(&chead,3,c); //vetcot c=( 0, 0,0); /* メソッド ( ベクトル加算 ) の呼び出しvector c=a+b */ vectoradd(&ahead,&bhead,&chead); printf(" n 抽象データ型的な方式 n"); // メソッド ( ベクトルの表示 ) の呼び出し display("a",&ahead); printf(" + n"); display("b",&bhead); printf(" = n"); display("c",&chead); // デストラクタの呼び出しを擬似る vectordestructor(&ahead); vectordestructor(&bhead); 17 vectordestructor(&chead);

抽象データ型的 (5) /* 実行結果 cell=6 cell=4 cell=2 cell=16 cell=14 cell=12 cell=0 cell=0 cell=0 抽象データ型的な方式 a=>(2,4,6) + b=>(12,14,16) = c=>(14,18,22) */ 18

C++ による演算子定義 (1) #include <iostream> using namespace std; #define MAX 5 class vec { int cell[max]; public: vec(); vec(int a,int b,int c,int d,int e); void get(int v[max]); vec operator+(vec v); vec operator*(int c); vec operator=(vec v); ; vec::vec() { for(int i=0;i<max;i++) cell[i]=0; vec::vec(int a,int b,int c,int d,int e) { cell[0]=a; cell[1]=b; cell[2]=c; cell[3]=d; cell[4]=e; void vec::get(int v[max]) { for(int i=0;i<max;i++) v[i]=cell[i]; 19

vec vec::operator+(vec v) { vec wk; for(int i=0;i<max;i++) wk.cell[i]=cell[i] + v.cell[i]; return wk; C++ による演算子定義 (2) vec vec::operator*(int c) { vec wk; for(int i=0;i<max;i++) wk.cell[i]=cell[i] * c; return wk; vec vec::operator=(vec v) { for(int i=0;i<max;i++) cell[i]=v.cell[i]; return *this; 20

int main() { int cell[max]; vec a(10,20,30,40,50),b(1,2,3,4,5),c(0,0,0,0,0); c = a + b; c.get(cell); C++ による演算子定義 (3) cout << "a+b => "; for(int i=0;i<max;i++) cout << cell[i] << " "; cout <<" n"; c = a * 10; c.get(cell); cout << "a*10 => "; for(int i=0;i<max;i++) cout << cell[i] << " "; cout <<" n"; c = a * 10 + b; c.get(cell); cout << "a*10+b => "; for(int i=0;i<max;i++) cout << cell[i] << " "; cout <<" n"; c = (a + b) * 10; c.get(cell); cout << "(a+b)*10 => "; for(int i=0;i<max;i++) cout << cell[i] << " "; cout <<" n"; a.get(cell); cout << " 今の a を表 示 "; for(int i=0;i<max;i++) cout << cell[i] << " "; cout <<" n a = c; a.get(cell); cout << "c を a に代入したあとの c を表 示 "; for(int i=0;i<max;i++) cout << cell[i] << " "; cout <<" n"; return 0; 21

C++ による演算子定義 (4) /* // 実行例 a+b => 11 22 33 44 55 a*10 => 100 200 300 400 500 a*10+b => 101 202 303 404 505 (a+b)*10 => 110 220 330 440 550 今の a を表示 10 20 30 40 50 c を a に代入したあとの c を表示 110 220 330 440 550 */ 22