puedes usar un firewall para caparle el acceso a internet.
Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes Menú#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stRegistro {
int ID;
char nombre[25];
int cantidad;
};
int Menu(void);
void ChangeStock(char *operacion, struct stRegistro *reg);
void MostrarCabecera(void);
void MostrarProducto(long n, struct stRegistro *reg);
long LeeNumero(void);
void Borrar(FILE **fa, long numero);
int Size(FILE **fa, int tam);
//--------------------------------------------------------------------------
int main()
{
struct stRegistro reg;
struct stRegistro aux;
char nombre[25];
FILE *fa;
int i, opcion, nRegistros, encontrado, ID;
long numero;
fa = fopen("stock.dat", "r+b"); // Este modo permite leer y escribir
if(!fa) fa = fopen("stock.dat", "w+b"); // si el fichero no existe, lo crea.
do {
opcion = Menu();
switch(opcion) {
case '1': // Añadir o incrementar el stock
encontrado = 0;
ChangeStock("Añadir al stock", &aux);
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®,sizeof(struct stRegistro),1,fa);
if(strcmp(aux.nombre, reg.nombre) == 0)
{
reg.cantidad += aux.cantidad;
fseek(fa,i*sizeof(struct stRegistro),SEEK_SET);
fwrite(®, sizeof(struct stRegistro), 1, fa);
encontrado = 1;
break;
}
}
if(encontrado == 0)
{
if(nRegistros == 0)
ID = 0;
else
{
fseek(fa, 0, nRegistros);
fread(®,sizeof(struct stRegistro), 1, fa);
ID = reg.ID + 1;
}
fseek(fa, 0, SEEK_END);
aux.ID = ID;
fwrite(&aux, sizeof(struct stRegistro), 1, fa);
}
break;
case '2': // Ventas de stockaje
encontrado = 0;
ChangeStock("Venta de stockaje", &aux);
nRegistros = Size(&fa, sizeof(struct stRegistro));
fseek(fa, 0, SEEK_SET);
for(i=0; i<nRegistros; i++)
{
fread(®,sizeof(struct stRegistro),1,fa);
if(strcmp(aux.nombre, reg.nombre) == 0)
{
encontrado = 1;
if(aux.cantidad <= reg.cantidad)
{
reg.cantidad -= aux.cantidad;
fseek(fa,i*sizeof(struct stRegistro),SEEK_SET);
fwrite(®, sizeof(struct stRegistro), 1, fa);
}else{
printf("No hay suficiente stockaje de ese producto para esta venta.\n");
MostrarCabecera();
MostrarProducto(i, ®);
printf("|---------|-------------------------|--------|\n\n");
system("PAUSE");
}
break;
}
}
if(encontrado == 0)
{
printf("No se puede encontrar el producto indicado.\n\n");
system("PAUSE");
}
break;
case '3': // Mostrar stock de un producto por su ID
encontrado = 0;
system("cls");
printf("Buscar stock por ID: ");
ID = LeeNumero();
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®,sizeof(struct stRegistro),1,fa);
if(ID == reg.ID)
{
encontrado = 1;
MostrarCabecera();
MostrarProducto(i, ®);
break;
}
}
if(encontrado == 0)
{
printf("No se puede encontrar el producto indicado.\n\n");
}
else
{
printf("|---------|-------------------------|--------|\n\n");
}
system("PAUSE");
break;
case '4': // Mostrar stock de un producto por su nombre
encontrado = 0;
system("cls");
printf("Buscar stock por nombre: ");
fgets(nombre,25,stdin);
for(i = strlen(nombre)-1; i && nombre[i] < ' '; i--)
nombre[i] = 0;
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®,sizeof(struct stRegistro),1,fa);
if(strcmp(nombre, reg.nombre) == 0)
{
encontrado = 1;
MostrarCabecera();
MostrarProducto(i, ®);
break;
}
}
if(encontrado == 0)
{
printf("No se puede encontrar el producto indicado.\n\n");
system("PAUSE");
}
else
{
printf("|---------|-------------------------|--------|\n\n");
}
system("PAUSE");
break;
case '5': // Mostrar todo el stockaje
encontrado = 0;
rewind(fa);
numero = 0;
system("cls");
nRegistros = Size(&fa,sizeof(struct stRegistro));
if(nRegistros > 0)
{
encontrado = 1;
MostrarCabecera();
for(i = 0; i < nRegistros; i++)
{
fread(®, sizeof(struct stRegistro), 1, fa);
MostrarProducto(numero++, ®);
}
}
if(encontrado == 0)
{
printf("No existen entradas en el registro.\n");
}
else
{
printf("|---------|-------------------------|--------|\n\n");
}
system("PAUSE");
break;
case '6': // Eliminar del stock
system("cls");
printf("Eliminar del stock: ");
numero = LeeNumero();
Borrar(&fa, numero);
break;
}
} while(opcion != '0');
fclose(fa);
return 0;
}
//--------------------------------------------------------------------------
// Muestra un menú con las opciones disponibles y captura una opción del usuario
int Menu()
{
char resp[20];
do {
system("cls");
printf("MENU PRINCIPAL\n");
printf("--------------\n\n");
printf("1- Añadir al stock\n");
printf("2- Venta\n");
printf("3- Buscar stock por su ID\n");
printf("4- Buscar stock por su nombre\n");
printf("5- Mostrar todo el stockaje\n");
printf("6- Eliminar producto del stock por su ID\n");
printf("0- Salir\n");
fgets(resp, 20, stdin);
} while(resp[0] < '0' && resp[0] > '6');
return resp[0];
}
//--------------------------------------------------------------------------
// Permite que el usuario introduzca un producto por pantalla
void ChangeStock(char * operacion, struct stRegistro *reg)
{
int i;
char numero[6];
system("cls");
printf("%s:", operacion);
printf("\n\n");
printf("Nombre: ");
fgets(reg->nombre, 25, stdin);
// la función fgets captura el retorno de línea, hay que eliminarlo:
for(i = strlen(reg->nombre)-1; i && reg->nombre[i] < ' '; i--)
reg->nombre[i] = 0;
printf("Cantidad: ");
fgets(numero, 6, stdin);
reg->cantidad = atoi(numero);
}
//--------------------------------------------------------------------------
// Muestra la cabecera de la tabla por pantalla
void MostrarCabecera(void)
{
printf("\n");
printf("|---------|-------------------------|--------|\n"
"|ID |Nombre |Cantidad|\n"
"|---------|-------------------------|--------|\n");
}
//--------------------------------------------------------------------------
// Muestra un producto por pantalla
void MostrarProducto(long n, struct stRegistro *reg)
{
printf("|[%6ld] |%-25s| %4d |\n", reg->ID, reg->nombre, reg->cantidad);
}
//--------------------------------------------------------------------------
// Lee un número suministrado por el usuario
long LeeNumero()
{
char numero[6];
fgets(numero, 6, stdin);
return atoi(numero);
}
//--------------------------------------------------------------------------
// Borra una entrada del archivo
void Borrar(FILE **fa, long numero)
{
struct stRegistro reg;
FILE *ftemp;
int largo, nRegistros, i;
rewind(*fa);
ftemp = fopen("temp.dat", "wb");
nRegistros = Size(*&fa,sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®,sizeof(struct stRegistro),1,*fa);
if(numero != reg.ID)
{
fwrite(®, sizeof(struct stRegistro), 1, ftemp);
}
}
fclose(ftemp);
fclose(*fa);
remove("stock.dat");
rename("temp.dat", "stock.dat");
*fa = fopen("stock.dat", "r+b");
}
//--------------------------------------------------------------------------
int Size(FILE **fa, int tam)
{
int largo, n;
fseek(*fa, 0, SEEK_END);
largo = ftell(*fa);
n = largo / tam;
fseek(*fa, 0, SEEK_SET);
return n;
}
//--------------------------------------------------------------------------