Advertisement
M-Q711599

unlimited

Mar 3rd, 2020
1,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.33 KB | None | 0 0
  1. 11.06 KB
  2. #include <iostream>
  3. #include <fstream>
  4. #include <graphics.h>
  5. #include <cmath>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. int coord_centre[50][50],viz[20];
  12.  
  13. ifstream fin("graf.in");
  14. #define PI 3.1415
  15.  
  16. int m[7], aux[100];
  17.  
  18. void itoaa(int nr, char s[3])
  19. {
  20.     int k=0, c;
  21.     while(nr)
  22.     {
  23.         c=nr%10;
  24.         s[k]=c+'0';
  25.         k++;
  26.         nr/=10;
  27.     }
  28.     s[k]='\0';
  29.     if(strlen(s)==2)
  30.     {
  31.         int aux=s[0];
  32.         s[0]=s[1];
  33.         s[1]=aux;
  34.     }
  35. }
  36.  
  37. void Init_Graf(int a[50][50])
  38. {
  39.     for(int i=0;i<50;i ++)
  40.         for(int j=0;j<50;j++)
  41.             a[i][j]=0;
  42. }
  43.  
  44. void Citire_muchii(int &n,int a[50][50])
  45. {
  46.     Init_Graf(a);
  47.     fin>>n ;
  48.     int i,j;
  49.     while(fin>>i>>j)
  50.         a[i][j]=a[j][i]=1;
  51. }
  52.  
  53. void Afisare_MA(int n,int a [50][50])
  54. {
  55.     for(int i=1;i<=n;i++)
  56.     {
  57.         for(int j=1;j<=n;j++)
  58.             cout<<a[i][j]<<" ";
  59.         cout<<endl;
  60.     }
  61. }
  62.  
  63. void deseneaza_graf(int a[50][50], int n)
  64. {
  65.     initwindow(1000,600);
  66.     int R=200,r=40;
  67.     double x=500,y=300,xi,yi;
  68.     char G[50];
  69.     double alfa;
  70.     alfa=2*PI/n;
  71.     char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};
  72.     for(int i=0;i<n;i++)
  73.     {
  74.         xi=x+R*sin(i*alfa);
  75.         yi=y+R*cos(i*alfa);
  76.         coord_centre[i+1][0]=xi;
  77.         coord_centre[i+1][1]=yi;
  78.         setcolor(RED);
  79.         circle(xi,yi,r);
  80.         setfillstyle(SOLID_FILL,MAGENTA);
  81.         fillellipse(xi,yi,r,r);
  82.         setfillstyle(SOLID_FILL,CYAN);
  83.         fillellipse(xi,yi,r-7,r-7);
  84.         char s[3];
  85.         itoaa(i+1,s);
  86.         setcolor(7);
  87.         settextstyle(10,0,3);
  88.         outtextxy((xi-r/2+17),(yi-r/2)+10,s);
  89.     }
  90.     ///char S[3];
  91.     for(int i=1;i<=n;i++)
  92.         for(int j=1;j<n;j++)
  93.             if(a[i][j]==1)
  94.             {
  95.                 setcolor(CYAN);
  96.                 line(coord_centre[i][0],coord_centre[i][1],coord_centre[j][0],coord_centre[j][1]);
  97.                 delay(500);
  98.             }
  99.  
  100.     for(int i=1; i<=n; i++)
  101.     {
  102.         xi=x+R*sin(i*alfa);
  103.         yi=y+R*cos(i*alfa);
  104.         coord_centre[i+1][0]=xi;
  105.         coord_centre[i+1][1]=yi;
  106.         char s[3];
  107.         itoaa(i+1,s);
  108.         setcolor(7);
  109.         settextstyle(10,0,3);
  110.         outtextxy((xi-r/2+17),(yi-r/2)+10,s);
  111.  
  112.     }
  113. }
  114.  
  115. void deseneaza_bf(int a[50][50], int n)
  116. {
  117.     initwindow(1000,1000);
  118.     int R=200,r=40;
  119.     double x=600,y=400,xi,yi;
  120.  
  121.     double alfa= 2*3.1415/n;
  122.     for(int i=0; i<n; i++)
  123.     {
  124.         xi=x+R*sin(i*alfa);
  125.         yi=y+R*cos(i*alfa);
  126.         coord_centre[i+1][0]=xi;
  127.         coord_centre[i+1][1]=yi;
  128.         setcolor(RED);
  129.         circle(xi,yi,r);
  130.         ///setfillpattern(pattern, MAGENTA);
  131.         setfillstyle(SOLID_FILL,MAGENTA);
  132.         fillellipse(xi,yi,r,r);
  133.         ///setfillpattern(pattern, CYAN);
  134.         setfillstyle(SOLID_FILL,CYAN);
  135.         fillellipse(xi,yi,r-7,r-7);
  136.         char s[3];
  137.         itoaa(i+1,s);
  138.         setcolor(7);
  139.         outtextxy((xi-r/2+17),(yi-r/2)+10,s);
  140.     }
  141. }
  142.  
  143.  
  144. void CitireLant(int v[50],int &l)
  145. {
  146.     int i=0,nod;
  147.     cout<<"Dati o secventa de noduri care se termina cu 0:";
  148.     /*cin>>nod;
  149.     while(nod)
  150.     {
  151.         v[i++]=nod;
  152.         cin>>nod;
  153.     }
  154.     l=i;*/
  155.  
  156.     do
  157.     {
  158.       cin>>nod;
  159.       l++;
  160.       v[l]=nod;
  161.       ///aux[nod]+=1;
  162.     }while(nod);
  163.     l--;
  164. }
  165.  
  166. int VerificareLant(int v[50], int l, int a[50][50], int n)
  167. {
  168.     int i,j;
  169.     for(i=0;i<l;i++)
  170.         if(v[i]>n)
  171.             return 0;
  172.     for(i=0;i<l-1;i++)
  173.         if(a[v[i]][v[i+1]]==0)
  174.             return 0;
  175.     return 1;
  176. }
  177.  
  178. int LantElementar(int v[50], int l, int a[50][50], int n)
  179. {
  180.     if(VerificareLant(v,l,a,n))
  181.     {
  182.         for(int i=0;i<l;i++)
  183.             for(int j=i+1;j<l;j++)
  184.                 if(v[i]==v[j])
  185.                     return 0;
  186.         return 1;
  187.     }
  188.     else
  189.         return 0;
  190. }
  191.  
  192. int LantSimplu(int v[50], int l, int a[50][50], int n)
  193. {
  194.     if(VerificareLant(v,l,a,n))
  195.     {
  196.         for(int i=0;i<l-1;i++)
  197.             for(int j=i+1;j<l-1;j++)
  198.                 if((v[i]==v[j] && v[i+1]==v[j+1]) || (v[i]==v[j+1] && v[i+1]==v[j]))
  199.                     return 0;
  200.         return 1;
  201.     }
  202.     else
  203.         return 0;
  204. }
  205.  
  206.  
  207.  
  208. void deseneaza_lant(int lant[50], int lg, int a[50][50], int n)
  209. {
  210.     initwindow(1000,1000);
  211.     int R=200,r=40;
  212.     double x=500,y=300,xi,yi;
  213.     char G[50];
  214.     int coord_centre[50][50];
  215.     ///setcolor(YELLOW);
  216.     ///circle(600,400,R);
  217.     double alfa = 2*PI/n;
  218.     char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};
  219.     for(int i=0;i<n;i++)
  220.     {
  221.         xi=x+R*sin(i*alfa);
  222.         yi=y+R*cos(i*alfa);
  223.         coord_centre[i+1][0]=xi;
  224.         coord_centre[i+1][1]=yi;
  225.         setcolor(RED);
  226.         circle(xi,yi,r);
  227.         ///setfillpattern(pattern, MAGENTA);
  228.         setfillstyle(SOLID_FILL,MAGENTA);
  229.         fillellipse(xi,yi,r,r);
  230.         ///setfillpattern(pattern, CYAN);
  231.         setfillstyle(SOLID_FILL,CYAN);
  232.         fillellipse(xi,yi,r-7,r-7);
  233.         char s[3];
  234.         itoaa(i+1,s);
  235.         setcolor(7);
  236.         outtextxy((xi-r/2+17),(yi-r/2)+10,s);
  237.     }
  238.     for(int i=0;i<n;i++)
  239.         for(int j=0;j<=n;j++)
  240.             if(a[i][j]==1)
  241.             {
  242.                 setcolor(CYAN);
  243.                 line(coord_centre[i][0],coord_centre[i][1],coord_centre[j][0],coord_centre[j][1]);
  244.                 delay(500);
  245.             }
  246.     char s[200], aux[5];
  247.     strcpy(s, "Lantul: ");
  248.     setcolor(YELLOW);
  249.     if(VerificareLant(lant,lg,a,n)==1)
  250.         for(int i=0;i<lg-1;i++)
  251.         {
  252.             itoaa(lant[i], aux);
  253.             strcat(s,aux);
  254.             strcat(s, " ");
  255.             line(coord_centre[lant[i]][0], coord_centre[lant[i]][1], coord_centre[lant[i+1]][0], coord_centre[lant[i+1]][1]);
  256.             delay(500);
  257.         }
  258.     itoaa(lant[lg-1],aux);
  259.     strcat(s,aux);
  260.     outtextxy(0,200,s);
  261.     if(LantElementar(lant,lg,a,n))
  262.         strcpy(s,"Lantul este elementar");
  263.     else
  264.         strcpy(s,"Lantul este neelementar");
  265.     outtextxy(0,250,s);
  266.     if(LantSimplu(lant,lg,a,n))
  267.         strcpy(s,"Lantul este simplu");
  268.     else
  269.         strcpy(s,"Lantul este compus");
  270.     outtextxy(0,300,s);
  271. }
  272.  
  273. void BF(int a[50][50],int n,int nodstart)
  274. {
  275.     double alfa= 2*3.1415/n;
  276.     double x=600,y=400,xi,yi;
  277.     int R=200,r=40;
  278.     int parc[50], viz[50],in=1,sf=1 ;
  279.     for(int i=0;i<50;i++)
  280.     {
  281.         parc[i]=0;
  282.         viz[i]=0;
  283.     }
  284.     if(nodstart>=1 && nodstart<= n)
  285.     {
  286.         parc[1]=nodstart;
  287.         viz[nodstart]=1;
  288.     }
  289.     while(in<=sf)
  290.     {
  291.         for (int j=1;j<=n;j++)
  292.             if(a[parc[in]][j]==1 && viz[j]==0)
  293.             {
  294.                 parc[++sf]=j;
  295.                 viz[j]=1 ;
  296.             }
  297.         in++;
  298.     }
  299.     for(int i=1;i<=sf;i++)
  300.     {
  301.         settextstyle(EUROPEAN_FONT, HORIZ_DIR,2);
  302.         xi=x+R*sin((parc[i]-1)*alfa);
  303.         yi=y+R*cos((parc[i]-1)*alfa);
  304.         setfillstyle(SOLID_FILL,YELLOW);
  305.         fillellipse(xi,yi,r-7,r-7);
  306.         char s[3];
  307.         itoaa(parc[i],s);
  308.         setcolor(7);
  309.         outtextxy((xi-r/2+17),(yi-r/2)+10,s);
  310.         getch();
  311.     }
  312.     for(int i=1;i<=n;i++)
  313.         cout<<parc[i]<<" ";
  314.     cout<<endl;
  315.  
  316. }
  317.  
  318. void init(int viz[50])
  319. {
  320.     for(int i=0; i<50; i++)
  321.         viz[i]=0;
  322. }
  323.  
  324. void DF(int a[50][50],int n,int nodstart)
  325. {
  326.     double alfa= 2*3.1415/n;
  327.     double x=600,y=400,xi,yi;
  328.     int R=200,r=40;
  329.     int st[50],viz[50],vf,ct=1,parc[50];
  330.     init(viz);
  331.     viz[nodstart]=1;
  332.     vf=1;
  333.     st[vf]=nodstart;
  334.     parc[vf]=nodstart;
  335.     cout<<nodstart<<" ";
  336.     while(vf!=0)
  337.     {
  338.         int i=1,k=st[vf];
  339.         while(i<=n && (a[k][i]==0 || (a[k][i]==1 && viz[i]==1)))
  340.             i++;
  341.         if(i==n+1)
  342.             vf--;
  343.         else
  344.         {
  345.             cout<<i<<" ";
  346.             vf++;
  347.             parc[++ct]=i;
  348.             st[vf]=i;
  349.             viz[i]=1;
  350.         }
  351.     }
  352.     for(int i=1; i<=ct; i++)
  353.     {
  354.         settextstyle(EUROPEAN_FONT, HORIZ_DIR, 2);
  355.         xi=x+R*sin((parc[i]-1)*alfa);
  356.         yi=y+R*cos((parc[i]-1)*alfa);
  357.         if(viz[i]==1)
  358.         setfillstyle(SOLID_FILL,YELLOW);
  359.         fillellipse(xi,yi,r-7,r-7);
  360.         char s[3];
  361.         itoaa(parc[i],s);
  362.         setcolor(7);
  363.         outtextxy((xi-r/2+17),(yi-r/2)+10,s);
  364.         getch();
  365.     }
  366. }
  367. int main()
  368. {
  369.     int x,n,a[50][50],v[50],l,lg,viz[50];
  370.     cout<<"MENIU"<<endl;
  371.     cout<<"1) citire graf"<<endl;
  372.     cout<<"2) afisare matrice"<<endl;
  373.     cout<<"3) afisare graf"<<endl;
  374.     cout<<"4) citire lant"<<endl;
  375.     cout<<"5) afisarea  lantului si tipul lantului"<<endl;
  376.     cout<<"6) BF"<<endl;
  377.     cout<<"7) DF"<<endl;
  378.     cout<<endl;
  379.     cin>>x;
  380.     while(x!=1)
  381.     {
  382.         cout<<"Va rog sa incepeti cu instructiunea 1"<<endl;
  383.         cin>>x;
  384.     }
  385.     while(x!=0)
  386.     {
  387.         if(x==1)
  388.         {
  389.             cout<<"Citire graf"<<endl;
  390.             cout<<endl;
  391.             Citire_muchii(n,a);
  392.         }
  393.         if(x==2)
  394.         {
  395.             cout<<"Afisare matrice de adiacenta"<<endl;
  396.             cout<<endl;
  397.             Afisare_MA(n,a);
  398.             cout<<endl;
  399.         }
  400.         if(x==3)
  401.         {
  402.             cout<<"Afisare graf"<<endl;
  403.             cout<<endl;
  404.             deseneaza_graf(a,n);
  405.         }
  406.         if(x==4)
  407.         {
  408.             closegraph();
  409.             cout<<"Citire lant"<<endl;
  410.             CitireLant(v,l);
  411.             cout<<endl;
  412.             m[4]=1;
  413.         }
  414.         if(x==5)
  415.         {
  416.             if(m[4]==0)
  417.             {
  418.                 cout<<"Va rog alegeti instructiunea 4 inaintea instructiunii 5"<<endl;
  419.                 cin>>x;
  420.             }
  421.             cout<<"Afisare lant"<<endl;
  422.             if(VerificareLant(v,l,a,n))
  423.                 deseneaza_lant(v,l,a,n);
  424.             else
  425.                 cout<<"Nu este lant\n"<<endl;
  426.             cout<<endl;
  427.         }
  428.         if(x==6)
  429.         {
  430.             cout<<"BF"<<endl;
  431.             closegraph();
  432.             for (int i=1;i<=n;i++)
  433.             {
  434.                 deseneaza_bf(a,n);
  435.                 settextstyle(DEFAULT_FONT,HORIZ_DIR,20);
  436.                 outtextxy(10,10,"BF(");
  437.                 char s[3];
  438.                 itoaa(i,s);
  439.                 setcolor(7);
  440.                 outtextxy(200,10,s);
  441.                 outtextxy(335,10,")");
  442.                 BF(a,n,i);
  443.                 getch();
  444.                 cleardevice();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement