Guest User

Untitled

a guest
Jan 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. int a,b;
  5.  
  6. int Suma (int *largo, int *matriz)
  7. {
  8. int suma=0,contador=1, i;
  9. contador+=2;
  10. for (i=0 ; i<*largo ; i++)
  11. {
  12. suma+=matriz[i];
  13. contador+=4;
  14. }
  15. printf("= %d\n",suma);
  16. return contador;
  17. }
  18.  
  19. int bbinaria (int *largo, int *lista, int *num)
  20. {
  21. int izq=0;
  22. int der= *largo;
  23. int posic = 0;
  24. int cent=(izq+der)/2;
  25. int cont = 9;
  26. while ( lista[cent] != *num && izq<der)
  27. {
  28. cont += 1;
  29. if (lista[cent]>*num)
  30. {
  31. cont += 2;
  32. der = cent;
  33. cent = (cent+izq)/2;
  34. }
  35. else
  36. {
  37. cont += 2;
  38. izq = cent;
  39. cent = (cent+der)/2;
  40. }
  41. }
  42. cont += 1;
  43. if(lista[cent]==*num)
  44. {
  45. cont += 1;
  46. posic = (cent+1);
  47. printf("\n>>%d\n",posic);
  48. }
  49. return cont;
  50. }
  51.  
  52. int Magico(int *n ,int matriz[b][b])
  53. {
  54. int i,j;
  55. int suma_columnas[*n],suma_filas[*n];
  56. int suma_diagonal_principal=0,suma_diagonal_secundaria=0,contador=2;
  57.  
  58. for (i=0 ; i<*n ; i++)
  59. {
  60. suma_filas[i]=0;
  61. suma_columnas[i]=0;
  62. }
  63. contador+=2;
  64. for(i=0;i< *n;i++)
  65. {
  66. contador+=2;
  67. for(j=0;j< *n;j++)
  68. {
  69. contador++;
  70. if(i==j)
  71. {
  72. suma_diagonal_principal+=matriz[i][j];
  73. contador++;
  74. }
  75. contador++;
  76. if(i+j==*n-1)
  77. {
  78. suma_diagonal_secundaria+=matriz[i][j];
  79. contador++;
  80. }
  81. suma_filas[i] += matriz[i][j];
  82. suma_columnas[j]+= matriz[i][j];
  83. contador+= 2;
  84. }
  85. }
  86.  
  87. contador+=2;
  88. for(i=0 ; i<*n ; i++)
  89. {
  90. contador+=2;
  91. for(j=0;j<*n;j++)
  92. {
  93. contador+=3;
  94. if((suma_columnas[i]!= suma_columnas[j])||(suma_filas[i]!= suma_filas[j])||(suma_columnas[i]!=suma_filas[j]) )
  95. {
  96. printf("el cuadrado no es magico\n");
  97. contador++;
  98. return contador;
  99. }
  100. }
  101. }
  102. contador+=2;
  103. if((suma_diagonal_secundaria==suma_diagonal_principal) && (suma_diagonal_principal==suma_columnas[0]))
  104. {
  105. printf("el cuadrado es magico\n");
  106. contador++;
  107. return contador;
  108. }
  109.  
  110. else
  111. {
  112. printf("el cuadrado no es magico\n");
  113. contador++;
  114. return contador;
  115. }
  116. }
  117.  
  118. int fibonacci (int n, int *cont)
  119. {
  120. *cont+=3;
  121. if(n==1 || n==0)
  122. {
  123. *cont += 2;
  124. return n;
  125. }
  126. else
  127. {
  128. *cont += 3;
  129. return (fibonacci(n-1,cont) + fibonacci(n-2,cont));
  130. }
  131.  
  132. }
  133.  
  134. int llenar1 (int *largo,FILE *arch)
  135. {
  136. int i;
  137. int *matriz = malloc(*largo * sizeof(int));
  138.  
  139. for(i=0; i<*largo ; i++)
  140. {
  141. fscanf(arch,"%d",&matriz[i]);
  142. printf("%d ", matriz[i]);
  143. }
  144. return Suma(largo,matriz);
  145.  
  146. }
  147.  
  148. int llenar2 (int *largo,FILE *arch)
  149. {
  150. int i;
  151. int *matriz = malloc(*largo * sizeof(int));
  152. int *aux = malloc(sizeof(int));
  153.  
  154. fscanf(arch,"%d",aux);
  155. for(i=0; i<*largo ; i++)
  156. {
  157. fscanf(arch,"%d",&matriz[i]);
  158. printf("%d ", matriz[i]);
  159. }
  160. return bbinaria(largo,matriz,aux);
  161.  
  162. }
  163.  
  164. int llenarcuadrada (int *largo,FILE *arch)
  165. {
  166. b=*largo;
  167. int i, j;
  168. int matriz[b][b];
  169.  
  170. for(i=0; i<b ; i++)
  171. for(j=0; j<b ; j++)
  172. {
  173. fscanf(arch,"%d", &matriz[i][j]);
  174. if(j==b-1)
  175. printf(" %d\t\n", matriz[i][j]);
  176. else
  177. printf("%d\t ", matriz[i][j]);
  178. }
  179. return Magico(largo,matriz);
  180. }
  181.  
  182. int escribir (int tamano[][a], int instrucciones[][a])
  183. {
  184. int i,j;
  185. FILE *out;
  186. out=fopen("salida.txt","w");
  187. if(out==NULL)
  188. {
  189. printf("El archivo no se abrio como corresponde\n");
  190. return 0;
  191. }
  192. for(i=0 ; i<4 ; i++)
  193. {
  194. fprintf(out,"Algoritmo %d\n",i+1);
  195. fprintf(out,"Tamaño\t");
  196. fprintf(out,"Nº medio Intrucciones\t\n");
  197. for(j=0 ; j<a ; j++)
  198. {
  199. fprintf(out,"%d\t",tamano[i][j]);
  200. fprintf(out,"%d\t\n",instrucciones[i][j]);
  201. }
  202. }
  203. fclose(out);
  204. return 0;
  205. }
  206.  
  207. void grafico (int tamano[][a], int instrucciones[][a])
  208. {
  209. int i,j,h=0,max=0,c,cero=0,barra;
  210. initscr();
  211. for(i=0 ; i<a ; i++)
  212. {
  213. refresh();
  214. erase();
  215. if(has_colors())
  216. start_color();
  217. init_pair(1,COLOR_WHITE,COLOR_BLUE);
  218. init_pair(2,COLOR_BLACK,COLOR_WHITE);
  219. init_pair(3,COLOR_RED,COLOR_CYAN);
  220. init_pair(4,COLOR_RED,COLOR_RED);
  221. init_pair(5,COLOR_RED,COLOR_YELLOW);
  222. init_pair(6,COLOR_RED,COLOR_GREEN);
  223. bkgd(COLOR_PAIR(1));
  224. attron(COLOR_PAIR(2));
  225. for(j=0 ; j<4 ; j++)
  226. if(max<instrucciones[j][i])
  227. max=instrucciones[j][i];
  228. move(2,35);
  229. printw("Grafico %d",i+1);
  230. c=max/10;
  231. max=max+c;
  232. move(4,4);
  233. for(j=0 ; j<=11 ; j++)
  234. {
  235. move(4+j,4);
  236. printw("%9.0d",max);
  237. max=max-c;
  238. if(max<=0)
  239. {
  240. move(5+j,4);
  241. printw("%7.0d 0",cero);
  242. }
  243. }
  244. move(17,4);
  245. for(j=1 ; j<=74 ; j++)
  246. {
  247. if((j>=14)&&(j<=24))
  248. {
  249. attron(COLOR_PAIR(3));
  250. barra=instrucciones[0][i];
  251. if(barra-c<0)
  252. h=2;
  253. while(barra>0)
  254. {
  255. h++;
  256. barra=barra-c;
  257. }
  258. for( ;h >0 ;h--)
  259. {
  260. move(18-h,3+j);
  261. printw(" ");
  262. }
  263. attron(COLOR_PAIR(2));
  264. }
  265. else
  266. {
  267. if(j>=29&&j<=39)
  268. {
  269. attron(COLOR_PAIR(6));
  270. barra=instrucciones[1][i];
  271. if(barra-c<0)
  272. h=2;
  273. while(barra>0)
  274. {
  275. h++;
  276. barra=barra-c;
  277. }
  278. for( ;h >0 ;h--)
  279. {
  280. move(18-h,3+j);
  281. printw(" ");
  282. }
  283. attron(COLOR_PAIR(2));
  284. }
  285. else if(j>=44&&j<=54)
  286. {
  287. attron(COLOR_PAIR(4));
  288. barra=instrucciones[2][i];
  289. if(barra-c<0)
  290. h=2;
  291. while(barra>0)
  292. {
  293. h++;
  294. barra=barra-c;
  295. }
  296. for( ;h >0 ;h--)
  297. {
  298. move(18-h,3+j);
  299. printw(" ");
  300. }
  301. attron(COLOR_PAIR(2));
  302. }
  303. else
  304. {
  305. if(j>=59&&j<=69)
  306. {
  307. attron(COLOR_PAIR(5));
  308. barra=instrucciones[3][i];
  309. while(barra>0)
  310. {
  311. h++;
  312. barra=barra-c;
  313. }
  314. for( ;h >0 ;h--)
  315. {
  316. move(18-h,3+j);
  317. printw(" ");
  318. }
  319. attron(COLOR_PAIR(2));
  320. }
  321. else
  322. printw(" ");
  323. }
  324. }
  325. }
  326.  
  327. move(21,3);
  328. attron(COLOR_PAIR(3));
  329. printw(" ");
  330. attroff(COLOR_PAIR(3));
  331. attron(COLOR_PAIR(2));
  332. printw(" Algoritmo 1 (%d)",instrucciones[0][i]);
  333. attroff(COLOR_PAIR(2));
  334. move(21,23);
  335. attron(COLOR_PAIR(6));
  336. printw(" ");
  337. attroff(COLOR_PAIR(6));
  338. attron(COLOR_PAIR(2));
  339. printw(" Algoritmo 2 (%d)",instrucciones[1][i]);
  340. attroff(COLOR_PAIR(2));
  341. move(21,43);
  342. attron(COLOR_PAIR(4));
  343. printw(" ");
  344. attroff(COLOR_PAIR(4));
  345. attron(COLOR_PAIR(2));
  346. printw(" Algoritmo 3 (%d)",instrucciones[2][i]);
  347. attroff(COLOR_PAIR(2));
  348. move(21,63);
  349. attron(COLOR_PAIR(5));
  350. printw(" ");
  351. attroff(COLOR_PAIR(5));
  352. attron(COLOR_PAIR(2));
  353. printw(" Algoritmo 4 (%d)",instrucciones[3][i]);
  354. move(24,1);
  355. printw("->Aprete la tecla 'Enter' para ver el proximo grafico");
  356. getch();
  357.  
  358. }
  359. getch();
  360. endwin();
  361. }
  362.  
  363. void Leer (FILE *arch)
  364. {
  365. int i,n=0,*elem;
  366. int tamano[4][a];
  367. int instrucciones[4][a];
  368. for(i=0 ; i<a ; i++)
  369. {
  370. elem=malloc(sizeof(int));
  371. fscanf(arch,"%d",elem);
  372. tamano[0][i]=*elem;
  373. instrucciones[0][i]=llenar1(elem,arch);
  374. fscanf(arch,"%d",elem);
  375. tamano[1][i]=*elem;
  376. instrucciones[1][i]=llenarcuadrada(elem,arch);
  377. fscanf(arch,"%d", elem);
  378. tamano[2][i]=*elem;
  379. instrucciones[2][i]=llenar2(elem,arch);
  380. fscanf(arch,"%d", elem);
  381. tamano[3][i]=*elem;
  382. printf(">>%d\n\n",fibonacci(*elem-1,&n));
  383. instrucciones[3][i]=n;
  384. }
  385. escribir(tamano,instrucciones);
  386. grafico(tamano,instrucciones);
  387. }
  388.  
  389. int main ()
  390. {
  391. FILE *arch;
  392. arch=fopen("entrada.txt","r");
  393. if(arch==NULL)
  394. {
  395. printf("El archivo no se abrio como corresponde\n");
  396. return 0;
  397. }
  398. fscanf(arch,"%d", &a);
  399. Leer(arch);
  400. fclose(arch);
  401. return 0;
  402. }
Add Comment
Please, Sign In to add comment