Hola!

Registrándote como bakuno podrás publicar, compartir y comunicarte en privado con otros bakuos :D

Regístrame ya!

busquedas en C

batiguason_11

Bovino adicto
Desde
27 Jun 2009
Mensajes
703
que tal?
veran hubiese tenido que entreegar este programa para mañana pero esta lago mal, en fin no me quiero quedar con la duda así que me podrian apoyar diciendome que esta mal plz.
El problema esta en la función busqueda cuando comparo las cadenas y al devolver valores me podrian ayudar a corregir plz

Código:
                 [FONT=&quot]#include<stdio.h>[/FONT]
  [FONT=&quot]#include<string.h>[/FONT]
  [FONT=&quot]#define T 20[/FONT]
  [FONT=&quot]struct contacto {[/FONT]
  [FONT=&quot]     char nombre[50],nick[50],marca,telefono[50],dire[50];[/FONT]
  [FONT=&quot]     int edad;[/FONT]
  [FONT=&quot]};[/FONT]
  [FONT=&quot]struct contacto datos[T];[/FONT]
  [FONT=&quot]int busqueda(char bus[]) {[/FONT]
  [FONT=&quot]     int i,back=0,r;[/FONT]
  [FONT=&quot]     for (i=0;i<T;i++) {[/FONT]
  [FONT=&quot]          if (datos[i].marca=='o') {[/FONT]
  [FONT=&quot]               r=strcmp(bus,datos[i].nombre);[/FONT]
  [FONT=&quot]               if (r==0) {[/FONT]
  [FONT=&quot]                    back=i;[/FONT]
  [FONT=&quot]                    printf("\ni:%i",i);[/FONT]
  [FONT=&quot]                    break;[/FONT]
  [FONT=&quot]               }[/FONT]
  [FONT=&quot]          }[/FONT]
  [FONT=&quot]     }[/FONT]
  [FONT=&quot]     if (back!=i)[/FONT]
  [FONT=&quot]          back=-1;[/FONT]
  [FONT=&quot]     printf("back:%i",back);[/FONT]
  [FONT=&quot]     return back;[/FONT]
  [FONT=&quot]}[/FONT]
  [FONT=&quot]void insertar () {[/FONT]
  [FONT=&quot]     int i,x;[/FONT]
  [FONT=&quot]     char aux[50];[/FONT]
  [FONT=&quot]     for (i=0;i<T;i++) [/FONT]
  [FONT=&quot]          if (datos[i].marca=='v') {[/FONT]
  [FONT=&quot]               printf("\nNombre: ");[/FONT]
  [FONT=&quot]               gets(datos[i].nombre);[/FONT]
  [FONT=&quot]               fpurge(stdin);[/FONT]
  [FONT=&quot]               printf("\nEdad: ");[/FONT]
  [FONT=&quot]               scanf("%i",&datos[i].edad);[/FONT]
  [FONT=&quot]               fpurge(stdin);[/FONT]
  [FONT=&quot]               printf("\nsobrenombre: ");[/FONT]
  [FONT=&quot]               gets(datos[i].nick);[/FONT]
  [FONT=&quot]               fpurge(stdin);[/FONT]
  [FONT=&quot]               printf("\nTelefono: ");[/FONT]
  [FONT=&quot]               scanf("%s",datos[i].telefono);[/FONT]
  [FONT=&quot]               fpurge(stdin);[/FONT]
  [FONT=&quot]               printf("\nDireccion: ");[/FONT]
  [FONT=&quot]               gets(datos[i].dire);[/FONT]
  [FONT=&quot]               fpurge(stdin);[/FONT]
  [FONT=&quot]               datos[i].marca='o';[/FONT]
  [FONT=&quot]               printf("\n%c",datos[i].marca);[/FONT]
  [FONT=&quot]               break;[/FONT]
  [FONT=&quot]          }[/FONT]
  [FONT=&quot]}[/FONT]
  [FONT=&quot]void limpiar() {[/FONT]
  [FONT=&quot]     int i;[/FONT]
  [FONT=&quot]     for (i=0;i<T;i++)[/FONT]
  [FONT=&quot]          datos[i].marca='v';[/FONT]
  [FONT=&quot]}[/FONT]
  [FONT=&quot]int eliminar (char elim[]) {[/FONT]
  [FONT=&quot]     int i,x;[/FONT]
  [FONT=&quot]     if ((x=busqueda(elim))!=-1) {[/FONT]
  [FONT=&quot]          datos[x].marca='v';[/FONT]
  [FONT=&quot]          return x;[/FONT]
  [FONT=&quot]     }[/FONT]
  [FONT=&quot]     else[/FONT]
  [FONT=&quot]          return 0;[/FONT]
  [FONT=&quot]}[/FONT]
  [FONT=&quot]void menu (char *op) {[/FONT]
  [FONT=&quot]     printf("\n\t\tMenu");[/FONT]
  [FONT=&quot]     printf("\n\t1-insertar");[/FONT]
  [FONT=&quot]     printf("\n\t2-buscar");[/FONT]
  [FONT=&quot]     printf("\n\t3-eliminar");[/FONT]
  [FONT=&quot]     printf("\n\t4-salir");[/FONT]
  [FONT=&quot]     printf("\n\t\t\topc: ");[/FONT]
  [FONT=&quot]     *op=getchar();[/FONT]
  [FONT=&quot]     fpurge(stdin);[/FONT]
  [FONT=&quot]}[/FONT]
  [FONT=&quot]main () {[/FONT]
  [FONT=&quot]     char op,buscan[50];[/FONT]
  [FONT=&quot]     limpiar();[/FONT]
  [FONT=&quot]     do {[/FONT]
  [FONT=&quot]          menu(&op);[/FONT]
  [FONT=&quot]          switch (op) {[/FONT]
  [FONT=&quot]               case '1':[/FONT]
  [FONT=&quot]                    insertar();[/FONT]
  [FONT=&quot]                    break;[/FONT]
  [FONT=&quot]               case '2':[/FONT]
  [FONT=&quot]                    printf("\nA quien buscas? ");[/FONT]
  [FONT=&quot]                    gets(buscan);[/FONT]
  [FONT=&quot]                    fpurge(stdin);[/FONT]
  [FONT=&quot]                    if(busqueda(buscan)!=-1)[/FONT]
  [FONT=&quot]                         printf("\nContacto encontrado");[/FONT]
  [FONT=&quot]                    else[/FONT]
  [FONT=&quot]                         printf("\nContacto no encontrado");[/FONT]
  [FONT=&quot]                    break;[/FONT]
  [FONT=&quot]               case '3':[/FONT]
  [FONT=&quot]                    printf("\nA quien desea eliminar? ");[/FONT]
  [FONT=&quot]                    gets(buscan);[/FONT]
  [FONT=&quot]                    fpurge(stdin);[/FONT]
  [FONT=&quot]                    if (eliminar(buscan)!=0) [/FONT]
  [FONT=&quot]                         printf("\nContacto eliminado");[/FONT]
  [FONT=&quot]                    else[/FONT]
  [FONT=&quot]                         printf("\nContacto no encontrado");[/FONT]
  [FONT=&quot]                    break;[/FONT]
  [FONT=&quot]               case '4': printf("\nSaldra del programa\n");[/FONT]
  [FONT=&quot]                    break;[/FONT]
  [FONT=&quot]               default: printf("\nopcion no valida");[/FONT]
  [FONT=&quot]          }[/FONT]
  [FONT=&quot]     }while (op!='4');[/FONT]
  [FONT=&quot]}[/FONT]

Sin más los dejo por el momento y agradezo sus respuesta desde ya :D

P.s. uso el estandar ANSI
 
Volver
Arriba