Bonjour,
Je débute en programmation,
voici mon programme en java :
package MonPack;
public class numero {
static void afficherNum(String tel, String nom, char chiffre)
{
char nombre = chiffre ;
int n = 0;
for(int i = 0; i < tel.length(); i++)
if (tel.charAt(i) == nombre)
n++;
System.out.printf("il y a %d fois le chiffre %c dans %6s\n", n, nombre, nom );
}
//on recherche les chiffres pairs et impairs grace a cette fonction
static void afficherImpairs(String tel, String nom, String choix)
{
//on initialise le total des nombre pairs et impars a zero
int nbPair =0, nbImpair=0;
//boucle pour parcourir le numero de tel de gauche a droite entirement
for(int i = 0 ; i < tel.length(); i++){
//le switch permet de nous dire soit cest un impair
switch (tel.charAt(i))
{
case '1' :
case '3' :
case '5' :
case '7' :
case '9' : nbImpair++;//si le caractere trouvé on incremente un impair et on sort sinon on increment pair et on sort
break;
default : nbPair++;}}
//enfonction du choix de depart on affiche le resultat pair ou impairs la fonction ne retourne rien
if (choix == "pair")
System.out.printf("Il y a %d chiffre pairs dans %6s : ",nbPair ,nom);
if (choix == "impair")
System.out.printf("Il y a %d chiffre impairs dans %6s : ",nbImpair ,nom);
System.out.printf("\n");
}
//permet d'afficher les nombre commun a deux numero de telephone
static void afficherCommun(String tel, String tel2, String nom, String nom2, String nbPair, String nbImpair)
{
//j'ai tout simplement ecrit les numero possible d'un numero de telephone
String num = "0123456789";
//la premiere boucle prend comme caractere la chaine que je viens d'ecrire dans l'ordre croissant
for (int i = 0; i <= 9; i++){
//initialisation de temoins
int t = 0, t2 = 0;
//la deuxieme compare si ce caractere est dans le tel 1 et 2 si oui on active le temoin du numero de telephonne
for(int j = 0; j < tel.length(); j++){
if(tel.charAt(j) == num.charAt(i)) t = 1;
if(tel2.charAt(j) == num.charAt(i)) t2 = 1;
}
//si les deux numero on en temoin le caractere de la chaine 'num' les deux temoins seront a 1 donc on ecrit le numero commun
if (t == 1 && t2 == 1)System.out.printf("%c, ", num.charAt(i));}
System.out.printf("Sont les nombre communs des numero %6s et %6s",nom, nom2);
}
//boucle qui parcour un numero de telephone si elle trouve un chiffre plus petit que 9 elle l'envoi a pluspetit chiffre
//une fois tou le numero parcouru elle ressort le plus petit chiffre trouvé.
static void afficherMini(String tel, String nom)
{
char plusPetitChif = '9' ;
for(int i = 0; i < tel.length(); i++)
if (tel.charAt(i) < plusPetitChif)
plusPetitChif = tel.charAt(i);
System.out.printf("Le chiffre le plus petit dans le numero %6s est %c\n",
nom, plusPetitChif);
}
//fonction principale avec les appel de fonctions.
public static void main(String[] args) {
String telUDM = "5143436111", telJean = "4506443544";
//affiche brut les numeros via un substring
System.out.printf("1. d’extraire de sous-chaînes et d’afficher :\n");
System.out.printf("Téléphone d’UdM : (%s) %s-%s\n",
telUDM.substring(0, 3), telUDM.substring(3, 6), telUDM.substring(6, 10));
System.out.printf("Téléphone de Jean : (%s) %s-%s\n \n",
telJean.substring(0, 3), telJean.substring(3, 6), telJean.substring(6, 10));
//tout les autres appels qui suivent sont fait de la meme facon on envoi le nom et le numero de tel a la fonction.
System.out.printf("2. de compter et d’afficher :\n");
afficherNum(telUDM, "telUDM", '3');
afficherNum(telUDM, "telUDM", '1');
afficherNum(telJean, "telJean", '4');
System.out.printf("\n");
System.out.printf("3. de compter et d’afficher :\n");
afficherImpairs(telUDM, "telUDM", "impair");
afficherImpairs(telJean, "telJean", "pair");
System.out.printf("\n");
System.out.printf("4. de déterminer et d’afficher les chiffres communs de ces 2 numéros de téléphone.\n");
afficherCommun(telJean, telUDM,"telJean" , "telUDM");
System.out.printf("\n \n");
System.out.printf("5. de déterminer et d’afficher :\n");
afficherMini(telUDM, "telUDM");
afficherMini(telJean, "telJean");
}
j'aimerai faire en sorte d'afficher les numeros communs impairs et pairs de ces deux numeros de tel, comment faire ?
et j'ai fais ce programme, mais je bloque a le transformer en fonction :
#include <stdio.h>
int main()
{
char poste[] = { 'A', 'P', 'O', 'P', 'O', 'A', 'P', 'P' };
int nbCafe[]= { 3, 1, 6, 1, 5, 18, 0, 3 },
age[] = { 25, 18, 23, 20, 49, 24, 56, 29 } ;
int nbPers = sizeof(poste) / sizeof(char);
int i, nbProgrammeur, nbSecretaire, cafeMinP, ageMaxA, somCafeP, somAge, j, indMin, tempo ;
char tempoCar;
/*affichage du tableau*/
printf("Contenu des 3 tableaux au debut\n");
printf("indice cafe age poste\n");
for ( i = 0; i < nbPers; i++)
{
printf("%1d%8.2i %7i", i, nbCafe[i], age[i]);
switch (poste[i])
{
case 'A' : printf(" Analystes\n"); break;
case 'P' : printf(" programmeurs\n"); break;
case 'O' : printf(" Operateurs\n");
}
}
printf("\n");
/*nombre de programmeurs*/
nbProgrammeur = 0 ;
for( i = 0 ; i < nbPers ; i++ )
if (poste[i] == 'P')
nbProgrammeur++;
printf("Le nombre de programmeurs : %d\n", nbProgrammeur);
printf("\n");
/* nombre de secrétaires*/
nbSecretaire = 0 ;
for( i = 0 ; i < nbPers ; i++ )
if (poste[i] == 'S')
nbSecretaire++;
printf("Le nombre de secrétaire : %d\n", nbSecretaire);
/* consommation cafe minimale des programmeurs*/
cafeMinP = 500;
for (i = 0; i < nbPers ; i++)
if (poste[i] == 'P')
if (nbCafe[i] < cafeMinP)
cafeMinP = nbCafe[i];
printf("\nCafe min Programmeurs : %d cafe\n", cafeMinP);
/* Age max des analystes*/
ageMaxA = 0;
for (i = 0; i < nbPers ; i++)
if (poste[i] == 'A')
if (age[i] > ageMaxA)
ageMaxA = age[i];
printf("\nAge max des analystes : %d ans\n", ageMaxA);
/* Consommation moyenne Cafe programmeurs*/
somCafeP =0;
for (i = 0; i < nbPers ; i++)
if (poste[i] == 'P')
somCafeP += nbCafe[i];
printf("\nLa consommation moyenne de café des programmeurs : %6.2i cafe\n", somCafeP / nbProgrammeur );
/* age moyen des employes*/
for (i = 0; i < nbPers ; i++)
somAge += age[i];
printf("\nL'age moyen des employes : %6.2i ans\n", somAge / nbPers );
/* tri selon l'age*/
for (i = 0; i < nbPers-1 ; i++)
{
indMin = i ;
for (j = i+1; j < nbPers; j++)
if (age[j] < age[indMin])
indMin = j;
if(indMin != i)
{
tempo = age[i];
age[i] = age[indMin];
age[indMin] = tempo;
tempoCar = poste[i];
poste[i] = poste[indMin];
poste[indMin] = tempoCar;
tempo = nbCafe[i];
nbCafe[i] = nbCafe[indMin];
nbCafe[indMin] = tempo;
}
}
/* afficher les tableaux après le tri : */
printf("Contenu du tableau apres le tri :\n");
for(i = 0; i < nbPers; i++)
printf("%5d %10i %10d%5c\n", i, nbCafe[i], age[i], poste[i]);
printf("\n");
system("pause");
return 0;
}
/* EXECUTION
Contenu des 3 tableaux au debut
indice cafe age poste
0 03 25 Analystes
1 01 18 programmeurs
2 06 23 Operateurs
3 01 20 programmeurs
4 05 49 Operateurs
5 18 24 Analystes
6 00 56 programmeurs
7 03 29 programmeurs
Le nombre de programmeurs : 4
Le nombre de secrÚtaire : 0
Cafe min Programmeurs : 0 cafe
Age max des analystes : 25 ans
La consommation moyenne de cafÚ des programmeurs : 01 cafe
L'age moyen des employes : 30 ans
Contenu du tableau apres le tri :
0 1 18 P
1 1 20 P
2 6 23 O
3 18 24 A
4 3 25 A
5 3 29 P
6 5 49 O
7 0 56 P
Appuyez sur une touche pour continuer...
--------------------------------
Process exited after 1.284 seconds with return value 0
Appuyez sur une touche pour continuer...
*/
Merci de m'aider !