|
Mostrar Mensajes
|
Páginas: 1 2 [3] 4
|
21
|
Programación / Programación C/C++ / Re: Inserción de datos ABB con archivos
|
en: 27 Octubre 2016, 21:52 pm
|
using namespace std; typedef struct arbol{ int id; char nombre[20]; char fecha[10]; int existencia; struct arbol *izquierda,*derecha; }; arbol* insercion(arbol*,int,char[],char[],int); char menu(); void captura(arbol*); void inOrder(arbol *); void mostrar(arbol*,int,char[],char[],int); void validar_letras (char *p); char getchass (); void insert(int key, struct node **leaf); main(){ arbol *raiz=NULL; char op; do{ op=menu(); switch(op){ case '1':captura(raiz);break; case '2': printf("Inorden: "); printf("%d",raiz->id); //inOrder(raiz); break; case '3':break; case '4':break; } }while(op!='5'); return 0; } char menu(){ char op; system("cls"); printf("1.- Capturar arbol\n"); printf("2.- Imprimir arbol\n"); printf("3.- Actualizar existencias\n"); printf("4.- Status del producto\n"); printf("5.- Salir\n"); printf("Opcion: ");fflush(stdin); op=getch(); printf("\n"); return op; } void captura(arbol *r){ arbol *raiz=NULL; int i=1; int idd; char nombree[20]; char fechaa[10]; int existenciaa; FILE *ptr; ptr = fopen("150941_Aguilar_Av1-2.txt", "r"); do{ fscanf(ptr,"%d",&idd); fscanf(ptr,"%s",nombree); fscanf(ptr,"%s",fechaa); fscanf(ptr,"%d",&existenciaa); i++; insercion(raiz,idd,nombree,fechaa,existenciaa); }while(!feof(ptr)); fclose(ptr); printf("Registros guardados %d\n",i); getchar(); /* while(!feof(fichero)) fputc(fgetc(fichero), stdout); fclose(fichero); getchar();*/ } arbol* insercion(arbol *r,int id,char nombre[20],char fecha[10],int existencia){ if(r==NULL) { r=(arbol*)malloc(sizeof(arbol)); r->id=id; strcpy(r->nombre,nombre); strcpy(r->fecha,fecha); r->existencia=existencia; r->izquierda=NULL; r->derecha=NULL; return r; } else { if(r->id > id) r->izquierda = insercion(r->izquierda,id,nombre,fecha,existencia); else r->derecha = insercion(r->derecha,id,nombre,fecha,existencia); } } void inOrder(arbol *r){ if(r!=NULL){ inOrder(r->izquierda); printf("%d -> ",r->id); inOrder(r->derecha); } getch(); }
Trate de hacer distinto el codigo, se supone que ya esta dentro del arbol, pero a la hora de mostrar lo que hay en el no me corre, alguien puede detectar mi error? O la manera en que estoy guardando los datos en el arbol es incorrecto y en lugar de hacerlo mediante fichero, hacerlo manualmente?
|
|
|
22
|
Programación / Programación C/C++ / Inserción de datos ABB con archivos
|
en: 26 Octubre 2016, 23:11 pm
|
Tengo una duda al querer capturar los datos de un archivo y a la vez guardarlos en el árbol como podría hacerlo? 1 leche 20-12-2016 10 2 arroz 22/02/2017 8 3 Huevos 25/11/2016 8 4 frijoles 29/12/2017 12 5 maiz 31/03/2017 8 6 papas 20/03/2017 8 7 caramelos 08/09/2022 15 9 refresco 09/09/2019 18 10 harina 22/05/2018 11 Carne 10/04/2017 Esos son mis datos y mi código es: typedef struct arbol{ int id; char nombre; char fecha; int existencia; struct arbol *izquierda,*derecha; }arbol; int menu(); void captura(); main(){ arbol *raiz=NULL; char op; do{ op=menu(); switch(op){ case 1:captura();break; case 2:printf("b"); case 3:break; case 4:break; case 5:break; default:printf("Opcion invalida\n");system("pause");break; } }while(op!=5); getch(); } int menu(){ char op[2]; int opp; system("cls"); printf("1.- Capturar arbol\n"); printf("2.- Imprimir arbol\n"); printf("3.- Actualizar existencias\n"); printf("4.- Status del producto\n"); printf("5.- Salir\n"); printf("Opcion: "); validar_letras (op); opp=atoi(op); return opp; } void captura(){ FILE *fichero; fichero = fopen("150941_Aguilar_Av1-2.txt", "r"); while(!feof(fichero)) fputc(fgetc(fichero), stdout); rewind(fichero); while(!feof(fichero)) fputc(fgetc(fichero), stdout); fclose(fichero); getchar(); }
|
|
|
23
|
Programación / Programación C/C++ / Imprimir metodos en C++
|
en: 26 Octubre 2016, 03:14 am
|
Tengo esta duda a la hora de hacer el .cpp de Imprimir y a la hora de crear el main.cpp , no logro entenderlo del todo y no puedo. Registro.hpp #include<iostream> using namespace std; class Registro{ private: string **nombres; int fila; public: Registro(); ~Registro(); void setFila(int); int getFila(); void setNombres(string **,int); string** getNombres(); };
Registro.cpp #include "Registro.hpp" Registro::Registro(){ cout<<endl<<"Filas: "; cin>>fila; nombres = new string*[fila]; for(int i=0;i<fila;i++){ *(nombres+i)=new string[3]; } setNombres(nombres,fila); } Registro::~Registro(){ delete []nombres; } void Registro::setFila(int fila){ this->fila=fila; } int Registro::getFila(){ return fila; } void Registro::setNombres(string **nombres,int fila){ for(int i=0;i<fila;i++){ cin.ignore(256,'\n'); cout<<"Nombre: "; getline(cin, *(*(nombres+i)+0)); cout<<"Apellido: "; getline(cin, *(*(nombres+i)+1)); cout<<"Edad: "; getline(cin,*(*(nombres+i)+2)); } this->nombres=nombres; } string ** Registro::getNombres(){ return nombres; }
Imprimir.hpp #include<iomanip> using namespace std; class Imprimir{ public: void imprime(string **,int); };
Hasta ahora llevo eso, y no logro llevar al main todo eso.
|
|
|
24
|
Programación / Programación C/C++ / Re: Problema con registro y funciones
|
en: 20 Octubre 2016, 04:09 am
|
#include<stdlib.h> #include<iomanip> #include<iostream> using namespace std; typedef struct{ char nombre[20]; int notas; }Dato; int main(){ int n; Dato *_dato; cout<<"Cuantos estudiantes ingresaras: "; cin>>n; _dato =(Dato *)malloc(n *sizeof(Dato )); for(int i=0; i<n; i++){ cout<<"Nombre del estudiante:"; cin.getline((_dato+i)->nombre,20, '\n'); //gets(_dato[i].nombre); cout<<endl<<"Nota: "; cin>>(_dato+i)->notas; //scanf("%d",&_dato[i].edad); } for(int i=0; i<n; i++){ cout<<endl<<(*(_dato+i)).nombre; cout<<endl<<(*(_dato+i)).notas; } }
Se me borro todo lo que escribi ,pero te dejo el código mas simple, para que puedas analizarlo, cualquier cosa mándame mensaje. Elimina cosas al código, modifícalo para que veas como funciona.
|
|
|
25
|
Programación / Programación C/C++ / Re: Problema con registro y funciones
|
en: 20 Octubre 2016, 02:17 am
|
_dato =(Dato *)malloc(n *sizeof(Dato ));/*al ser una estructura dinámica, tu tienes que reservar memoria para esta es lo mismo que con los arrays tu tienes un int[5]; declarado, pero esto tu lo defines, no el usuario al reservar memoria es algo similar a int[n], con malloc y realloc asignas la memoria dinámica, no se si me entidas?*/ recibe(_dato,n); /*Esta es una función, que puedes invocar en el main, La función recibe la cantidad de estudiantes a registrar y los registra.*/ /*asi como reservas memoria, la tienes que liberar, y el usuario reservo memoria dinámica para la estructura*/ /*Forma parte de la librería conio.h */ /*limpia el buffer, para cuando reciba los nombres */ cin.getline((_dato+i)->nombre,20, '\n'); /*escaneas nombre la posición de i es lo mismo que escribir //gets(_dato[i].nombre);*/ cin>>(_dato+i)->notas; /*es lo mismo escaneas notas en la posición de i es lo mismo que escribir //scanf("%d",&_dato[i].edad); */
No se si me entiendas? Puedo explicarlo mejor si deseas, tu pregunta con confianza.
|
|
|
26
|
Programación / Programación C/C++ / Re: Problema con registro y funciones
|
en: 20 Octubre 2016, 00:31 am
|
#include<stdlib.h> #include<iomanip> #include<iostream> #include<conio.h> using namespace std; typedef struct{ char nombre[20]; int notas; }Dato; void recibe(Dato *,int); void imprime(Dato *,int); int main(){ int n; Dato *_dato; printf("Cantidad de estudiantes: "); cin>>n; _dato=(Dato *)malloc(n*sizeof(Dato)); recibe(_dato,n); imprime(_dato,n); free(_dato); getch(); } void recibe(Dato *_dato,int n){ for(int i=0; i<n; i++){ cout<<"Nombre del estudiante:"; fflush(stdin); cin.getline((_dato+i)->nombre,20, '\n'); //gets(_dato[i].nombre); cout<<endl<<"Nota: "; cin>>(_dato+i)->notas; //scanf("%d",&_dato[i].edad); } } void imprime(Dato *_dato,int n){ for(int i=0; i<n; i++){ cout<<endl<<(*(_dato+i)).nombre; cout<<endl<<(*(_dato+i)).notas; } }
Buscas hacer es algo asi no?
|
|
|
28
|
Programación / Programación C/C++ / Re: Ayuda para generar un ID automatico
|
en: 13 Octubre 2016, 06:12 am
|
En tu estructura solo defines artista, albulm y genero, el id lo declaras como variable local, y cada vez que preguntas la música aumentas en 1 el id. Y al imprimir muestras id, artista,albulm y genero. Tengo un código parecido por si ocupas mas ayuda
|
|
|
29
|
Programación / Programación C/C++ / Re: Puzzle 8 en C
|
en: 26 Abril 2016, 05:26 am
|
uy te falto las etiquetas geshi!!!
Me funciona pero al momento de cambiar los numeros por el 0, no lo hace en el orden que deberia lo hace cruzado y asi. Alguna ayuda?
|
|
|
30
|
Programación / Programación C/C++ / Re: Puzzle 8 en C
|
en: 26 Abril 2016, 03:57 am
|
Cambie el codigo y lo hice 3 x 3, pero al momento de validar los movimientos no logro hacer que se muevan correctamente. #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> void gotoxy(short x, short y){ COORD pos = {x , y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);} main() { int r, x, y, j, vm, k, v,i; int a[9], b[3][3],c[3][3]; srand (time(NULL)); for (i=0; i<=7; i++) { a[i] = rand()%(7+1); } r = 1; while (r == 1) { r = 0; for (i=0; i<=7; i++) { if (a[i]==0) a[i]++; for (j=i+1; j<=7; j++) { if (a[i] == a[j] && a[i] !=8) { a[i] = a[i] +1; r = 1; } if (a[i] == a[j] && a[i] ==8) { a[i] = a[i] -rand()%(7+1); r = 1; } }}} j=0; for (x=0; x<=2; x++) { for(y=0; y<=2; y++) {b[x][y]=a[j];j++; }} x=2; y=2; fflush(stdin); b[2][2]=0; c[0][0]=1; c[0][1]=2; c[0][2]=3; c[1][0]=4; c[1][1]=5; c[1][2]=6; c[2][0]=7; c[2][1]=8; c[2][2]=0; do{ ///////////////////IZQ//////////////////// gotoxy(1,2);printf("%c",201); gotoxy(1,3);printf("%c",186); gotoxy(1,4);printf("%c",204); gotoxy(1,5);printf("%c",186); gotoxy(1,6);printf("%c",204); gotoxy(1,7);printf("%c",186); gotoxy(1,8);printf("%c",200);
///////////////////Arriba//////////////////// gotoxy(2,2);printf("%c",205); gotoxy(3,2);printf("%c",205); gotoxy(4,2);printf("%c",203); gotoxy(5,2);printf("%c",205); gotoxy(6,2);printf("%c",205); gotoxy(7,2);printf("%c",203); gotoxy(8,2);printf("%c",205); gotoxy(9,2);printf("%c",205); gotoxy(10,2);printf("%c",187);
/////////////////linea 1/////////////////// gotoxy(4,3);printf("%c",186); gotoxy(4,5);printf("%c",186); gotoxy(4,7);printf("%c",186);
/////////////////linea 2/////////////////// gotoxy(7,3);printf("%c",186); gotoxy(7,5);printf("%c",186); gotoxy(7,7);printf("%c",186);
/////////////////linea 3/////////////////// gotoxy(10,3);printf("%c",186); gotoxy(10,5);printf("%c",186); gotoxy(10,7);printf("%c",186); //////////////////ABAJO 1///////////////////// gotoxy(2,4);printf("%c",205); gotoxy(3,4);printf("%c",205); gotoxy(4,4);printf("%c",206); gotoxy(5,4);printf("%c",205); gotoxy(6,4);printf("%c",205); gotoxy(7,4);printf("%c",206); gotoxy(8,4);printf("%c",205); gotoxy(9,4);printf("%c",205); gotoxy(10,4);printf("%c",185);
//////////////////ABAJO 1///////////////////// gotoxy(2,6);printf("%c",205); gotoxy(3,6);printf("%c",205); gotoxy(4,6);printf("%c",206); gotoxy(5,6);printf("%c",205); gotoxy(6,6);printf("%c",205); gotoxy(7,6);printf("%c",206); gotoxy(8,6);printf("%c",205); gotoxy(9,6);printf("%c",205); gotoxy(10,6);printf("%c",185);
//////////////////ABAJO 1///////////////////// gotoxy(2,8);printf("%c",205); gotoxy(3,8);printf("%c",205); gotoxy(4,8);printf("%c",202); gotoxy(5,8);printf("%c",205); gotoxy(6,8);printf("%c",205); gotoxy(7,8);printf("%c",202); gotoxy(8,8);printf("%c",205); gotoxy(9,8);printf("%c",205); gotoxy(10,8);printf("%c",188); //////////////////////////////////////////// ///////////////////////// gotoxy(2,3);printf("%d",b[0][0]); gotoxy(5,3);printf("%d",b[0][1]); gotoxy(8,3);printf("%d",b[0][2]); gotoxy(2,7);printf("%d",b[1][0]); gotoxy(2,5);printf("%d",b[1][1]); gotoxy(5,5);printf("%d",b[1][2]); gotoxy(8,5);printf("%d",b[2][0]); gotoxy(5,7);printf("%d",b[2][1]); gotoxy(8,7);printf("%d",b[2][2]); ////////////////////////////////////// vm=getch(); system("cls"); ////////////////////////////////////// vm=getch(); system("cls"); switch(vm) { case 80: //FLECHA DERECHA if(x <= 2){ v=b[x+1][y]; b[x][y]=v; b[x+1][y]=0; x++; } break; case 72: //FLECHA IZQUIERDA if(x>0) { v=b[x-1][y]; b[x-1][y]=0; b[x][y]=v; x--; } break; case 77: //FLECHA ABAJO if(y <= 2) { v=b[x][y+1]; b[x][y+1]=0; b[x][y]=v; y++; } break; case 75: //FLECHA ARRIBA if(y > 0) { v=b[x][y-1]; b[x][y-1]=0; b[x][y]=v; y--; } break; } }while(vm!=13); }
|
|
|
|
|
|
|