Traduction de C++ en C

Fermé
Koda94 Messages postés 7 Date d'inscription samedi 9 mai 2015 Statut Membre Dernière intervention 25 mai 2015 - 9 mai 2015 à 16:58
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 - 12 mai 2015 à 22:01
Bonjour,

J'aimerais traduire mon programme en C mais je ne sais pas du tout comment faire. Est-ce que quelqu'un pourrait m'aider ?

Merci

#include <cstdlib>
#include <iostream>
using namespace std;

const int NCOUPS = 7;

const int NMIN = 3;
const int NMAX = 10;
typedef char TabCarac[NMAX];

bool estLettre(char c)
{
return c >= 'a' && c <= 'z' || c == '_';
}

void saisirLettre(char& c)
{
cin >> c;
while (!estLettre(c))
{
cout << c << " n'est pas une lettre valide" << endl;
cin >> c;
}
}

void saisirMot(TabCarac& mot, int &n)
{
cout << "Tapez votre mot (lettres en minuscule) -- Finissez par _" << endl;
n = 0;
char c;
saisirLettre(c);
while (n < NMAX && c != '_')
{
mot[n] = c;
++n;
saisirLettre(c);
}
}

void melangerMot(TabCarac& mot, int n)
{
for (int j = n-1; j > 0; --j)
{
int k = rand()%(j+1);
char tmp = mot[k];
mot[k] = mot[j];
mot[j] = tmp;
}
}

void afficherMot(const TabCarac& mot, int n)
{
for (int j = 0; j < n; ++j)
{
cout << mot[j];
}
cout << endl;
}

void copierMot(const TabCarac& mot, TabCarac& copie, int n)
{
for (int j = 0; j < n; ++j)
{
copie[j] = mot[j];
}
}

int rechlin(char c, const TabCarac& mot, int n)
{
int j = 0;
while (j < n && mot[j] != c)
{
++j;
}
return (j < n) ? j : -1;
}

void proposerMot(TabCarac& mot, int n, const TabCarac& ana)
{
TabCarac copie;
copierMot(ana, copie, n);
int j = 0;
char c;
while (j < n)
{
saisirLettre(c);
int k = rechlin(c, copie, n);
if (k != -1)
{
mot[j] = c;
++j;
copie[k] = '_';
afficherMot(copie, n);
}
else
{
cout << "Lettre non presente dans l'anagramme..." << endl;
}
}
cout << "==> Votre Mot = ";
afficherMot(mot, n);
}

int bienPlaces(const TabCarac& mot, int n, const TabCarac& adeviner)
{
int bp = 0;
for (int j = 0; j < n; ++j)
{
if (mot[j] == adeviner[j])
{
++bp;
}
}
return bp;
}

void devinerMot(const TabCarac& adeviner, int n)
{
TabCarac ana;
copierMot(adeviner, ana, n);
melangerMot(ana, n);
TabCarac mot;
int ncoups = 0;
int nbp = 0;
while (ncoups < NCOUPS && nbp != n)
{
++ncoups;
cout << "coup " << ncoups << " -- Anagramme du mot a trouver = ";
afficherMot(ana, n);
proposerMot(mot, n, ana);
nbp = bienPlaces(mot,n,adeviner);
cout << nbp << " lettres bien placees" << endl;
}
if (nbp == n)
{
cout << "Super Gagne en " << ncoups << " coups" << endl;
}
else
{
cout << "Perdu" << endl;
}
}

int main()
{
TabCarac adeviner;
int n;
saisirMot(adeviner, n);
devinerMot(adeviner, n);
}

2 réponses

jisisv Messages postés 3645 Date d'inscription dimanche 18 mars 2001 Statut Modérateur Dernière intervention 15 janvier 2017 934
10 mai 2015 à 16:52
A vue de nez, il n'y a pas grand chose de spécifique au C++ là-dedans,
mis à part les fichiers en-te et les I/O.
Cela se modifie facilement (même manuellement, i.e sans sed) .
0
Koda94 Messages postés 7 Date d'inscription samedi 9 mai 2015 Statut Membre Dernière intervention 25 mai 2015
10 mai 2015 à 23:55
Merci pour ta réponse !
Est-ce que je peux remplacer les fichiers en tête par :
#include <stdio.h>
#include <stdlib>
?
Je veux dire par là : est-ce que ce sont les équivalents de #include <cstdlib>
#include <iostream> en C ?

Et que veut dire I/O ? ( in out ? )
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 840
11 mai 2015 à 00:24
Oui tu peux, et il faut.
I/O pour Input/Output. Autrement dit, les std::cout << "bidule"; doivent être remplacés par printf("bidule"); de même pour les std::cin var; qui seront substitués par scanf(...);

Et n'oublie pas de rajouter un return 0; à la fin du main().
0
Koda94 Messages postés 7 Date d'inscription samedi 9 mai 2015 Statut Membre Dernière intervention 25 mai 2015
11 mai 2015 à 13:24
Merci beaucoup !

Je bloque à TabCarac qui n'est apparament pas reconnu par CodeBlocks. Par quoi puis-je le remplacer ?
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 840
11 mai 2015 à 13:37
TabCarac n'est pas un mot-clé du langage, il n'est donc pas à remplacer...
N'aurais-tu pas, par hasard, supprimer à tort : typedef char TabCarac[NMAX]; ?

Cdlt,
0
Koda94 Messages postés 7 Date d'inscription samedi 9 mai 2015 Statut Membre Dernière intervention 25 mai 2015
11 mai 2015 à 14:37
En fait, quand j'écris cette expression dans CodeBlocks, il me met un carré rouge et indique error : variably modified "TabCarac" at file scope

Pour l'instant, je n'ai traduit que ça :
#include <stdio.h>

int NCOUPS = 7;
int NMIN = 3;
int NMAX = 10;

typedef char TabCarac[NMAX];
0
chris79 Messages postés 97 Date d'inscription lundi 3 octobre 2005 Statut Membre Dernière intervention 1 février 2016 25
12 mai 2015 à 20:14
Slt,

Il va aussi falloir revoir tes prototypes de fonctions pour supprimer les passages par référence ;)

++
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 840
12 mai 2015 à 22:01
Bien vu :-).
0