结构数组。 [英] array of structures.

查看:58
本文介绍了结构数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有这样的结构 -


typedef vector vectorstruct

{

double x,y,z ;

}向量;


typedef struct verticesstruct

{

vector v;

}顶点; / *一个顶点是一个向量* /


typedef struct triangletruct

{

int v0,v1,v2;

}三角形;


typedef struct objectstruct

{

int nvert;

int ntri;

vertex * vert;

triangle * tri;

}对象;


................

...............


稍后在程序的某个地方我有这样的声明 -


object * obj;


obj-> vert = malloc (nvert * sizeof(vertex)); / *创建一个

顶点数组* /


obj-> tri = malloc(ntri * sizeof(triangle));


for(i = 0; i< obj-> nvert; i ++)/ *试图获取每个顶点的输入

其中包含x,y,z分量是一个向量* /

scanf("%f%f%f",& obj-> vert [i] .x,& obj-> vert [i] .y ,& obj-> vert [i] .z);


^^^^^^^^这是上面创建数组的表示法然后

正确访问元素??

稍后我也会这样做 -


for(i = 0; i< obj-> ntri; i ++)

scanf("%d%d%d"& obj-> tri [i] .v0,& obj-> tri [i] .v1,& obj-> tri [i] .v2);

I have structures like this in my program -

typedef vector vectorstruct
{
double x, y,z;
} vector;

typedef struct verticesstruct
{
vector v;
} vertex; /*a vertex is a vector */

typedef struct trianglestruct
{
int v0,v1, v2;
}triangle;

typedef struct objectstruct
{
int nvert;
int ntri;
vertex *vert;
triangle *tri;
}object;

................
...............

later somewhere in the program i have a statement like this -

object *obj;

obj->vert = malloc( nvert * sizeof(vertex)); /* Creating an array of
vertices */

obj->tri = malloc(ntri * sizeof(triangle));

for(i=0;i<obj->nvert; i++)/* trying to take input for each vertex
which has x, y, z components as it is a vector*/
scanf("%f %f %f", &obj->vert[i].x, &obj->vert[i].y, &obj->vert[i].z);

^^^^^^^^ is this above notation of creating an array and then
accessing the elements correct ??
later on i also do this -

for(i=0;i<obj->ntri;i++)
scanf("%d %d %d", &obj->tri[i].v0, &obj->tri[i].v1, &obj->tri[i].v2);

推荐答案

On Sun,2008年3月9日06:45:54 -0700,Cell写道:
On Sun, 09 Mar 2008 06:45:54 -0700, Cell wrote:

我的程序中有这样的结构 -


typedef vector vectorstruct
I have structures like this in my program -

typedef vector vectorstruct



语法错误。 "向量';应该是结构。请尝试从您实际尝试使用的代码中尽可能复制并粘贴

,以便可以避免错误

等。

Syntax error. "vector" should be "struct". Please try to copy and paste
from code you''ve actually tried to use as much as possible, so that errors
such as these can be avoided.


{

double x,y,z;

} vector;


typedef struct verticesstruct

{

vector v;
{
double x, y,z;
} vector;

typedef struct verticesstruct
{
vector v;



这很好,但是......

This is fine, but...


} vertex; / *一个顶点是一个向量* /
} vertex; /*a vertex is a vector */



.... vertex是一个有一个成员的结构。该成员名为v,

具有类型向量。

....vertex is a structure with one member. This member is named "v", and
has type vector.


typedef struct triangletruct

{

int v0,v1,v2;

}三角形;
typedef struct trianglestruct
{
int v0,v1, v2;
}triangle;



好​​的,所以三角形有三个成员v0,v1和v2。

Okay, so a triangle has three members v0, v1, and v2.


typedef struct objectstruct

{

int nvert;

int ntri;

vertex * vert;

triangle * tri;

}对象;


...............

..............


稍后在程序的某个地方我有这样的声明 -


object * obj;


obj-> vert = malloc(nvert * sizeof(vertex)); / *创建一个

