Advertisement
Dani_info

Grafica cercurci - bubble

Nov 12th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <graphics.h>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. void BubleSort(int v[], int n);
  8.  
  9. int main()
  10. {
  11.     int n;
  12.     char text[3];
  13.     cout<<"Cate numere se citesc?";
  14.     cin >> n;
  15.     int L=n*140, l=400;
  16.     int v[n];
  17.     for (int i=0; i<n; i++)
  18.     {
  19.         cout<<"v["<<i<<"]=";
  20.         cin>>v[i];
  21.     }
  22.     initwindow(L, l);
  23.     int x=100;
  24.     for (int i=0; i<n; i++, x+=100) // afisare numere
  25.     {
  26.         setcolor(WHITE);
  27.         circle (x, 300, 40);
  28.         sprintf(text, "%d", v[i]);
  29.         settextstyle(3, 0, 2);
  30.         int latime=textwidth(text);
  31.         int inaltime=textheight(text);
  32.         outtextxy(x-latime/2, 300-inaltime/2, text);
  33.     }
  34.     BubleSort(v, n);
  35.     getch();
  36.     return 0;
  37. }
  38.  
  39.  
  40.  
  41. void BubleSort (int v[], int n)
  42. {
  43.     int ok, x=10, latime, inaltime;
  44.     do{
  45.         ok=1;
  46.         x=100;
  47.         for (int i=0; i<n-1; i++, x+=100)
  48.         {
  49.             cout<<"Se compara "<<v[i]<<" cu "<<v[i+1]<<endl;
  50.             setcolor(RED);
  51.             circle(x, 300, 40);
  52.             circle(x+100, 300, 40);
  53.             delay(1000);
  54.             setcolor(WHITE);
  55.             circle(x, 300, 40);
  56.             circle(x+100, 300, 40);
  57.             delay (500);
  58.             char text[3];
  59.             if (v[i]>v[i+1])
  60.             {
  61.                 sprintf(text, "%d", v[i+1]);
  62.                 settextstyle(3, 0, 2);
  63.                 latime=textwidth(text);
  64.                 inaltime=textheight(text);
  65.                 setcolor(BLACK);
  66.                 outtextxy(x-20, 300-inaltime/2, "sters");
  67.                 outtextxy(x+80, 300-inaltime/2, "sters");
  68.                 setcolor(WHITE);
  69.                 outtextxy(x-latime/2, 300-inaltime/2, text);
  70.                 sprintf(text, "%d", v[i]);
  71.                 settextstyle(3, 0, 2);
  72.                 latime=textwidth(text);
  73.                 inaltime=textheight(text);
  74.                 outtextxy(x+100-latime/2, 300-inaltime/2, text);
  75.                 delay(1000);
  76.                 cout<<"Se interschimba "<<v[i]<<" cu "<<v[i+1]<<endl;
  77.                 swap(v[i], v[i+1]);
  78.                 ok=0;
  79.             }
  80.         }
  81.     }while (!ok);
  82.     setcolor(RED);
  83.     settextstyle(3, 0, 3);
  84.     outtextxy(50, 50, "Sirul este sortat!");
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement