作者PanJC ((#‵Д′)f〒﹌﹌﹌﹌﹌ꄩ
看板NCTU-STAT100
標題[閒聊] struct vect
時間Thu Jul 28 11:39:34 2011
#include <stdio.h>
#include <stdlib.h>
struct vect{
int n;
double *array;
};
void allocVect(struct vect *v1, int n);
void freeVect(struct vect *v1);
int setVect(struct vect *v1, int i, double value);
double getVect(struct vect *v1, int i);
int main(){
struct vect v;
int n = 10, i;
allocVect(&v,n);
for( i = 0 ; i < n+1 ; i++ ){
setVect(&v,i,i*10.0);
//v.array[i] = i*10.0;
printf("v.array[%d] = %lf\n",i,getVect(&v,i));
}
freeVect(&v);
}
void allocVect(struct vect *v1, int n){
v1->array = (double *) calloc(n, sizeof(double));
v1->n = n;
}
void freeVect(struct vect *v1){
free(v1->array);
}
int setVect(struct vect *v1, int i, double value){
if( (i >= v1->n) || (i<0) ){
printf("Index out of element range in Vector");
return 0;
}
v1->array[i] = value;
return 1;
}
double getVect(struct vect *v1, int i){
if( (i >= v1->n) || (i<0) ){
printf("Index out of element range in Vector");
return 0;
}
return(v1->array[i]);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.7.248