Alx09

Untitled

Oct 5th, 2020
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include<stdio.h.>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. int cautare_liniara(int *a, int n, int x)
  7. {
  8.     int i = 0;
  9.  
  10.     for (i = 0; i < n; i++)
  11.     {
  12.         if (a[i] == x)
  13.             break;
  14.     }
  15.     return i;
  16. }
  17.  
  18. int cautare_fanion(int *a, int n, int x)
  19. {
  20.     int i = 0;
  21.     a[n] = x;
  22.     while (a[i] != x)
  23.     {
  24.         i = i + 1;
  25.     }
  26.     return i;
  27. }
  28.  
  29. int cautare_binara(int a[30], int n, int x)
  30. {
  31.     int S, D, m;
  32.     S = 0;
  33.     D = n - 1;
  34.     do
  35.     {
  36.         m = (S + D) / 2;
  37.         if (x > a[m])
  38.             S = m + 1;
  39.         else
  40.             D = m - 1;
  41.     } while (a[m] != x && S <= D);
  42.     return m;
  43. }
  44.  
  45. int cautare_binara_performanta(int a[30], int n, int x)
  46. {
  47.     int S, D, m;
  48.     S = 0;
  49.     D = n - 1;
  50.     do
  51.     {
  52.         m = (S + D) / 2;
  53.         if (x > a[m])
  54.             S = m + 1;
  55.         else
  56.             D = m;
  57.     } while (S < D);
  58.     return D;
  59. }
  60.  
  61. int cautare_interpolare(int a[30], int n, int x)
  62. {
  63.     int S, D, m;
  64.     S = 0;
  65.     D = n - 1;
  66.     if (x <= a[D] && x >= a[S])
  67.         do
  68.         {
  69.             m = S + (x - a[S]) * (D - S) / (a[D] - a[S]);
  70.             if (x > a[m])
  71.                 D = m - 1;
  72.         } while ((a[m] != x) && (S < D) && (a[S] != a[D]) && (x > a[S]) && (x < a[D]));
  73.  
  74.         return m;
  75. }
  76.  
  77. int main()
  78. {
  79.     int n, x, a[30], i, opt;
  80.     FILE* f;
  81.     fopen_s(&f, "sir.txt", "r");
  82.     fscanf_s(f, "%d",&n);
  83.     fscanf_s(f, "%d",&x);
  84.     for (i = 0; i < n; i++)
  85.     {
  86.         fscanf_s(f, "%d", &a[i]);
  87.     }
  88.     fclose(f);
  89.     do
  90.     {
  91.         printf("1.Cautare liniara\n");
  92.         printf("2.Cautare fanion\n");
  93.         printf("3.Cautare binara\n");
  94.         printf("4.Cautare binara performanta\n");
  95.         printf("5.Cautare prin interpolare\n");
  96.         printf("0.Iesire\n");
  97.         printf("Alegeti o optiune\n");
  98.         scanf_s("%d", &opt);
  99.  
  100.         switch (opt)
  101.         {
  102.         case 1: cautare_liniara(a, n, x);
  103.             break;
  104.         case 2: cautare_fanion(a, n, x);
  105.             break;
  106.         case 3: cautare_binara(a, n, x);
  107.             break;
  108.         case 4: cautare_binara_performanta(a, n, x);
  109.             break;
  110.         case 5: cautare_interpolare(a, n, x);
  111.             break;
  112.         case 0:
  113.             break;
  114.         }
  115.  
  116.     } while (opt != 0);
  117.  
  118.     system("pause");
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment