如何组织我的主文件? [英] how to organize my main file ?

查看:53
本文介绍了如何组织我的主文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过主文件,我的意思是包含主程序的那个。可以一些

一个请提供关于如何改进主文件组织的建议吗?我刚才有一个粗糙的骨架。


在我的光线追踪项目中,我必须执行以下任务(在

序列中)


1.从ascii文件中读取网格并将其存储在网格数据中

结构。

2.创建光线列表和存储它在光线列表中。

3.为快速网格遍历创建二进制空间分区树。

4.跟踪所有光线并计算散射和事件

电场。


我正在考虑为每项任务编写一个函数。


#include" main .h"


静态网格* m; / *指向网格的指针* /

static bsptree * tree; / *指向bsp树的指针* /

static ray * raylist; / *指向光线列表的指针* /

/ *函数原型* /


int read_mesh(char *);

int init_plane_wave(void);

int create_bsp_tree(void);

int calc_e_fields(void);

/ *提供ascii文件的名称(从中读取网格)

作为命令行参数,例如。 main sphere.dat * /


int main(int argc char * argv [])

{

if(argc< ; 2)

{

fprintf(stderr,insufficient argumens\ n);

返回-1;

}


if(argc 2)

{

fprintf(stderr,太多论点 n");

返回-1;

}


if(read_mesh(argv [1])

返回-1;

if(init_plane_wave())

返回-1;

if(create_bsp_tree())

返回-1;

if(calc_e_fields())

返回-1;


返回0 ;

}


/ *我决定将上述数据结构设为静态全局

,因为整个程序都需要它们* /


int read_mesh(char * filename)

{

FILE * fp;


fp = fopen(filename," r");

if(fp == NULL)

{

fprintf( stderr,错误的同时打开文件%s \ n",filename);

返回-1;

}

m = malloc(sizeof * m);

if(m == NULL)

{

fprintf(stderr,无法为mesh \ n分配内存 ;);

返回-1;

}

/ * parse_dat_file如果在解析文件时发生错误则返回-1 * /

if(parse_dat_file(fp,& m))

返回-1;

}


/ *这个函数将初始化平面,如同阅读

规范相关的平面波,如频率,电场

在参考点,平面波的方向等。它将为光线列表分配

内存。在此之后,它将调用init_rays,其中

初始化一组平行光线。正在模拟一个平面波。

a平行射线密集网格* /

int init_plane_wave(无效)

{

...

...

}


/ *此函数将读取允许的最大值树的深度,

为它分配内存* /


int create_bsp_tree(无效)

{


}


/ *此函数将调用光线跟踪函数,之后它将执行一些计算以找出分散的光线跟踪函数和事件

电场* /


int calc_e_fields(无效)

{


}

By main file, I mean the one which contains the main routine. Can some
one please provide suggestions as to how I can improve the
organization of main file ? I have just though about a rough skeleton.

In my ray tracing project, I have to carry out following task (in
sequence)

1. Read the mesh from an ascii file and store it in the mesh data
structure.
2. Create a ray list and store it in a ray list.
3. Create the binary space partitioning tree for fast mesh traversal.
4. Trace all the rays and calculate the scattered and incident
electric fields.

I''m thinking of writing a function for every task.

#include "main.h"

static mesh *m; /* pointer to the mesh */
static bsptree *tree; /* pointer to the bsp tree */
static ray *raylist; /* pointer to the ray list */

/* function prototypes */

int read_mesh(char *);
int init_plane_wave(void);
int create_bsp_tree(void);
int calc_e_fields (void);
/* Provide the name of the ascii file(from which mesh is to be read)
as a command line argument eg. main sphere.dat */

int main(int argc char *argv[])
{
if(argc < 2)
{
fprintf(stderr, "Insufficient argumens\n");
return -1;
}

if(argc 2)
{
fprintf(stderr, "Too many arguments\n");
return -1;
}

if(read_mesh(argv[1])
return -1;
if(init_plane_wave())
return -1;
if(create_bsp_tree())
return -1;
if(calc_e_fields())
return -1;

return 0;
}

/* I decided to make the above data structures as static global
because they are needed throughout the program */

int read_mesh(char *filename)
{
FILE *fp;

fp = fopen(filename, "r");
if(fp == NULL)
{
fprintf(stderr, "Error while opening the file %s\n", filename);
return -1;
}
m = malloc(sizeof *m);
if(m == NULL)
{
fprintf(stderr, "Couldn''t allocate memory for the mesh\n");
return -1;
}
/* parse_dat_file returns -1 if error occured while parsing file */
if(parse_dat_file(fp, &m))
return -1;
}

/* This function will initiailize the plane as in read the
specification related to a plane wave like frequency, electric field
at reference point, direction of the plane wave etc. It will allocate
memory for the ray list. After this it will call init_rays which
initializes a set of parallel rays. A plane wave is being simulated by
a dense grid of parallel rays */

int init_plane_wave(void)
{
...
...
}

/* This function will read the maximum allowable depth for the tree ,
allocate memory for it*/

int create_bsp_tree(void)
{

}

/* This function will call the raytrace function and after that it
will perform some calculations to find out the scattered and incident
electric fields */

int calc_e_fields (void)
{

}

推荐答案

我还有另外一个问题:编写一个函数可以销毁所有

对象(它不仅仅是一个简单的''免费''调用btw,在列表中有列表

必须首先销毁)
I have another question: Is it ok to write a function to destroy all
the objects(Its not just a simple ''free'' call btw, there are lists
within objects which must be destroyed first)


文章< 66 ***************** ***************** @ y22g2000prd。 googlegroups.com>,

pereges< Br ***** @ gmail.comwrote:
In article <66**********************************@y22g2000prd. googlegroups.com>,
pereges <Br*****@gmail.comwrote:

>我有另一个问题:是否可以编写一个函数来销毁所有对象(它不仅仅是一个简单的''免费''调用btw,在对象中有列表
必须首先销毁)
>I have another question: Is it ok to write a function to destroy all
the objects(Its not just a simple ''free'' call btw, there are lists
within objects which must be destroyed first)



当然,为什么不呢?只要对象是动态分配的,那就是。

-

" MAMA:哦 - 现在这就是生命。金钱就是生命。曾几何时自由

曾经是生活 - 现在这是钱。我猜这个世界真的有所改变。

沃尔特:不 - 这总是金钱,妈妈。我们只是不知道它。

- Lorraine Hansberry

Sure, why not? As long as the objects are dynamically allocated, that is.
--
"MAMA: Oh--So now it''s life. Money is life. Once upon a time freedom
used to be life--now it''s money. I guess the world really do change.
WALTER: No--it was always money, Mama. We just didn''t know about it."
-- Lorraine Hansberry


pereges说:
pereges said:

通过主文件,我指的是包含主例程的文件。可以一些

一个请提供关于如何改进主文件组织的建议吗?我刚才有一个粗糙的骨架。


在我的光线追踪项目中,我必须执行以下任务(在

序列中)


1.从ascii文件中读取网格并将其存储在网格数据中

结构。

2.创建光线列表和存储它在光线列表中。

3.为快速网格遍历创建二进制空间分区树。

4.跟踪所有光线并计算散射和事件

电场。


我正在考虑为每项任务编写一个函数。
By main file, I mean the one which contains the main routine. Can some
one please provide suggestions as to how I can improve the
organization of main file ? I have just though about a rough skeleton.

In my ray tracing project, I have to carry out following task (in
sequence)

1. Read the mesh from an ascii file and store it in the mesh data
structure.
2. Create a ray list and store it in a ray list.
3. Create the binary space partitioning tree for fast mesh traversal.
4. Trace all the rays and calculate the scattered and incident
electric fields.

I''m thinking of writing a function for every task.



在我看来,任务1-3可以合理地称为初始化。

任务4似​​乎是有用的一点处理。


每个任务的函数总是一个好主意,但为什么不将你的前三个任务抽象为一个名为initialise()的函数或其他东西比如

那个。它可能看起来像这样:


#include" whatever.h"


int initialise(const char * infile)

{

int rc = read_mesh(infile); / *改变read_mesh()取const char *

* /

if(0 == rc)

{

rc = init_plane_wave();

}

if(0 == rc)

{

rc = create_bsp_tree();

}

返回rc;

}


然后是main看起来像这样:


#include< stdio.h>

#include< stdlib.h>

#include" whatever.h"


int main(int argc,char ** argv)

{

int result = EXIT_FAILURE;


if(argc!= 2)

{

printf("%s arguments \\\
" ,argc< 2?" Insufficient":Too many);

}

else

{

if(0 == initialise(argv [1]))

{

if(0 == calc_e_fields())

{

结果= EXIT_SUCCESS;

}

}

}

返回结果;

}


您可能想要把main()和initialise()放在一个源文件中(你的

"主文件"正如你所说的那样),初始化例程(read_mesh和

另外两个),例如,initialise.c和calcs.c中的calcs - 这将是

帮助将每个源文件保持在一个可管理的大小,并让你更容易找到。


注意来自main的-1返回值保证有意义,

而EXIT_FAILURE则是。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

It seems to me that tasks 1-3 might reasonably be called initialisation.
Task 4 appears to be the bit that does the useful processing.

A function for every task is always a good idea, but why not abstract your
first three tasks into a function called initialise() or something like
that. It might look something like this:

#include "whatever.h"

int initialise(const char *infile)
{
int rc = read_mesh(infile); /* change read_mesh() to take const char *
*/
if(0 == rc)
{
rc = init_plane_wave();
}
if(0 == rc)
{
rc = create_bsp_tree();
}
return rc;
}

and then main would look something like this:

#include <stdio.h>
#include <stdlib.h>
#include "whatever.h"

int main(int argc, char **argv)
{
int result = EXIT_FAILURE;

if(argc != 2)
{
printf("%s arguments\n", argc < 2 ? "Insufficient" : "Too many");
}
else
{
if(0 == initialise(argv[1]))
{
if(0 == calc_e_fields())
{
result = EXIT_SUCCESS;
}
}
}
return result;
}

You might want to put main() and initialise() in one source file (your
"main file" as you call it), the initialisation routines (read_mesh and
the other two) in, say, initialise.c, and the calcs in calcs.c - this will
help to keep each source file down to a manageable size and make things
easier for you to find.

Note that a -1 return value from main isn''t guaranteed to be meaningful,
whereas EXIT_FAILURE is.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于如何组织我的主文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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