Advertisement
nhdb

prueba programacion

Jun 22nd, 2020
1,827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define CANT_REGISTROS 3000
  4. #define ARCHIVO_ENTRADA "STOCK.dat"
  5. #define ARCHIVO_UPDATE "STOCK_ACT.dat"
  6.  
  7. struct registro{
  8.   int articulo;
  9.   char descripcion[25];
  10.   float precio_unitario;
  11.   int punto_pedido;
  12.   int stock;
  13. };
  14.  
  15.  
  16. void test_prepare(){
  17.   FILE *f = fopen(ARCHIVO_ENTRADA, "w");
  18.  
  19.   struct registro test[] = {{1, "uno", 1.0, 5, 5},
  20.                           {2, "dos", 2.0, 2, 7},
  21.                           {3, "tres", 4.0, 75, 5},
  22.                           {4, "cuatro", 59.9, 3, 5}};
  23.  
  24.   fwrite(test, sizeof(struct registro), 4, f);
  25.  
  26.   fclose(f);
  27. }
  28.  
  29. float calculo_porcentaje(int n1, int n2){
  30.   return (n1*100)/n2;
  31. }
  32.  
  33. int read_articulo(struct registro registros_generales[]){
  34.   FILE *f = fopen(ARCHIVO_ENTRADA, "r");
  35.  
  36.   if(f == NULL)
  37.     return -1;
  38.  
  39.   struct registro r;
  40.   int i=0;
  41.   while(fread(&r, sizeof(struct registro), 1, f)){
  42.     registros_generales[i] = r;
  43.     i++;
  44.   }
  45.   fclose(f);
  46.   return i;
  47. }
  48.  
  49. int actualizar_articulo_array(int cant_vendida, int id, int cant_registros_leidos, struct registro registros_generales[]){
  50.   int i;
  51.   for(i=0;i<cant_registros_leidos;i++){
  52.     if(registros_generales[i].articulo == id){
  53.       registros_generales[i].stock -= cant_vendida;
  54.       return (registros_generales[i].stock < registros_generales[i].punto_pedido ? 0 : 1);
  55.     }
  56.   }
  57.   return -1;
  58. }
  59.  
  60. void actualizar_articulo_archivo(int cant_registros_leidos, struct registro registros_generales[]){
  61.   FILE *f = fopen(ARCHIVO_UPDATE, "w");
  62.   if(f == NULL)
  63.     return;
  64.   fwrite(registros_generales, sizeof(struct registro), cant_registros_leidos, f);
  65.  
  66.   fclose(f);
  67. }
  68.  
  69. int validar(int b, int u, char data[]){
  70.   int v;
  71.   scanf("%d", &v);
  72.   while(v < b && v > u){
  73.     printf("Error: reingrese el %s:/n", data);
  74.     scanf("%d", &v);
  75.   }
  76.   return v;
  77. }
  78.  
  79. int main(){
  80.   int i;
  81.   int vendedor, n_articulo, dia;
  82.   float cant_vendida;
  83.   float importe_vendido_julio[31] = {0.0};
  84.   int menor_vendida = 1000, menor_vendedor=0;
  85.   int rechazado=0, total_ventas=0, pedido_inferior=0;
  86.   int estado_venta;
  87.   struct registro registros_generales[CANT_REGISTROS];
  88.   test_prepare();
  89.   int cant_registros_leidos = read_articulo(registros_generales);
  90.   if(cant_registros_leidos == -1){
  91.     printf("Error leyendo el archivo inicial. Abra de nuevo el programa");
  92.     return 0;
  93.   }
  94.   printf("Ingrese El codigo del vendedor: \n");
  95.   vendedor = validar(0, 20, "vendedor");
  96.   while(vendedor != 0){
  97.     printf("Ingrese el numero del articulo: \n");
  98.     scanf("%d", &n_articulo);
  99.     printf("Ingrese el dia: \n");
  100.     dia = validar(1, 30, "dia");
  101.     printf("Ingrese la cantidad vendida: \n");
  102.     scanf("%f", &cant_vendida);
  103.  
  104.     if(menor_vendida > cant_vendida){
  105.       menor_vendida = cant_vendida;
  106.       menor_vendedor = vendedor;
  107.     }
  108.  
  109.     importe_vendido_julio[dia] += cant_vendida;
  110.     estado_venta = actualizar_articulo_array(cant_vendida, n_articulo, cant_registros_leidos, registros_generales);
  111.  
  112.     if(estado_venta == 0)
  113.       pedido_inferior++;
  114.     else if(estado_venta == -1)
  115.       rechazado++;
  116.  
  117.     total_ventas++;
  118.  
  119.     printf("Ingrese El codigo del vendedor: \n");
  120.     vendedor = validar(0, 20, "vendedor");
  121.   }
  122.  
  123.   actualizar_articulo_archivo(cant_registros_leidos, registros_generales);
  124.  
  125.   if(menor_vendida != 1000)
  126.     printf("El vendedor %d vendio lo minimo: %d\n", menor_vendedor, menor_vendida);
  127.  
  128.  
  129.   if(total_ventas > 0){
  130.     printf("Dia\tImporte Vendido\n");
  131.     for(i=1;i<31;i++)
  132.       if(importe_vendido_julio[i] > 0)
  133.         printf("%d \t %.2f\n", i, importe_vendido_julio[i]);
  134.   }
  135.  
  136.   if(total_ventas > 0)
  137.     printf("Porcentaje de movimientos rechazados: %.2f\n", calculo_porcentaje(rechazado, total_ventas));
  138.  
  139.   if(cant_registros_leidos >0)
  140.     printf("Porcentaje de articulos debajo del punto de pedido: %.2f\n", calculo_porcentaje(pedido_inferior, cant_registros_leidos));
  141.  
  142.   return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement