timon44
Messages postés53Date d'inscriptiondimanche 28 décembre 2008StatutMembreDernière intervention17 décembre 2010
-
6 déc. 2009 à 19:08
fiddy
Messages postés11069Date d'inscriptionsamedi 5 mai 2007StatutContributeurDernière intervention23 avril 2022
-
6 déc. 2009 à 19:54
Bonjour à tous !
Voila je voudrai lire un fichier texte qui contient un algoritheme simple
(si...alors...sinon...fsi / tantque...faire.....fintantque)
J'arrive à lire le fichier texte .... mais je n'arrive vraiment pas à faire ce que je veux (surtout s'il y a de l'imbrication ...)
Voila ce que j'ai fait pour le moment :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char mot[200];
char arc[200];
int debut = 1;
int compteurSi = 0;
int compteurTantQue = 0;
int s=1; //numerode sommet
int a=1; // numero d'arc
int position=0;
int numSommet=1;
int numArc=1;
typedef struct Arc Arc;
struct Arc
{
int numeroArc;
char etiquette[100];
};
typedef struct Sommet Sommet;
struct Sommet
{
int numeroSommet;
char etiquette[100]; // si, sinon, alors
Arc arcEntrant;
Arc arcSortant;
};
Sommet TabSommet[1000];
Arc TabArc[1000];
// affiche le graphe aprés un alors/sinon/faire
void affiche1(FILE *fichier)
{
TabSommet[s].numeroSommet = s;
strcpy (TabSommet[s].etiquette,mot);
printf("Numero du sommet : %d \n",TabSommet[s].numeroSommet);
printf("Etiquette sommet : %s \n",TabSommet[s].etiquette);
printf("Numero arc entrant: %d \n",TabArc[a-1].numeroArc);
printf("Numero arc sortant: %d \n\n",TabArc[a].numeroArc);
fscanf(fichier, "%s", arc);
fscanf(fichier, "%s", mot);
/*si aprés la condition, il y a "alors" on ne fait pas la boucle
sinon on lit les conditions jusqu'à ce qu'il y ai un "alors" */
while (strcmp(mot, "alors") != 0)
{
strcat(arc," ");
strcat(arc,mot);
fscanf(fichier, "%s", mot);
} // fin boucle pour l'arc
TabArc[a].numeroArc=a;
strcpy (TabArc[a].etiquette,arc);
s++;
TabSommet[s].numeroSommet = s;
strcpy (TabSommet[s].etiquette,mot); // le dernier mot qu'on a lu est un alors (fin de boucle !)
TabSommet[s].arcSortant=TabArc[a+1];
TabSommet[s].arcEntrant=TabArc[a];
printf("Numero arc: %d \n",TabArc[a].numeroArc);
printf("Etiquette arc: %s \n\n",TabArc[a].etiquette);
//sommet "alors"
printf("Numero du sommet : %d \n",TabSommet[s].numeroSommet);
printf("Etiquette sommet : %s \n",TabSommet[s].etiquette);
printf("Numero arc entrant: %d \n",TabArc[a].numeroArc);
printf("Numero arc sortant: %d \n\n",TabArc[a].numeroArc);
a++;
s++;
affiche2(fichier);
} // fin affiche graphe
void affiche2(FILE *fichier)
{
/*afficheDebutGraphe, on a affiché le "si" ou le 'tantque', larc, et le "alors" ou le "faire"
*/
fscanf(fichier, "%s", mot); // on lit le mot aprés le "alors"
if (strcmp(mot, "si") == 0)
{
compteurSi++;
affiche1(fichier); // on affiche le sommet "si", son arc, et le "alors" puis on reviens sur affiche2
}
else if (strcmp(mot, "tantque") == 0)
{
affiche1(fichier); // affiche sommet "tantque", l'arc, et le sommet "faire"
// compteurfaire++
}
else
{
memset(arc, 0, sizeof(arc)); //remise a zero de arc
int arret =0;
while (arret== 0)
{
if ((strcmp(mot,"sinon"))==0)
{ arret=1;}
else
{
if ((strcmp(mot,"fsi"))==0)
{ arret=1;}
else if ((strcmp(mot,"ftantque"))==0)
{ arret=1;}
else {
strcat(arc," ");
strcat(arc,mot);
fscanf(fichier, "%s", mot);
}
}
} // fwhile
// le dernier mot, est soit une "sinon" soit un "fsi"
TabArc[a].numeroArc=a;
strcpy (TabArc[a].etiquette,arc);
printf("Numero arc: %d \n",TabArc[a].numeroArc);
printf("Etiquette arc: %s \n",TabArc[a].etiquette);
a++;
if ((strcmp(mot,"sinon"))==0)
{
affiche2(fichier);// ffihce lesommmet sinon / et on refait affiche
}
else if ((strcmp(mot,"fsi"))==0)
{
affiche2(fichier); // compteurfsi++ ; affihce lesommmet fsi / on renvoi affiche
}
else if ((strcmp(mot,"ftantque"))==0)
{
// compteurftantque++ ; affihce lesommmet ftantque / on renvoi affiche
}
} //felse
}
void afficheDebutGraphe(FILE *fichier)
{
fscanf(fichier, "%s", mot);
if (strcmp(mot, "si") == 0)
{
compteurSi++;
debut=0;
// le numero du sommet
TabSommet[s].numeroSommet = s;
strcpy (TabSommet[s].etiquette,mot);
TabSommet[s].arcSortant=TabArc[a];
TabSommet[s].arcEntrant=TabArc[0];
s++;
fscanf(fichier, "%s", arc); // on lit la condition du si
fscanf(fichier, "%s", mot); // ce qu'il y a aprés: il peut y avoir plsrs conditions
/*si aprés la condition, il y a "alors" on ne fait pas la boucle
sinon on lit les conditions jusqu'à ce qu'il y ai un "alors" */
while (strcmp(mot, "alors") != 0)
{
strcat(arc," "); //on rajoute un espace car le fscanf ne les prends pas en compte
strcat(arc,mot);
fscanf(fichier, "%s", mot); // ce qu'il y a aprés: il peut y avoir plsrs conditions
} // fin boucle pour l'arc
// numArc : a = 1 (premiere lecture !)
TabArc[a].numeroArc=a;
strcpy (TabArc[a].etiquette,arc);
TabSommet[s].numeroSommet = s;
strcpy (TabSommet[s].etiquette,mot); // le dernier mot qu'on a lu est un alors (fin de boucle !)
printf("Numero du sommet : %d \n",TabSommet[s-1].numeroSommet);
printf("Etiquette sommet : %s \n",TabSommet[s-1].etiquette);
printf("Numero arc entrant: %d \n",0); //il n'y en a pas = 0
printf("Numero arc sortant: %d \n\n",TabArc[s-1].numeroArc);
printf("Numero arc: %d \n",TabArc[a].numeroArc);
printf("Etiquette arc: %s \n\n",TabArc[a].etiquette);
//sommet "alors" ou "faire"
printf("Numero du sommet : %d \n",TabSommet[s].numeroSommet);
printf("Etiquette sommet : %s \n",TabSommet[s].etiquette);
printf("Numero arc entrant: %d \n",TabArc[a].numeroArc);
printf("Numero arc sortant: %d \n\n",TabArc[a+1].numeroArc);
a++;
s++;
affiche2(fichier);
} //fin if "si"
else if (strcmp(mot, "tantque") == 0)
{
compteurTantQue++;
debut=0;
}
else
{
position = fgetc(fichier);
fseek(fichier, -1, SEEK_CUR);
if (position != EOF)
{
// on lit l'arc (instruction), genre initialisation de variable
}
else
{
if (debut==1){printf("\n le fichier est vide: il n'y a pas d'algorithme \n\n");}
}
}
}// fin affiche Debutgraphe
void prog(FILE *fichier)
{
int debut=1;
while (position != EOF)
{
//on test si c'est le premier while = premier sommet
fseek(fichier, -1, SEEK_CUR);
/*fscanf(fichier, "%s", mot);
//test si, sinon, alors ...
printf("%s \n",mot);*/
afficheDebutGraphe(fichier);
if (position != EOF){position = fgetc(fichier); }// On lit le caractère
}//fin while
}// FIN prog
int main(int argc, char *argv[])
{
FILE* fichier = NULL;
fichier = fopen("test.txt", "r+");
if (fichier != NULL)// On peut lire et écrire dans le fichier
{
prog(fichier);
fclose(fichier); // On ferme le fichier qui a été ouvert
}
else
{
// On affiche un message d'erreur si on veut
printf("Impossible d'ouvrir le fichier test.txt\n");
}
return 0;
} // fin main
Bien sûr, erreur de compil : j'appelle la fonction affiche2 avant qu'elle ne soit déclarer. Et affiche1 est appelé dans affiche2 bien sûr ...
Quelqu'un a une solution à ce problème ?
fiddy
Messages postés11069Date d'inscriptionsamedi 5 mai 2007StatutContributeurDernière intervention23 avril 20221 844 6 déc. 2009 à 19:54
Salut,
Ce que tu peux tout simplement faire, c'est de définir les prototypes des fonctions affiche1 et affiche2.
Comme ça tu pourras les appeler à n'importe quel moment.
De plus, je te déconseille l'utilisation de variables globales. C'est pas très propre.
Cdlt
Forum Programmation
Trouvez des réponses à vos questions sur les langages, les frameworks et les astuces de codage. Échangez avec d'autres développeurs passionnés pour améliorer vos compétences en programmation et rester au fait des dernières tendances du secteur.