我需要帮助将一段代码转换成C语言。

时间:2021-01-03 02:03:43

I am new in programming. I need to convert a Fortran 95 file into a C file. At the beginning of the Fortran file I have a module that contains a bunch of variables that are used inside various functions (don't mind the comments):

我是编程新手。我需要将Fortran 95文件转换成C文件。在Fortran文件的开头,我有一个模块,其中包含了一些在各种函数中使用的变量(请不要介意注释):

MODULE data

IMPLICIT NONE
SAVE

DOUBLE PRECISION, PARAMETER :: tmax_dsmc  = 450.D0     ! durata simulazione
DOUBLE PRECISION, PARAMETER :: tim_dsmc   = 150.D0     ! istante inizio campionamento
DOUBLE PRECISION, PARAMETER :: dt_dsmc    = 0.05D0     ! passo temporale
DOUBLE PRECISION, PARAMETER :: alpha_dsmc = 0.02D0     ! gradiente velocita' 

Is there a way to replicate this in C? I know that I could use the command #define variable x but I don't know if it is correct; my goal is to have these constants defined somewhere in the code so that if I modify one, every single part of the program could knows the new value I have assigned. Of course I can define each constant in every function I have in my code but it would be a great waste of time.

是否有办法在C中复制这个?我知道我可以使用命令#define变量x但是我不知道它是否正确;我的目标是将这些常量定义在代码中的某个地方,这样如果我修改了一个,程序的每个部分都可以知道我分配的新值。当然,我可以在我的代码中定义每个函数,但是这是浪费时间。

1 个解决方案

#1


3  

If you want to define constant in C, you can use #define PI 3.1415926. If you don't want to duplicate this everywhere, then you can use #includes like this:

如果想在C中定义常数,可以使用#define PI 3.1415926。如果您不想在任何地方复制这个,那么您可以使用#include如下:

First a header (in a file named MyConsts.h):

第一个标题(在一个名为MyConsts.h的文件中):

/* My constants */
#define PI 3.1415926

then a module (in some .c file):

然后一个模块(在一些.c文件中):

/* A module */
#include "MyConsts.h" // include here the contents of the file
...
double p = 2*PI*r;
...

and another one (in another .c file):

另一个(在另一个.c文件中):

/* Another module */
#include "MyConsts.h" // include here the contents of the file
...
double s = PI*r*r;
...

If you use a clean compilation procedure by describing the dependencies in a Makefile for example, then every modification made in MyConsts.h will be reflected into the object modules.

如果您使用一个干净的编译过程,例如在Makefile中描述依赖关系,那么在MyConsts中进行的每一个修改。h将被反射到对象模块中。

#1


3  

If you want to define constant in C, you can use #define PI 3.1415926. If you don't want to duplicate this everywhere, then you can use #includes like this:

如果想在C中定义常数,可以使用#define PI 3.1415926。如果您不想在任何地方复制这个,那么您可以使用#include如下:

First a header (in a file named MyConsts.h):

第一个标题(在一个名为MyConsts.h的文件中):

/* My constants */
#define PI 3.1415926

then a module (in some .c file):

然后一个模块(在一些.c文件中):

/* A module */
#include "MyConsts.h" // include here the contents of the file
...
double p = 2*PI*r;
...

and another one (in another .c file):

另一个(在另一个.c文件中):

/* Another module */
#include "MyConsts.h" // include here the contents of the file
...
double s = PI*r*r;
...

If you use a clean compilation procedure by describing the dependencies in a Makefile for example, then every modification made in MyConsts.h will be reflected into the object modules.

如果您使用一个干净的编译过程,例如在Makefile中描述依赖关系,那么在MyConsts中进行的每一个修改。h将被反射到对象模块中。