elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Guía rápida para descarga de herramientas gratuitas de seguridad y desinfección


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación C/C++ / Problemas listas en: 17 Mayo 2014, 19:50 pm
Estoy trabajando con listas dobles circulares, espero puedan ayudarme.
Lo que intento hacer es que al ir agregando se vayan ordenando los datos
Pero tengo problemas con el ultimo caso, como hago la comparacion y como voy avanzando si lo tengo que hacer de forma recursiva??


Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SALIR 0
  6.  
  7. typedef struct nodo
  8. {
  9.    int dato;
  10.    struct nodo *sig;
  11.    struct nodo *ant;
  12. }NODO;
  13.  
  14. typedef struct punteros
  15. {
  16.    struct nodo *inicio;
  17.    struct nodo *fin;
  18. }LISTA;
  19.  
  20. int agregar_ordenado(LISTA *aux, int dato)
  21. {
  22.    if(aux->inicio==NULL)//LISTA VACIA
  23.    {
  24.        NODO *nuevo=(NODO *)malloc(sizeof(NODO));
  25.        nuevo->dato=dato;
  26.        nuevo->ant=nuevo;
  27.        nuevo->sig=nuevo;
  28.        aux->fin=nuevo;
  29.        aux->inicio=nuevo;
  30.        return 1;
  31.    }
  32.    if(dato > aux->inicio->dato)//ES MAYOR AL INICIO
  33.    {
  34.        NODO *nuevo=(NODO *)malloc(sizeof(NODO));
  35.        nuevo->dato=dato;
  36.        nuevo->sig=aux->inicio;
  37.        aux->inicio->ant=nuevo;
  38.        aux->inicio=nuevo;
  39.        nuevo->ant=aux->fin;
  40.        aux->fin->sig=aux->inicio;
  41.        return 1;
  42.    }
  43.    if(dato < aux->fin->dato)//ES MENOR AL FIN
  44.    {
  45.        NODO *nuevo=(NODO *)malloc(sizeof(NODO));
  46.        nuevo->dato=dato;
  47.        nuevo->ant=aux->fin;in        aux->fin->sig=nuevo;
  48.        nuevo->sig=aux->inicio;
  49.        aux->inicio->ant=nuevo;
  50.        return 1;
  51.    }
  52.    if()//EN MEDIO DE LA LISTA
  53.    {
  54.  
  55.    }
  56.  
  57.    agregar_ordenado(aux->inicio->sig)
  58.  
  59. }
  60.  
  61.  
  62.  
  63. void imprimir(LISTA *aux)//DEL ULTIMO AREGADO AL PRIMERO
  64. {
  65. NODO *actual;
  66. actual = aux->inicio;
  67. while(actual != aux->fin)
  68.    {
  69. printf(" %i ",actual->dato);
  70. actual = actual->sig;
  71. }
  72. printf(" %i ",actual->dato);
  73. printf("\n");
  74. }
  75.  
  76.  
  77. int main()
  78. {
  79.    NODO *lista =NULL;
  80.    LISTA *aux=(LISTA *)malloc(sizeof(LISTA));
  81.    int op,dat,num;
  82.  
  83.    aux->inicio = NULL;
  84.    aux->fin = NULL;
  85.    do
  86.    {
  87.        printf("\n 1.Agregar elemento");
  88.        printf("\n 2.Imprimir lista");
  89.        printf("\n 0.salir");
  90.        scanf(" %d",&op);
  91.        switch(op)
  92.        {
  93.        case 0:
  94.            exit(0);
  95.        case 1:
  96.            printf("\n Ingrese dato: ");
  97.            scanf(" %d",&dat);
  98.            agregar_ordenado(aux,dat);
  99.            break;
  100.        case 2:
  101.            imprimir(aux);
  102.            break;
  103.        }
  104.  
  105.  
  106.    }while(op!=SALIR);
  107.    return 0;
  108. }
  109.  

    
2  Programación / Programación C/C++ / Ficheros y estructuras en: 18 Abril 2014, 23:29 pm
Saludos a toda la comunidad, tengo algunas dudas espero puedan auxiliarme con algunas.
Estoy trabajando en el siguiente codigo y me he topado con algunos problemas

En la parte de buscar por titulo a la hora de ingresar el titulo el programa se cierra, el problema sera con el fgets()? intente usar el gets() pero me pasaba lo mismo
Código:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

struct libros
{
    char id[30];
    char titulo[30];
    char autor[30];
    char editorial[30];
    int paginas;
}; libros registro [30];

