PROBLEMA CON CADENAS DE CARACTERES
Publicado por MarMed (1 intervención) el 06/06/2005 05:01:23
Tengo el siguiente código, q escribí para la escuela.
EL enunciado es el q abajo está descripto.
Tengo un problema en el punto 3) del enunciado, ya que cuando hago el ordenamiento (mediante burbujeo) no pasa nada.
¿En q me equivoqué?....
pruebenló y ayudenmé
gracias
[email protected]
/*
Name: ALUMNOS.C
Copyright: MarMed 2005
Author: Mart¡n Alejandro Mednik
Date: 26/05/05 16:57
Description:
Cargar la lista de alumnos de 3ø 4ø (apellido y nombres). Se pide:
1) Validar que el ingreso no supere la cantidad definida (40 caracteres)
2) Validar que en cada nombre no se ingresen n£meros.
3) Ordenar alfab‚ticamente la lista.
4) Imprimir de la siguiente forma:
APELLIDO Nombre Nombre
*/
#include<stdio.h>
#include<string.h>
int main(){
char alum[26][40], aux[127], c, aux2[127], tmp[127];
int MAX=40, l=0, ok=0, ok2=1, x=0, ALMN=0, CAR=0, i=0, buc;
for(x=0;x<=26;x++){
for(i=0;i<=40;i++){
alum[x][i]='';
}
}
for(x=0;x<=127;x++){
aux[x]=aux2[x]=tmp[x]='';
}
x=i=0;
clrscr();
for(ALMN=0;ALMN<=26;ALMN++) /*Pide el ingreso de nombres para cada alumno*/
{
printf("Ingrese el apellido y los nombres del %dø alumno: \n",ALMN+1);
/*Bucle de validaci¢n*/
do{
gets(aux);
l=strlen(aux);
if(l==0){
printf("El nombre no puede estar vac¡o!");
ok=0;
} else
if(l>MAX){
printf("El nombre no puede tener m s de 40 caracteres!");
ok=0;
}
else {
for(x=0;x<=l;x++){
if((aux[x]=='0')||(aux[x]=='1')||(aux[x]=='2')||
(aux[x]=='3')||(aux[x]=='4')||(aux[x]=='5')||
(aux[x]=='6')||(aux[x]=='7')||(aux[x]=='8')||
(aux[x]=='9')){
ok2=0;
}
else ok=1;
}
}
if(ok2==0){
printf("No se puede ingresar n£meros!");
ok=0;
ok2=1;
}
if(ok==0) printf("\nIngrese nuevamente el nombre.\n\n");
}while(ok==0);
/*Fin del bucle de validaci¢n*/
/* Guarda en la matriz alum los valores del vector aux.
Adem s agregar un punto ('.') al final del nombre. */
for(CAR=0;CAR<=l;CAR++){
alum[ALMN][CAR]=aux[CAR];
}
alum[ALMN][l]='.';
/* PARA MOSTRAR LOS NOMBRES
printf("\n");
for(CAR=0;CAR<=l;CAR++) printf("%c",alum[ALMN][CAR]);
*/
printf("\n\n");
}
/*ORDENA ALFABETICAMENTE*/
for(x=0;x<=25;x++){
for(i=0;i<26-x;i++){
l=0;
CAR=-1;
ALMN=i;
do{
l++;
CAR++;
}while(alum[ALMN][CAR]!='.');
for(buc=0;buc<=l;buc++){
aux[buc]=alum[ALMN][CAR];
}
CAR=-1;
l=0;
do{
l++;
CAR++;
}while(alum[ALMN+1][CAR]!='.');
for(buc=0;buc<=l;buc++){
aux2[buc]=alum[ALMN][CAR];
}
if((strcmp(aux,aux2)>0)){
strcpy(tmp,aux);
strcpy(aux,aux2);
strcpy(aux2,tmp);
}
}
}
/*MUESTRA SEGUN LA FORMA ESPECIFICADA.
ANDA BIEN SI SE RESPETA EL INGRESO ESPECIFICADO.*/
for(ALMN=0;ALMN<=26;ALMN++){
CAR=0;
c=alum[ALMN][CAR];
c=toupper(c);
printf("%c",c);
CAR++;
while(alum[ALMN][CAR]!=' '){
c=alum[ALMN][CAR];
c=toupper(c);
printf("%c",c);
CAR++;
}
for(x=1;x<=2;x++){
printf("%c",alum[ALMN][CAR]);
CAR++;
c=alum[ALMN][CAR];
c=toupper(c);
printf("%c",c);
CAR++;
if(alum[ALMN][CAR]=='.') break;
while((alum[ALMN][CAR]!=' ')){
c=alum[ALMN][CAR];
c=tolower(c);
printf("%c",c);
CAR++;
if(alum[ALMN][CAR]=='.') break;
}
}
printf("\n");
}
getch();
}
EL enunciado es el q abajo está descripto.
Tengo un problema en el punto 3) del enunciado, ya que cuando hago el ordenamiento (mediante burbujeo) no pasa nada.
¿En q me equivoqué?....
pruebenló y ayudenmé
gracias
[email protected]
/*
Name: ALUMNOS.C
Copyright: MarMed 2005
Author: Mart¡n Alejandro Mednik
Date: 26/05/05 16:57
Description:
Cargar la lista de alumnos de 3ø 4ø (apellido y nombres). Se pide:
1) Validar que el ingreso no supere la cantidad definida (40 caracteres)
2) Validar que en cada nombre no se ingresen n£meros.
3) Ordenar alfab‚ticamente la lista.
4) Imprimir de la siguiente forma:
APELLIDO Nombre Nombre
*/
#include<stdio.h>
#include<string.h>
int main(){
char alum[26][40], aux[127], c, aux2[127], tmp[127];
int MAX=40, l=0, ok=0, ok2=1, x=0, ALMN=0, CAR=0, i=0, buc;
for(x=0;x<=26;x++){
for(i=0;i<=40;i++){
alum[x][i]='';
}
}
for(x=0;x<=127;x++){
aux[x]=aux2[x]=tmp[x]='';
}
x=i=0;
clrscr();
for(ALMN=0;ALMN<=26;ALMN++) /*Pide el ingreso de nombres para cada alumno*/
{
printf("Ingrese el apellido y los nombres del %dø alumno: \n",ALMN+1);
/*Bucle de validaci¢n*/
do{
gets(aux);
l=strlen(aux);
if(l==0){
printf("El nombre no puede estar vac¡o!");
ok=0;
} else
if(l>MAX){
printf("El nombre no puede tener m s de 40 caracteres!");
ok=0;
}
else {
for(x=0;x<=l;x++){
if((aux[x]=='0')||(aux[x]=='1')||(aux[x]=='2')||
(aux[x]=='3')||(aux[x]=='4')||(aux[x]=='5')||
(aux[x]=='6')||(aux[x]=='7')||(aux[x]=='8')||
(aux[x]=='9')){
ok2=0;
}
else ok=1;
}
}
if(ok2==0){
printf("No se puede ingresar n£meros!");
ok=0;
ok2=1;
}
if(ok==0) printf("\nIngrese nuevamente el nombre.\n\n");
}while(ok==0);
/*Fin del bucle de validaci¢n*/
/* Guarda en la matriz alum los valores del vector aux.
Adem s agregar un punto ('.') al final del nombre. */
for(CAR=0;CAR<=l;CAR++){
alum[ALMN][CAR]=aux[CAR];
}
alum[ALMN][l]='.';
/* PARA MOSTRAR LOS NOMBRES
printf("\n");
for(CAR=0;CAR<=l;CAR++) printf("%c",alum[ALMN][CAR]);
*/
printf("\n\n");
}
/*ORDENA ALFABETICAMENTE*/
for(x=0;x<=25;x++){
for(i=0;i<26-x;i++){
l=0;
CAR=-1;
ALMN=i;
do{
l++;
CAR++;
}while(alum[ALMN][CAR]!='.');
for(buc=0;buc<=l;buc++){
aux[buc]=alum[ALMN][CAR];
}
CAR=-1;
l=0;
do{
l++;
CAR++;
}while(alum[ALMN+1][CAR]!='.');
for(buc=0;buc<=l;buc++){
aux2[buc]=alum[ALMN][CAR];
}
if((strcmp(aux,aux2)>0)){
strcpy(tmp,aux);
strcpy(aux,aux2);
strcpy(aux2,tmp);
}
}
}
/*MUESTRA SEGUN LA FORMA ESPECIFICADA.
ANDA BIEN SI SE RESPETA EL INGRESO ESPECIFICADO.*/
for(ALMN=0;ALMN<=26;ALMN++){
CAR=0;
c=alum[ALMN][CAR];
c=toupper(c);
printf("%c",c);
CAR++;
while(alum[ALMN][CAR]!=' '){
c=alum[ALMN][CAR];
c=toupper(c);
printf("%c",c);
CAR++;
}
for(x=1;x<=2;x++){
printf("%c",alum[ALMN][CAR]);
CAR++;
c=alum[ALMN][CAR];
c=toupper(c);
printf("%c",c);
CAR++;
if(alum[ALMN][CAR]=='.') break;
while((alum[ALMN][CAR]!=' ')){
c=alum[ALMN][CAR];
c=tolower(c);
printf("%c",c);
CAR++;
if(alum[ALMN][CAR]=='.') break;
}
}
printf("\n");
}
getch();
}
Valora esta pregunta


0