#include <stdio.h>
#include <stdlib.h>
#define N 20
typedef struct vendedor {
int factura;
float monto;
};
typedef struct nodoLista{
struct vendedor ven;
struct nodoLista* ptrSiguiente;
};
typedef struct nodoLista NodoLista;
typedef NodoLista *ptrNodoLista;
void insertar( ptrNodoLista *ptrS, struct vendedor valor );
char eliminar( ptrNodoLista *ptrS, struct vendedor valor );
void buscarFactura( ptrNodoLista *ptrS, int f );
void calcularComision( ptrNodoLista ptrActual );
int estaVacia( ptrNodoLista ptrS );
void imprimir( ptrNodoLista ptrActual );
void insertar( ptrNodoLista *ptrS, struct vendedor valor){
ptrNodoLista ptrNuevo;
ptrNodoLista ptrAnterior;
ptrNodoLista ptrActual;
ptrNuevo = malloc( sizeof (NodoLista) );
if( ptrNuevo != NULL ){
ptrNuevo->ven = valor;
ptrNuevo->ptrSiguiente = NULL;
ptrAnterior = NULL;
ptrActual = *ptrS;
while( ptrActual != NULL && valor.factura > ptrActual->ven.factura ){
ptrAnterior = ptrActual;
ptrActual = ptrActual->ptrSiguiente;
}
if( ptrAnterior == NULL ){
ptrNuevo->ptrSiguiente = *ptrS;
*ptrS = ptrNuevo;
}
else {
ptrAnterior->ptrSiguiente = ptrNuevo;
ptrNuevo->ptrSiguiente = ptrActual;
}
}
else {
printf( "No se inserto %d. No hay memoria disponible.", valor.factura );
}
}
char eliminar( ptrNodoLista *ptrS, struct vendedor valor ){
ptrNodoLista ptrAnterior;
ptrNodoLista ptrActual;
ptrNodoLista tempPtr;
if( valor.factura == ( *ptrS )->ven.factura ) {
tempPtr = *ptrS;
*ptrS = ( *ptrS )->ptrSiguiente;
free( tempPtr );
return valor.factura;
}
else {
ptrAnterior = *ptrS;
ptrActual = ( *ptrS )->ptrSiguiente;
while( ptrActual != NULL && ptrActual->ven.factura != valor.factura ) {
ptrAnterior = ptrActual;
ptrActual = ptrActual->ptrSiguiente;
}
if( ptrActual != NULL ){
tempPtr = ptrActual;
ptrAnterior->ptrSiguiente = ptrActual->ptrSiguiente;
free( tempPtr );
return valor.factura;
}
}
return '\0';
}
void buscarFactura( ptrNodoLista *ptrS, int factura ){
ptrNodoLista ptrAnterior;
ptrNodoLista ptrActual;
ptrNodoLista tempPtr;
if( factura == ( *ptrS )->ven.factura ) {
printf( "\nFactura %d: cantidad %f", ( *ptrS )->ven.factura, ( *ptrS )->ven.monto );
}
else {
ptrAnterior = *ptrS;
ptrActual = ( *ptrS )->ptrSiguiente;
while( ptrActual != NULL && ptrActual->ven.factura != factura ) {
ptrAnterior = ptrActual;
ptrActual = ptrActual->ptrSiguiente;
}
if( ptrActual != NULL ){
printf( "\nFactura %d: cantidad %.2f", ptrActual->ven.factura, ptrActual->ven.monto );
}
}
}
void calcularComision( ptrNodoLista ptrActual ){
float total = 0.0;
if( ptrActual == NULL){
printf( "\nLa lista esta vacia.\n\n" );
}
else {
printf( "La lista de facturas es:\n" );
while( ptrActual != NULL ){
total += ptrActual->ven.monto;
printf( "%d -> ", ptrActual->ven.factura );
ptrActual = ptrActual->ptrSiguiente;
}
printf( "NULL\n\n" );
}
printf( "\nTotal facturacion: %.2f", total );
if( total >= 50000 )
printf( "\nBono: %d ", 10000 );
else
printf( "\nNo corresponde bono." );
}
int estaVacia( ptrNodoLista ptrS ){
return ptrS == NULL;
}
void imprimir( ptrNodoLista ptrActual ) {
if( ptrActual == NULL){
printf( "\nLa lista esta vacia.\n\n" );
}
else {
printf( "La lista de facturas es:\n" );
while( ptrActual != NULL ){
printf( "%d -> ", ptrActual->ven.factura );
ptrActual = ptrActual->ptrSiguiente;
}
printf( "NULL\n\n" );
}
}
void instrucciones(){
printf( "\n Introduzca su eleccion:\n" );
printf( " 1.- Nueva factura.\n" );
printf( " 2.- Eliminar Factura.\n" );
printf( " 3.- Buscar factura.\n" );
printf( " 4.- Calcular comision.\n" );
printf( " 5.- Salir.\n" );
}
int main() {
char nombre1[N], nombre2[N];
printf( "\nNombre del primer vendedor: " );
scanf( "%s", nombre1 );
printf( "\nNombre del segundo vendedor: " );
scanf( "%s", nombre2 );
ptrNodoLista ptrInicial1 = NULL;
ptrNodoLista ptrInicial2 = NULL;
struct vendedor v1, v2;
int eleccion, comerciante, numero;
instrucciones();
printf( "? " );
scanf( "%d", &eleccion);
while( eleccion != 5 ){
switch( eleccion ){
case 1:
printf( "\nComerciante (1/2): " );
scanf( "%d", &comerciante );
if( comerciante == 1 ) {
printf( "\nVendedor: %s", nombre1 );
printf( "\nNumero de factura: " );
scanf( "%d", &v1.factura );
printf( "Cantidad: " );
scanf( "%f", &v1.monto );
insertar( &ptrInicial1, v1 );
imprimir( ptrInicial1 );
}
else if( comerciante == 2 ){
printf( "\nVendedor: %s", nombre1 );
printf( "\nNumero de factura: " );
scanf( "%d", &v2.factura );
printf( "Cantidad: " );
scanf( "%f", &v2.monto );
insertar( &ptrInicial2, v2 );
imprimir( ptrInicial2 );
}
else
printf( "\nEl vendedor no existe." );
break;
case 2:
printf( "\nComerciante (1/2): " );
scanf( "%d", &comerciante );
if( comerciante == 1 ) {
if( !estaVacia( ptrInicial1 ) ){
printf( "Introduzca factura a eliminar: " );
scanf( "%d", &v1.factura );
if( eliminar( &ptrInicial1, v1 ) ){
printf( "Factura %d eliminada.\n", v1.factura );
imprimir( ptrInicial1 );
}
else {
printf( "No se encuentra la factura %d", v1.factura );
}
}
else {
printf( "La lista esta vacia.\n" );
}
} else if( comerciante == 2 ) {
if( !estaVacia( ptrInicial2 ) ){
printf( "Introduzca factura a eliminar: " );
scanf( "%d", &v2.factura );
if( eliminar( &ptrInicial2, v2 ) ){
printf( "Factura %d eliminado.\n", v2.factura );
imprimir( ptrInicial2 );
}
else {
printf( "No se encuentra la factura %d", v2.factura );
}
}
else {
printf( "La lista esta vacia.\n" );
}
} else
printf( "\nEl vendedor no existe." );
break;
case 3:
printf( "\nComerciante (1/2): " );
scanf( "%d", &comerciante );
if( comerciante == 1 ) {
printf( "Introduzca factura a buscar: " );
scanf( "%d", &numero );
buscarFactura( &ptrInicial1, numero );
}
else if( comerciante == 2 ) {
printf( "Introduzca factura a buscar: " );
scanf( "%d", &numero );
buscarFactura( &ptrInicial2, numero );
}
else
printf( "\nEl vendedor no existe." );
break;
case 4:
printf( "\nComerciante (1/2): " );
scanf( "%d", &comerciante );
if( comerciante == 1 ) {
printf( "\nComerciante: %s: ", nombre1 );
calcularComision( ptrInicial1 );
}
else if( comerciante == 2 ) {
printf( "\nComerciante: %s: ", nombre2 );
calcularComision( ptrInicial2 );
}
else
printf( "\nEl vendedor no existe." );
break;
break;
default:
printf( "Opcion invalida.\n\n" );
break;
}
instrucciones();
printf( "? " );
scanf( "%d", &eleccion);
}
printf( "Fin de la ejecución.\n" );
return 0;
}