Recent

Author Topic: Need help with finding the lowest number  (Read 14707 times)

Pog_Champ

  • Guest
Need help with finding the lowest number
« on: October 19, 2016, 03:03:51 pm »
Hello guys  :)
As you can tell from the title i need some help with my program,I just started learning how to program and i'm already having some difficulties.
So i got a file with numbers that is Duom.txt and in it the numbers are:
2 9 20 510
15 51 68 -8 2 16 13
I need for the program to show what numbers were given ,find the lowest one and create a file with the results
so the file schould be:
What numbers were given 2 9 20 510
15 51 68 -8 2 16 13
The lowest one is -8.

And this is how far i got

 
program find_number;
const Cn = 100;
      CD = 'Duom.txt';
      CR = 'Prat22.rez';
type masyvas = array[0..Cn] of real;
var A,B :masyvas;
    n,
    k,
    u :integer;
    D,R: text;
//-----------------------------------
procedure Reading(var Failas:text);
var i:integer;
begin
    Readln(Failas,n,u);
    for i := 0 to n-1 do
        read(Failas,A);
end;
//--------------------------------------------------------------------------
procedure Format;
var i:integer;
begin

                   // I have no idea what to write here

end;
//--------------------------------------------------------
procedure Writing(E:string; M:masyvas; kiek:integer; var Failas:text);
var i:integer;
begin
     Writeln(Failas,E);
     for i := 0 to kiek-1 do
        write(Failas,M:6:2);
     writeln(Failas);
end;
begin
    Assign(D,CD);
    Reset(D);
    Assign(R,CR);
    Rewrite(R);
    Reading(D);
    Writing('What numbers were given',A,n,R);
    Format;
    Writing('The lowest one is ',B,k,R);
    Close(D);
    Close(R);
end.

Any help is really appreciated. :)
 

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Need help with finding the lowest number
« Reply #1 on: October 19, 2016, 03:48:01 pm »
Try to be more systematic and write down the algorithm first.
E.g. first you should read the numbers from the file.
Then you should analyze the numbers and find the smallest one.
And third you should write something for the result.

You have chosen too complex approach for your current tasks.
No need to write down everything in procedures that might make you loose the idea behind them.

Always try to do something "simple" first and then extend your program. Don't try to write the whole program at once.
Write something simple. Compile it and see the result. The error messages will point you to some obvious typos and errors in your code. E.g your program won't compile due to several reasons.

Code: Pascal  [Select][+][-]
  1. program find_number;
  2. const Cn = 100;
  3.       CD = 'Duom.txt';
  4.       CR = 'Prat22.rez';
  5. type masyvas = array[0..Cn] of real;
  6. var A,B :masyvas;
  7.     n,
  8.     k,
  9.     u :integer;
  10.     D,R: text;
  11. //-----------------------------------
  12. procedure Reading(var Failas:text);
  13. var i:integer;
  14. begin
  15.     Readln(Failas,n,u); //<------- Your file doesn't seem to contain "n" value. Neither I can't see anywhere you use "u"
  16.     for i := 0 to n-1 do
  17.         read(Failas,A); //<-------------- you can't read the array at once. You have to read elements one-by-one. Referring to an array element is A[i] which means i-th element
  18. end;
  19. //--------------------------------------------------------------------------
  20. procedure Format;
  21. var i:integer;
  22. begin
  23.  
  24.                    // I have no idea what to write here
  25. //<--------------- and I have no Idea what you need to do here :)
  26. end;
  27. //--------------------------------------------------------
  28. procedure Writing(E:string; M:masyvas; kiek:integer; var Failas:text); //<------ Are you really sure you need it THAT COMPLEX???
  29. var i:integer;
  30. begin
  31.      Writeln(Failas,E);
  32.      for i := 0 to kiek-1 do
  33.         write(Failas,M:6:2); //<-------- well, you can do it this way, but I'd recommend you adding a #9 separator after each record, just in case the number M is large. And again you wrongly refer to an array element
  34.      writeln(Failas);
  35. end;
  36. begin
  37.     Assign(D,CD);
  38.     Reset(D);
  39.     Assign(R,CR);
  40.     Rewrite(R);
  41.     Reading(D);
  42.     Writing('What numbers were given',A,n,R);
  43.     Format; //<---------- what do you expect to happen here?
  44.     Writing('The lowest one is ',B,k,R);
  45.     Close(D);
  46.     Close(R);
  47. end.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Need help with finding the lowest number
