thelittlewozniak

Untitled

Jan 18th, 2019
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // Prototype des fonctions
  6. int Menu();
  7. double OpeAri(int a, int b);
  8. int InitTab(int taille, int choix);
  9. int FormTab(int tab[], int taille);
  10.  
  11. int main()
  12. {
  13.     // Déclaration des variables
  14.     int choix=0, tab[9], i=0,tabi[2];
  15.     char recommencer='';
  16.  
  17.     // Code
  18.     srand(time(NULL));
  19.     rand();
  20.  
  21.     do{
  22.         choix = Menu();
  23.  
  24.         switch(choix)
  25.         {
  26.             case 1: //Entrée des nombres, appel de la fonction OpeAri, affichage du resultat et possibilité de recommencer
  27.                 do{
  28.                     for (int j = 1; j < 3; j++) {
  29.                         do
  30.                         {
  31.                             printf("Entrez le %d",j);
  32.                             fflush(stdin);
  33.                             scanf("%d", &tabi[j - 1]);
  34.                             if (tabi[j] < 0 || tabi[j]>50) {
  35.                                 printf("Erreur, le nombre doit etre compris entre 0 et 50!\n");
  36.                             }
  37.                         } while (tabi[j]<0 || tabi[j]>50);
  38.                     }
  39.                     printf("Resultat: %lf.\n", OpeAri(tabi[0], tabi[1]));
  40.                     do {
  41.                         printf("Voulez-vous recommencer ? (O)ui ou (N)on\n");
  42.                         fflush(stdin);
  43.                         scanf("%c", &recommencer);
  44.                         if (recommencer != 'O' && recommencer != 'o' && recommencer != 'N' && recommencer != 'n') {
  45.                             printf("Erreur, veuillez entrer (O)ui ou (N)on!\n");
  46.                         }
  47.                     } while (recommencer != 'O' && recommencer != 'o' && recommencer != 'N' && recommencer != 'n')
  48.                 }while(recommencer == 'O' || recommencer == 'o');
  49.                 break;
  50.             case 2:
  51.                 printf("%d\n", InitTab(8, 0));
  52.                 system("pause");
  53.                 break;
  54.             case 3:
  55.                 printf("%d\n", InitTab(5, 0));
  56.                 system("pause");
  57.                 break;
  58.             case 4:
  59.                 printf("%d\n", InitTab(50, 1));
  60.                 system("pause");
  61.                 break;
  62.             case 5:
  63.                 printf("%d\n", InitTab(33, 2));
  64.                 system("pause");
  65.                 break;
  66.             case 6:
  67.                 for(i = 0; i < 9; i++)
  68.                 {
  69.                     tab[i] = InitTab(20, 3);
  70.                     printf("Le resultat du tableau numero %d est %d\n", i+1, tab[i]);
  71.                 }
  72.                 printf("Le resultat du tableau stockant les resultats precedents est: %d\n", FormTab(tab, 9));
  73.                 system("pause");
  74.                 break;
  75.             default:
  76.                 break;
  77.         }
  78.     }while(choix != 7);
  79.  
  80.     return 0;
  81. }
  82.  
  83. int Menu()
  84. {
  85.     // Déclaration des variables
  86.     int choix;
  87.  
  88.     // Code bete d'utiliser plusieurs printf surtout que tu fais des retours de chariots à chaque fois
  89.     system("cls");
  90.     printf("Voulez-vous:\n");
  91.     printf("1. Effectuer une operation arithmetique ?\n");
  92.     printf("2. Appliquer une formule sur un tableau 1x8 ?\n");
  93.     printf("3. Appliquer une formule sur un tableau 1x5 ?\n");
  94.     printf("4. Appliquer une formule sur un tableau 1x50 ?\n");
  95.     printf("5. Appliquer une formule sur un tableau 1x33 ?\n");
  96.     printf("6. Appliquer une formule sur plusieurs tableaux ?\n");
  97.     printf("7. Quitter le programme ?\n");
  98.     do
  99.     {
  100.         printf("Votre choix: "); fflush(stdin); scanf("%d", &choix);
  101.         if (choix < 1 || choix>7) {
  102.             printf("Choix incorrect!\n");
  103.         }
  104.     } while (choix<1 || choix>7);
  105.     return choix;
  106. }
  107.  
  108. double OpeAri(int a, int b){return (5 / (double)(b - 10)) + (b / (double)(a + b)) + ((double)(3 * a*b) / 2);}
  109.  
  110. int InitTab(int taille, int choix)
  111. {
  112.     // Déclaration des variables, instencie tjs tes variables
  113.     int tab[taille], i=0;
  114.     char conf='';
  115.  
  116.     // Code
  117.     do{
  118.         for(i = 0; i <= taille-1; i++)
  119.         {
  120.             switch(choix)
  121.             {
  122.                 case 0:
  123.                     printf("Entrez le nombre allant dans la case: %d: \n", i); fflush(stdin); scanf("%d", &tab[i]);
  124.                     break;
  125.                 case 1:
  126.                     tab[i] = rand() % 10 + 1;
  127.                     break;
  128.                 case 2:
  129.                     tab[i] = rand() % 15 + 1;
  130.                     break;
  131.                 case 3:
  132.                     tab[i] = rand() % 21;
  133.                     break; //inutile de mettre un default si c'est toi qui l'utilise et pas l'utilisateur
  134.             }
  135.         }
  136.  
  137.         printf("Le tableau contient les nombres suivants: ");
  138.         for(i = 0; i < taille; i++)
  139.         {
  140.             printf("%d, ", tab[i]);
  141.         }
  142.         do
  143.         {
  144.             printf("\nConfirmez-vous ces entrees ? (O)ui ou (N)on\n");
  145.             fflush(stdin);
  146.             scanf("%c", &conf);
  147.             if (conf != 'O' && conf != 'o' && conf != 'N' && conf != 'n') {
  148.                 printf("Erreur, veuillez entrer (O)ui ou (N)on!\n");
  149.             }
  150.         } while (conf != 'O' && conf != 'o' && conf != 'N' && conf != 'n');
  151.     }while(conf == 'N' || conf == 'n');
  152.  
  153.     return FormTab(tab, taille);
  154. }
  155.  
  156. int FormTab(int tab[], int taille)
  157. {
  158.     // Déclaration des variables
  159.     int i=0, total = 0;
  160.     int j = taille - 1;
  161.  
  162.     for(i = 0; i <= ((taille-1)/2); i++){
  163.         if(i == j){
  164.             total += tab[i];
  165.         }
  166.         else{
  167.             total += (tab[i] * tab[j]);
  168.         }
  169.         j--;
  170.     }
  171.     return total;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment