Retaper code c en c++

Fermé
nissan1 Messages postés 16 Date d'inscription vendredi 7 mai 2010 Statut Membre Dernière intervention 25 juillet 2010 - 24 juin 2010 à 12:13
nissan1 Messages postés 16 Date d'inscription vendredi 7 mai 2010 Statut Membre Dernière intervention 25 juillet 2010 - 24 juin 2010 à 18:47
Bonjour,
J'ai mon code que j'ai ecrit en C sous editeur Code Blocks et je veut le reecrire en C++.
est ce que quelqu'un pourra m'aider.
Merci
/**************************************************************/

/***************** Analyse d'impacts ****************/
/**************************************************************/

#include <stdio.h>
#include <math.h>
#include <time.h>
#define MAXSIZE 8// taille des tableaux

/****************************************************/
/************* Prototypes des fonctions *************/
/****************************************************/

void create_matrix(float *, int, int);
void display_matrix(float *, int, int);
int compute(float *, float *, const dim);

/****************************************************/
/************* Fonction Principale **************/
/****************************************************/

int main (int argc, const char * argv[]) {
int dim;
int i,j;
int toto;
float e[MAXSIZE * MAXSIZE];
float c[MAXSIZE];
FILE * pFile;

pFile = fopen ("fiche.txt","r");

for (i=0 ; i<MAXSIZE ;i++)
{
for (j=0; j<MAXSIZE; j++)
{
fscanf (pFile, "%f ",&e[i + MAXSIZE * j]);

printf("%f ", e[i + MAXSIZE * j]);

}
printf("\n");
}

fclose(pFile);
printf ("\nQuelle est la dimention du vecteur (Max %d): ",dim);
scanf ("%d",&dim);
printf ("\n le nombre d'iteration est %d\n",compute(e, c, dim));
scanf("%d", &toto);
getchar();
return 0;
}

/****************************************************/
/************* Fonction Secondaires **************/
/****************************************************/

/****************************************************/
/************* Fonction Secondaires **************/
/****************************************************/


/*******************************************************************/
/****** Fonction pour créer une matrice [taille_1 x taille_2] ******/
/*******************************************************************/

void create_matrix(float *tableau, int taille_1, int taille_2 )
{
int i_t1, i_t2;

for ( i_t1= 0; i_t1 < taille_1; i_t1++)
{

printf ("Line %d:\t",i_t1);
for ( i_t2 = 0; i_t2 < taille_2; i_t2++)

scanf ("%f",&tableau[((i_t1 * taille_1) + i_t2)]);
printf ("\n");
}

}

/*******************************************************************/
/****** Fonction pour afficher une matrice [taille_1 x taille_2] ***/
/*******************************************************************/

void display_matrix(float *tableau, int taille_1, int taille_2 )
{
int i_t1, i_t2;
for (i_t1 = 0; i_t1 < taille_1; i_t1++)
{
for ( i_t2 = 0; i_t2 < taille_2; i_t2++)
printf ("%f\t", tableau[((i_t1 * taille_1) + i_t2)]);
}
printf ("\n");
}

/*******************************************************************/
/***** Fonction pour calculer et rechercher le point d'équilibre ***/
/*******************************************************************/

int compute(float *e, float *c, const dim)
{
int vecteur_egaux;
float r[MAXSIZE];
float a[MAXSIZE];
float b[MAXSIZE];
int i=0;
int j=0;
int nb_iteration;

nb_iteration=0;
vecteur_egaux = 0;

/*printf ("\n\n\n\t******* CREATION DE LA MATRICE E *******\n");
printf ("\n*******Saisie de la matrice E*******\n\n");
create_matrix(e, dim, dim);*/

printf ("\n\t******* CREATION DU VECTEUR C *******\n");
printf ("\n*******Saisie de la matrice C*******\n\n");
create_matrix(c, 1, dim);

for (i = 0; i < dim; i++)
{
a[i]=c[i];

}
while (!vecteur_egaux)
{
for (i = 0; i < dim; i++)
{
b[i]=c[i];

}

for ( i= 0; i < dim; i++)
{
r[i] = 0;
for (j = 0; j < dim; j++)
{
r[i]=(r[i])+((c[j]) * (e[j + MAXSIZE * i]));

}
}
vecteur_egaux = 1;

for (i = 0; i < dim; i++)
{
c[i]=((expf (2 * r[i]) - 1) / (expf (2 * r[i]) + 1))+ a[i];
}
for (i = 0; i < dim; i++)
{
if (c[i] != b[i])
{
vecteur_egaux = 0;
}
}

printf ("\n");
nb_iteration++;


printf ("\n*******VECTEUR R%d:\n",nb_iteration);
display_matrix(r, 1,dim);
printf ("\n*******VECTEUR C%d:\n",nb_iteration);
display_matrix(c, 1,dim);


}
printf ("\n\n\n\t\t*******RESULTAT*******\n\n\n");

return nb_iteration;


}
/****************** The end *********************************/

5 réponses

fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 816
24 juin 2010 à 12:57
Bonjour,

C'est quoi qui te bloque ? La non connaissance du C++ ?
Sinon, je te propose que tu fasses un premier jet et on te corrigera.

