アセンブラ (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