顶点数组* /
typedef struct objectstruct
{
int nvert;
int ntri;
vertex *vert;
triangle *tri;
}object;

...............
..............

later somewhere in the program i have a statement like this -

object *obj;

obj->vert = malloc( nvert * sizeof(vertex)); /* Creating an array of
vertices */



什么是nvert?你还没有宣布它。你的意思是obj-> nvert?请

尝试从您实际尝试使用的代码中复制和粘贴尽可能多的

,以便可以避免这些错误。

What is nvert? You haven''t declared it. Did you mean obj->nvert? Please
try to copy and paste from code you''ve actually tried to use as much as
possible, so that errors such as these can be avoided.


obj-> tri = malloc(ntri * sizeof(triangle));
obj->tri = malloc(ntri * sizeof(triangle));



见上文。

See above.


for(i = 0; i< obj-> nvert; i ++ )/ *试图获取每个顶点的输入,其中
有x,y,z分量,因为它是向量* / scanf(%f%f%f,

& obj-> vert [i] .x,& obj-> vert [i] .y,& obj-> vert [i] .z);
for(i=0;i<obj->nvert; i++)/* trying to take input for each vertex which
has x, y, z components as it is a vector*/ scanf("%f %f %f",
&obj->vert[i].x, &obj->vert[i].y, &obj->vert[i].z);



顶点有一个成员。该成员的名称为v。顶点不具有任何成员x,y或z的
。矢量确实。

A vertex has one member. This member has a name "v". A vertex does not
have any members x, y, or z. A vector does.


^^^^^^^^这是上面创建数组的表示法
^^^^^^^^ is this above notation of creating an array



数组创建没问题。

The array creation is okay.


然后访问

元素正确吗?
and then accessing
the elements correct ??



访问权限不合适。

The access is not okay.


稍后我也会这样做 -


for(i = 0; i< obj-> ntri; i ++)

scanf("%d%d%d",& obj-> tri [i] .v0,& obj-> tri [i] .v1,& obj-> tri [i] .v2);
later on i also do this -

for(i=0;i<obj->ntri;i++)
scanf("%d %d %d", &obj->tri[i].v0, &obj->tri[i].v1, &obj->tri[i].v2);



三角形有成员v0,v1和v2,你正在访问成员v0,

v1和v2。毫不奇怪,这是有效的。 :)

A triangle has members v0, v1, and v2, and you''re accessing members v0,
v1, and v2. Not surprisingly, this works. :)


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


typedef struct vectorstruct

{

double x,y,z;

} vector;


typedef struct verticesstruct

{

vector v;

} vertex;


typedef struct triangletruct

{

int v0,v1,v2;

}三角形;


typedef struct objectstruct

{

int nvert;

int ntri;

vertex * vert;

triangle * tri;

}对象;


int read_dat_file(char * dat_file, object * obj)