Cdlt,
0
nissan1 Messages postés 16 Date d'inscription vendredi 7 mai 2010 Statut Membre Dernière intervention 25 juillet 2010
24 juin 2010 à 14:16
Merci tout dabord,
oui enfait la non connaissance du C++ est comment me y prendre
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 816
24 juin 2010 à 14:24
Comment t'y prendre ?
Ben, il suffit de reprendre ligne par ligne et d'adapter en son équivalent (par exemple printf => std::cout, ...
Tu peux même utiliser la classe Vector.
Sinon pour apprendre le C++ : https://openclassrooms.com/fr/courses/1894236-programmez-avec-le-langage-c
0
nissan1 Messages postés 16 Date d'inscription vendredi 7 mai 2010 Statut Membre Dernière intervention 25 juillet 2010
24 juin 2010 à 14:47
ok je vais le faire en me basant sur le lien que tu ma passer, et j'aimerai bien que vous me corrigiez apres que je termine le code si c'est possible.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
nissan1 Messages postés 16 Date d'inscription vendredi 7 mai 2010 Statut Membre Dernière intervention 25 juillet 2010
Modifié par nissan1 le 24/06/2010 à 18:49
Bonsoir,
voila le code en C++ equivalent a mon code en C que j'ai mis toute a l'heure en haut
</**************************************************************/
/***************** Analyse d'impacts ****************/
/**************************************************************/
#include <iostream>
# include <conio.h>
# include <iomanip.h>
# include <math.h>
#define MAXSIZE 8// taille des tableaux

/****************************************************/
/************* Prototypes des fonctions *************/
/****************************************************/

void create_matrix(float *, int, int);
void display_matrix(float *, int, int);
int compute(float *, float *, const dim);

/****************************************************/
/************* Fonction Principale **************/
/****************************************************/
using namespace std;
int main (int argc, const char * argv[]) {
int dim;
int toto;
float e[MAXSIZE * MAXSIZE];
float c[MAXSIZE];
FILE * pFile;

pFile = fopen ("fiche.txt","r");

for (int i=0 ; i<MAXSIZE ;i++)
{
for (int j=0; j<MAXSIZE; j++)
{
fscanf (pFile, "%f ",&e[i + MAXSIZE * j]);

cout << e[i + MAXSIZE * j];

}
cout << endl <<;
}

fclose(pFile);
cout <<"Quelle est la dimention du vecteur: " << dim << endl;
scanf ("%d",&dim);
cout << "le nombre d'iteration est \n" << compute(e, c, dim) << endl;
cin >>&toto;
getchar();
return 0;
}

/****************************************************/
/************* Fonction Secondaires **************/
/****************************************************/

/****************************************************/
/************* Fonction Secondaires **************/
/****************************************************/


/*******************************************************************/
/****** Fonction pour créer une matrice [taille_1 x taille_2] ******/
/*******************************************************************/

void create_matrix(float *tableau, int taille_1, int taille_2 )
{


for (int i_t1= 0; i_t1 < taille_1; i_t1++)
{

cout <<"Line:" << i_t1 << endl;
for (int i_t2 = 0; i_t2 < taille_2; i_t2++)

cin >> &tableau[((i_t1 * taille_1) + i_t2)];
cout << endl<<;
}

}

/*******************************************************************/
/****** Fonction pour afficher une matrice [taille_1 x taille_2] ***/
/*******************************************************************/

void display_matrix(float *tableau, int taille_1, int taille_2 )
{

for (int i_t1 = 0; i_t1 < taille_1; i_t1++)
{
for (int i_t2 = 0; i_t2 < taille_2; i_t2++)
cout << tableau[((i_t1 * taille_1) + i_t2)];
}
cout << endl<<;
}

/*******************************************************************/
/***** Fonction pour calculer et rechercher le point d'équilibre ***/
/*******************************************************************/

int compute(float *e, float *c, const dim)
{
int vecteur_egaux;
float r[MAXSIZE];
float a[MAXSIZE];
float b[MAXSIZE];
int nb_iteration;

nb_iteration=0;
vecteur_egaux = 0;

cout <<endl<<"******* CREATION DU VECTEUR C *******"<< endl;
cout <<endl<<"*******Saisie de la matrice C*******" << endl <<endl;
create_matrix(c, 1, dim);

for (int i = 0; i < dim; i++)
{
a[i]=c[i];

}
while (!vecteur_egaux)
{
for (int i = 0; i < dim; i++)
{
b[i]=c[i];

}

for (int i= 0; i < dim; i++)
{
r[i] = 0;
for (int j = 0; j < dim; j++)
{
r[i]=(r[i])+((c[j]) * (e[j + MAXSIZE * i]));

}
}
vecteur_egaux = 1;

for (int i = 0; i < dim; i++)
{
c[i]=((expf (2 * r[i]) - 1) / (expf (2 * r[i]) + 1))+ a[i];
}
for (int i = 0; i < dim; i++)
{
if (c[i] != b[i])
{
vecteur_egaux = 0;
}
}

cout <<endl<<;
nb_iteration++;


cout <<endl<<"*******VECTEUR R:",nb_iteration<< endl;
display_matrix(r, 1,dim);
cout <<endl<<endl<<"*******VECTEUR C:",nb_iteration << endl;
display_matrix(c, 1,dim);


}
cout << endl<<endl<<endl<<"*******RESULTAT*******"<< endl<< endl<< endl;

return nb_iteration;


}
/****************** The end *********************************/
>
Est ce que je l'ai bien convertis?
0