|
Mostrar Mensajes
|
Páginas: [1]
|
2
|
Programación / Programación C/C++ / combinar programas
|
en: 8 Enero 2019, 13:31 pm
|
Hola me estan pidiendo encontrar la forma de juntar estos 2 codigo que les dejo aca abajo pero lo he intentado de diversas manera pero no me resulta #include <iostream> #include <stdlib.h> using namespace std; struct nodo{ float nro; // en este caso es un numero entero struct nodo *sgte; }; typedef struct nodo *Tlista; void insertarInicio(Tlista &lista, float valor) { Tlista q; q = new(struct nodo); q->nro = valor; q->sgte = lista; lista = q; } void insertarFinal(Tlista &lista, float valor) { Tlista t, q = new(struct nodo); q->nro = valor; q->sgte = NULL; if(lista==NULL) { lista = q; } else { t = lista; while(t->sgte!=NULL) { t = t->sgte; } t->sgte = q; } } int insertarAntesDespues() { int _op, band; printf("\t 1. Antes de la posicion \n"); printf("\t 2. Despues de la posicion \n"); printf("\n\t Opcion : "); scanf("%d",&_op); if(_op==1) band = -1; else band = 0; return band; } void insertarElemento(Tlista &lista, float valor, int pos) { Tlista q, t; int i; q = new(struct nodo); q->nro = valor; if(pos==1) { q->sgte = lista; lista = q; } else { int x = insertarAntesDespues(); t = lista; for(i=1; t!=NULL; i++) { if(i==pos+x) { q->sgte = t->sgte; t->sgte = q; return; } t = t->sgte; } } printf(" Error...Posicion no encontrada..!"); } void buscarElemento(Tlista lista, float valor) { Tlista q = lista; int i = 1, band = 0; while(q!=NULL) { if(q->nro==valor) { cout<<endl<<" Encontrada en posicion "<< i <<endl; band = 1; } q = q->sgte; i++; } if(band==0) printf("\n\n Numero no encontrado..!"); } void reportarLista(Tlista lista) { int i = 0; while(lista != NULL) { cout <<' '<< i+1 <<") " << lista->nro << endl; lista = lista->sgte; i++; } } void eliminarElemento(Tlista &lista, float valor) { Tlista p, ant; p = lista; if(lista!=NULL) { while(p!=NULL) { if(p->nro==valor) { if(p==lista) lista = lista->sgte; else ant->sgte = p->sgte; delete(p); return; } ant = p; p = p->sgte; } } else printf(" Lista vacia..!"); } void eliminaRepetidos(Tlista &lista, float valor) { Tlista q, ant; q = lista; ant = lista; while(q!=NULL) { if(q->nro==valor) { if(q==lista) // primero elemento { lista = lista->sgte; delete(q); q = lista; } else { ant->sgte = q->sgte; delete(q); q = ant->sgte; } } else { ant = q; q = q->sgte; } }// fin del while printf("\n\n Valores eliminados..!"); } /* Funcion Principal ---------------------------------------------------------------------*/ struct dati{ float mod; float ang; }; int main() { Tlista lista = NULL; int op; // opcion del menu float _dato; // elemenento a ingresar int pos; // posicion a insertar do { printf("LISTA ENLAZADA SIMPLE \n"); printf("1. INSERTAR AL INICIO\n"); printf("2. INSERTAR AL FINAL\n"); printf("3. INSERTAR EN UNA POSICION \n"); printf("4. BUSCAR ELEMENTO\n"); printf("5. ELIMINAR ELEMENTO 'V'\n"); printf("6. ELIMINAR ELEMENTOS CON VALOR 'V\n"); printf("7. SALIR \n"); printf("INGRESE OPCION: "); scanf("%d",&op); switch(op) { case 1: printf("\nNUMERO A INSERTAR "); scanf("%f",&_dato); insertarInicio(lista, _dato); printf("\nMOSTRANDO LISTA\n"); reportarLista(lista); break; case 2: printf("\nNUMERO A INSERTAR "); scanf("%f",&_dato); insertarFinal(lista, _dato ); printf("\nMOSTRANDO LISTA\n"); reportarLista(lista); break; case 3: printf("\nNUMERO A INSERTAR "); scanf("%f",&_dato); printf("\nPosicion : "); scanf("%d",&pos); insertarElemento(lista, _dato, pos); printf("\nMOSTRANDO LISTA\n"); reportarLista(lista); break; case 4: printf("\nValor a buscar: "); scanf("%f",&_dato); buscarElemento(lista, _dato); break; case 5: printf("\nValor a eliminar: "); scanf("%f",&_dato); eliminarElemento(lista, _dato); printf("\nMOSTRANDO LISTA\n"); reportarLista(lista); break; case 6: printf("\nValor repetido a eliminar: "); scanf("%f",&_dato); eliminaRepetidos(lista, _dato); printf("\nMOSTRANDO LISTA\n"); reportarLista(lista); break; } printf("\n\n"); system("pause"); system("cls"); }while(op<7); return 0; }
y este codigo #include<iostream> #include<complex> using namespace std; double ang,rest,re,im,pot,mod; int main() { cout<<"Por favor introduzca la parte real de su numero complejo: "; cin>>re; cout<<endl<<"Ahora la parte imaginaria: "; cin>>im; ang = (atan (im/re))* 57,29578; mod = sqrt ((re*re)+(im*im)); cout<<"La expresion en forma polar es: ("<<mod<<" con angulo "<<ang<<")"<<endl; system ("pause"); cin.get(); return 0; }
|
|
|
3
|
Programación / Programación C/C++ / Re: ingresar datos a lista
|
en: 7 Enero 2019, 14:36 pm
|
hola de nuevo, como dicjiste realice la estructura pero cunado corro el programa no me lo guarda en la listam, es mas, me guarda un 0 //Cola de datos.Programa de prueba #include<iostream> #include "list.h" #include<cmath> using namespace std;
template<class T> void testList(List<T> &listObject, const char *type) { cout<<"Programa de prueba de una cola de valores "<<type<<".\n"; cout<<"Ingrese una de las siguientes opciones:\n" <<" 1 para insertar al final de la cola.\n" <<" 2 para eliminar al principio de la cola.\n" <<" 3 para finalizar.\n"; struct polar { double modulo,angulo; }; int choice;
T polar; do { cout<<"? "; cin>>choice; switch(choice) { case 1: double angulo,real,imaginario,modulo;
cout<<"Por favor introduzca la parte real de su numero complejo: "; cin>>real; cout<<endl<<"Ahora la parte imaginaria: "; cin>>imaginario;
angulo = (atan (imaginario/real))* 57,29578; modulo = sqrt (real*real+(imaginario*imaginario));
cout<<"La expresion en forma polar es: ("<<modulo<<" con angulo "<<angulo<<")"<<endl;
listObject.insertAtBack(polar); listObject.print(); break; case 2: if(listObject.removeFromFront(polar)) cout<<polar<<" removido de la cola.\n"; listObject.print(); break; } } while(choice!=3); cout<<"Finalizar prueba de cola.\n"; } int main() { List<int>integerQueue; testList(integerQueue,"entero"); List<double>doubleQueue; testList(doubleQueue,"doble precision"); return 0; }
|
|
|
4
|
Programación / Programación C/C++ / ingresar datos a lista
|
en: 4 Enero 2019, 04:03 am
|
Hola como estan Necesito ayuda para ingresar 2 datos en una lista, ya tengo un codigo en donde me permite agregar o quitarvalores de una lista, pero no me permite hacerlo para 2, en mi caso quiero registras valores rectangulares y polares. les dejo los codigos que tengo #include<iostream> #include<cmath>
//Programa para transformar de Rectangular a Polar
using namespace std; int n,i; double ang,rest,re,im,pot,mod;
int main() { cout<<"\nIngrese cantidad de Tensiones Complejas\n"; cin>>n; for(i=1;i<=n;i++) { cout<<"Por favor introduzca la parte real de su numero complejo"<<i<<": \n"; cin>>re; cout<<endl<<"Ahora la parte imaginaria"<<i<<": \n"; cin>>im;
ang = (atan (im/re))* 57,29578; mod = sqrt ((re*re)+(im*im));
cout<<"La expresion en forma polar"<<i<<" es: "<<mod<<" con angulo "<<ang<<"\n"<<endl;
}
system ("pause"); cin.get(); return 0; } // Programa para pasar de Polar a Rctangular #include<iostream> #include<cmath>
using namespace std; int i,n; double ang,rest,re,im,pot,mod,rad;
int main() { cout<<"\nIngrese cantidad de Tensiones Polares : "; cin>>n; for(i=1;i<=n;i++) { cout<<"Por favor introduzca el modulo: "<<i<<": \n"; cin>>mod; cout<<endl<<"Ahora el angulo en grados: "<<i<<": \n"; cin>>ang;
rad=ang*3.1416/180.0; re = mod * cos (rad); im = mod * sin(rad);
cout<<"La expresion en forma rectangular"<<i<<" es: "<<re<<" "<<im<<"j \n"<<endl;
} system ("pause"); cin.get(); return 0; }
|
|
|
6
|
Programación / Programación C/C++ / Listas enlazadas por punteros (SOLUCIONADO)
|
en: 3 Enero 2019, 01:30 am
|
que tal, tengo una tarea en donde me piden: "elaborar un programa para anotar en una lista enlazada por punteros, el voltaje y la corriente a través de los componentes de un circuito de n componentes resistivos puros, calculando cada vez el valor de la resistencia correspondiente"
tengo hecho un programa en el cual puedo ingresar estas n variables y hacer el calculo para que me de la resistencia como resultado, mi problema es que no entiendo como hacer una lista enlazada por punteros, ojala puedan ayudarme.
les dejo el programa que hice:
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<iostream>
using std::cin; using std::cout;
main () { int n,i; float r,v,c; cout << "\ningrese cantidad de resistencias a calcular : \n"; cin>> n; for(i=1;i<=n;i++) { cout<<"\ningrese tension "<< i<<":"<<"\n"; cin>> v; cout<< "\ningrese corriente "<< i<<":"<<"\n"; cin>> c; r= (v/c); cout<< "el resultado de la resistencia R "<< i << " es :"<< r<<"\n"; } system ("pause"); return 0; }
|
|
|
8
|
Programación / Programación C/C++ / porque aparece Id returned 1 exit status??? (solucionado)
|
en: 1 Enero 2019, 23:46 pm
|
hola como estan?, hace un tiempo mi profesor nos dio este programa sin decirnos la gran cosa solo dijo "intenten ejecutarla para antes del examen", lo arregle y todo pero aun me arroja error, ojala puedan ayudarme
les dejo el programa
// Fig. 17.4: list.h // Template List class definition. #ifndef LIST_H #define LIST_H #include <iostream> using std::cout; #include <new> #include "list" // ListNode class definition template< class NODETYPE > class List { public: List(); // constructor ~List(); // destructor void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack( NODETYPE & ); bool isEmpty() const; void print() const; private: List< NODETYPE > *firstPtr; // pointer to first node List< NODETYPE > *lastPtr; // pointer to last node // utility function to allocate new node List< NODETYPE > *getNewNode( const NODETYPE & ); }; // end class List // default constructor template< class NODETYPE > List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { // empty body } // end List constructor // destructor template< class NODETYPE > List< NODETYPE >::~List() { if ( !isEmpty() ) { // List is not empty cout << "Destroying nodes ...\n"; List< NODETYPE > *currentPtr = firstPtr; List< NODETYPE > *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes tempPtr = currentPtr; cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } // end while } // end if cout << "All nodes destroyed\n\n"; } // end List destructor // insert node at front of list template< class NODETYPE > void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { List< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; } // end else } // end function insertAtFront // insert node at back of list template< class NODETYPE > void List< NODETYPE >::insertAtBack( const NODETYPE &value ) { List< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; } // end else } // end function insertAtBack // delete node from front of list template< class NODETYPE > bool List< NODETYPE >::removeFromFront( NODETYPE &value ) { if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { List< NODETYPE > *tempPtr = firstPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; value = tempPtr->data; // data being removed delete tempPtr; return true; // delete successful } // end else } // end function removeFromFront // delete node from back of list template< class NODETYPE > bool List< NODETYPE >::removeFromBack( NODETYPE &value ) { if ( isEmpty() ) return false; // delete unsuccessful else { List< NODETYPE > *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { List< NODETYPE > *currentPtr = firstPtr; // locate second-to-last element while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } // end else value = tempPtr->data; delete tempPtr; return true; // delete successful } // end else } // end function removeFromBack // is List empty? template< class NODETYPE > bool List< NODETYPE >::isEmpty() const { return firstPtr == 0; } // end function isEmpty // return pointer to newly allocated node template< class NODETYPE > List< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value ) { return new List< NODETYPE >( value ); } // end function getNewNode // display contents of List template< class NODETYPE > void List< NODETYPE >::print() const { if ( isEmpty() ) { cout << "The list is empty\n\n"; return; } // end if List< NODETYPE > *currentPtr = firstPtr; cout << "The list is: "; while ( currentPtr != 0 ) { cout << currentPtr->data << ' '; currentPtr = currentPtr->nextPtr; } // end while cout << "\n\n"; } // end function print
#endif
|
|
|
|
|
|
|