Emmanuel Delahaye C is a sharp tool | PoLuxR a écrit :
Salut à tous. J'aurais simplement eu besoin d'un petit coup de main pour mon programme qui compile bien mais qui ne fonctionne pas comme je le souhaiterais.
|
Tu ne respectes pas les regles de codage du langage C. Le comportement est indéfni.
Compiling: main.c
main.c: In function `main_':
main.c:27: error: implicit declaration of function `vitessex'
main.c:27: warning: nested extern declaration of `vitessex'
main.c:27: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:27: error: implicit declaration of function `accelerationx'
main.c:27: warning: nested extern declaration of `accelerationx'
main.c:29: error: implicit declaration of function `vitessey'
main.c:29: warning: nested extern declaration of `vitessey'
main.c:29: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:29: error: implicit declaration of function `accelerationy'
main.c:29: warning: nested extern declaration of `accelerationy'
main.c: At top level:
main.c:37: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:37: warning: no previous prototype for 'vitessex'
main.c: In function `vitessex':
main.c:39: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:39: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: At top level:
main.c:44: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:44: warning: no previous prototype for 'vitessey'
main.c: In function `vitessey':
main.c:46: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:46: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: At top level:
main.c:52: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:52: warning: no previous prototype for 'accelerationx'
main.c: In function `accelerationx':
main.c:55: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:55: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: At top level:
main.c:61: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:61: warning: no previous prototype for 'accelerationy'
main.c: In function `accelerationy':
main.c:64: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:64: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
Process terminated with status 1 (0 minutes, 0 seconds)
4 errors, 26 warnings
|
Le plus simple, pour commencer, est de suivre la regle 'définir avant d'utiliser... (petit coup de réindentation au passage...)
Code :
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #define G 6.67e-11
- #define M 5.9736e24
- double dt = 1; //déclaration du pas temporel
- int vitessex(double x0, double y0, double vxp){ //fonction vitesse selon x (vxp=vx précédent)
- double vx0;
- vx0 = vxp + dt * ( -(G * M * x0) / (pow((pow(x0, 2) + pow(y0, 2)), 1.5))); //vx0=vx0+dt*ax0
- return (vx0);
- }
- int vitessey(double x0, double y0, double vyp){ //fonction vitesse selon y (vyp = vy précédent)
- double vy0;
- vy0 = vyp + dt * ( -(G * M * y0) / (pow((pow(x0, 2) + pow(y0, 2)), 1.5))); //vy0=vy0+dt*ay0
- return (vy0);
- }
- int accelerationx(double x0, double y0){ //fonction acceleration selon x
- double ax0;
- ax0 = -(G * M * x0) / (pow((pow(x0, 2) + pow(y0, 2)), 1.5));
- return (ax0);
- }
- int accelerationy(double x0, double y0){ //fonction acceleration selon y
- double ay0;
- ay0 = -(G * M * y0) / (pow((pow(x0, 2) + pow(y0, 2)), 1.5));
- return (ay0);
- }
- int main(){
- double x, y, vx, vy, ax, ay; //déclaration position-vitesse-acceleration
- double t; //déclaration variable temporelle
- x = 35784000; //position initiale du satellite (metres)
- y = 0; //
- vx = 0; //vitesse initiale du satellite (m/s)
- vy = 2.648865e8; // perimetre/période de révolution
- ax = -3.357201e-2; //acceleration initiale du satellite=-v²/r (m/s²)
- ay = 0; //
- FILE *PositionS = fopen("PositionS.out", "w" );
- for (t = 0;t <= 86160;t = t + dt)
- { //developpement limité abscisses & ordonnées
- fprintf(PositionS, "%f %f\n", x, y); //création du fichier affichant la position du satellite
- x = x + dt * vitessex(x, y, vitessex(x, y)) + ((pow(dt, 2)) / 2) * accelerationx(x, y); //calcul abscisse position
- y = y + dt * vitessey(x, y) + ((pow(dt, 2)) / 2) * accelerationy(x, y); //calcul ordonnée position
- }
- return 0;
- }
|
Léger mieux, mai c'est pas encore ça...
Compiling: main.c
main.c:10: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:10: warning: no previous prototype for 'vitessex'
main.c: In function `vitessex':
main.c:12: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:12: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: At top level:
main.c:17: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:17: warning: no previous prototype for 'vitessey'
main.c: In function `vitessey':
main.c:19: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:19: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: At top level:
main.c:25: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:25: warning: no previous prototype for 'accelerationx'
main.c: In function `accelerationx':
main.c:28: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:28: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: At top level:
main.c:34: warning: declaration of 'y0' shadows a global declaration
C:/Program Files/CodeBlocks/include/math.h:251: warning: shadowed declaration is here
main.c:34: warning: no previous prototype for 'accelerationy'
main.c: In function `accelerationy':
main.c:37: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:37: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c: In function `main_':
main.c:60: error: too few arguments to function `vitessex'
main.c:60: warning: passing arg 3 of `vitessex' as floating rather than integer due to prototype
main.c:60: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
main.c:62: error: too few arguments to function `vitessey'
main.c:62: warning: passing arg 2 of `pow' as floating rather than integer due to prototype
Process terminated with status 1 (0 minutes, 0 seconds)
2 errors, 23 warnings
|
Le plus gros problème est que tu ne passes pas le bon nombre de paramètres aux fonctions vitessex() et vitessey()... Je ne sais pas comment corriger, car je ne connais pas la partie 'métier' de ton code. (Au feeling, je mets vx, vy...)
J'ai passé les types retournés en double (je doute fort que tu ais voulu des int !)
Ceci compile et fonctionne (un fichier est crée ...)
Code :
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #define G 6.67e-11
- #define M 5.9736e24
- //fonction vitesse selon x (vxp=vx précédent)
- static double vitessex(double x, double y, double vxp, double dt)
- {
- double vx0;
- vx0 = vxp + dt * ( -(G * M * x) / (pow((pow(x, 2.0) + pow(y, 2.0)), 1.5))); //vx0=vx0+dt*ax0
- return vx0;
- }
- //fonction vitesse selon y (vyp = vy précédent)
- static double vitessey(double x, double y, double vyp, double dt)
- {
- double vy0;
- vy0 = vyp + dt * ( -(G * M * y) / (pow((pow(x, 2.0) + pow(y, 2.0)), 1.5))); //vy0=vy0+dt*ay0
- return vy0;
- }
- //fonction acceleration selon x
- static double accelerationx(double x, double y)
- {
- double ax0;
- ax0 = -(G * M * x) / (pow((pow(x, 2.0) + pow(y, 2.0)), 1.5));
- return ax0;
- }
- //fonction acceleration selon y
- static int accelerationy(double x, double y)
- {
- double ay0;
- ay0 = -(G * M * y) / (pow((pow(x, 2.0) + pow(y, 2.0)), 1.5));
- return ay0;
- }
- int main()
- {
- /* invariants */
- double const dt = 1; //déclaration du pas temporel
- double const vx = 0; //vitesse initiale du satellite (m/s)
- double const vy = 2.648865e8; // perimetre/période de révolution
- double const ax = -3.357201e-2; //acceleration initiale du satellite=-v²/r (m/s²)
- double const ay = 0; //
- /* variables */
- double x = 35784000; //position initiale du satellite (metres)
- double y = 0; //
- double t; //déclaration variable temporelle
- FILE *PositionS = fopen("PositionS.out", "w" );
- if (PositionS != NULL)
- {
- for (t = 0;t <= 86160;t = t + dt)
- {
- //developpement limité abscisses & ordonnées
- //création du fichier affichant la position du satellite
- fprintf(PositionS, "%f %f\n", x, y);
- //calcul abscisse position
- x = x + dt * vitessex(x, y, vitessex(x, y, vx, dt), dt)
- + ((pow(dt, 2.0)) / 2.0) * accelerationx(x, y);
- //calcul ordonnée position
- y = y + dt * vitessey(x, y, vy, dt) + ((pow(dt, 2.0)) / 2.0) * accelerationy(x, y);
- }
- fclose (PositionS) , PositionS = NULL;
- }
- return 0;
- }
|
Message édité par Emmanuel Delahaye le 20-05-2006 à 23:17:31 ---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
|