Probleme langage c
ghostdz
-
Char Snipeur Messages postés 9813 Date d'inscription Statut Contributeur Dernière intervention -
Char Snipeur Messages postés 9813 Date d'inscription Statut Contributeur Dernière intervention -
Bonjour,
j'ai crée un pgm pour resoudre tour de hanoi
avec les pils
mais mn probleme ce pgm resoudre le probleme
pour le nombre de disque egal 1 et nombre de disk egal 2
si nb disk egal 3 dans la dernier etape Entre des numéros étranges
le code source
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
typedef struct pile
{
int disk;
struct pile *suiv;
}stack;
stack *empiler (stack *p,int x)
{
stack *tmp;
tmp=(stack*)malloc(sizeof(stack));
tmp->disk=x;
tmp->suiv=p;
p=tmp;
return p;
}
int depiler (stack **d)
{
int y;
stack **tmp2=NULL;
y=(*d)->disk;
*tmp2=*d;
*d=(*d)->suiv;
free (tmp2);
return y;
}
void afficher (stack *p)
{
stack *tmp;
if (p==NULL)
printf("_");
tmp=p;
while (tmp!=NULL)
{
printf("%d",tmp->disk);
tmp=tmp->suiv;
}
}
void deplacer(int n,stack**from,stack**to,stack**aux)
{
int x;
if (n==1)
{
x=depiler(from);
*to=empiler(*to,x);
getchar();
afficher(*from);printf("\t");afficher(*to);printf("\t");afficher(*aux);
printf("\n");
return;
}
else
{
deplacer(n-1,from,to,aux);
deplacer(1,from,aux,to);
deplacer(n-1,to,from,aux);
}
}
int main()
{
stack *pileA=NULL;
stack *pileB=NULL;
stack *pileC=NULL;
int i,n;
printf("\ndonner le nombre de disks:");
scanf("%d",&n);
while(n<0)
{
printf("\nerror! donner donner un nombre superieur de Zero 0\n");
printf("\ndonner le nombre de disks:");
scanf("%d",&n);
}
for(i=n;i>=1;i--)
pileA=empiler(pileA,i);
afficher(pileA);printf("\t");afficher(pileC);printf("\t");afficher(pileB);
printf("\n");
deplacer(n,&pileA,&pileB,&pileC);
return 0;
}
j'ai crée un pgm pour resoudre tour de hanoi
avec les pils
mais mn probleme ce pgm resoudre le probleme
pour le nombre de disque egal 1 et nombre de disk egal 2
si nb disk egal 3 dans la dernier etape Entre des numéros étranges
le code source
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
typedef struct pile
{
int disk;
struct pile *suiv;
}stack;
stack *empiler (stack *p,int x)
{
stack *tmp;
tmp=(stack*)malloc(sizeof(stack));
tmp->disk=x;
tmp->suiv=p;
p=tmp;
return p;
}
int depiler (stack **d)
{
int y;
stack **tmp2=NULL;
y=(*d)->disk;
*tmp2=*d;
*d=(*d)->suiv;
free (tmp2);
return y;
}
void afficher (stack *p)
{
stack *tmp;
if (p==NULL)
printf("_");
tmp=p;
while (tmp!=NULL)
{
printf("%d",tmp->disk);
tmp=tmp->suiv;
}
}
void deplacer(int n,stack**from,stack**to,stack**aux)
{
int x;
if (n==1)
{
x=depiler(from);
*to=empiler(*to,x);
getchar();
afficher(*from);printf("\t");afficher(*to);printf("\t");afficher(*aux);
printf("\n");
return;
}
else
{
deplacer(n-1,from,to,aux);
deplacer(1,from,aux,to);
deplacer(n-1,to,from,aux);
}
}
int main()
{
stack *pileA=NULL;
stack *pileB=NULL;
stack *pileC=NULL;
int i,n;
printf("\ndonner le nombre de disks:");
scanf("%d",&n);
while(n<0)
{
printf("\nerror! donner donner un nombre superieur de Zero 0\n");
printf("\ndonner le nombre de disks:");
scanf("%d",&n);
}
for(i=n;i>=1;i--)
pileA=empiler(pileA,i);
afficher(pileA);printf("\t");afficher(pileC);printf("\t");afficher(pileB);
printf("\n");
deplacer(n,&pileA,&pileB,&pileC);
return 0;
}
A voir également:
- Probleme langage c
- Langage ascii - Guide
- Langage binaire - Guide
- Pascal langage - Télécharger - Édition & Programmation
- Langage visual basic - Télécharger - Langages
- Langage basic gratuit - Télécharger - Édition & Programmation