Advertisement
Guest User

Untitled

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