1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| # include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define TAILLE 37
typedef struct {
char lettre[2];
char morse[6];
} code;
void AfficherMenu(void);
void fra2mor(void);
void mor2fra(void);
/******************************************************************************/
/* Main */
/******************************************************************************/
int main(void) {
int choix=0;
do {
AfficherMenu();
scanf("%d",&choix);
while(getchar()!='\n'); // pour vider le buffer
switch(choix) {
case 1 :
fra2mor();
choix=0;
system("PAUSE");
break;
case 2:
mor2fra();
choix=0;
system("PAUSE");
break;
}
} while ( choix>3 || choix <1);
return 0;
}
/******************************************************************************/
/* Affichage du menu */
/******************************************************************************/
void AfficherMenu(void) {
system("CLS");
printf("********************\n");
printf("* Traducteur Morse *\n");
printf("********************\n");
printf("\n1. Francais => Morse");
printf("\n2. Morse => Francais");
printf("\n3. Quitter");
printf("\n\nChoisissez un mode : ");
}
/******************************************************************************/
/* Traduction Francais -> Morse */
/******************************************************************************/
void fra2mor(void) {
int i, j, L, ecrit=0;
char phrase[256];
code table[TAILLE] = {
{"A",".-"}, {"B","-.."}, {"C","-.-."}, {"D","-.."}, {"E","."},
{"F","..-."}, {"G","--."}, {"H","...."}, {"I",".."}, {"J",".---"},
{"K","-.-"}, {"L",".-.."}, {"M","--"}, {"N","-."}, {"O","---"},
{"P",".--."}, {"Q","--.-"}, {"R",".-."}, {"S","..."}, {"T","-"},
{"U","..-"}, {"V","...-"}, {"W",".--"}, {"X","-..-"}, {"Y","-.--"},
{"Z","--.."}, {".",".-.-.-"}, {"0","-----"}, {"1",".----"},
{"2","..---"}, {"3","...--"}, {"4","....-"}, {"5","....."},
{"6","-...."}, {"7","--..."}, {"8","---.."},
{"9","----."}
};
system("CLS");
printf("Traduction Francais -> Morse\n\n");
printf("Entrez une phrase en francais (sans accent) :\n");
fgets(phrase,256,stdin);
printf("\nTraduction en morse :\n");
strupr(phrase);
L=strlen(phrase);
for (i=0;i<L;i++) {
if ( phrase[i]!='.' && (phrase[i]<'0' || phrase[i]>'9') && (phrase[i]<'A' || phrase[i]>'Z') ) {
if ( ecrit ) {
printf("/");
ecrit=0;
}
} else {
j=0;
while ( phrase[i] != table[j].lettre[0] && j++<TAILLE-1 );
if (j<TAILLE) {
printf("%s/", table[j].morse);
ecrit=1;
}
}
}
printf("\n\n");
}
/******************************************************************************/
/* Traduction Morse -> Francais */
/******************************************************************************/
void mor2fra(void) {
int i, j, k, L, nb=0;
char phrase[256], lettre[256];
code table[TAILLE] = {
{"A",".-"}, {"B","-.."}, {"C","-.-."}, {"D","-.."}, {"E","."},
{"F","..-."}, {"G","--."}, {"H","...."}, {"I",".."}, {"J",".---"},
{"K","-.-"}, {"L",".-.."}, {"M","--"}, {"N","-."}, {"O","---"},
{"P",".--."}, {"Q","--.-"}, {"R",".-."}, {"S","..."}, {"T","-"},
{"U","..-"}, {"V","...-"}, {"W",".--"}, {"X","-..-"}, {"Y","-.--"},
{"Z","--.."}, {".",".-.-.-"}, {"0","-----"}, {"1",".----"},
{"2","..---"}, {"3","...--"}, {"4","....-"}, {"5","....."},
{"6","-...."}, {"7","--..."}, {"8","---.."},
{"9","----."}
};
system("CLS");
printf("Traduction Morse -> Francais\n");
do {
printf("\nEntrez une phrase en morse (se terminant par //) :\n");
fgets(phrase,256,stdin);
L=strlen(phrase);
} while ( phrase[L-3]!='/' || phrase[L-2]!='/' );
printf("\nTraduction en francais :\n");
for (i=0;i<L;i++) {
if ( phrase[i]!='.' && phrase[i]!='-' && phrase[i]!='/' && phrase[i]!='\n' ) {
nb++;
}
}
if ( nb ) {
printf("Erreur !! Il y a %d caracteres incorrectes !\n\n",nb);
} else {
i=0;
j=0;
while (i<L) {
if ( phrase[i]=='/' ) {
lettre[j]='\0';
k=0;
while ( strcmp(lettre,table[k].morse) && k++<TAILLE-1 );
if (k<TAILLE) {
printf("%s",table[k].lettre);
} else {
printf("(inc)");
}
j=0;
if ( phrase[i+1]=='/' ) {
i++;
printf(" ");
}
} else {
lettre[j] = phrase[i];
j++;
}
i++;
}
printf("\n\n");
}
} |