Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- int Algoritmo1(int a, int* calg1)
- {
- (*calg1)++;
- if (a%7==0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int Algoritmo2(char a, char b[], int* calg2)
- {
- int x,y,count;
- count=0;
- (*calg2)++;
- y=strlen(b);
- (*calg2)++;
- for (x=0;x<y;x++)
- {
- *calg2 = *calg2 + 3 ;
- if (a==b[x])
- {
- count++;
- *calg2 = *calg2 + 2 ;
- }
- }
- return count;
- }
- int Algoritmo3(int n, int m1[][n], int m2[][n], int* calg3)
- {
- int x,y,z,a,b;
- for (x=0;x<n;x++)
- {
- *calg3 = *calg3 + 3;
- for (y=0;y<n;y++)
- {
- a=0;
- *calg3 = *calg3 + 4;
- for (z=0;z<n;z++)
- {
- b=m1[z][x]*m2[y][z];
- a=a+b;
- *calg3 = *calg3 + 5;
- }
- }
- }
- return(0);
- }
- int Algoritmo4(int a,int b,int*point, int* calg4)
- {
- int c=*point;
- (*calg4)++;
- int*p;
- if (a==0)
- {
- c++;
- p=&c;
- *point=*p;
- *calg4 = *calg4 + 4;
- return b;
- }
- if (b==0)
- {
- c++;
- p=&c;
- *point=*p;
- *calg4 = *calg4 + 4;
- return a;
- }
- c++;
- p=&c;
- *point=*p;
- *calg4 = *calg4 + 3;
- return Algoritmo4(a-1,b,point, calg4)+Algoritmo4(a,b-1,point, calg4);
- }
- int Algoritmo5(int a,int b,char c[a][b], int* calg5)
- {
- int x,y=0,v=0;
- *calg5 = *calg5 + 2;
- for (x=0;x<a;x++)
- {
- y=strlen(&c[x][0]);
- *calg5 = *calg5 + 4;
- if (y>v)
- {
- v=y;
- *calg5 = *calg5 + 2;
- }
- }
- char buff[v];
- for (x=0;x<a-1;x++)
- {
- *calg5 = *calg5 + 3;
- for (y=0;y<a-1;y++)
- {
- *calg5 = *calg5 + 3;
- if (strcmp(&c[y][0],&c[y+1][0])>0)
- {
- (*calg5)++;
- strcpy(buff,&c[y][0]);
- strcpy(&c[y][0],&c[y+1][0]);
- strcpy(&c[y+1][0],buff);
- }
- }
- }
- return 0;
- }
- #define MAXCHARS 100 // Máximo ancho de una línea.
- void borrarCR(char* str){
- int i;
- for(i = 0; str[i] != '\n'; i++)
- ;
- str[i] = ' ';
- }
- int main()
- {
- int calg1=0, calg2=0, calg3=0, calg4=0, calg5=0; //contadores de instrucciones de cada algoritmo
- char buff[100],buff1[30][30],esp[] = " ",x;
- char *par=NULL;
- int a,it=0,c=0,c1,c2,c3,n,m,i,count=0;
- int*point=&count;
- FILE *archivo;
- archivo = fopen("Entrada.txt", "rt");
- fseek(archivo, 0L, SEEK_SET);
- int casos = fgetc(archivo); //no está leyendo bien los casos
- printf("%d\n",casos);
- rewind(archivo);
- char textosalida[][MAXCHARS] = {
- "Algoritmo 1\nNúmero Nº de Instrucciones\n",\
- "\nAlgoritmo 2\nLetra-Palabra Nº de Instrucciones\n",\
- "\nAlgoritmo 3\nOrden Nº de Instrucciones\n",\
- "\nAlgoritmo 4\nNúmeros Nº de Instrucciones\n",\
- "\nAlgoritmo 5\nTamaño Nº de Instrucciones\n"};
- char** datos = malloc(casos*5*sizeof(char*)); // los datos de cada algoritmo en un arreglo de cadenas
- int j;
- char gran_espacio[] = " ";
- char instrucciones[10];
- memset(instrucciones, 0, 10);
- for(j = 0; j < casos*5; j++)
- datos[j] = malloc(MAXCHARS*sizeof(char));
- j = 0;
- while (fgets(buff,100,archivo)!=NULL)
- {
- if (it==0)
- {
- a=atoi(buff);
- }
- if (it-1==5*a)
- {
- break;
- }
- it++;
- if (c>5)
- {
- c=1;
- }
- switch( c )
- {
- case 1:
- {
- strcpy(datos[j], buff);
- strcat(datos[j], gran_espacio);
- Algoritmo1(atoi(buff), &calg1);
- memset(instrucciones, 0, 10);
- sprintf(instrucciones, "%d", calg1);
- strcat(datos[j], instrucciones);
- borrarCR(datos[j]);
- j++;
- break;
- }
- case 2 :
- {
- strcpy(datos[j], buff);
- strcat(datos[j], gran_espacio);
- c1=0;
- par = strtok( buff, esp );
- while( par != NULL ) {
- if (c1==0)
- {
- x=par[0];
- c1=1;
- }
- else
- {
- Algoritmo2(x, par, &calg2);
- memset(instrucciones, 0, 10);
- sprintf(instrucciones, "%d", calg2);
- strcat(datos[j], instrucciones);
- borrarCR(datos[j]);
- j++;
- }
- par = strtok( NULL, esp );
- }
- break;
- }
- case 3 :
- {
- strncpy(datos[j], buff, 2);
- strcat(datos[j], gran_espacio);
- c1=0;
- c2=0;
- c3=0;
- par = strtok( buff, esp );
- n=atoi(par);
- int ma1[n][n],ma2[n][n];
- while( par != NULL ) {
- if (c1==0)
- {
- //Salto
- }
- else
- {
- if (c1-1<(n*n))
- {
- ma1[c2][c3]=atoi(par);
- c2++;
- if (c2==n)
- {
- c2=0;
- c3++;
- }
- if (c1==n*n)
- {
- c2=0;
- c3=0;
- }
- }
- else
- {
- ma2[c2][c3]=atoi(par);
- c2++;
- if (c2==n)
- {
- c2=0;
- c3++;
- }
- }
- }
- par = strtok( NULL, esp );
- c1++;
- }
- Algoritmo3(n,ma1,ma2, &calg3);
- memset(instrucciones, 0, 10);
- sprintf(instrucciones, "%d", calg3);
- strcat(datos[j], instrucciones);
- // borrarCR(datos[j]);
- j++;
- break;
- }
- case 4 :
- {
- strcpy(datos[j], buff);
- strcat(datos[j], gran_espacio);
- c1=0;
- par = strtok( buff, esp );
- while( par != NULL ) {
- if (c1==0)
- {
- c2=atoi(par);
- }
- else
- {
- c3=atoi(par);
- }
- par = strtok( NULL, esp );
- c1++;
- }
- Algoritmo4(c2,c3,point, &calg4);
- memset(instrucciones, 0, 10);
- sprintf(instrucciones, "%d", calg4);
- strcat(datos[j], instrucciones);
- borrarCR(datos[j]);
- j++;
- // printf("%d",count);
- // printf("%c",' ');
- count=0;
- break;
- }
- case 5 :
- {
- strcpy(datos[j], buff);
- strcat(datos[j], gran_espacio);
- c1=0;
- c3=0;
- par = strtok( buff, esp );
- while( par != NULL ){
- if (c1==0)
- {
- c2=atoi(par);
- m=0;
- }
- else
- {
- if (strlen(par)>c3)
- {
- c3=strlen(par);
- }
- strcpy(&buff1[m][0],par);
- m++;
- }
- par = strtok( NULL, esp );
- c1++;
- }
- char strs[c2][c3+1];
- for (i=0;i<m;i++)
- {
- strcpy(&strs[i][0],&buff1[i][0]);
- }
- Algoritmo5(c2,c3+1,strs, &calg5);
- memset(instrucciones, 0, 10);
- sprintf(instrucciones, "%d", calg5);
- strcat(datos[j], instrucciones);
- borrarCR(datos[j]);
- j++;
- break;
- }
- }
- c++;
- }
- FILE* salida;
- salida = fopen("Salida.txt", "w");
- for(j = 0, i = 0; j < casos*5; j++, i++){
- if( i == 5)
- i = 0;// Volver al inicio del array
- fputs(textosalida[i], salida);
- fputs(datos[j], salida);
- }
- fclose(salida);
- for(j = 0; j < casos*5; j++)
- free(datos[j]);
- free(datos);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement