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");
}
]
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");
}
Donde pone p->i o p=malloc, en verdad es p, pero no se pq no me lo pone bien.
p->i o p=malloc Es p i , y la i entre corchetes pero no me sale cuando la escribo por aki
#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");
}
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;
}
}
}