Afficher les opération et les resultats dans un tableau

mast20 Messages postés 12 Date d'inscription   Statut Membre Dernière intervention   -  
mast20 Messages postés 12 Date d'inscription   Statut Membre Dernière intervention   -
svp est ce que quelqu'un peut m'aider à modifier ce programme pour qu il soit afficher dans un tableau.voici le programme :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 50
#define M 3
void afficher_etat_vars(char opt[N],int*p1,int*p2,int*pta,int*ptb,int*ptc)
{
char ch1[M],ch2[M];
if(p1==pta)
strcpy(ch1,"&a");
else if(p1==ptb)
strcpy(ch1,"&b");
else if(p1==ptc)
strcpy(ch1,"&c");
else
strcpy(ch1,"");

if(p2==pta)
strcpy(ch2,"&a");
else if(p2==ptb)
strcpy(ch2,"&b");
else if(p2==ptc)
strcpy(ch2,"&c");
else
strcpy(ch2,"");
printf("%10s| %3d | %3d | %3d | %s | %s \n",opt,*ptb,*pta,*ptc,ch1,ch2) ;

}


int main( )
{
printf("operation | a | b | c | p1 | p2 \n");
int a=1;
int b=2;
int c=3;
int*p1=NULL,*p2=NULL;


p1=&a;
afficher_etat_vars("p1=&a",p1,p2,&a,&b,&c);

p2=&c;
afficher_etat_vars("p2=&c",p1,p2,&a,&b,&c);
  • p1=*p2;

afficher_etat_vars("*p1=*p2" ,p1,p2,&a,&b,&c);

(*p2)++;
afficher_etat_vars("(*p2)++",p1,p2,&a,&b,&c);

p1=p2;
afficher_etat_vars("p1=p2",p1,p2,&a,&b,&c);

p2=&b;
afficher_etat_vars("p2=&b",p1,p2,&a,&b,&c);
  • p1=*p2;

afficher_etat_vars("*p1=*p2",p1,p2,&a,&b,&c);

++(*p2);
afficher_etat_vars("++(*p2)",p1,p2,&a,&b,&c);
  • p1*=*p2;

afficher_etat_vars("*p1*=*p2",p1,p2,&a,&b,&c);

++(*p2);
afficher_etat_vars("++(*p2)",p1,p2,&a,&b,&c);

a=*p2**p1;
afficher_etat_vars("a=*p2**p1",p1,p2,&a,&b,&c);

p1=&a;
afficher_etat_vars("p1=&a",p1,p2,&a,&b,&c);
  • p2=*p1/=*p2;

afficher_etat_vars("*p2=*p1/=*p2",p1,p2,&a,&b,&c);

return (0);

}


A voir également:

1 réponse

Dalfab Messages postés 706 Date d'inscription   Statut Membre Dernière intervention   101
 
Bonjour,
Tu souhaites faire des opérations et les afficher sous forme d'un tableau.
Ton code n'est pas mal, mais tu finis par tout mélanger à l'affichage.
Je te propose le code suivant (je suis fainéant comme tout informaticien et j'ai utilisé des macros et des boucles), j'ai ôté les codes qui n'ont pas de sens.
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define N 50 
#define M 3 

void afficher_etat_vars(char opt[N] , int*p1 , int*p2 , int*pta , int*ptb , int*ptc) {
	const char* c[2]; // ou pointent p1 p2
	int* ps[2];       // p1 et p2 dans un tableau
	ps[0] = p1;
	ps[1] = p2;

	for ( int i = 0 ; i < 2 ; ++i ) {
		if ( ps[i] == NULL ) c[i] = "NULL";
		else if ( ps[i] == pta ) c[i] = " &a ";
		else if ( ps[i] == ptb ) c[i] = " &b ";
		else if ( ps[i] == ptc ) c[i] = " &c ";
		else c[i] = "autre";
	}
	printf("%-12s| %3d | %3d | %3d | %s | %s |\n" , opt , *pta , *ptb , *ptc , c[0] , c[1] );
}

#define FaireEtAfficher(exp)  exp; afficher_etat_vars( #exp , p1 , p2 , &a , &b , &c );

int main() {
	printf("operation   |  a  |  b  |  c  |  p1  |  p2  |\n");
	int a = 1;
	int b = 2;
	int c = 3;
	int*p1 = NULL , *p2 = NULL;
	
	FaireEtAfficher(p1 = &a);
	FaireEtAfficher(p2 = &c);
	////p1 = *p2;
	FaireEtAfficher(*p1=*p2);
	FaireEtAfficher(( *p2 )++);
	FaireEtAfficher(p1 = p2);
	FaireEtAfficher(p2 = &b);
	////p1 = *p2;
	FaireEtAfficher(++( *p2 ));
	////p1 *= *p2;
	FaireEtAfficher(++( *p2 ));
	FaireEtAfficher(a = *p2**p1);
	FaireEtAfficher(p1=&a);
	////p2 = *p1 /= *p2;

	return 0;
}
0
mast20 Messages postés 12 Date d'inscription   Statut Membre Dernière intervention  
 
merci pour l aide
0