Recent

Author Topic: need help with show a file like this  (Read 41416 times)

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: need help with create a file like this
« Reply #90 on: September 28, 2015, 11:24:08 pm »
GMP_47, for your encode_date you will need to use:
DecodeDate (function in pascal)    //google to know how to use
shl   (reserved word in pascal)  //google to know how to use
and (reserved word in pascal)  //google to know how to use
or    (reserved word in pascal)  //google to know how to use
generate y_offset according the value of year
the right bit mask for day, month and y_offset (which is already there).



GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with create a file like this
« Reply #91 on: September 29, 2015, 01:07:24 am »
Code: [Select]
function decode_date ( sdate:word ): TDateTime;
begin
     y_offset := (sdate  AND %1111111000000000) shr 9;
     month    := (sdate AND  %0000000111100000) shr 5;
     day      := (sdate  AND %0000000000011111);
 
     if y_offset < 100 then
       year := 2000 + y_offset
     else
       year := 1999 - y_offset + 100;
       result := EnCodeDate(year,month,day);
end;

function encode_date( stdate :TDateTime ) : word;
begin
     y_offset := (stdate  OR %0000000001111111)shl 9;
     month    := (stdate OR  %0000000000001111)shl 5;
     day      := (stdate  OR %0000000000011111);

     if y_offset > 100 then
       year := 2000 - y_offset
     else
       year := 1999 + y_offset - 100;
     result := DecodeDate(stdate,year,month,day);
end;


got error in results. I dont know how to write the results.

edit: I've set TDateTime = type double; and comented (removed) function encode_date. it compiles but date is just a large number and  a +
Writeln ('Fecha Modificacion: ', decode_date(Header.Date) );
« Last Edit: September 29, 2015, 02:16:48 am by GMP_47 »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with create a file like this
« Reply #92 on: September 29, 2015, 04:51:26 am »
First of all a hint. When using variables inside a function/procedure, sometimes they are bound to that function (that is even advised)

In that case it's much better to write something like:
Code: [Select]
function decode_date ( sdate:word ): TDateTime;
var
  y_offset : word;
  month    : word;
  day      : word;
  year     : word;
begin
  y_offset := (sdate  AND %1111111000000000) shr 9;
  month    := (sdate AND  %0000000111100000) shr 5;
  day      := (sdate  AND %0000000000011111);

  if y_offset < 100 then
    year := 2000 + y_offset
  else
    year := 1999 - y_offset + 100;
  result := EnCodeDate(year,month,day);
end;

... so that way you will not mess up your _global_ defined variables.

Quote
got error in results. I dont know how to write the results.
If you take a good look at the description of DecodeDate you'll read the following:

Quote
DecodeDate decodes the Year, Month and Day stored in Date, and returns them in the Year, Month and Day variables.
That is why the declaration of DecodeDate reads:
Quote
procedure DecodeDate(
  Date: TDateTime;
  out Year: Word;
  out Month: Word;
  out Day: Word
);
.. because the given Date will be decoded and returned in the variables (prefixed with out) Year, Month and Day.

Did you remember what rvk said about doing the reverse ?

Once you have decoded the encoded Date variable in individual Year, Month and Day variables, _then_ you'll be able to construct the stored date as described in the assignment.

But i'm afraid that all advise given won't help you much. For sure it looks likes you don't even master the basics of the programming language itself  :-\

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with create a file like this
« Reply #93 on: September 29, 2015, 11:35:03 am »
I put those in the main Var.
Code: [Select]
begin
  assign(BinaryStream, 'C:\Dev-Pas\EXAMEN2.dat');
  reset(BinaryStream, 1);
  myReadWord(Header.Serial);
  myReadString(Header.Filename);

  decode_date(myReadWord(Header.Date));

  myReadWord(Header.Fieldnrs);


  for nrs := 0 to Header.Fieldnrs - 1 do
  begin
       myReadWord (Fields[nrs].Fieldnr);
       myReadString (Fields[nrs].Fieldname);
  end;
       myReadWord(Header.NrsRecords);
       Writeln ('Nro. de Serie: ', Header.Serial);
       Writeln ('Full Filename: ', Header.Filename);
       Writeln ('Fecha Modificacion: ', year,'/', month,'/', day);
       Writeln ('Cantidad de Campos Customizados: ', Header.Fieldnrs);

rvk

  • Hero Member
  • *****
  • Posts: 6695
Re: need help with create a file like this
« Reply #94 on: September 29, 2015, 11:40:33 am »
I put those in the main Var.
Code: [Select]
  decode_date(myReadWord(Header.Date));
You treat decode_date as a procedure here. You don't do anything with the result of that function. In the olden days of Turbo Pascal the line you have now wasn't even possible because you don't assign the decode_date() result to anything. Below is an example line how to use it.

--------
Oef...  %) The others are right... at this moment you lack some basic programming knowledge in Pascal. That's nothing to be ashamed of, everyone needs to learn at some point, but it does mean we need to go back to the basics. So I apologize to the others for going really basic in this post. (I'm not a teacher so I hope I can explain this clearly.)

First... you've been given a perfectly good function by shobits1 where you (like molly already pointed out) stripped out the local variable. Why? (Do you know the difference between local and global variables??) You only need those variable year, day etc. in that procedure so keep them there. Don't make them global because it will only confuse you.
Code: [Select]
function decode_date( sdate:word): TDateTime;
var
  year : integer;
  day, month, y_offset: byte;
  begin
    y_offset := (sdate AND %1111111000000000) shr 9;
    month    := (sdate AND %0000000111100000) shr 5;
    day      := (sdate AND %0000000000011111);
    if y_offset < 100 then
      year := 2000 + y_offset
   else
      year := 1999 - y_offset + 100;
    result := EncodeDate(year, month, day);
  end;

When I do the following it works perfectly so don't mess with it. Try to understand the function before you change it.
Code: [Select]
Writeln('Fecha Modificacion: ', datetostr(decode_date(Header.Date)));

Next your encode-procedure:
Code: [Select]
function encode_date( stdate :TDateTime ) : word;
begin
  y_offset := (stdate OR %0000000001111111) shl 9;
  month    := (stdate OR %0000000000001111) shl 5;
  day      := (stdate OR %0000000000011111);
  if y_offset > 100 then
    year := 2000 - y_offset
  else
    year := 1999 + y_offset - 100;
  result := DecodeDate(stdate,year,month,day);
end;
I have 2 major problems (besides some little ones) with that one.
First one is this
Code: [Select]
  y_offset := (stdate OR %0000000001111111) shl 9;
You think it's the opposite of this:
Code: [Select]
  y_offset := (sdate AND %1111111000000000) shr 9;
Lets create another example (bear with me):
Code: [Select]
X := (Y + 5) / 5
Now I want the reverse. I mean by that... I want you to do "Y := something".
(note: please pay attention to the brackets. Do you remember the order in which everything is calculated?)
Can you give me the correct line???

So in this line
Code: [Select]
  y_offset := (stdate OR %0000000001111111) shl 9;
I don't want to calculate y_offset, but I want to calculate stdate FROM y_offset.
Can you give me the correct line???

Next problem is that I've already given you 3 times the way you need to build the encode_date() function. Begin at the end of decode_date() and do those instruction at the beginning of encode_date().
Let's look at another simple example:
Code: [Select]
function foo(Xin: word): Word;
var
  helper: Word;
begin
  helper := Xin + 5;
  helper := helper / 5
  Result := helper; 
end;
How would you "reverse this procedure" ?????
I want this to work:
Code: [Select]
  Q := foo(5);
  Z := foo_reversed(Q);
  writeln('Z should be 5');
How would your foo_reversed look like?? With what instruction would you begin that function and what instruction second?


Maybe if I give you a description of what encode_date() needs to do it will become clear:
encode_date() gets a stdate parameter which is a TDateTime in Object Pascal format (i.e. double). You want the result of the function to be a WORD with specification as given (with bits). So you first need to extract the year, month and day from the stdate (that should be your FIRST lines in the function, i.e. decodeDate). Then you need to calculate y_offset which you're going to use in your bitwise-operations. AFTER that you need to do the bitwise operation. Starting with stdate as 0 and adding the result of those 3 bitwiseoperations to it.     

