Advertisement
edutedu

proiect grafica

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