|
Mostrar Mensajes
|
Páginas: [1] 2
|
1
|
Programación / Programación C/C++ / Re: Ordenamiento estructuras
|
en: 31 Mayo 2012, 20:05 pm
|
Por si a alguien le sirve, el problema estaba en el bucle for
for (j=0; j<i-1; j++){ for (k=0; k<i-j-1; k++){ if ((strcmp(p[j]->zk, p[j+1]->zk)) > 0) { aux = p[j]->zk; p[j]->zk = p[j+1]->zk; p[j+1]->zk = aux; } } }
hay que poner una "k" donde va la "j".
for (j=0; j<i-1; j++){ for (k=0; k<i-j-1; k++){ if ((strcmp(p[k]->zk, p[k+1]->zk)) > 0) { aux = p[k]->zk; p[k]->zk = p[k+1]->zk; p[k+1]->zk = aux; } } }
|
|
|
2
|
Programación / Programación C/C++ / Re: Ordenamiento estructuras
|
en: 31 Mayo 2012, 19:21 pm
|
#include <stdio.h> #include <string.h> #include <stdlib.h> #define Maxanzahl 100 #define Maxstrl 50
struct element { int nr; char *zk; };
int main(){ struct element *p[100]; char *aux; char *temp=NULL; int i=0,j,k; temp=(char *)malloc(Maxstrl*sizeof(char)); while(fgets(temp,Maxstrl,stdin)!=NULL){ if(temp[strlen(temp)-1]=='\n'){ temp[strlen(temp)-1]='\0'; } p[i]=malloc(sizeof(struct element)); p[i]->nr=i; if (p[i]!=NULL){ p[i]->zk = (char *)calloc(strlen(temp)+1,sizeof(char)); strcpy(p[i]->zk,temp); p[i]->nr=i; i++; } } for (j=0; j<i-1; j++){ for (k=0; k<i-j-1; k++){ if ((strcmp(p[j]->zk, p[j+1]->zk)) > 0) { aux = p[j]->zk; p[j]->zk = p[j+1]->zk; p[j+1]->zk = aux; } } }
printf("Salida:\n"); for (j=0; j<i ; j++){ printf("%d %s\n",p[j]->nr+1 , p[j]->zk); } for (j=0; j<i ; j++){ free(p[j]->zk); free(p[j]); } free(temp); return 0; system("pause"); }
|
|
|
5
|
Programación / Programación C/C++ / Re: Ordenamiento estructuras
|
en: 31 Mayo 2012, 19:06 pm
|
He corregido el codigo, pero sigue sin ordenar, por favor una ayudita..
#include <stdio.h> #include <string.h> #include <stdlib.h> #define Maxanzahl 100 #define Maxstrl 50
struct element { int nr; char *zk; };
int main(){ struct element *p[100]; char *aux; char *temp=NULL; int i=0,j,k; temp=(char *)malloc(Maxstrl*sizeof(char)); while(fgets(temp,Maxstrl,stdin)!=NULL){ if(temp[strlen(temp)-1]=='\n'){ temp[strlen(temp)-1]='\0'; } p=malloc(sizeof(struct element)); p->nr=i; if (p!=NULL){ p->zk = (char *)calloc(strlen(temp)+1,sizeof(char)); strcpy(p->zk,temp); p->nr=i; i++; } } for (j=0; j<i-1; j++){ for (k=0; k<i-j-1; k++){ if ((strcmp(p[j]->zk, p[j+1]->zk)) > 0) { aux = p[j]->zk; p[j]->zk = p[j+1]->zk; p[j+1]->zk = aux; } } }
printf("Salida:\n"); for (j=0; j<i ; j++){ printf("%d %s\n",p[j]->nr+1 , p[j]->zk); } for (j=0; j<i ; j++){ free(p[j]->zk); free(p[j]); } free(temp); return 0; system("pause"); }
|
|
|
6
|
Programación / Programación C/C++ / Ordenamiento estructuras
|
en: 31 Mayo 2012, 18:12 pm
|
Hola a todos, Estoy haciendo un programa en c que recibe por teclado cadenas de caracteres, que se guarda en una estructura y luego hay que ordenarla. No encuentro cual es el fallo, a ver si me pudieses ayudar, gracias de antemano.
Este es el codigo: [#include <stdio.h> #include <string.h> #include <stdlib.h> #define Maxanzahl 100 #define Maxstrl 50
struct element { int nr; char *zk; };
int main(){ struct element *p[100]; char *aux; char *temp=NULL; int i=0,j,k; temp=(char *)malloc(Maxstrl*sizeof(char)); while(fgets(temp,Maxstrl,stdin)!=NULL){ if(temp[strlen(temp)-1]=='\n'){ temp[strlen(temp)-1]=='\0'; } p=malloc(sizeof(struct element)); p->nr=i; if (p!=NULL){ p->zk = (char *)calloc(strlen(temp)+1,sizeof(char)); strcpy(p->zk,temp); p->nr=i; i++; } } for (j=0; j<i-1; j++){ for (k=0; k<i-j-1; k++){ if ((strcmp(p[j]->zk, p[j+1]->zk)) > 0) { aux = p[j]->zk; p[j]->zk = p[j+1]->zk; p[j+1]->zk = aux; } } }
printf("Salida:\n"); for (j=0; j<i ; j++){ printf("%d %s\n",p[j]->nr+1 , p[j]->zk); } for (j=0; j<i ; j++){ free(p[j]->zk); free(p[j]); } free(temp); return 0; system("pause"); }
]
|
|
|
7
|
Programación / Programación C/C++ / Memoria dinamica matriz
|
en: 24 Abril 2012, 23:42 pm
|
Hola tengo que hacer un programa en c que lea nombres desde teclado y los ordene alfabeticamente.
He hecho el programa para que acepte 10 nombres. El codigo es el siguiente :
codigo[#include <stdio.h> #include <stdlib.h> void main(void) { char **cadena, *aux; int i, j, n; char temp[20]; printf("%s%s\n", "Este programa ordena diez nombres", "introducidos por teclado."); printf("Introduzca los diez nombres:\n"); cadena = (char**)malloc(10*sizeof(char*)); for (i=0; i<10; i++) { printf("Nombre %d: ", i+1); scanf(" %[^\n]", temp); cadena = (char*)malloc((strlen(temp)+1)*sizeof(char)); strcpy(cadena, temp); } for (i=0; i<9; i++) for (j=i+1; j<10; j++) if ((strcmp(cadena, cadena[j])) > 0) { aux = cadena; cadena = cadena[j]; cadena[j] = aux; } printf("La lista ordenada es:\n"); for (i=0; i<10 ; i++) printf("%s\n", cadena); }]
Mi pregunta es, que es lo que tendria que cambiar para que el programa acepte "n" nombre hasta el caracter EOF. Lo he intentado de varias formas pero siempre me da warning por culpa de los punteros. Alguien me podria ayudar,? Muchas gracias
|
|
|
9
|
Programación / Programación C/C++ / Re: Ordenar matriz alfabeticamente
|
en: 24 Abril 2012, 05:19 am
|
Hola, de nuevo, gracias por tu respuesta.
He corregido algunas cosas y me da un warning : warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default] /usr/include/string.h:143:12: note: expected ‘const char *’ but argument is of type ‘char’
Este es el nuevo codigo :
#include <stdio.h> #include <string.h> #define Maxstrl 50 #define Maxanzahl 100
int main(){ char x[Maxanzahl][Maxstrl]; char str[Maxstrl]; char M[Maxanzahl][Maxstrl]; int i,j,min,m,k=0; for(i=0;i<Maxanzahl;i++){ for(j=0;j<Maxstrl;j++){ M[j]= ' '; }
} while(fgets(str,Maxstrl,stdin)!=NULL){ if(str[strlen(str)-1]=='\n'){ str[strlen(str)-1]='\0'; strcpy(M[k],str); k++; } } /* for(i=0;i<strlen(str);i++){ for(j=0;j<Maxstrl;j++){ printf("%c\t",M[j]); } printf("\n"); }*/ for(j=0;j<k;j++){ for(m=j+1;m<Maxstrl;m++){ if(strcmp(M[j][m],M[j][m+1])>0) strcpy(x,M[j][m]); strcpy(M[j][m],M[j][m+1]); strcpy(M[j][m+1],x); } for(i=0;i<k;i++){ for(j=0;j<Maxstrl;j++){ printf("%c\t",M[j]); } printf("\n"); } return 0; }
Lo que me pide el programa es que imprima la matriz en forma de matriz ordenada alfabteticamente, La copia de la cadena en la matriz se hace bien no? La comparacion es la que da el fallo creo
Gracias
EI: juntando mensajes.
Lo he corregido de nuevo, pero sigo dando error
#include <stdio.h> #include <string.h> #define Maxstrl 50 #define Maxanzahl 100
int main(){ char x[Maxanzahl][Maxstrl]; char str[Maxstrl]; char M[Maxanzahl][Maxstrl]; int i,j,min,m,k=0; for(i=0;i<Maxanzahl;i++){ for(j=0;j<Maxstrl;j++){ M[j]= ' '; }
} while(fgets(str,Maxstrl,stdin)!=NULL){ if(str[strlen(str)-1]=='\n'){ str[strlen(str)-1]='\0'; strcpy(M[k],str); k++; } } for(j=0;j<k;j++){ for(m=0;m<Maxstrl;m++){ if(strcmp(M[j][m],M[j][m+1])>0) strcpy(x,M[j][m]); strcpy(M[j][m],M[j][m+1]); strcpy(M[j][m+1],x); } for(i=0;i<k;i++){ for(j=0;j<Maxstrl;j++){ printf("%c\t",M[j]); } printf("\n"); } return 0; }
|
|
|
10
|
Programación / Programación C/C++ / programacion C en eclipse ubuntu
|
en: 24 Abril 2012, 04:00 am
|
Hola, he instalado el programa eclipse en mi kubuntu, instalé tambien los plugins para CDT. Cuando voy a ejecutar un programa, no me aparece "Local Application C/C++" en la configuracion de ejecutar, y no consigo saber el por qué Alguien tiene alguna de idea? Gracias
|
|
|
|
|
|
|