i'm trying create class can manage bidimensional array. used same algorithm in c without objects (obviously) , works fine. problem when try modify variable of object doesn't modify in main program, , absurd!
nsmatrice.h
#import <cocoa/cocoa.h> #import <foundation/foundation.h> struct ogmat { float value; int row, col; struct ogmat *next; }; typedef struct ogmat tmat; typedef tmat* tptmat; @interface nsmatrice : nsobject{ tptmat m; } - (void) crea; //inizializza la matrice - (int) destroy; //la distrugge - (bool) exisistsatxy :(int) x :(int) y; - (void) setvalueatxy :(float) value :(int) x :(int) y; - (float) getvalueatxy :(int) x :(int) y; @end
nsmatrice.c
#import "nsmatrice.h" @implementation nsmatrice - (void) crea{ //inizializza la matrice m=null; } - (int) destroy{ //la distrugge while (m!=null){ tptmat pa; pa=m->next; free(m); m=null; m=pa; } return 1; } - (bool) exisistsatxy :(int) x :(int) y{ while (m!=null&&(!(m->col==y&&m->row==x))){ m=m->next; } if (m==null) return false; else return true; } - (void) setvalueatxy :(float) value :(int) x :(int) y{ tmat *pa; if (!([self exisistsatxy:x:y])){//nuova testa modifico pa pa=malloc(sizeof(tmat)); pa->col=y; pa->row=x; pa->value=value; pa->next=m; m=pa; }else{ pa=m; while (pa!=null&&(!(pa->col==y&&pa->row==x))){//stessa testa pa=pa->next; } if (pa!=null){ pa->value=value; } } } - (float) getvalueatxy :(int) x :(int) y{ tmat *pa; pa=m; while (pa!=null&&(!(pa->col==y&&pa->row==x))){ pa=pa->next; } if (pa!=null){ return pa->value; } else return 0; } @end
obj_mat.m
#import <foundation/foundation.h> #import "nsmatrice.h" #import <time.h> int main () { nsmatrice *m; int i,j; m=[nsmatrice alloc]; [m crea]; srand(time(null)); (i=0;i<5;i++) (j=0;j<5;j++) [m setvalueatxy:rand()%20 :i :j]; (i=0;i<5;i++){ (j=0;j<5;j++) printf("%.2f\t",[m getvalueatxy:i :j]); printf("\n"); } }
i suspect got exisistsatxy
method - changes link in list m
addresses, rather making copy of pointer , using traverse - in setvalueatxy
method.
Comments
Post a Comment