Advertisement
zwastik

tarea edd

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