Advertisement
Z-F-I

Nombre complexe

Dec 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int** setMatrice(){
  6.     int **M;
  7.     int nbl,nbc;
  8.     FILE *fi= fopen("matriceCo.txt","r+");
  9.     if(fi){
  10.         fscanf(fi,"%d %d",&nbc,&nbl);
  11.         M=malloc(nbl*sizeof(int*));
  12.         for(int i=0;i<nbl;i++)
  13.             M[i]=malloc(nbc*sizeof(int));
  14.  
  15.         for(int i=0; i<nbl; i++){
  16.             for(int j=0; j<nbc; j++)
  17.                 fscanf(fi,"%d",&M[i][j]);    
  18.         }
  19.         fclose(fi);
  20.     }
  21.     return M;
  22. }
  23. //===================================================================
  24. int* setMot(FILE* f , int *taille)
  25.  {
  26.     char* mot=malloc(20*sizeof(char));
  27.     int* tab;
  28.      
  29.     if(f){
  30.         fscanf(f,"%s ",mot);
  31.         tab=malloc(strlen(mot)*sizeof(int));
  32.         for(int i=0; i<strlen(mot); i++)
  33.          {
  34.             if(mot[i]=='+')
  35.                 tab[i]=10;
  36.             else if(mot[i]=='-')
  37.                 tab[i]=11;
  38.             else if(mot[i]=='i')
  39.                 tab[i]=12;
  40.             else
  41.                 tab[i]=mot[i]-'0';
  42.         }
  43.         *taille=strlen(mot);
  44.     }
  45.     return tab;
  46. }
  47. //==================================================================================
  48. int trace(int** M, int* mot, int taille_mot, int ETAT_INIT){
  49.     int suivant=ETAT_INIT;
  50.     for(int i=0 ; i<taille_mot && suivant!=-1 ; i++){
  51.         suivant=M[suivant][mot[i]];
  52.     }
  53.     return suivant;
  54. }
  55. //==================================================================================
  56. void reconaissance(int R)
  57. {
  58.     if(R==5)
  59.         printf(" ==> mot existe ");
  60.     else
  61.         printf(" ==> mot n'existe pas ");
  62. }
  63. //========================================================================
  64. int main(){
  65.     int **M;
  66.     FILE* fi;
  67.     int* mot;
  68.     int ETAT_INIT,taille;
  69.     M=setMatrice();
  70.  
  71.     printf("Entrer l'etat initial :");scanf("%d",&ETAT_INIT);
  72.  
  73.     fi=fopen("motComplexeE.txt","r+");
  74.     system("cls");
  75.     while(!feof(fi))
  76.     {
  77.     mot=setMot(fi,&taille);
  78.     for(int i=0 ;i<taille ; i++)
  79.     {
  80.         if(mot[i]==10)
  81.             printf("+");
  82.         else if(mot[i]==11)
  83.             printf("-");
  84.         else if(mot[i]==12)
  85.             printf("i");
  86.         else
  87.             printf("%c",mot[i]+'0');
  88.     }
  89.         reconaissance(trace(M,mot,taille,ETAT_INIT));
  90.         printf("\n");
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement