Recent

Author Topic: help please :c  (Read 5912 times)

deewfrank

  • Newbie
  • Posts: 4
help please :c
« on: August 04, 2016, 08:33:46 pm »
Hi, first sorry for my english, im from Chile, i have a program who load an grid in lazarus.

but in the other grid i need to calculate the
total sales of every product with a button click(in my program are 10 products)
and i dont know how to do it :( any little help please and sorry for my very bad english, hope you can understand, here is my code

(pic of my first grid with 10000 reg)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
  9.   ComCtrls, StdCtrls, Types;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     ImageList1: TImageList;
  18.     ImageList2: TImageList;
  19.     ImageList3: TImageList;
  20.     ImageList4: TImageList;
  21.     MUESTRA_DATOS: TButton;
  22.     PageControl: TPageControl;
  23.     StringGrid1: TStringGrid;
  24.     StringGrid2: TStringGrid;
  25.     VER_VENTAS: TTabSheet;
  26.     VER_TOTAL_VENTAS: TTabSheet;
  27.     procedure Button1Click(Sender: TObject);
  28.     procedure MUESTRA_DATOSClick(Sender: TObject);
  29.     procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  30.               aRect: TRect; aState: TGridDrawState);
  31.     procedure StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  32.               aState: TGridDrawState);
  33.     procedure StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer;
  34.               aRect: TRect; aState: TGridDrawState);
  35.   private
  36.     { private declarations }
  37.   public
  38.     { public declarations }
  39.   end;
  40.  
  41.   type
  42.   eFicha = record
  43.     reg   : Integer;
  44.     prod  : Integer;
  45.     venta : Integer;
  46.     fpago : Integer;
  47.   end;
  48.  
  49. var
  50.   Form1: TForm1;
  51.   registros : array[1..10000] of eFicha;
  52.   ingresa: eFicha;
  53.   archivo1 : File of eFicha;
  54.   archivo2 : File of eFicha;
  55.   archivo3 : File of eFicha;
  56.   totalventa1 : Integer;
  57.   totalventa2 : Integer;
  58.   totalventaprod1 : Integer;
  59.   totalventaprod2 : Integer;
  60.   c : Integer;
  61.   d : Integer;
  62.   e : Integer;
  63.   x : Integer;
  64.  
  65. implementation
  66.  
  67. {$R *.lfm}
  68.  
  69. procedure cargadatos;
  70.  
  71. var
  72.    e : integer;
  73.    precios: array [1..10] of  Integer = (1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000);
  74.    productos: array [1..10] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  75.    formapago: array [1..4] of Integer = (0, 1, 2, 3);
  76.  
  77. begin
  78.    randomize;
  79.    for e := 1 to 10000 do
  80.    begin
  81.      registros[e].reg := e;
  82.      registros[e].prod := (productos[low(productos) + random(length(productos))]);
  83.      registros[e].venta := (precios[low(precios) + random(length(precios))]);
  84.      registros[e].fpago := (formapago[low(formapago) + random(length(formapago))]);
  85.    end;
  86. end;
  87.  
  88. procedure escribedatos;
  89.  
  90. var
  91.   x : integer;
  92.  
  93. begin
  94.   assignfile(archivo1,'registros.bin');
  95.   rewrite(archivo1);
  96.   for x := 1 to 10000 do
  97.   try
  98.     ingresa.reg:= registros[x].reg;
  99.     ingresa.prod:= registros[x].prod;
  100.     ingresa.venta:= registros[x].venta;
  101.     ingresa.fpago:= registros[x].fpago;
  102.     write(archivo1,ingresa);
  103.   except
  104.     writeLn('Error al escribir en archivo');
  105.   end;
  106.   close(archivo1);
  107. end;
  108.  
  109. { TForm1 }
  110. procedure TForm1.MUESTRA_DATOSClick(Sender: TObject);
  111.  
  112. var
  113.   x : integer;
  114.  
  115. begin
  116.    assignfile(archivo2,'registros.bin');
  117.    c:=0;
  118.    x:=1;
  119.    reset(archivo2);
  120.    while NOT EOF(archivo2)do
  121.    begin
  122.        seek(archivo2,c);
  123.        BlockRead(archivo2,registros[x],1);
  124.        With Form1.StringGrid1 do
  125.        begin
  126.          RowCount:=RowCount+1;
  127.          Row:=RowCount;
  128.          Cells[0,Row]:= IntToStr(registros[x].reg);
  129.          Cells[1,Row]:= IntToStr(registros[x].prod);
  130.          Cells[2,Row]:= '$'+IntToStr(registros[x].venta);
  131.          Cells[3,Row]:= IntToStr(registros[x].fpago);
  132.          x:=x+1;
  133.        end;
  134.        c:=c+1;
  135.    end;
  136.    CloseFile(archivo2);
  137. end;
  138.  
  139. procedure TForm1.Button1Click(Sender: TObject);
  140. begin
  141.      for d := 1 to 10000 do
  142.      x:=1;
  143.      With Form1.StringGrid2 do
  144.      begin
  145.         Cells[1,1]:= IntToStr(registros[x].venta);
  146.         Cells[1,2]:= IntToStr(registros[x].venta);
  147.         Cells[1,3]:= IntToStr(totalventaprod1);
  148.         Cells[1,4]:= IntToStr(totalventaprod2);
  149.         x:=x+1;
  150.      end;
  151. end;
  152.  
  153.  
  154. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  155.   aRect: TRect; aState: TGridDrawState);
  156.  
  157. begin
  158.      With Form1.StringGrid1 do
  159.      if (aCol = 3) then
  160.      begin
  161.         if cells[aCol,aRow] = '0' then
  162.         begin
  163.              ImageList1.Draw(Canvas, aRect.Left, aRect.Top,0);
  164.         end;
  165.         With Form1.StringGrid1 do
  166.         if cells[aCol,aRow] = '1' then
  167.         begin
  168.              ImageList1.Draw(Canvas, aRect.Left, aRect.Top,1);
  169.         end;
  170.         With Form1.StringGrid1 do
  171.         if cells[aCol,aRow] = '2' then
  172.         begin
  173.             ImageList1.Draw(Canvas, aRect.Left, aRect.Top,2);
  174.         end;
  175.         With Form1.StringGrid1 do
  176.         if cells[aCol,aRow] = '3' then
  177.         begin
  178.             ImageList1.Draw(Canvas, aRect.Left, aRect.Top,3);
  179.         end;
  180.      end;
  181.  
  182.      With Form1.StringGrid1 do
  183.      if (aCol = 1) then
  184.      begin
  185.         if cells[aCol,aRow] = '1' then
  186.         begin
  187.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,0);
  188.         end;
  189.         With Form1.StringGrid1 do
  190.         if cells[aCol,aRow] = '2' then
  191.         begin
  192.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,1);
  193.         end;
  194.         With Form1.StringGrid1 do
  195.         if cells[aCol,aRow] = '3' then
  196.         begin
  197.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,2);
  198.         end;
  199.         With Form1.StringGrid1 do
  200.         if cells[aCol,aRow] = '4' then
  201.         begin
  202.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,3);
  203.         end;
  204.         With Form1.StringGrid1 do
  205.         if cells[aCol,aRow] = '5' then
  206.         begin
  207.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,4);
  208.         end;
  209.         With Form1.StringGrid1 do
  210.         if cells[aCol,aRow] = '6' then
  211.         begin
  212.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,5);
  213.         end;
  214.         With Form1.StringGrid1 do
  215.         if cells[aCol,aRow] = '7' then
  216.         begin
  217.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,6);
  218.         end;
  219.         With Form1.StringGrid1 do
  220.         if cells[aCol,aRow] = '8' then
  221.         begin
  222.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,7);
  223.         end;
  224.         With Form1.StringGrid1 do
  225.         if cells[aCol,aRow] = '9' then
  226.         begin
  227.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,8);
  228.         end;
  229.         With Form1.StringGrid1 do
  230.         if cells[aCol,aRow] = '10' then
  231.         begin
  232.              ImageList2.Draw(Canvas, aRect.Left, aRect.Top,9);
  233.         end;
  234.      end;
  235. end;
  236.  
  237. procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState);
  238.  
  239. var
  240.   ts: TTextStyle;
  241. begin
  242.   if (ACol < 5) and (ARow = 0) then
  243.   begin
  244.     ts := StringGrid1.Canvas.TextStyle;
  245.     ts.Alignment := taCenter;
  246.     StringGrid1.Canvas.Font.Style := [fsBold];
  247.     StringGrid1.Canvas.TextStyle := ts;
  248.   end;
  249.   if (ACol = 2) and (ARow > 0) then
  250.   begin
  251.     ts := StringGrid1.Canvas.TextStyle;
  252.     ts.Alignment := taRightJustify;
  253.     StringGrid1.Canvas.Font.Style := [fsBold];
  254.     StringGrid1.Canvas.TextStyle := ts;
  255.   end;
  256.   if (ACol = 0) and (ARow > 0) then
  257.   begin
  258.     ts := StringGrid1.Canvas.TextStyle;
  259.     ts.Alignment := taCenter;
  260.     StringGrid1.Canvas.Font.Style := [fsBold];
  261.     StringGrid1.Canvas.TextStyle := ts;
  262.   end;
  263. end;
  264.  
  265. procedure TForm1.StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer;
  266.   aRect: TRect; aState: TGridDrawState);
  267. begin
  268.      With Form1.StringGrid2 do
  269.      if (ACol = 0) and (ARow > 0) then
  270.      begin
  271.           if cells[aCol,aRow] = '0' then
  272.           begin
  273.                ImageList3.Draw(Canvas, aRect.Left, aRect.Top,0);
  274.           end;
  275.           if cells[aCol,aRow] = '1' then
  276.           begin
  277.                ImageList3.Draw(Canvas, aRect.Left, aRect.Top,1);
  278.           end;
  279.           if cells[aCol,aRow] = '2' then
  280.           begin
  281.                ImageList3.Draw(Canvas, aRect.Left, aRect.Top,2);
  282.           end;
  283.           if cells[aCol,aRow] = '3' then
  284.           begin
  285.                ImageList3.Draw(Canvas, aRect.Left, aRect.Top,3);
  286.           end;
  287.      end;
  288.      With Form1.StringGrid2 do
  289.      if (ACol > 0) and (ARow = 0) then
  290.      begin
  291.           if cells[aCol,aRow] = '0' then
  292.           begin
  293.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,0);
  294.           end;
  295.           if cells[aCol,aRow] = '1' then
  296.           begin
  297.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,1);
  298.           end;
  299.           if cells[aCol,aRow] = '2' then
  300.           begin
  301.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,2);
  302.           end;
  303.           if cells[aCol,aRow] = '3' then
  304.           begin
  305.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,3);
  306.           end;
  307.           if cells[aCol,aRow] = '4' then
  308.           begin
  309.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,4);
  310.           end;
  311.           if cells[aCol,aRow] = '5' then
  312.           begin
  313.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,5);
  314.           end;
  315.           if cells[aCol,aRow] = '6' then
  316.           begin
  317.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,6);
  318.           end;
  319.           if cells[aCol,aRow] = '7' then
  320.           begin
  321.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,7);
  322.           end;
  323.           if cells[aCol,aRow] = '8' then
  324.           begin
  325.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,8);
  326.           end;
  327.           if cells[aCol,aRow] = '9' then
  328.           begin
  329.                ImageList4.Draw(Canvas, aRect.Left, aRect.Top,9);
  330.           end;
  331.      end;
  332. end;
  333.  
  334. begin
  335.   cargadatos;
  336.   escribedatos;
  337. end.  



lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: help please :c
« Reply #1 on: August 04, 2016, 10:27:44 pm »
Tenes que iterar sobre "registros": guardar el total en $ y el total en cantidades para cada uno de los 10 productos, después ya sabés como usar esos valores para ponerlos en la otra TStringGrid.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4.   p1,..,p10: integer; // totales $
  5.   c1,..,c10: integer; // totales cantidad
  6. begin
  7.   p1 := 0;
  8.   ...
  9.   p10 := 0;
  10.   c1 := 0;
  11.   ...
  12.   c10 := 0;
  13.  
  14.   for i := 1 to 10000 do
  15.   begin
  16.     case registros[i].prod of
  17.       1: begin
  18.          p1 := p1 + registros[i].venta;
  19.          c1 := c1 + 1;
  20.       end;
  21.       2: begin
  22.  
  23.       end;
  24.       3: begin
  25.  
  26.       end;
  27.       4: begin
  28.  
  29.       end;
  30.       5: begin
  31.  
  32.       end;
  33.       6: begin
  34.  
  35.       end;
  36.       7: begin
  37.  
  38.       end;
  39.       8: begin
  40.  
  41.       end;
  42.       9: begin
  43.  
  44.       end;
  45.       10: begin
  46.  
  47.       end;
  48.     end;
  49.   end;
  50. end;  


