#include <stdio.h>
#include <stdlib.h>
#define SALIR 9
typedef enum{FALSO, VERDADERO}boolean; /*Datos tipo boolean*/
/*Prototipos funciones*/
void cargamem(struct memorias *, int*, int*, struct moned *);
void selector(float, struct memorias, int, int, struct moned);
void menuadm(struct memorias, int, int, struct moned);
void menuser(struct memorias, float, int, struct moned, int);
void liberamem(struct memorias, struct moned);
void carga (struct memorias, int);
void infoprod(struct memorias, int);
void infomon(struct memorias, int);
void carga(struct memorias, int);
void cargamon(struct memorias, int, struct moned);
void copiacadena(char*, struct memorias, int);
char largocadena(char *);
boolean monvalida(float, struct memorias, int);
int compra(int, float, struct memorias, struct moned, int);
void importe(struct moned, float *, int, float);
boolean darvuelto(struct memorias, struct moned, int, float, int);
void ordenamon(struct memorias, struct moned, int);
int obtienepos(float, int, struct memorias);
void actualiza(int, struct memorias, struct moned, int);
struct memorias
{
float *clasemon;
int *monedas;
int *stock;
char **desc;
float *precio;
float *recaud;
};
struct moned
{
float *clasemon;
int *monedas;
int *cont;
};
int main(void)
{
struct memorias mem;
struct moned moned;
int mon, stck;
float moneda = 0;
cargamem(&mem, &mon, &stck, &moned);
while(VERDADERO)
selector(moneda, mem, mon, stck, moned);
liberamem(mem, moned);
return 0;
}
/*Pide memoria para la maquina*/
void cargamem(struct memorias *mem, int *mon, int *stock, struct moned *moned)
{
int i;
printf("Ingrese la cantidad de monedas diferentes admitidas: ");
scanf("%d", mon);
printf("Ingrese la cantidad de productos admitidos: ");
scanf("%d", stock);
if((mem->monedas = (int*)malloc(*(mon)*sizeof(int))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
exit(1);
}
if((moned->monedas = (int*)malloc(*(mon)*sizeof(int))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
exit(1);
}
if((mem->clasemon = (float*)malloc(*(mon)*sizeof(float))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
free(moned->monedas);
exit(1);
}
if((moned->cont = (int*)malloc(*(mon)*sizeof(int))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
exit(1);
}
if((moned->clasemon = (float*)malloc(*(mon)*sizeof(float))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
free(moned->monedas);
exit(1);
}
if((mem->stock = (int*)malloc(*(stock)*sizeof(int))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
free(mem->monedas);
exit(2);
}
if((mem->precio = (float*)malloc(*(stock)*sizeof(float))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
free(mem->monedas);
free(mem->stock);
exit(3);
}
if((mem->desc = (char**)malloc(*(stock)*sizeof(char*))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
free(mem->monedas);
free(mem->stock);
free(mem->precio);
exit(4);
}
for(i = 0; i < *stock; i++)
mem->desc[i] = (char*)malloc(50);
if((mem->recaud = (float*)malloc(*(stock)*sizeof(float))) == NULL)
{
printf("\b\nNo hay memoria suficiente\n");
free(mem->monedas);
free(mem->stock);
free(mem->precio);
free(mem->desc);
exit(5);
}
for(i = 0; i < *stock; i++)
mem->stock[i] = -1;
menuadm(*mem, *mon, *stock, *moned);
}
/*Deriva segun el primer ingreso de la moneda*/
void selector(float moneda, struct memorias mem, int mon, int stock, struct moned moned)
{
printf("\nIngrese moneda: ");
scanf("%f", &moneda);
if(moneda == 0)
menuadm(mem, mon, stock, moned);
else
menuser(mem, moneda, mon, moned, stock);
}
void menuadm(struct memorias mem, int mon, int stock, struct moned moned)
{
int opcion = 999;
while(opcion != SALIR)
{
printf("\nMenu Administracion:\n");
printf("1. Stock actual, recaudacion por producto y recaudacion total\n");
printf("2. Stock actual de monedas\n");
printf("3. Carga de productos, actualizacion de stock, precio y descripcion de los productos\n");
printf("4. Carga de monedas\n");
printf("Ingrese la opcion deseada (%d para salir): ", SALIR);
scanf("%d", &opcion);
switch(opcion)
{
case 1:
infoprod(mem, stock);
break;
case 2:
infomon(mem, mon);
break;
case 3:
carga(mem, stock);
break;
case 4:
cargamon(mem, mon, moned);
break;
}
}
}
void carga(struct memorias mem, int stock)
{
char buff[50], aux;
int i;
printf("Ingrese el codigo del producto (%d para salir): ", stock);
scanf("%d", &i);
fflush(stdin);
while(i < stock)
{
if(mem.stock[i] == -1)
{
printf("\nIngrese la descripcion: ");
gets(buff);
copiacadena(buff, mem, i);
printf("\nIngrese la cantidad: ");
scanf("%d", &mem.stock[i]);
fflush(stdin);
printf("\nIngrese el precio: ");
scanf("%f", &mem.precio[i]);
fflush(stdin);
mem.recaud[i] = 0;
}
else
{
printf("\n%s:", mem.desc[i]);
printf("\nIngrese el nuevo stock: ");
scanf("%d", &mem.stock[i]);
fflush(stdin);
do{
printf("\n¿Desea cambiar el precio (s/n)?: ");
scanf("%c", &aux);
fflush(stdin);
}while(aux != 's' && aux != 'n');
if(aux == 's')
{
printf("\nIngrese el nuevo precio: ");
scanf("%f", &mem.precio[i]);
fflush(stdin);
}
do{
printf("\n¿Desea cambiar la descripcion (s/n)?: ");
scanf("%c", &aux);
fflush(stdin);
}while(aux != 's' && aux != 'n');
if(aux == 's')
{
printf("\nIngrese la descripcion: ");
gets(buff);
copiacadena(buff, mem, i);
}
}
printf("Ingrese el codigo del producto (%d para salir): ", stock);
scanf("%d", &i);
fflush(stdin);
}
}
void copiacadena(char *origen, struct memorias mem, int i)
{
char j = 0, largo;
largo = largocadena(origen);
if(origen[largo-1] == '\n')
origen[largo-1] = '\0';
while(origen[j] != '\0')
{
mem.desc[i][j] = origen[j];
j++;
}
mem.desc[i][j] = '\0';
}
char largocadena(char *cadena)
{
char largo = 0;
while(*(cadena++))
largo++;
return largo;
}
void cargamon(struct memorias mem, int mon, struct moned moned)
{
int i;
float m;
static boolean flag = VERDADERO;
if(flag)
{
for(i = 0; i < mon; i++)
{
printf("Ingrese el valor de la moneda: ");
scanf("%f", &mem.clasemon[i]);
moned.clasemon[i] = mem.clasemon[i];
printf("Ingrese el stock de dicha moneda: ");
scanf("%d", &mem.monedas[i]);
}
flag = FALSO;
}
else
{
printf("Ingrese el valor de la moneda a cambiar (0 para salir): ");
scanf("%f", &m);
fflush(stdin);
while(m != 0)
{
i = obtienepos(m, mon, mem);
printf("Ingrese el nuevo valor de la moneda: ");
scanf("%f", &mem.clasemon[i]);
moned.clasemon[i] = mem.clasemon[i];
printf("Ingrese el stock de dicha moneda: ");
scanf("%d", &mem.monedas[i]);
printf("Ingrese el valor de la moneda a cambiar (0 para salir): ");
scanf("%f", &m);
fflush(stdin);
}
}
ordenamon(mem, moned, mon);
}
int obtienepos(float m, int mon, struct memorias mem)
{
int i = 0;
while(i < mon)
{
if(m == mem.clasemon[i])
return i;
i++;
}
}
void ordenamon(struct memorias mem, struct moned moned, int mon)
{
int i=1, j;
float tmp;
boolean ordenado = FALSO;
while(!ordenado)
{
ordenado = VERDADERO;
for(j = 0; j < (mon-i); j++)
if(mem.clasemon[j] < mem.clasemon[j+1])
{
tmp = mem.clasemon[j];
mem.clasemon[j] = mem.clasemon[j+1];
mem.clasemon[j+1] = tmp;
tmp = moned.clasemon[j];
moned.clasemon[j] = moned.clasemon[j+1];
moned.clasemon[j+1] = tmp;
ordenado = FALSO;
}
i++;
}
}
void liberamem(struct memorias mem, struct moned moned)
{
free(mem.monedas);
free(mem.stock);
free(mem.precio);
free(mem.desc);
free(mem.clasemon);
free(mem.recaud);
free(moned.clasemon);
free(moned.monedas);
}
void infoprod(struct memorias mem, int n)
{
int i;
float rec = 0;
for(i = 0; i < n; i++)
if(mem.stock[i] != -1)
{
printf("\nDescripcion: %s", mem.desc[i]);
printf("\nStock: %d", mem.stock[i]);
printf("\nRecaudacion: %f", mem.recaud[i]);
rec += mem.recaud[i];
}
printf("\nRecaudacion total: %f\n", rec);
}
void infomon(struct memorias mem, int n)
{
int i;
for(i = 0; i < n; i++)
printf("$%1.2f\t%d\n", mem.clasemon[i], mem.monedas[i]);
}
void menuser(struct memorias mem, float moneda, int mon, struct moned moned, int stock)
{
float ing = 0;
int codprod, i;
for(i = 0; i < mon; i++)
moned.monedas[i] = 0;
do{
if(!monvalida(moneda, mem, mon))
{
printf("\nCompra cancelada, moneda invalida\n");
return;
}
else
importe(moned, &ing, mon, moneda);
printf("\nIngrese moneda: ");
scanf("%f", &moneda);
}while(moneda != 0);
do
{
printf("\nCodigo producto (%d cancela): ", stock);
}while(scanf("%d", &codprod) != 1 && codprod > stock);
if(codprod == stock)
{
printf("\nCompra cancelada, importe devuelto");
return;
}
i = compra(codprod, ing, mem, moned, mon);
switch(i)
{
case 1:
{
printf("\nCompra cancelada, importe insuficiente\n");
return;
break;
}
case 2:
{
printf("\nCompra cancelada, vuelto insuficiente\n");
return;
break;
}
}
}
boolean monvalida(float moned, struct memorias mem, int mon)
{
int i = 0;
boolean flag = FALSO;
while(i < mon)
{
if(moned == mem.clasemon[i])
flag = VERDADERO;
i++;
}
return flag;
}
void importe(struct moned moned, float *ing, int mon, float moneda)
{
int i;
for(i = 0; i < mon; i++)
if(moneda == moned.clasemon[i])
moned.monedas[i] += 1;
*ing += moneda;
}
int compra(int codprod, float ing, struct memorias mem, struct moned moned, int mon)
{
int i;
for(i = 0; i < mon; i++)
moned.cont[i] = 0;
if(ing < mem.precio[codprod])
return 1;
if(ing == mem.precio[codprod])
{
printf("\nProducto entregado. Gracias por su compra\n");
actualiza(codprod, mem, moned, mon);
}
if(!darvuelto(mem, moned, mon, ing, codprod))
return 2;
return 0;
}
boolean darvuelto(struct memorias mem, struct moned moned, int mon, float ing, int codprod)
{
int i = 0;
ing -= mem.precio[codprod];
while(i < mon)
{
while(moned.monedas[i] > moned.cont[i] || mem.monedas[i] > (moned.cont[i]-moned.monedas[i]))
{
if(ing >= moned.clasemon[i])
{
ing -= moned.clasemon[i];
moned.cont[i] += 1;
}
else
break;
}
i++;
}
if(ing > 0.00999999999)
return FALSO;
printf("\nSu vuelto:\n");
for(i = 0; i < mon; i++)
printf("$%f\t%d\n", moned.clasemon[i], moned.cont[i]);
actualiza(codprod, mem, moned, mon);
return VERDADERO;
}
void actualiza(int codprod, struct memorias mem, struct moned moned, int mon)
{
int i = 0;
mem.recaud[codprod] += mem.precio[codprod];
mem.stock[codprod] -= 1;
while(i < mon)
{
while(moned.cont[i] > 0)
{
if(moned.monedas[i] > 0)
{
moned.monedas[i] -= 1;
moned.cont[i] -= 1;
}
else
{
mem.monedas[i] -= 1;
moned.cont[i] -= 1;
}
}
i++;
}
for(i = 0; i < mon; i++)
mem.monedas[i] += moned.monedas[i];
}
Comentarios sobre la versión: Versión 1 (3)
sos un capo! jeje
=)
Muy interesante tu codigo fuente ...
suerte en todo lo que emprendas.....t.q.m....vero...
beshos......