« Reply #2 on: October 19, 2016, 03:49:35 pm »
Smells like a hoework assignement to me.

Code: [Select]
procedure Reading(var Failas:text);
var i:integer;
begin
    Readln(Failas,n,u);
    for i := 0 to n-1 do
        read(Failas,A[i]);
end;

This wil fail.
Assuming the first number is the number of lines, you still have to read the first line and then the second.
You never process the end-of-line in the file and try to read 9 numbers form the first line, which will crash the program.

So, try to understand the difference between read() and readln().
Since you do NOT know how many numbers there are on the frist line, nor on the second line you cannot use a for loop.

You might use EOF and EOL functions while reading, or you can read each line as a string and extract the numbers from that.

About determining the lowest number:
First try to come up with an algorithm, no need for code at this stage.
Simply describe how you would do this, given that you have already read all the numbers.

Once done write "pseudocode".
Then translate into real code.

At each step, when you have difficulties, describe what you have done and what fails, then we can give some constructive feedback.

If you do not want to learn, but simply want a solution, there are people around here who will do your homework for you, in return for a donation to our project.

I do not do so.

Bart

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Need help with finding the lowest number
« Reply #3 on: October 19, 2016, 08:08:06 pm »
Smells like a homework assignment to me.
I Agree.
Still here is small Algorithm try and convert it to real code.
1.Open source file ** Assign
2.Goto the beginning of the file ** Reset
3.Read Line as String ** ReadLn
4.Iterate over String and extract numbers choosing the smallest one ** StrToInt
5.Repeat from 3 until end of the file
6.Close the file ** Close
7.Open result file for rewrite. ** ReWrite
8.Write result and save the file ** Write/WriteLn/Flush
9.Close the file ** Close
10.display result until key pressed ** Write/ReadLn

Done.

PS:
- All those function are documented and have examples of use, just google it.
- I don't think you need any array to hold the values of numbers unless stated so.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Need help with finding the lowest number
« Reply #4 on: October 19, 2016, 09:02:33 pm »
If indeed a homework assignment then it would be much better for TS to show the assignment in full. Right now there is quite some ambiguity.

- Indeed nowhere is it mentioned to use arrays.
- is it necessary to output the numbers given using the same ordering per line ?
- notice how 2 correspond to two lines and 9 to nine numbers. might be coincidental of course.

Each and every one of those influences a possible (correct) solution.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Need help with finding the lowest number
« Reply #5 on: October 20, 2016, 06:25:50 pm »
Seems Pog_Champ isn't interested anymore?

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Need help with finding the lowest number
« Reply #6 on: October 20, 2016, 09:07:13 pm »
Seems Pog_Champ isn't interested anymore?

Bart
Probably the assignment is due already.

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: Need help with finding the lowest number
« Reply #7 on: October 21, 2016, 08:09:30 pm »
Just searched YouTube and found over 12,400 tutorial videos on Lazarus/Free Pascal.  Even found one that could have easily been modified to do exactly what Pog_Champ wanted!

BZMan
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Need help with finding the lowest number
« Reply #8 on: October 22, 2016, 12:04:01 am »
Seems like Pog_Champ is not registered anymore as a member of this forum.
Probably we were not quick enough in supplying a ready made solution.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Need help with finding the lowest number
« Reply #9 on: October 22, 2016, 04:15:10 pm »
Here is the over-engineered solution:

Code: Pascal  [Select][+][-]
  1. program nf;
  2.  
  3. {$mode objfpc}
  4. {$H-}  //don't need ansistrings
  5.  
  6. uses
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   cthreads,
  9.   {$ENDIF}{$ENDIF}
  10.   Classes, SysUtils;
  11.  
  12. {$R *.res}
  13.  
  14. {.$DEFINE DEBUG}
  15. {.$DEFINE TEST}
  16.  
  17. type
  18.   TNumberFileOption = (nfoFirstValueIsNrOfLines, nfoSecondValueIsCount);
  19.   TNumberFileOptions = set of TNumberFileOption;
  20.   TSolution = (solLowest, solHighest);
  21.   TSolutions = set of TSolution;
  22.  
  23.   { TNumberFileStream }
  24.  
  25.   TNumberFileStream = class
  26.   private
  27.     FOptions: TNumberFileOptions;
  28.     FList: TList;
  29.     FExpectedNrOfLines: Integer;
  30.     FRealNrOfLines: Integer;
  31.     FExpectedCount: Integer;
  32.     FSolutions: TSolutions;
  33.     procedure SetOptions(AValue: TNumberFileOptions);
  34.     procedure Scan(AStream: TStream);
  35.     procedure AddToList(L: PtrInt);
  36.     procedure CheckFileValidity;
  37.     function CountLineEndings(S: String): Integer;
  38.     function Count: Integer;
  39.     function FindLowest: PtrInt;
  40.     function FindHighest: PtrInt;
  41.  
  42.     {$IFDEF DEBUG}
  43.     procedure DebugOutPut;
  44.     {$ENDIF}
  45.   protected
  46.   public
  47.     constructor Create;
  48.     destructor Destroy; override;
  49.     procedure ReadAssignmentFromFile(const Fn: String);
  50.     procedure DisplaySolution;
  51.     procedure SaveSolutionToFile(const Fn: String);
  52.     property Options: TNumberFileOptions read FOptions write SetOptions default [nfoFirstValueIsNrOfLines];
  53.     property RequiredSolutions: TSolutions read FSolutions write FSolutions default [solLowest];
  54.   end;
  55.  
  56.   EInvalidFileFormat = class (EStreamError);
  57.  
  58. function ListSortUp(Item1, Item2: Pointer): Integer;
  59. begin
  60.   Result := {%H-}{%H-}PtrInt(Item1) - {%H-}{%H-}PtrInt(Item2);
  61. end;
  62.  
  63. function ListSortDown(Item1, Item2: Pointer): Integer;
  64. begin
  65.   Result := {%H-}{%H-}PtrInt(Item2) - {%H-}{%H-}PtrInt(Item1);
  66. end;
  67.  
  68. procedure TNumberFileStream.SetOptions(AValue: TNumberFileOptions);
  69. begin
  70.   if FOptions = AValue then Exit;
  71.   FOptions := AValue;
  72. end;
  73.  
  74. procedure TNumberFileStream.Scan(AStream: TStream);
  75. var
  76.   C: Char;
  77.   S, LEString: String;
  78.   L: PtrInt;
  79. const
  80.   WhiteSpace = [#0,#9,#10,#13,#32];
  81.   NewLineChars = [#10,#13];
  82. begin
  83.   AStream.Position := 0;
  84.   if (AStream.Size <> 0) then
  85.     FRealNrOfLines := 1;
  86.   FList.Clear;
  87.   S:= '';
  88.   LEString := '';
  89.   while (AStream.Read(C{%H-}, 1) = 1) do
  90.   begin
  91.     if (C in NewLineChars) then
  92.     begin
  93.       LEString := LEString + C;
  94.     end;
  95.     if (C in WhiteSpace) then
  96.     begin
  97.       if (S <> '') then
  98.       begin
  99.         if not TryStrToInt(S, L) then
  100.           Raise EInvalidFileFormat.CreateFmt('Invalid file format: %s is not a number.',[S]);
  101.         S := '';
  102.         AddToList(L);
  103.       end;
  104.     end
  105.     else
  106.     begin
  107.       S := S + C;
  108.     end;
  109.   end;
  110.   FRealNrOfLines := CountLineEndings(LEString) + FRealNrOfLines;
  111.   if (C in NewLineChars) then Dec(FRealNrOfLines); //file ended with empty line
  112.  
  113.   CheckFileValidity;
  114.   {$IFDEF DEBUG}
  115.   DebugOutPut;
  116.   {$ENDIF}
  117. end;
  118.  
  119. procedure TNumberFileStream.AddToList(L: PtrInt);
  120. begin
  121.   if (FList.Count = 0) and (nfoFirstValueIsNrOfLines in FOptions) and (FExpectedNrOfLines = -1) then
  122.   begin
  123.     if (L <= 0) then
  124.       Raise EInvalidFileFormat.CreateFmt('Invalid file format: %d is not a valid value for "expected number of lines".',
  125.                                          [L]);
  126.     FExpectedNrOfLines := L
  127.   end
  128.   else
  129.   begin
  130.     if (FList.Count = 0) and (nfoSecondValueIsCount in FOptions) and (FExpectedNrOfLines <> -1) and (FExpectedCount = -1) then
  131.     begin
  132.       if (L < 0) then
  133.         Raise EInvalidFileFormat.CreateFmt('Invalid file format: %d is not a valid value for "expected amount of numbers".',
  134.                                            [L]);
  135.       FExpectedCount := L;
  136.     end
  137.     else
  138.       FList.Add({%H-}Pointer(L));
  139.   end;
  140. end;
  141.  
  142. procedure TNumberFileStream.CheckFileValidity;
  143. begin
  144.   //nfoFirstValueIsNrOfLines, nfoSecondValueIsCount
  145.   if (Count = 0) then
  146.     Raise EInvalidFileFormat.Create('Invalid file format: number of values is zero.');
  147.   if FOptions = [] then Exit;
  148.   if (nfoFirstValueIsNrOfLines in FOptions) and (FExpectedNrOfLines <> FRealNrOfLines) then
  149.     Raise EInvalidFileFormat.CreateFmt('Invalid file format:'+LineEnding+
  150.                                        'Expected number of lines (%d) does not match real number of lines (%d)',
  151.                                        [FExpectedNrOfLines,FRealNrOfLines]);
  152.   if (nfoSecondValueIsCount in FOptions) and (FExpectedCount <> FList.Count) then
  153.     Raise EInvalidFileFormat.CreateFmt('Invalid file format:'+LineEnding+
  154.                                        'Expected number of values (%d) does not match real number of values (%d)',
  155.                                        [FExpectedCount,FList.Count]);
  156. end;
  157.  
  158. function TNumberFileStream.CountLineEndings(S: String): Integer;
  159. //Assumes S only contains LineEnding characters: #10 and/or #13
  160. begin
  161.   S := AdjustLineBreaks(S);
  162.   Result := Length(S) div Length(LineEnding);
  163. end;
  164.  
  165. function TNumberFileStream.Count: Integer;
  166. begin
  167.   Result := FList.Count;
  168. end;
  169.  
  170. function TNumberFileStream.FindLowest: PtrInt;
  171. var
  172.   NewList: TList;
  173.   i: Integer;
  174. begin
  175.   NewList := TList.Create;
  176.   try
  177.     for i := 0 to FList.Count - 1 do
  178.       NewList.Add(FList.Items[i]);
  179.     NewList.Sort(@ListSortUp);
  180.     Result := {%H-}PtrInt(NewList.Items[0]);
  181.   finally
  182.     NewList.Free;
  183.   end;
  184. end;
  185.  
  186. function TNumberFileStream.FindHighest: PtrInt;
  187. var
  188.   NewList: TList;
  189.   i: Integer;
  190. begin
  191.   NewList := TList.Create;
  192.   try
  193.     for i := 0 to FList.Count - 1 do
  194.       NewList.Add(FList.Items[i]);
  195.     NewList.Sort(@ListSortDown);
  196.     Result := {%H-}PtrInt(NewList.Items[0]);
  197.   finally
  198.     NewList.Free;
  199.   end;
  200. end;
  201.  
  202. {$IFDEF DEBUG}
  203. procedure TNumberFileStream.DebugOutPut;
  204. var
  205.   i: Integer;
  206. begin
  207.   writeln('FExpectedNrOfLines = ',FExpectedNrOfLines);
  208.   writeln('FRealNrOfLines     = ',FRealNrOfLines);
  209.   writeln('FExpectedCount     = ',FExpectedCount);
  210.   writeln('Count              = ',FList.Count);
  211.   for i := 0 to FList.Count - 1 do
  212.   begin
  213.     writeln(' * ',i:2,#32,{%H-}PtrInt(FList.Items[i]));
  214.   end;
  215.   writeln('Lowest             = ',FindLowest);
  216.   writeln('Highest            = ',FindHighest);
  217. end;
  218. {$ENDIF}
  219.  
  220. constructor TNumberFileStream.Create;
  221. begin
  222.   FList := TList.Create;
  223.   FExpectedNrOfLines := -1;
  224.   FRealNrOfLines := 0;
  225.   FExpectedCount := -1;
  226.   FOptions := [nfoFirstValueIsNrOfLines];
  227.   FSolutions := [solLowest];
  228. end;
  229.  
  230. destructor TNumberFileStream.Destroy;
  231. begin
  232.   FList.Free;
  233.   inherited Destroy;
  234. end;
  235.  
  236. procedure TNumberFileStream.ReadAssignmentFromFile(const Fn: String);
  237. var
  238.   FS: TFileStream;
  239. begin
  240.   FS := TFileStream.Create(Fn, fmOpenRead or fmShareDenyNone);
  241.   try
  242.     Scan(FS);
  243.   finally
  244.     FS.Free;
  245.   end;
  246. end;
  247.  
  248. procedure TNumberFileStream.DisplaySolution;
  249. var
  250.   i: Integer;
  251. begin
  252.   if (FSolutions = []) then Exit;
  253.   writeln('Numbers found: ');
  254.   for i := 0 to FList.Count - 1 do
  255.   begin
  256.     write({%H-}PtrInt(FList.Items[i]), #32);
  257.   end;
  258.   writeln;
  259.   if (solLowest in FSolutions) then  writeln('Lowest number : ',FindLowest);
  260.   if (solHighest in FSolutions) then writeln('Highest number: ',FindHighest);
  261. end;
  262.  
  263. procedure TNumberFileStream.SaveSolutionToFile(const Fn: String);
  264. var
  265.   SL: TStringList;
  266.   S: String;
  267.   i: Integer;
  268. begin
  269.   if (FSolutions = []) then Exit;
  270.   SL := TStringList.Create;
  271.   try
  272.     SL.Add('Numbers Found: ');
  273.     S := '';
  274.     for i := 0 to FList.Count - 1 do
  275.     begin
  276.       S := S + IntToStr({%H-}PtrInt(FList.Items[i])) + #32;
  277.     end;
  278.     S := Trim(S);
  279.     SL.Add(S);
  280.     if (solLowest in FSolutions) then SL.Add(Format('Lowest number : %d',[FindLowest]));
  281.     if (solHighest in FSolutions) then SL.Add(Format('Highest number: %d',[FindHighest]));
  282.     SL.SaveToFile(Fn);
  283.   finally
  284.     SL.Free;
  285.   end;
  286. end;
  287.  
  288. procedure SyntaxHelp;
  289. begin
  290.   writeln('Syntax:');
  291.   writeln('nf --in=Filename1 [--out=Filename2] [--sol=Lowest|Highest|Both]');
  292. end;
  293.  
  294. function ParseCommandLine(out InFn, OutFn: String; out Sol: TSolutions): Boolean;
  295. {$IFNDEF TEST}
  296. var
  297.   i: Integer;
  298.   S:String;
  299.   P: SizeInt;
  300.   Ext: RawByteString;
  301.   {$IFDEF DEBUG}
  302.   ASol: TSolution;
  303.   {$ENDIF}
  304. {$ENDIF TEST}
  305. begin
  306.   {$IFDEF TEST}
  307.   InFn := 'in.txt';
  308.   OutFn := 'out.txt';
  309.   Sol := [solLowest, solHighest];
  310.   Result := True;
  311.   {$ELSE}
  312.   Result := False;
  313.   InFn := '';
  314.   OutFn := '';
  315.   Sol := [solLowest];
  316.   if (ParamCount < 1) or (ParamCount > 3)
  317.      or (UpperCase(ParamStr(1)) = '-h')
  318.      or (UpperCase(ParamStr(1)) = '--help')
  319.      or (UpperCase(ParamStr(1)) = '-?')
  320.      or (UpperCase(ParamStr(1)) = '/?') then
  321.   begin
  322.     Exit;
  323.   end;
  324.   for i := 1 to ParamCount do
  325.   begin
  326.     S := ParamStr(i);
  327.     if (Pos('--IN=',UpperCase(S)) = 1) then
  328.     begin
  329.       S := Copy(S, 6, MaxInt);
  330.       P := Pos(#32, S);
  331.       if (P > 0) then System.Delete(S, P, MaxInt);
  332.       InFn := Trim(S);
  333.     end
  334.     else if (Pos('--OUT=',UpperCase(S)) = 1) then
  335.     begin
  336.       S := Copy(S, 7, MaxInt);
  337.       P := Pos(#32, S);
  338.       if (P > 0) then System.Delete(S, P, MaxInt);
  339.       OutFn := Trim(S);
  340.     end
  341.     else if (Pos('--SOL=',UpperCase(S)) = 1) then
  342.     begin
  343.       S := Copy(S, 7, MaxInt);
  344.       P := Pos(#32, S);
  345.       if (P > 0) then System.Delete(S, P, MaxInt);
  346.       S := Trim(S);
  347.       case UpperCase(S) of
  348.         'LOWEST': Sol := [solLowest];
  349.         'HIGHEST': Sol := [solHighest];
  350.         'BOTH': Sol := [solLowest, solHighest];
  351.         else
  352.         begin
  353.           writeln('Ilegal parameter: ',ParamStr(i));
  354.           Exit;
  355.         end;
  356.       end;
  357.     end
  358.     else
  359.     begin
  360.       writeln('Illegal parameter: ',ParamStr(i));
  361.       Exit;
  362.     end;
  363.   end;
  364.   if (InFn = '') then
  365.   begin
  366.     writeln('InFn is empty');
  367.     Exit;
  368.   end;
  369.   if (OutFn = '') then
  370.   begin
  371.     OutFn := InFn;
  372.     Ext := ExtractFileExt(OutFn);
  373.     OutFn := Copy(OutFn, 1, Length(OutFn) - Length(Ext));
  374.     OutFn := OutFn + '-solution' + Ext;
  375.   end;
  376.   Result := True;
  377.   {$IFDEF DEBUG}
  378.   writeln('InFn  = ',InFn);
  379.   writeln('OutFn = ',OutFn);
  380.   writeln('Sol   = ');
  381.   for ASol in Sol do writeln(#32, ASol);
  382.   {$ENDIF DEBUG}
  383.   {$ENDIF TEST}
  384. end;
  385.  
  386.  
  387. var
  388.   NFS: TNumberFileStream;
  389.   InFn, OutFn: String;
  390.   Solutions: TSolutions;
  391.  
  392. begin
  393.   if ParseCommandLine(InFn, OutFn, Solutions) then
  394.   begin
  395.     NFS := TNumberFileStream.Create;
  396.     try
  397.       try
  398.         NFS.Options := [nfoFirstValueIsNrOfLines, nfoSecondValueIsCount];
  399.         NFS.RequiredSolutions := Solutions;
  400.         NFS.ReadAssignmentFromFile(InFn);
  401.         NFS.DisplaySolution;
  402.         NFS.SaveSolutionToFile(OutFn);
  403.       except
  404.         on E: EStreamError do
  405.         begin
  406.           write('An unhandled exception has occurred of type ',E.ClassName);
  407.           writeln(', with message:');
  408.           writeln(E.Message);
  409.         end;
  410.       end;
  411.     finally
  412.       NFS.Free;
  413.     end;
  414.   end
  415.   else
  416.     SyntaxHelp;
  417. end.
  418.  

Bart  :D

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Need help with finding the lowest number
« Reply #10 on: October 22, 2016, 04:25:21 pm »
Sure that stuff can be used in a school assignment  :D

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Need help with finding the lowest number
« Reply #11 on: October 22, 2016, 04:44:26 pm »
Sure that stuff can be used in a school assignment  :D

Of course it can.
No chance the teacher is going to suspect it's not written by TS.

I was unable to create more obfuscation unfortunately.
If only this were an assignment in C, at least then you can really over-enginere even a simple "hello world":

Code: C  [Select][+][-]
  1. [
  2.   uuid(2573F8F4-CFEE-101A-9A9F-00AA00342820)
  3.   ]
  4.   library LHello
  5.   {
  6.     // bring in the master library
  7.       importlib("actimp.tlb");
  8.       importlib("actexp.tlb");
  9.               // bring in my interfaces
  10.       #include "pshlo.idl"
  11.      [
  12.       uuid(2573F8F5-CFEE-101A-9A9F-00AA00342820)
  13.      ]
  14.  
  15.    cotype THello
  16.    {
  17.  
  18.      interface IHello;
  19.      interface IPersistFile;
  20.    };
  21. };
  22.   [
  23.   exe,
  24.   uuid(2573F890-CFEE-101A-9A9F-00AA00342820)
  25.   ]
  26.   module CHelloLib
  27.   {
  28.       // some code related header files
  29.      importheader(<windows.h>);
  30.      importheader(<ole2.h>);
  31.      importheader(<except.hxx>);
  32.      importheader("pshlo.h");
  33.      importheader("shlo.hxx");
  34.      importheader("mycls.hxx");
  35.       // needed typelibs
  36.     importlib("actimp.tlb");
  37.     importlib("actexp.tlb");
  38.     importlib("thlo.tlb");
  39.  
  40.      [
  41.       uuid(2573F891-CFEE-101A-9A9F-00AA00342820),
  42.       aggregatable
  43.      ]
  44.      coclass CHello
  45.      {
  46.            cotype THello;
  47.      };
  48. };
  49.  
  50.   #include "ipfix.hxx"
  51.   extern HANDLE hEvent;
  52.   class CHello : public CHelloBase
  53.   {
  54.   public:
  55.      IPFIX(CLSID_CHello);
  56.      CHello(IUnknown *pUnk);
  57.      ~CHello();
  58.      HRESULT  __stdcall PrintSz(LPWSTR pwszString);
  59.   private:
  60.      static int cObjRef;
  61. };
  62.  
  63. #include <windows.h>
  64. #include <ole2.h>
  65.  
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include "thlo.h"
  69. #include "pshlo.h"
  70. #include "shlo.hxx"
  71. #include "mycls.hxx"
  72.   int CHello::cObjRef = 0;
  73.   CHello::CHello(IUnknown *pUnk) : CHelloBase(pUnk)
  74. {
  75.     cObjRef++;
  76.     return;
  77. }
  78. HRESULT  __stdcall  CHello::PrintSz(LPWSTR pwszString)
  79. {
  80.     printf("%ws\n", pwszString);
  81.     return(ResultFromScode(S_OK));
  82. }
  83. CHello::~CHello(void)
  84. {
  85.   // when the object count goes to zero, stop the server
  86.   cObjRef--;
  87.   if( cObjRef == 0 )
  88.  
  89.       PulseEvent(hEvent);
  90.   return;
  91. }
  92.  
  93.  
  94. #include <windows.h>
  95. #include <ole2.h>
  96. #include "pshlo.h"
  97. #include "shlo.hxx"
  98. #include "mycls.hxx"
  99.   HANDLE hEvent;
  100.   int _cdecl main(
  101.   int argc,
  102.   char * argv[])
  103. {
  104.   ULONG ulRef;
  105.   DWORD dwRegistration;
  106.   CHelloCF *pCF = new CHelloCF();
  107.   hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  108.   // Initialize the OLE libraries
  109.   CoInitializeEx(NULL, COINIT_MULTITHREADED);
  110.   CoRegisterClassObject(CLSID_CHello, pCF, CLSCTX_LOCAL_SERVER,
  111.  
  112.       REGCLS_MULTIPLEUSE, &dwRegistration);
  113.   // wait on an event to stop
  114.   WaitForSingleObject(hEvent, INFINITE);
  115.   // revoke and release the class object
  116.   CoRevokeClassObject(dwRegistration);
  117.   ulRef = pCF->Release();
  118.   // Tell OLE we are going away.
  119.   CoUninitialize();
  120.   return(0);
  121. }
  122.  
  123.   extern CLSID CLSID_CHello;
  124.   extern UUID LIBID_CHelloLib;
  125.   CLSID CLSID_CHello = { /*  > 573F891-CFEE-101A-9A9F-00AA00342820 */
  126.       0x2573F891,
  127.       0xCFEE,
  128.       0x101A,
  129.  
  130.       { 0x9A, 0x9F, 0x00, 0xAA, 0x00, 0x34, 0x28, 0x20 }
  131.   };
  132.   UUID LIBID_CHelloLib = { /*  > 573F890-CFEE-101A-9A9F-00AA00342820 */
  133.       0x2573F890,
  134.       0xCFEE,
  135.       0x101A,
  136.      { 0x9A, 0x9F, 0x00, 0xAA, 0x00, 0x34, 0x28, 0x20 }
  137. };
  138.  
  139. #include <windows.h>
  140. #include <ole2.h>
  141. #include <stdlib.h>
  142. #include <string.h>
  143. #include <stdio.h>
  144. #include "pshlo.h"
  145. #include "shlo.hxx"
  146. #include "clsid.h"
  147.   int _cdecl main(
  148.   int argc,
  149.   char * argv[]) {
  150.   HRESULT  hRslt;
  151.  
  152.   IHello        *pHello;
  153.   ULONG  ulCnt;
  154.   IMoniker * pmk;
  155.   WCHAR  wcsT[_MAX_PATH];
  156.   WCHAR  wcsPath[2 * _MAX_PATH];
  157.   // get object path
  158.   wcsPath[0] = '\0';
  159.   wcsT[0] = '\0';
  160.   if( argc > 1) {
  161.      mbstowcs(wcsPath, argv[1], strlen(argv[1]) + 1);
  162.      wcsupr(wcsPath);
  163.      }
  164.   else {
  165.      fprintf(stderr, "Object path must be specified\n");
  166.       return(1);
  167.       }
  168.   // get print string
  169.   if(argc > 2)
  170.       mbstowcs(wcsT, argv[2], strlen(argv[2]) + 1);
  171.  
  172.   else
  173.      wcscpy(wcsT, L"Hello World");
  174.   printf("Linking to object %ws\n", wcsPath);
  175.   printf("Text String %ws\n", wcsT);
  176.   // Initialize the OLE libraries
  177.   hRslt = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  178.   if(SUCCEEDED(hRslt)) {
  179.       hRslt = CreateFileMoniker(wcsPath, &pmk);
  180.    if(SUCCEEDED(hRslt))
  181.    hRslt = BindMoniker(pmk, 0, IID_IHello, (void **)&pHello);
  182.      if(SUCCEEDED(hRslt)) {
  183.    // print a string out
  184.    pHello->PrintSz(wcsT);
  185.    Sleep(2000);
  186.  
  187.    ulCnt = pHello->Release();
  188.    }
  189.       else
  190.    printf("Failure to connect, status: %lx", hRslt);
  191.       // Tell OLE we are going away.
  192.       CoUninitialize();
  193.       }
  194.   return(0);
  195. }
  196.  

Notice that the TP help I found online for the SeekEoln function has an example that only needs a little modifications to get the job done  8-)

Bart

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Need help with finding the lowest number
« Reply #12 on: October 22, 2016, 04:50:17 pm »
I learned some C this year (the most basic stuff, is the first programming class in the university, so don't expect too much) and I can't read your code, so in fact I not learned C  ::)

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Need help with finding the lowest number
« Reply #13 on: October 22, 2016, 04:51:44 pm »
Quote
Here is the over-engineered solution:
Bart's broadminded, social day ...  :D

btw: I need help with my FREE PASCAL - PHOTOSHOP version... so if you don't mind ...

This is what I've got so far:
Code: Pascal  [Select][+][-]
  1. PROGRAM FPC_PHOTOSHOP;
  2.  {$mode objfpc}{$H+}
  3.  
  4.  USES
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.   cthreads,
  7.   {$ENDIF}{$ENDIF}
  8.   Interfaces,
  9.   Forms, Unit1;
  10.  
  11.   {$R *.res}
  12.  
  13. Begin
  14.  RequireDerivedFormResource:= True;
  15.  Application.Initialize;
  16.  Application.CreateForm(TForm1, Form1);
  17.  Application.Run;
  18. End.    
  19.  
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Need help with finding the lowest number
« Reply #14 on: October 22, 2016, 05:09:58 pm »
What filter did you use for the PROGRAM and USES?
I dunno that one...
Great visual effect! I must say.

[edit]
and Bart, don't forget macro's to obfuscate make your program more readable... :o
The C people have a A++ grade in that field.
« Last Edit: October 22, 2016, 05:23:01 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018