Es una forma de hacerlo. Otra forma es tener un array de todos los productos con registros y directamente iterar sobre ello:

Code: Pascal  [Select][+][-]
  1. type
  2. resultados = record
  3.   total_dinero: integer;
  4.   total_cantidad: integer;
  5. end;
  6.  
  7. productos: array[1..10] of resultados;
  8.  
  9. for i:=0 to 10000 do
  10. begin
  11.   productos[registros.prod].total_dinero += registros[i].venta;
  12.   productos[registros.prod].total_cantidad += 1;
  13. end;

deewfrank

  • Newbie
  • Posts: 4
Re: help please :c
« Reply #2 on: August 04, 2016, 10:50:29 pm »
Tenes que iterar sobre "registros": guardar el total en $ y el total en cantidades para cada uno de los 10 productos, después ya sabés como usar esos valores para ponerlos en la otra TStringGrid.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4.   p1,..,p10: integer; // totales $
  5.   c1,..,c10: integer; // totales cantidad
  6. begin
  7.   p1 := 0;
  8.   ...
  9.   p10 := 0;
  10.   c1 := 0;
  11.   ...
  12.   c10 := 0;
  13.  
  14.   for i := 1 to 10000 do
  15.   begin
  16.     case registros[i].prod of
  17.       1: begin
  18.          p1 := p1 + registros[i].venta;
  19.          c1 := c1 + 1;
  20.       end;
  21.       2: begin
  22.  
  23.       end;
  24.       3: begin
  25.  
  26.       end;
  27.       4: begin
  28.  
  29.       end;
  30.       5: begin
  31.  
  32.       end;
  33.       6: begin
  34.  
  35.       end;
  36.       7: begin
  37.  
  38.       end;
  39.       8: begin
  40.  
  41.       end;
  42.       9: begin
  43.  
  44.       end;
  45.       10: begin
  46.  
  47.       end;
  48.     end;
  49.   end;
  50. end;  


