Advertisement
zwastik

tarea edd rev3

Dec 19th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.59 KB | None | 0 0
  1. #include "gnuplot_i.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6.  
  7.  
  8. int Algoritmo1(int a, int* calg1)
  9. {
  10.       (*calg1)++;
  11.     if (a%7==0)
  12.      {
  13.         return true;
  14.      }
  15.     else
  16.      {
  17.         return false;
  18.      }
  19. }
  20.  
  21. int Algoritmo2(char a, char b[], int* calg2)
  22. {
  23.     int x,y,count;
  24.  
  25.     count=0;
  26.      (*calg2)++;
  27.  
  28.     y=strlen(b);
  29.      (*calg2)++;
  30.  
  31.     for (x=0;x<y;x++)
  32.     {
  33.      *calg2 = *calg2 + 3 ;
  34.         if (a==b[x])
  35.         {
  36.             count++;
  37.                 *calg2 = *calg2 + 2 ;
  38.         }
  39.     }
  40.  
  41. return count;
  42. }
  43.  
  44. int Algoritmo3(int n, int m1[][n], int m2[][n], int* calg3)
  45. {
  46.     int x,y,z,a,b;
  47.  
  48.     for (x=0;x<n;x++)
  49.     {
  50.          *calg3 = *calg3 + 3;
  51.         for (y=0;y<n;y++)
  52.         {
  53.             a=0;
  54.                 *calg3 = *calg3 + 4;
  55.  
  56.             for (z=0;z<n;z++)
  57.             {
  58.                 b=m1[z][x]*m2[y][z];
  59.  
  60.                 a=a+b;
  61.                     *calg3 = *calg3 + 5;
  62.             }
  63.  
  64.         }
  65.  
  66.     }
  67. return(0);
  68. }
  69.  
  70.  
  71. int Algoritmo4(int a,int b, int* calg4)
  72. {
  73.     if (a==0)
  74.     {
  75.         (*calg4)++;
  76.         return b;
  77.  
  78.     }
  79.  
  80.     if (b==0)
  81.     {
  82.  
  83.         *calg4 = *calg4 + 2;
  84.  
  85.         return a;
  86.     }
  87.  
  88.           *calg4 = *calg4 + 2;
  89.  
  90. return Algoritmo4(a-1,b, calg4)+Algoritmo4(a,b-1, calg4);
  91. }
  92.  
  93. int Algoritmo5(int a,int b,char c[a][b], int* calg5)
  94. {
  95.  
  96.     int x,y=0,v=0;
  97.     *calg5 = *calg5 + 2;
  98.  
  99.     for (x=0;x<a;x++)
  100.     {
  101.         y=strlen(&c[x][0]);
  102.          *calg5 = *calg5 + 4;
  103.  
  104.         if (y>v)
  105.         {
  106.             v=y;
  107.                 *calg5 = *calg5 + 2;
  108.         }
  109.     }
  110.  
  111.     char buff[v];
  112.  
  113.     for (x=0;x<a-1;x++)
  114.     {
  115.           *calg5 = *calg5 + 3;
  116.  
  117.         for (y=0;y<a-1;y++)
  118.         {
  119.                 *calg5 = *calg5 + 3;
  120.  
  121.             if (strcmp(&c[y][0],&c[y+1][0])>0)
  122.             {
  123.                     (*calg5)++;
  124.                 strcpy(buff,&c[y][0]);
  125.                 strcpy(&c[y][0],&c[y+1][0]);
  126.                 strcpy(&c[y+1][0],buff);
  127.             }
  128.         }
  129.     }
  130. return 0;
  131. }
  132.  
  133.  
  134. #define MAXCHARS 100 // Máximo ancho de una línea.
  135.  
  136. void borrarCR(char* str){
  137.     int i;
  138.  
  139.     for(i = 0; str[i] != '\n'; i++)
  140.         ;
  141.     str[i] = ' ';
  142. }
  143.  
  144. int main()
  145. {
  146.      int calg1=0, calg2=0, calg3=0, calg4=0, calg5=0;
  147.      int k,r;
  148.     char buff[100],buff1[30][30],esp[] = " ",x;
  149.  
  150.     char *par=NULL;
  151.      char cr = '\n';
  152.  
  153.     int a,it=0,c=0,c1,c2,c3,n,m,i;
  154.  
  155.     FILE *archivo;
  156.  
  157.     archivo = fopen("Entrada.txt", "rt");
  158.  
  159.      fseek(archivo, 0L, SEEK_SET);
  160.      int casos = getc(archivo) - 48;
  161.      rewind(archivo);
  162.  
  163.      char textosalida[][MAXCHARS] = {
  164.          "\n\nAlgoritmo 1\nNúmero                      Nº de Instrucciones\n",\
  165.          "\n\nAlgoritmo 2\nLetra-Palabra                Nº de Instrucciones\n",\
  166.          "\n\nAlgoritmo 3\nOrden                    Nº de Instrucciones\n",\
  167.          "\n\nAlgoritmo 4\nNúmeros                 Nº de Instrucciones\n",\
  168.          "\n\nAlgoritmo 5\nTamaño                  Nº de Instrucciones\n"};
  169.  
  170.    char** datos = malloc(casos*5*sizeof(char*));
  171.  
  172.     int j;
  173.     char gran_espacio[] = "                     ";
  174.     char instrucciones[10];
  175.     memset(instrucciones, 0, 10);
  176.  
  177.     for(j = 0; j < casos*5; j++)
  178.         datos[j] = malloc(MAXCHARS*sizeof(char));
  179.  
  180.     j = 0;
  181.  
  182.     while (fgets(buff,100,archivo)!=NULL)
  183.     {
  184.             if (it==0)
  185.             {
  186.                 a=atoi(buff);
  187.             }
  188.  
  189.             if (it-1==5*a)
  190.             {
  191.                 break;
  192.             }
  193.  
  194.             it++;
  195.  
  196.              if (c>5)
  197.             {
  198.                 c=1;
  199.             }
  200.  
  201.             switch( c )
  202.  
  203.             {
  204.                 case 1:
  205.                 {
  206.  
  207.                           strcpy(datos[j], buff);
  208.                           strcat(datos[j], gran_espacio);
  209.  
  210.                     Algoritmo1(atoi(buff), &calg1);
  211.  
  212.                           memset(instrucciones, 0, 10);
  213.                           sprintf(instrucciones, "%d", calg1);
  214.                           strcat(instrucciones, &cr);
  215.                           strcat(datos[j], instrucciones);
  216.                           borrarCR(datos[j]);
  217.                           j++;
  218.                     calg1=0;
  219.                     break;
  220.  
  221.                 }
  222.  
  223.                 case 2 :
  224.                 {
  225.                          strcpy(datos[j], buff);
  226.                          strcat(datos[j], gran_espacio);
  227.  
  228.                     c1=0;
  229.  
  230.                     par = strtok( buff, esp );
  231.  
  232.                     while( par != NULL ) {
  233.  
  234.                         if (c1==0)
  235.                         {
  236.                             x=par[0];
  237.  
  238.                             c1=1;
  239.                         }
  240.  
  241.                         else
  242.                         {
  243.                             Algoritmo2(x, par, &calg2);
  244.                                      memset(instrucciones, 0, 10);
  245.                                      sprintf(instrucciones, "%d", calg2);
  246.                                      strcat(instrucciones, &cr);
  247.                                      strcat(datos[j], instrucciones);
  248.                                      borrarCR(datos[j]);
  249.                                      j++;
  250.                         }
  251.  
  252.                         par = strtok( NULL, esp );
  253.                     }
  254.                     calg2=0;
  255.                     break;
  256.                 }
  257.  
  258.                 case 3 :
  259.                 {
  260.                          strncpy(datos[j], buff, 2);
  261.                          strcat(datos[j], gran_espacio);
  262.  
  263.                     c1=0;
  264.  
  265.                     c2=0;
  266.  
  267.                     c3=0;
  268.  
  269.                     par = strtok( buff, esp );
  270.  
  271.                     n=atoi(par);
  272.  
  273.                     int ma1[n][n],ma2[n][n];
  274.  
  275.                     while( par != NULL ) {
  276.  
  277.                         if (c1==0)
  278.                         {
  279.                             //Salto
  280.                         }
  281.  
  282.                         else
  283.                         {
  284.  
  285.                             if (c1-1<(n*n))
  286.                             {
  287.  
  288.                                 ma1[c2][c3]=atoi(par);
  289.  
  290.                                 c2++;
  291.  
  292.                                 if (c2==n)
  293.                                 {
  294.                                     c2=0;
  295.  
  296.                                     c3++;
  297.                                 }
  298.  
  299.                                 if (c1==n*n)
  300.                                 {
  301.                                     c2=0;
  302.  
  303.                                     c3=0;
  304.                                 }
  305.                             }
  306.  
  307.                             else
  308.                             {
  309.  
  310.                                 ma2[c2][c3]=atoi(par);
  311.  
  312.                                 c2++;
  313.  
  314.                                 if (c2==n)
  315.                                 {
  316.                                     c2=0;
  317.  
  318.                                     c3++;
  319.                                 }
  320.  
  321.                             }
  322.                         }
  323.  
  324.                     par = strtok( NULL, esp );
  325.  
  326.                     c1++;
  327.  
  328.                     }
  329.  
  330.                     Algoritmo3(n,ma1,ma2, &calg3);
  331.                             memset(instrucciones, 0, 10);
  332.                             sprintf(instrucciones, "%d", calg3);
  333.                              strcat(instrucciones, &cr);
  334.                              strcat(datos[j], instrucciones);
  335. //                           borrarCR(datos[j]);
  336.                              j++;
  337.                     calg3=0;
  338.                     break;
  339.                 }
  340.  
  341.                 case 4 :
  342.                 {
  343.                         strcpy(datos[j], buff);
  344.                         strcat(datos[j], gran_espacio);
  345.  
  346.                     c1=0;
  347.  
  348.                     par = strtok( buff, esp );
  349.  
  350.                     while( par != NULL ) {
  351.  
  352.                         if (c1==0)
  353.                         {
  354.                             c2=atoi(par);
  355.                         }
  356.  
  357.                         else
  358.                         {
  359.                             c3=atoi(par);
  360.                         }
  361.  
  362.                         par = strtok( NULL, esp );
  363.  
  364.                         c1++;
  365.                     }
  366.                     Algoritmo4(c2,c3,&calg4);
  367.                             memset(instrucciones, 0, 10);
  368.                             sprintf(instrucciones, "%d", calg4);
  369.                              strcat(instrucciones, &cr);
  370.                              strcat(datos[j], instrucciones);
  371.                              borrarCR(datos[j]);
  372.                              j++;
  373.                     calg4=0;
  374.                     break;
  375.                 }
  376.  
  377.                 case 5 :
  378.                 {
  379.                          strcpy(datos[j], buff);
  380.                          strcat(datos[j], gran_espacio);
  381.  
  382.                     c1=0;
  383.  
  384.                     c3=0;
  385.  
  386.                     par = strtok( buff, esp );
  387.  
  388.                     while( par != NULL ){
  389.  
  390.                         if (c1==0)
  391.  
  392.                         {
  393.                             c2=atoi(par);
  394.  
  395.                             m=0;
  396.                         }
  397.  
  398.                         else
  399.                         {
  400.  
  401.                             if (strlen(par)>c3)
  402.                             {
  403.                                 c3=strlen(par);
  404.                             }
  405.  
  406.  
  407.  
  408.                             strcpy(&buff1[m][0],par);
  409.  
  410.                             m++;
  411.                         }
  412.  
  413.                         par = strtok( NULL, esp );
  414.  
  415.                         c1++;
  416.  
  417.                     }
  418.  
  419.                     char strs[c2][c3+1];
  420.  
  421.                     for (i=0;i<m;i++)
  422.                     {
  423.  
  424.                         strcpy(&strs[i][0],&buff1[i][0]);
  425.  
  426.                     }
  427.  
  428.                     Algoritmo5(c2,c3+1,strs, &calg5);
  429.                             memset(instrucciones, 0, 10);
  430.                             sprintf(instrucciones, "%d", calg5);
  431.                              strcat(instrucciones, &cr);
  432.                              strcat(datos[j], instrucciones);
  433.                              borrarCR(datos[j]);
  434.                              j++;
  435.                             calg5=0;
  436.  
  437.                 break;
  438.                 }
  439.             }
  440.  
  441.             c++;
  442.        }
  443.     char plot1[] = "1 1\n2 39\n3 62\n4 85798\n5 37";
  444.     char plot2[] = "1 1\n2 37\n3 180\n4 8\n5 59";
  445.     // Reordenar datos
  446.      char** datos_ordn = malloc(casos*5*sizeof(char*));
  447.  
  448.      if(casos % 2 != 0)
  449.          for(i = 0, j = 0, k= 1, r = 5; i <= casos*5 - 1; i++, j++, k+=2, r++){
  450.              datos_ordn[i*2] = datos[j];
  451.              datos_ordn[k] = datos[r];
  452.          }
  453.      else
  454.          for(i = 0, j = 0, k= 1, r = 5; i <= casos*5 - 2; i++, j++, k+=2, r++){
  455.              datos_ordn[i*2] = datos[j];
  456.              datos_ordn[k] = datos[r];
  457.          }
  458.  
  459.  
  460.             // printf("i = %d, j = %d, k = %d\n", i, j, k);
  461.              //puts(datos_ordn[i]);
  462.  
  463.  
  464.      fclose(archivo);
  465.  
  466.      /*FILE *ploteo;
  467.      gnuplot_ctrl* h ;
  468.      h = gnuplot_init() ;
  469.  
  470.      gnuplot_setstyle(h, "boxes") ;
  471.      gnuplot_set_xlabel(h, "Algorirmos") ;
  472.      gnuplot_set_ylabel(h, "Instrucciones") ;
  473.      gnuplot_cmd(h, "set style data histograms");
  474.      gnuplot_cmd(h, "set boxwidth 1 relative");
  475.      gnuplot_cmd(h, "set style fill solid 1.0 border -1");
  476.  
  477.      ploteo = fopen("plot.txt", "w");
  478.  
  479.      fputs(plot1, ploteo);
  480.      gnuplot_cmd(h, "plot \"plot.txt\"");
  481.      gnuplot_close(h);
  482.      fclose(ploteo);*/
  483.  
  484.      FILE *salida;
  485.      salida = fopen("Salida.txt", "w");
  486.  
  487.      for(i = 0, j = 0; i < 5; i++){
  488.          fputs(textosalida[i], salida);
  489.          for(k = 0;  k < 2; j++, k++){
  490.              fputs(datos_ordn[j], salida);
  491. //          printf("%s",datos_ordn[j]);
  492.          }
  493.  
  494.      }
  495.  
  496.     fclose(salida);
  497.  
  498. //   for(j = 0; j < casos*5; j++)
  499. //       puts(datos_ordn[j]);
  500.  
  501.      for(j = 0; j < casos*5 - 1; j++){
  502.          free(datos[j]);
  503.      }
  504.      free(datos);
  505. return 0;
  506. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement