Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ncurses.h>
- #include <stdlib.h>
- #include <stdio.h>
- int a,b;
- int Suma (int *largo, int *matriz)
- {
- int suma=0,contador=1, i;
- contador+=2;
- for (i=0 ; i<*largo ; i++)
- {
- suma+=matriz[i];
- contador+=4;
- }
- printf("= %d\n",suma);
- return contador;
- }
- int bbinaria (int *largo, int *lista, int *num)
- {
- int izq=0;
- int der= *largo;
- int posic = 0;
- int cent=(izq+der)/2;
- int cont = 9;
- while ( lista[cent] != *num && izq<der)
- {
- cont += 1;
- if (lista[cent]>*num)
- {
- cont += 2;
- der = cent;
- cent = (cent+izq)/2;
- }
- else
- {
- cont += 2;
- izq = cent;
- cent = (cent+der)/2;
- }
- }
- cont += 1;
- if(lista[cent]==*num)
- {
- cont += 1;
- posic = (cent+1);
- printf("\n>>%d\n",posic);
- }
- return cont;
- }
- int Magico(int *n ,int matriz[b][b])
- {
- int i,j;
- int suma_columnas[*n],suma_filas[*n];
- int suma_diagonal_principal=0,suma_diagonal_secundaria=0,contador=2;
- for (i=0 ; i<*n ; i++)
- {
- suma_filas[i]=0;
- suma_columnas[i]=0;
- }
- contador+=2;
- for(i=0;i< *n;i++)
- {
- contador+=2;
- for(j=0;j< *n;j++)
- {
- contador++;
- if(i==j)
- {
- suma_diagonal_principal+=matriz[i][j];
- contador++;
- }
- contador++;
- if(i+j==*n-1)
- {
- suma_diagonal_secundaria+=matriz[i][j];
- contador++;
- }
- suma_filas[i] += matriz[i][j];
- suma_columnas[j]+= matriz[i][j];
- contador+= 2;
- }
- }
- contador+=2;
- for(i=0 ; i<*n ; i++)
- {
- contador+=2;
- for(j=0;j<*n;j++)
- {
- contador+=3;
- if((suma_columnas[i]!= suma_columnas[j])||(suma_filas[i]!= suma_filas[j])||(suma_columnas[i]!=suma_filas[j]) )
- {
- printf("el cuadrado no es magico\n");
- contador++;
- return contador;
- }
- }
- }
- contador+=2;
- if((suma_diagonal_secundaria==suma_diagonal_principal) && (suma_diagonal_principal==suma_columnas[0]))
- {
- printf("el cuadrado es magico\n");
- contador++;
- return contador;
- }
- else
- {
- printf("el cuadrado no es magico\n");
- contador++;
- return contador;
- }
- }
- int fibonacci (int n, int *cont)
- {
- *cont+=3;
- if(n==1 || n==0)
- {
- *cont += 2;
- return n;
- }
- else
- {
- *cont += 3;
- return (fibonacci(n-1,cont) + fibonacci(n-2,cont));
- }
- }
- int llenar1 (int *largo,FILE *arch)
- {
- int i;
- int *matriz = malloc(*largo * sizeof(int));
- for(i=0; i<*largo ; i++)
- {
- fscanf(arch,"%d",&matriz[i]);
- printf("%d ", matriz[i]);
- }
- return Suma(largo,matriz);
- }
- int llenar2 (int *largo,FILE *arch)
- {
- int i;
- int *matriz = malloc(*largo * sizeof(int));
- int *aux = malloc(sizeof(int));
- fscanf(arch,"%d",aux);
- for(i=0; i<*largo ; i++)
- {
- fscanf(arch,"%d",&matriz[i]);
- printf("%d ", matriz[i]);
- }
- return bbinaria(largo,matriz,aux);
- }
- int llenarcuadrada (int *largo,FILE *arch)
- {
- b=*largo;
- int i, j;
- int matriz[b][b];
- for(i=0; i<b ; i++)
- for(j=0; j<b ; j++)
- {
- fscanf(arch,"%d", &matriz[i][j]);
- if(j==b-1)
- printf(" %d\t\n", matriz[i][j]);
- else
- printf("%d\t ", matriz[i][j]);
- }
- return Magico(largo,matriz);
- }
- int escribir (int tamano[][a], int instrucciones[][a])
- {
- int i,j;
- FILE *out;
- out=fopen("salida.txt","w");
- if(out==NULL)
- {
- printf("El archivo no se abrio como corresponde\n");
- return 0;
- }
- for(i=0 ; i<4 ; i++)
- {
- fprintf(out,"Algoritmo %d\n",i+1);
- fprintf(out,"Tamaño\t");
- fprintf(out,"Nº medio Intrucciones\t\n");
- for(j=0 ; j<a ; j++)
- {
- fprintf(out,"%d\t",tamano[i][j]);
- fprintf(out,"%d\t\n",instrucciones[i][j]);
- }
- }
- fclose(out);
- return 0;
- }
- void grafico (int tamano[][a], int instrucciones[][a])
- {
- int i,j,h=0,max=0,c,cero=0,barra;
- initscr();
- for(i=0 ; i<a ; i++)
- {
- refresh();
- erase();
- if(has_colors())
- start_color();
- init_pair(1,COLOR_WHITE,COLOR_BLUE);
- init_pair(2,COLOR_BLACK,COLOR_WHITE);
- init_pair(3,COLOR_RED,COLOR_CYAN);
- init_pair(4,COLOR_RED,COLOR_RED);
- init_pair(5,COLOR_RED,COLOR_YELLOW);
- init_pair(6,COLOR_RED,COLOR_GREEN);
- bkgd(COLOR_PAIR(1));
- attron(COLOR_PAIR(2));
- for(j=0 ; j<4 ; j++)
- if(max<instrucciones[j][i])
- max=instrucciones[j][i];
- move(2,35);
- printw("Grafico %d",i+1);
- c=max/10;
- max=max+c;
- move(4,4);
- for(j=0 ; j<=11 ; j++)
- {
- move(4+j,4);
- printw("%9.0d",max);
- max=max-c;
- if(max<=0)
- {
- move(5+j,4);
- printw("%7.0d 0",cero);
- }
- }
- move(17,4);
- for(j=1 ; j<=74 ; j++)
- {
- if((j>=14)&&(j<=24))
- {
- attron(COLOR_PAIR(3));
- barra=instrucciones[0][i];
- if(barra-c<0)
- h=2;
- while(barra>0)
- {
- h++;
- barra=barra-c;
- }
- for( ;h >0 ;h--)
- {
- move(18-h,3+j);
- printw(" ");
- }
- attron(COLOR_PAIR(2));
- }
- else
- {
- if(j>=29&&j<=39)
- {
- attron(COLOR_PAIR(6));
- barra=instrucciones[1][i];
- if(barra-c<0)
- h=2;
- while(barra>0)
- {
- h++;
- barra=barra-c;
- }
- for( ;h >0 ;h--)
- {
- move(18-h,3+j);
- printw(" ");
- }
- attron(COLOR_PAIR(2));
- }
- else if(j>=44&&j<=54)
- {
- attron(COLOR_PAIR(4));
- barra=instrucciones[2][i];
- if(barra-c<0)
- h=2;
- while(barra>0)
- {
- h++;
- barra=barra-c;
- }
- for( ;h >0 ;h--)
- {
- move(18-h,3+j);
- printw(" ");
- }
- attron(COLOR_PAIR(2));
- }
- else
- {
- if(j>=59&&j<=69)
- {
- attron(COLOR_PAIR(5));
- barra=instrucciones[3][i];
- while(barra>0)
- {
- h++;
- barra=barra-c;
- }
- for( ;h >0 ;h--)
- {
- move(18-h,3+j);
- printw(" ");
- }
- attron(COLOR_PAIR(2));
- }
- else
- printw(" ");
- }
- }
- }
- move(21,3);
- attron(COLOR_PAIR(3));
- printw(" ");
- attroff(COLOR_PAIR(3));
- attron(COLOR_PAIR(2));
- printw(" Algoritmo 1 (%d)",instrucciones[0][i]);
- attroff(COLOR_PAIR(2));
- move(21,23);
- attron(COLOR_PAIR(6));
- printw(" ");
- attroff(COLOR_PAIR(6));
- attron(COLOR_PAIR(2));
- printw(" Algoritmo 2 (%d)",instrucciones[1][i]);
- attroff(COLOR_PAIR(2));
- move(21,43);
- attron(COLOR_PAIR(4));
- printw(" ");
- attroff(COLOR_PAIR(4));
- attron(COLOR_PAIR(2));
- printw(" Algoritmo 3 (%d)",instrucciones[2][i]);
- attroff(COLOR_PAIR(2));
- move(21,63);
- attron(COLOR_PAIR(5));
- printw(" ");
- attroff(COLOR_PAIR(5));
- attron(COLOR_PAIR(2));
- printw(" Algoritmo 4 (%d)",instrucciones[3][i]);
- move(24,1);
- printw("->Aprete la tecla 'Enter' para ver el proximo grafico");
- getch();
- }
- getch();
- endwin();
- }
- void Leer (FILE *arch)
- {
- int i,n=0,*elem;
- int tamano[4][a];
- int instrucciones[4][a];
- for(i=0 ; i<a ; i++)
- {
- elem=malloc(sizeof(int));
- fscanf(arch,"%d",elem);
- tamano[0][i]=*elem;
- instrucciones[0][i]=llenar1(elem,arch);
- fscanf(arch,"%d",elem);
- tamano[1][i]=*elem;
- instrucciones[1][i]=llenarcuadrada(elem,arch);
- fscanf(arch,"%d", elem);
- tamano[2][i]=*elem;
- instrucciones[2][i]=llenar2(elem,arch);
- fscanf(arch,"%d", elem);
- tamano[3][i]=*elem;
- printf(">>%d\n\n",fibonacci(*elem-1,&n));
- instrucciones[3][i]=n;
- }
- escribir(tamano,instrucciones);
- grafico(tamano,instrucciones);
- }
- int main ()
- {
- FILE *arch;
- arch=fopen("entrada.txt","r");
- if(arch==NULL)
- {
- printf("El archivo no se abrio como corresponde\n");
- return 0;
- }
- fscanf(arch,"%d", &a);
- Leer(arch);
- fclose(arch);
- return 0;
- }
Add Comment
Please, Sign In to add comment