{


FILE * fp;

int i;


if(!(fp = fopen(dat_file," r")))

返回-1;

while(fp!= NULL)

{

fscanf(fp,"%d%d",&(obj-> nvert),&(obj-> ntri));

printf(&\\; \ n%d%d&quo t;,obj-> nvert,obj-> ntri);


obj-> vert = malloc(obj-> nvert * sizeof(vertex));


obj-> tri = malloc(obj-> ntri * sizeof(triangle));


for(i = 0; i< obj-> nvert; i ++)

{

fscanf(fp,"%f%f%f",&(obj-> vert [i] .vx),&(obj-> vert [i] .vy),&(obj-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct vectorstruct
{
double x, y, z;
}vector;

typedef struct verticesstruct
{
vector v;
}vertex;

typedef struct trianglestruct
{
int v0, v1, v2;
}triangle;

typedef struct objectstruct
{
int nvert;
int ntri;
vertex *vert;
triangle *tri;
}object;

int read_dat_file( char * dat_file, object * obj )
{

FILE *fp;
int i;

if(!(fp = fopen(dat_file,"r")))
return -1;
while(fp!=NULL)
{
fscanf(fp, "%d %d", &(obj->nvert), &(obj->ntri));
printf("\n%d %d", obj->nvert, obj->ntri);

obj->vert= malloc(obj->nvert * sizeof(vertex));

obj->tri = malloc(obj->ntri * sizeof(triangle));

for(i=0;i<obj->nvert;i++)
{
fscanf(fp, "%f %f %f",&(obj->vert[i].v.x), &(obj->vert[i].v.y), &(obj-

> vert [i] .vz)) ;
>vert[i].v.z));



printf("%f%f%f",obj-> vert [i] .vx,obj-> vert [i] .vy, obj-

printf("%f %f %f", obj->vert[i].v.x, obj->vert[i].v.y, obj-


> vert [i] .vz);
>vert[i].v.z);



}


for(i = 0; i< obj-> ntri; i ++)

{

fscanf(fp,"%d%d%d"&(obj-> tri [i] .v0),&(obj-> tri [ i] .v1),&(obj-

}

for(i=0;i<obj->ntri;i++)
{
fscanf(fp, "%d %d %d",&(obj->tri[i].v0), &(obj->tri[i].v1), &(obj-


> tri [i] .v2));
>tri[i].v2));



printf("%d%d%d",obj-> tri [i] .v0,obj-> tri [i] .v1, obj-> tri [i] .v2);

}


}

返回1;

}


int main()

{


object * obj;

char * s;

clrscr();

obj = malloc(sizeof(object));

strcpy(s,& ; sphere.dat");

if(!(read_dat_file(s,obj)))

printf(" unsuccessful");

返回0;


}

printf("%d %d %d", obj->tri[i].v0, obj->tri[i].v1, obj->tri[i].v2);
}

}
return 1;
}

int main()
{

object *obj;
char *s;
clrscr();
obj = malloc(sizeof(object));
strcpy(s, "sphere.dat");
if(!(read_dat_file(s , obj)))
printf("unsuccessful");
return 0;

}



sphere.dat文件包含球体的描述已经

三角化..


sphere.dat的第一行是这样的 -


nvert ntri / *顶点的数量,三角形的数量* /


在球体的情况下,nvert = 602,ntri = 1200


来自第二行,我们有每个顶点的坐标。组合

的3个顶点形成一个三角形。


x1 y1 z1

x2 y2 z2

.........

.........

.........

xnvert ynvert znvert


我把它存储在顶点列表中。因此对象中的vert指针

结构。类似地,有一个三指针,可用于动态创建三角形列表。


在n个顶点之后,我们有三角形的描述( 1200

条目)


12 0 1

3 4 5

23 8 5

.......

.......

.......

这些数字基本上是顶点列表的索引。所以12表示第12个顶点是
,或者是顶点列表中的12个表示。 0表示

顶点列表中的第0个(第一个条目)1表示顶点的第一个元素

列表。所以基本上3个顶点的组合形成一个三角形。

这就是我试图从文件中读取并打印出来的。
the sphere.dat file contains description of a sphere which has been
triangulated..

the first line of sphere.dat is something like this -

nvert ntri /*the number of vertices, number of triangles */

in the case of a sphere, nvert = 602, ntri = 1200

from the second line we have coordinates of each vertex. A combination
of 3 vertices forms a triangle.

x1 y1 z1
x2 y2 z2
.........
.........
.........
xnvert ynvert znvert

I stored this in a list of vertices. Hence the vert pointer in object
structure. Similarly there is a tri pointer which can be used for
dynamically creating a list of triangles.

After the n vertices, we have the description of triangles (1200
entries)

12 0 1
3 4 5
23 8 5
.......
.......
.......

Those numbers are basically indices to vertex list. So 12 indicates
the 12th vertex or 12 the entry in the vertex list. 0 indicates
0th(first entry) in the vertex list 1 indicates 1st element of vertex
list. So basically a combination of 3 vertices is forming a triangle.
And this is what Im trying to read from the file and print.


这篇关于结构数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