[j2me] trouver le pgcd de deux nombres

Contenu du snippet

Salut les amis voici mon premier MIDlet, ma première appli pour portable.
C'est en regardant des sources de ce site que j'ai pu apprendre sur les MIDlets alors je vous lache ce code et tiens à remercier les auteurs des sources que j'ai pu lire.

Source / Exemple :


import javax.microedition.midlet.*; // contient les éléments de base
import javax.microedition.lcdui.*; // contient les éléments permettant de gérer l'interface
 
public class PGCDFinderMIDlet extends MIDlet implements CommandListener
{	
	private Form homeform, euclform1, euclform2, sousform1, sousform2;
	private Command home_start, home_exit;
	private Command eucl_find, eucl_menu, eucl_menu2, eucl_again;
	private Command sous_find, sous_menu, sous_menu2, sous_again;
	private ChoiceGroup method;
	private TextField tf_eucl_nombre1, tf_eucl_nombre2, tf_eucl_cal;
	private TextField tf_sous_nombre1, tf_sous_nombre2, tf_sous_cal;
	private String cal;
	private int i_nombre1, i_nombre2, methodIndex, currentIndex;

	public PGCDFinderMIDlet()
	{ 
		/* mise en place du homeform */
		homeform = new Form("Menu");
		home_exit = new Command("Quitter", Command.EXIT,1);
		home_start = new Command("Commencer", Command.ITEM,1);
		method = new ChoiceGroup("Choisir une méthode", Choice.EXCLUSIVE);
		method.append("Par division", null);
		currentIndex = method.append("Par soustraction", null);
		method.setSelectedIndex(currentIndex, true);
		homeform.addCommand(home_exit);
		homeform.addCommand(home_start);
		methodIndex = homeform.append(method);
		homeform.setCommandListener(this); 
		
		/* mise en place du euclform1 */
		euclform1 = new Form("Trouver le PGCD de deux nombres");
		eucl_menu = new Command("Menu", Command.ITEM, 1);
		eucl_find = new Command("Trouver", Command.ITEM, 2);
		tf_eucl_nombre1 = new TextField("Nombre1", "", 15, TextField.DECIMAL);
		tf_eucl_nombre2 = new TextField("Nombre2", "", 15, TextField.DECIMAL);
		euclform1.addCommand(eucl_menu);
		euclform1.addCommand(eucl_find);
		euclform1.append(tf_eucl_nombre1);
		euclform1.append(tf_eucl_nombre2);
		euclform1.setCommandListener(this);
		
		/* mise en place du euclform2 */
		euclform2 = new Form("Résultats");
		eucl_menu2 = new Command("Menu", Command.ITEM,1);
		eucl_again = new Command("Encore", Command.ITEM,1);
		euclform2.addCommand(eucl_menu2);
		euclform2.addCommand(eucl_again);
		euclform2.setCommandListener(this); 
		
		/* mise en place du sousform1 */
		sousform1 = new Form("Trouver le PGCD de deux nombres");
		sous_menu = new Command("Menu", Command.ITEM, 1);
		sous_find = new Command("Trouver", Command.ITEM, 2);
		tf_sous_nombre1 = new TextField("Nombre1", "", 15, TextField.DECIMAL);
		tf_sous_nombre2 = new TextField("Nombre2", "", 15, TextField.DECIMAL);
		sousform1.addCommand(sous_menu);
		sousform1.addCommand(sous_find);
		sousform1.append(tf_sous_nombre1);
		sousform1.append(tf_sous_nombre2);
		sousform1.setCommandListener(this);
		
		/* mise en place du sousform2 */
		sousform2 = new Form("Résultats");
		sous_menu2 = new Command("Menu", Command.ITEM,1);
		sous_again = new Command("Encore", Command.ITEM,1);
		sousform2.addCommand(sous_menu2);
		sousform2.addCommand(sous_again);
		sousform2.setCommandListener(this); 

	}
 
	/* évènement exécuté au démarrage de l'application */
	public void startApp()
	{  
		Display.getDisplay(this).setCurrent(homeform);
	}	
 
	/* évènement exécuté lors de la mise en pause de l'application */
	public void pauseApp()
	{
	}
	
	/* évènement exécuté lorsque l'application se termine */
	public void destroyApp(boolean unconditional)
	{
	}
	
	public void commandAction(Command cid, Displayable s)
	{
		if(cid == home_exit)
		{
			destroyApp(false);
			notifyDestroyed();
		}
		if(cid == home_start)
		{
			currentIndex = method.getSelectedIndex();
			if(method.getString(currentIndex) == "Par soustraction")
			{

				Display.getDisplay(this).setCurrent(sousform1);
			}
			else
			{
				Display.getDisplay(this).setCurrent(euclform1);
			}
		}
		if(cid == eucl_menu || cid == sous_menu || cid == eucl_menu2 || cid == sous_menu2)
		{
			Display.getDisplay(this).setCurrent(homeform);
		}
		if(cid == eucl_find)
		{
			i_nombre1 = Integer.parseInt(tf_eucl_nombre1.getString());
			i_nombre2 = Integer.parseInt(tf_eucl_nombre2.getString());
			PGCD_eucl(i_nombre1, i_nombre2);
			tf_eucl_cal = new TextField("",cal, cal.length(),TextField.UNEDITABLE);
			euclform2.append(tf_eucl_cal);
			Display.getDisplay(this).setCurrent(euclform2);
		}
		if(cid == eucl_again)
		{
			Display.getDisplay(this).setCurrent(euclform1);
		}
		if(cid == sous_again)
		{
			Display.getDisplay(this).setCurrent(sousform1);
		}
		if(cid == sous_find)
		{
			i_nombre1 = Integer.parseInt(tf_sous_nombre1.getString());
			i_nombre2 = Integer.parseInt(tf_sous_nombre2.getString());
			PGCD_sous(i_nombre1, i_nombre2);
			tf_sous_cal = new TextField("",cal, cal.length(),TextField.UNEDITABLE);
			sousform2.append(tf_sous_cal);
			Display.getDisplay(this).setCurrent(sousform2);
		}
	}
	
	/* fonction qui calcul le PGCD avec la méthode euclide */
	protected void PGCD_eucl(int a, int b) 
	{
		int rest, resultat, mem_a, mem_b;
		mem_a = a;
		mem_b = b;
		cal = "";
		do
		{
			resultat = a/b;
			rest = a%b;
			cal += a + " = " + b + " x " + (a-rest)/b + " + " + rest + "\n";
			a = b;
			b = rest;
		}while(rest != 0);
		cal += "le PGCD de " + mem_a + " et de " + mem_b + " est " + a;
	}
	
	/* fonction qui calcul le PGCD avec la méthode par soustraction */
	protected void PGCD_sous(int a, int b)
	{
		int c;
		int mem_a = a;
		int mem_b = b;
		cal = "";
		a = (a>b)?a:b;
		b = (a==b)?mem_a:b;
		while(a != b)
		{
			c = a-b;
			cal += a + " - " + b + " = " + c +"\n";
			a = (c>b)?c:b;
			b = (a==c)?b:c;
		}
		cal += "le PGCD de " + mem_a + " et de " + mem_b + " est " + a;
	}
}

Conclusion :


Je trouve que le code est assez explicite comme ça pour ne pas commenter plus mais si vous avez besoin éclaircissement n'hésitez pas.

Hasta pronto :)

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.