Código de C/Visual C - Control de una expendedora de dulces en C

sin imagen de perfil

Control de una expendedora de dulces en Cgráfica de visualizaciones


C/Visual C

Publicado el 9 de Julio del 2002 por Sebastián E Barreda
37.176 visualizaciones desde el 9 de Julio del 2002
Programa realizado en C que simula un PLD de una máquina expendedora de dulces o gaseosas. El ingreso se realiza cargando primero el contenido (como un administrador) y luego se escribe moneda a moneda y se ingresa 0 para finalizar el ingreso. Si se introduce una moneda 0 se ingresa al menu de administracion (el que aparece al cominezo, como si recien se encendiera la maquina).

Versión 1
estrellaestrellaestrellaestrellaestrella(3)

Publicado el 9 de Julio del 2002gráfica de visualizaciones de la versión: Versión 1
37.177 visualizaciones desde el 9 de Julio del 2002
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#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)

13 de Julio del 2002
estrellaestrellaestrellaestrellaestrella
Seba............
sos un capo! jeje
=)
Muy interesante tu codigo fuente ...
Responder
14 de Julio del 2002
estrellaestrellaestrellaestrellaestrella
Hola!!!
suerte en todo lo que emprendas.....t.q.m....vero...
beshos......
Responder
18 de Julio del 2002
estrellaestrellaestrellaestrellaestrella
Ha realizado una valoración positiva de este curso.
Responder

Comentar la versión: Versión 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s254