Es una forma de hacerlo. Otra forma es tener un array de todos los productos con registros y directamente iterar sobre ello:

Code: Pascal  [Select][+][-]
  1. type
  2. resultados = record
  3.   total_dinero: integer;
  4.   total_cantidad: integer;
  5. end;
  6.  
  7. productos: array[1..10] of resultados;
  8.  
  9. for i:=0 to 10000 do
  10. begin
  11.   productos[registros.prod].total_dinero += registros[i].venta;
  12.   productos[registros.prod].total_cantidad += 1;
  13. end;

Muchas gracias por responder!!, intentare hacer lo que me dijiste, esta parte me tiene super enredado  :(

PD: Dejo una foto del recuadro del cual hablo, saludos.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: help please :c
« Reply #3 on: August 04, 2016, 11:58:25 pm »
En este caso como el resultado tiene que ir por tarjeta tenés que usar un registro para cada tarjeta:

type
resultados = record
  total_dinero_tarjeta1: integer; //efectivo
  total_dinero_tarjeta2: integer; // master
  total_dinero_tarjeta3: integer; //visa
  total_dinero_tarjeta4: integer; //cheque
end;

usarias el mismo array

productos: array[1..10] of resultados; // todos los productos tienen los 4 medios de pago

sumar los 4 totales no es dificil.

simplemente usa un if--else para saber en que tarjeta guardar.

deewfrank

  • Newbie
  • Posts: 4
Re: help please :c
« Reply #4 on: August 05, 2016, 01:35:20 am »
En este caso como el resultado tiene que ir por tarjeta tenés que usar un registro para cada tarjeta:

type
resultados = record
  total_dinero_tarjeta1: integer; //efectivo
  total_dinero_tarjeta2: integer; // master
  total_dinero_tarjeta3: integer; //visa
  total_dinero_tarjeta4: integer; //cheque
end;

usarias el mismo array

productos: array[1..10] of resultados; // todos los productos tienen los 4 medios de pago

sumar los 4 totales no es dificil.

simplemente usa un if--else para saber en que tarjeta guardar.

Muchísimas gracias  lainz!! ya tengo casi todo listo, un abrazo desde Chile, gracias por ayudarme  :D

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: help please :c
« Reply #5 on: August 05, 2016, 10:26:33 am »
Bienvenido. :D

Un consejo: pon títulos descriptivos a los mensajes. "help please :c" no dice nada acerca de tu problema, y mucha gente puede pasar por encima sin entrar siquiera si no saben de qué va el tema.  Hazlo también para ayudar al buscador del foro.
___________________________

Welcome. :D

An advice: use descriptive titles in your messages. "help please :c" doesn't tells about your problem and a lot of people will not read it if they don't know what are you talking about.  It also help the forum's search engine.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

deewfrank

  • Newbie
  • Posts: 4
Re: help please :c
« Reply #6 on: August 05, 2016, 10:27:44 am »
Bienvenido. :D

Un consejo: pon títulos descriptivos a los mensajes. "help please :c" no dice nada acerca de tu problema, y mucha gente puede pasar por encima sin entrar siquiera si no saben de qué va el tema.  Hazlo también para ayudar al buscador del foro.
___________________________

Welcome. :D

An advice: use descriptive titles in your messages. "help please :c" doesn't tells about your problem and a lot of people will not read it if they don't know what are you talking about.  It also help the forum's search engine.

Gracias! jaja lo tendre en cuenta, es que no sabia como resumir el problema  :D

 

TinyPortal © 2005-2018