I hope that with that description you can create a new encode_date() which will work better.


Edit: Before you get to the bitwise-operations in your encode_date() function I'll give you another big hint. OR is not the opposite of AND. Look again at the wiki. The opposite of AND is XOR. But it might be better to clear your head when you come to those instructions because you don't even need AND or XOR for encode_date(). Just the shl's are sufficient. Because if you shift bits to the left the bits which appear at the right are already 0. But you'll see when you get there.
« Last Edit: September 29, 2015, 12:07:27 pm by rvk »

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with create a file like this
« Reply #95 on: September 29, 2015, 06:01:54 pm »
thanks rvk I'll give it a try. but what if I just create a procedure that, like decode_date, calculates year/month/day and just shows it up on the screen?
I'm hoping to finish SHOW.pas before tomorrow night, it's the only time in the week I get to see Pablo.
( I came back from college just now I have to get some sleep cuz I'm going back at night, so I dont have much time today to code)

Code: [Select]
procedure decodificar_date ( var y_offset,month,day,year:word);
begin
     y_offset := (sdate  AND %1111111000000000) shr 9;
     month    := (sdate AND  %0000000111100000) shr 5;
     day      := (sdate  AND %0000000000011111);
 
     if y_offset < 100 then begin
       year := 2000 + y_offset
     end else begin
       year := 1999 - y_offset + 100;
     end;
Writeln ('Fecha Modificacion: ', year,'/', month,'/', day);
end;
« Last Edit: September 29, 2015, 06:14:39 pm by GMP_47 »

rvk

  • Hero Member
  • *****
  • Posts: 6695
Re: need help with create a file like this
« Reply #96 on: September 29, 2015, 06:22:52 pm »
but what if I just create a procedure that, like decode_date, calculates year/month/day and just shows it up on the screen?
You could do it like that.
But it's not really "clean" or clear (or "pascal-like").
Decoding the date to the internal TDateTime format and returning it to you main program-flow is much more pascal-like (working with procedures and functions).
Also... now you have a writeln in a procedure (away from all the other writeln's).
Also not really "clean".

But it does works. So if you're happy with it, who am I to complain  :D

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: need help with create a file like this
« Reply #97 on: September 29, 2015, 11:47:35 pm »
Quote
..The opposite of AND is XOR ..
rvk, XOR is not the opposite of AND; the opposite of AND is NAND or NOT AND.

rvk

  • Hero Member
  • *****
  • Posts: 6695
Re: need help with create a file like this
« Reply #98 on: September 30, 2015, 03:08:38 pm »
Quote
..The opposite of AND is XOR ..
rvk, XOR is not the opposite of AND; the opposite of AND is NAND or NOT AND.
Mmm, NAND isn't defined as an operator in FPC, is it?
(We might use NOT(x AND y) as x NAND y)

Anyway... it also depends on what you call opposite or reverse.
You also might consider "(NOT x) AND y", or x AND (NOT y) opposite of "x AND y"  :D
(It all depends how you look at it)

But correct, XOR might not be a good choice for any of the considered opposite-definitions.

Luckily NAND, XOR or AND are not necessary here, since shifting to the left already clears the bit appearing at the right.

Thaddy

  • Hero Member
  • *****
  • Posts: 16672
  • Kallstadt seems a good place to evict Trump to.
Re: need help with create a file like this
« Reply #99 on: September 30, 2015, 03:13:27 pm »
@rvk:
It might be useful if you explain your overly use of brackets? And operator precedence?

If you did this because of clarity that is ok with me.

Also: shifting in this case or similar would reduce resolution by 1 bit, but that is not to put too fine a point to it (but might introduce bugs).
« Last Edit: September 30, 2015, 03:17:57 pm by Thaddy »
But I am sure they don't want the Trumps back...

rvk

  • Hero Member
  • *****
  • Posts: 6695
Re: need help with create a file like this
« Reply #100 on: September 30, 2015, 03:25:35 pm »
It might be useful if you explain your overly use of brackets? And operator precedence?
Yes, the overly use of brackets were for clarity.

For the NOT(x AND y) it was necessary. But for the others it was just for clarification.


The precedence of operators can be found here:
http://www.freepascal.org/docs-html/ref/refch12.html

Code: [Select]
Operator                          Precedence      Category                                 
------------------------------------------------------------------------
Not, @                            Highest (first) Unary operators                       
* / div mod and shl shr as << >>  Second          Multiplying operators
+ - or xor                        Third           Adding operators                             
= <> < > <= >= in is              Lowest (Last)   relational operators       

Quote
(note: The order in which expressions of the same precedence are evaluated is not guaranteed to be left-to-right. In general, no assumptions on which expression is evaluated first should be made in such a case. The compiler will decide which expression to evaluate first based on optimization rules.)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: need help with create a file like this
« Reply #101 on: September 30, 2015, 03:28:51 pm »
Quote
..The opposite of AND is XOR ..
rvk, XOR is not the opposite of AND; the opposite of AND is NAND or NOT AND.
Mmm, NAND isn't defined as an operator in FPC, is it?
(We might use NOT(x AND y) as x NAND y)

Anyway... it also depends on what you call opposite or reverse.

no its not opposite on binary operations has very narrow meaning it can't have more than 2 values so the opposite of 0 is 1, you do remember the AND table don't you? eg

And    0  |  1
         -------------
      0| 0 |  0  |
         -------------
      1| 0 |  1  |

the opposite is an operation that will have the opposite values in all cells not just one of them.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6695
Re: need help with create a file like this
« Reply #102 on: September 30, 2015, 03:36:55 pm »
no its not opposite on binary operations has very narrow meaning it can't have more than 2 values so the opposite of 0 is 1
Yes, but I wasn't talking about the literal (binary) opposite of the bits.

I was talking about the broad meaning of the opposite of a complete statement.

For example if you have this:
Code: [Select]
xxxxxxxxxxxxxxxx
0000111100001111 AND
----------------
0000xxxx0000xxxx =
I might mean with opposite this: This is with all the bits that could be set. Now I want the result with all the other bits.
"with all the other bits" being opposite of "all the bits that could be set".
Code: [Select]
xxxxxxxxxxxxxxxx
1111000011110000 AND
----------------
xxxx0000xxxx0000 =

You see where I'm going with this?

If case 1 was x AND %0000111100001111 then case 2 would be x AND NOT %0000111100001111.

Another question might be... do you want the opposite of the final result or did you want the opposite of one of the values before getting the result?


(I already stated that XOR was the wrong choice in any situation, binary or broad meaning)
« Last Edit: September 30, 2015, 03:52:11 pm by rvk »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: need help with create a file like this
« Reply #103 on: September 30, 2015, 07:45:23 pm »
no its not opposite on binary operations has very narrow meaning it can't have more than 2 values so the opposite of 0 is 1
Yes, but I wasn't talking about the literal (binary) opposite of the bits.

I was talking about the broad meaning of the opposite of a complete statement.
AND is a bitwise operator everything is bit based logic, it makes no sense to project its meaning outside the bit it self and it makes no difference how many bits are going to be processed. The opposite is defined per bit and must be opposite in all possible pairs.

For example if you have this:
Code: [Select]
xxxxxxxxxxxxxxxx
0000111100001111 AND
----------------
0000xxxx0000xxxx =
I might mean with opposite this: This is with all the bits that could be set. Now I want the result with all the other bits.
"with all the other bits" being opposite of "all the bits that could be set".
Code: [Select]
xxxxxxxxxxxxxxxx
1111000011110000 AND
----------------
xxxx0000xxxx0000 =

You see where I'm going with this?

If case 1 was x AND %0000111100001111 then case 2 would be x AND NOT %0000111100001111.

Another question might be... do you want the opposite of the final result or did you want the opposite of one of the values before getting the result?

(I already stated that XOR was the wrong choice in any situation, binary or broad meaning)
Although valid questions about opposites they have nothing to do with AND. AND is an operator its opposite must be an operator as well that given the same input will produce the opposite output the meaning of the input or output has no weight what so ever. I find my self on the same trap from time to time it helps to remember that any meaning AND might give to the data is just a byproduct of its attribute and not knowledge about the data.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with create a file like this
« Reply #104 on: September 30, 2015, 07:46:55 pm »
I'm trying to make it show the Header.Date. Now just crashes. I heard pablo is just lazy checking for errors.
Code: [Select]
program Crear;
uses SysUtils;
type
  THeader = record
    Serial: Word;   //2 bytes
    Filename: String[255];  //1 byte
    Date: Word;  //2 bytes
    Fieldnrs: Word;  //2 bytes
    NrsRecords: Word;  //2 bytes
  end;
  TFields = record
    Fieldnr: Word;   //2 bytes
    Fieldname: ShortString;   //1 byte
  end;
  TRecordField = record
    Fieldnr: Word;  //2 bytes
    FieldText: String[255];  //1 byte
  end;
    TDateTime = double;
var
  BinaryStream: File;
  Header: THeader;                     //Header.Serial Filename Date Fieldnrs NrsRecords
  Fields: array [0..20] of TFields;    //Fields.Fieldnr     Fields.Fieldname
  Rec: array[0..20] of TRecordField;   //Rec.Fieldnr        Rec.FieldText
  Len: Byte;
  nrs: Integer;  //2 bytes
  I,N: Integer;


procedure myRead(var Buffer; Size: Integer);
begin
    BlockRead(BinaryStream, Buffer, Size);
end;

procedure myReadWord(var W:Word);
begin
  myRead(W, 2);
  W := Swap(W);
end;

procedure myReadString(var S:ShortString);
begin
  myRead(Len, 1);
  S[0] := Chr(Len);
  myRead(S[1], Len);
end;

function decode_date ( sdate:word ): TDateTime;
var
  year : integer;
  day, month, y_offset: byte;
begin
     y_offset := (sdate  AND %1111111000000000) shr 9;
     month    := (sdate AND  %0000000111100000) shr 5;
     day      := (sdate  AND %0000000000011111);
 
     if y_offset < 100 then
       year := 2000 + y_offset
     else
       year := 1999 - y_offset + 100;
       result := EnCodeDate(year, month, day);
end;

//function encode_date( stdate :TDateTime ) : word;

begin
  assign(BinaryStream, 'C:\Dev-Pas\EXAMEN2.dat');
  reset(BinaryStream, 1);
  myReadWord(Header.Serial);
  myReadString(Header.Filename);
  myReadWord(Header.Fieldnrs);
  for nrs := 0 to Header.Fieldnrs - 1 do
  begin
       myReadWord (Fields[nrs].Fieldnr);
       myReadString (Fields[nrs].Fieldname);
  end;
       myReadWord(Header.NrsRecords);
       Writeln ('Nro. de Serie: ', Header.Serial);
       Writeln ('Full Filename: ', Header.Filename);
       Writeln('Fecha Modificacion: ', datetostr(decode_date(Header.Date)));
       Writeln ('Cantidad de Campos Customizados: ', Header.Fieldnrs);

       for nrs := 0 to Header.Fieldnrs - 1 do
       begin
            Writeln ('Campo [codigo: ',Fields[nrs].Fieldnr,', ','descripcion: ',Fields[nrs].Fieldname ,']' );
       end;
       Writeln ('Cantidad de Registros: ', Header.NrsRecords);
       Writeln ('------------------------');

       for N := 0 to Header.NrsRecords - 1 do
         begin
         myReadWord (Header.Fieldnrs);

                for I := 0 to Header.Fieldnrs - 1 do
                begin
                     myReadWord (Rec[I].Fieldnr);
                     myReadString (Rec[I].FieldText);
                     Writeln (Fields[I].Fieldname,': ',Rec[I].FieldText);
                end;
                Writeln ('------------------------');
       end;
Writeln ('----[FIN CONTENIDO DEL ARCHIVO]-----------------');
Close(BinaryStream);
ReadLn;
end.

 

TinyPortal © 2005-2018