int main()
{
int op,op2,i,j, cont=0,cant;
char temp[60];
char temp2[60],elim[60];
char *bus,*eli, *mos;
FILE *datalib;

        
         printf("\n Escoja su opcion: ");
         printf("\n 1 - Busqueda de registo de libro ");
         printf("\n 2 - Agregar nuevo registro de libro ");
         printf("\n 3 - Quitar registro de libro ");
         printf("\n 4 - Modificar registro de libro ");
         printf("\n 5 - Imprimir todos los registros ");
         printf("\n 0 - Salir");
         scanf("%d",&op);

        switch(op)
{
   case 0:
                    exit(0);
            break;
            case 1:
                    

printf("\n Escoja la opcion: ");
printf("\n 1 - Busqueda por ID: ");
printf("\n 2 - Busqueda por titulo: ");
printf("\n 0 - Salir: ");

scanf("%d",&op2);

datalib=fopen("libros.csv","r");
                            if(datalib==NULL){
                            printf("\n No se ha podido abrir el archivo");
                            exit(1);}
                            
switch (op2)
{
                    case 0:
                        exit(0);
                    case 1:
                            printf("\n Introduzca el ID del libro deseado:");
                            scanf("%s",&temp);
                            for(i=0;!feof(datalib);i++){
                                fgets(temp2,60,datalib);
                                bus=strtok(temp2,",");
                                if(strcmp(bus,temp)==0){
                                    printf("\n Registro encontrado");
                                    printf("\n Los datos son:");
                                    printf("\n\n");
                                    while(bus!=NULL){
                                        printf("\n  %s\n",bus);
                                        bus=strtok(NULL,",");}
                                    cont++;}
                            }
                            if(cont==0){
                                printf("\n ID no encontrada");}
                            system("PAUSE");

                        break;

                        case 2:
                            printf("\n Introduzca el titulo del libro deseado:");
                            fgets(temp,60,stdin);
                            for(i=0;!feof(datalib);i++){
                                fgets(temp2,60,datalib);
                                bus=strtok(temp2,",");
                                bus=strtok(NULL,",");
                                if(strcmp(bus,temp)==0){
                                    printf("\n Registro encontrado");
                                    printf("\n Los datos son:");
                                    printf("\n\n");

                                    while(bus!=NULL){
                                        printf("\n  %s\n",bus);
                                        bus=strtok(NULL,",");}
                                    cont++;}
                                    
                            }
                            if(cont==0){
                                printf("\n Titulo no encontrado");}
                                system("PAUSE");
                        break;

                        default:
                            printf("\nLa opcion no es correcta ");
                            printf("\n\n");
                            system("PAUSE");
                            system("cls");
break;
                        fclose(datalib);
                    }
            break;
system("PAUSE");
return 0;
}

El fichero tiene lo siguiente:

Citar

   DR453,INGENIERIA INDUSTRIAL,PHILIP HICKS,PATRIA,450
   RT567,TRASMISION DE DATOS Y REDES,FOROUZAN,MC GRAW HILL,920
   TJ567,REDES DE COMPUTADORAS,ANDREW TANENBAUM,PEARSON,870

3  Programación / Programación C/C++ / Contar nuemero de lineas de un fichero en: 17 Abril 2014, 23:26 pm
Estoy escribiendo un código usando ficheros, necesito saber cuantas lineas tiene el archivo para almacenar el espacio correspondiente. ¿¿como puedo contar el numero de lineas usando el fscanf??


A partir de ahí tengo que separar la informacion y guardarla en la estructura.
en el archivo tengo algo como esto
Código:
daniela,castillo,biologia,15
carlos,cervantes,fisica,14
diego,garcia,quimica,16

Algun consejo para hacerlo?? tenia pensado dividirlo usando la funcion strtok y despues ir copiando a cada variable pero aun no se muy bien como plantearlo.

 


Código
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. struct alumno
  6. {
  7.       char nombre[30];
  8.       char apellido[30];
  9.       char materia[30];
  10.       int edad;
  11. }; struct alumno reg*;
  12.  
  13.  
  14. int main()
  15. {
  16.     FILE *ptr;
  17.     char c[80];
  18.     char *b;
  19.     int x=0;
  20.  
  21.     ptr=fopen("alumno.csv","r+");
  22.  
  23.     while(!feof (ptr))
  24.     {
  25.            fscanf(ptr,"%[^\n]s",&c);
  26.            x++;
  27.     }
  28.  
  29.     reg=(struct alumno*)malloc(x*sizeof(struct alumno));
  30.     rewind(ptr);
  31.  
  32.     printf("numero de filas: %i",x);
  33.  
  34.      .
  35.      .
  36.      .
  37.      .
  38.      .
  39.      .
  40.      .
  41.      .
  42.      .    
  43.  
  44.     getch();
  45. }
4  Programación / Programación C/C++ / Struct y apuntadores en: 3 Abril 2014, 04:39 am
Saludos a toda la comunidad, tengo el siguiente programa que maneja una estructura y apartir de un numero n dado por el usuario reserva memoria para guardar todos los registros.
Tengo varias dudas al usar los apuntadores y la forma de acceder a las variables del struct. La forma en que lo estoy haciendo es correcta?  :huh: :huh:
No tengo muy claro la diferencia entre estas dos formas.
Código
  1. reg->editorial
Código
  1. reg.editorial

Código
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. struct libros
  6. {
  7.    char titulo[60];
  8.    char autor[60];
  9.    char editorial[60];
  10.    int num_paginas[60];
  11. } struct libros *reg;
  12.  
  13. void* cast(int m)
  14. {
  15.    return(malloc(m*sizeof(struct libros)));
  16. }
  17.  
  18. main ()
  19. {
  20.    int x;
  21.    int n;
  22.  
  23.    printf("Cuantos registros deseas ingresar?");
  24.    scanf("%d",n);
  25.  
  26.    reg=(struct libros*)cast(n);
  27.  
  28.    for(x=0;x<n;x++)
  29.    {
  30.        printf("Titulo: ");
  31.        gets(reg->titulo);
  32.        printf("Autor; ");
  33.        gets(reg->autor);
  34.        printf("Editorial: ");
  35.        gets(reg->editorial);
  36.        printf("Numero de paginas: ");
  37.        scanf("%d",reg->num_paginas);
  38.  
  39.        reg++;
  40.    }
  41.  
  42.     for(x=0;x<n;x++)
  43.    {
  44.        printf("Titulo: %s",reg->titulo);
  45.        printf("Autor; %s",reg->autor);
  46.        printf("Editorial: %s",reg->editorial);
  47.        printf("Numero de paginas: %s",reg->num_paginas);
  48.  
  49.        reg++;
  50.    }
  51.  
  52.  
  53.    getch();
  54. }
5  Programación / Programación C/C++ / Ayuda con programa en: 15 Marzo 2014, 06:03 am
Necesito crear un programa que a partir de una frase la imprima por palabras pero sin usar la funcion strtok, tengo lo siguiente pero no logro que imprima. Espero me puedan ayudar.
Por ejemplo si la frase es "hoy es viernes"
debe imprimir:
hoy
es
viernes

Código:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;


int mi_strtok(char* frase)
{
     int cont = 0;
     int x,c=0;
    
     while(*frase++ != '\0')
     {
        if(*frase!= ' ')
          {  
                
               cont++;
              
            }
        else
            {
                for(x=c;x<cont;x++)
                {
                   cout<<frase[x];
                   }
                   c=c+cont;
                  
                
            }
            
        }
            
     return 0;
}

main()
{
     char frase[500];
     cout<<"Ingrese la frase: ";
     gets(frase);  
     mi_strtok(frase);
    
     getch();
6  Programación / Programación C/C++ / Abrir y leer un archivo en: 23 Febrero 2014, 05:52 am
Hola, esta semana empece a trabajar con archivos. Tengo este codigo que crea un archivo
Código:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#include <fstream>
#include <cstring>
#include <sstream>

struct alu
{
char nombre[30];
int edad;
float altura;
};

alu vec[10];

main()
{
int x,y,orden,n;

printf("Cuantas personas desea ingresar?: ");
scanf("%i",&n);
printf("\n");
ofstream fsalida("Datos.alu.dat",ios::out|ios::binary);

for (x=0; x<n;x++)
{
printf("Introduce el nombre: ");
setbuf(stdin,NULL);
gets(vec[x].nombre);

printf("Introduce la edad: ");
scanf("%i",&vec[x].edad);

printf("Introduce la altura: ");
   cin>>vec[x].altura;

printf("\n\n");

fsalida.write(reinterpret_cast<char *>(&vec[x]),sizeof (alu));

}
fsalida.close();


ifstream fentrada ("Datos_alu.dat",ios::in |ios::binary);
for (x=0; x<n;x++)//Imprimir

{
fentrada.read(reinterpret_cast<char *>(&vec[x]),sizeof(alu));

printf ("Nombre: %s\n",vec[x].nombre);
printf ("Edad: %i\n",vec[x].edad);
printf ("Altura %.1f\n",vec[x].altura);
printf("\n\n");

}
fentrada.close();

getch();

}

Como puedo hacer otro programa pero que esta vez lea el archivo "Datos.alu.dat" sume las edades, imprima y lo guarde en el mismo archivo.
No tengo muy claro como hacer que abra el archivo  y lo lea para poder manejar las edades.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines