Recent

Author Topic: Measurement converter in Pascal  (Read 2746 times)

dzoole

  • Guest
Measurement converter in Pascal
« on: March 16, 2022, 05:51:33 pm »
Hi, i' m a newbie in pascal programing. I have need for measurement converter in Pascal to use in Subtitle Workshop as pascal script.
Found some codes online for other converters, but can't figure out how to do it for example miles to kilometres.
Can someone help me to do it.

This is the pascal script.

unit units;

interface

uses
  Classes,
  Controls,
  Dialogs,
  Forms,
  StrUtils,
  SysUtils,
  Variants,
  Windows;

begin
function MilesToKillometres(MyNumber: variant): string;
function InchesToCentimetres(MyNumber: variant): string;
function FeetToCentrimetres(MyTens: variant): string;
function GalonsToLitres(MyDigit: variant): string;

implementation

function MilesToKillometres(MyNumber: variant): string;

var
Temp: string;
  Miles, Killometres, Words: string;
  km, iCount: string;

begin
  //Find any number before miles
  Words:= (MyNumber + 'miles');
end;

  begin
    //Convert Miles
    Temp  := LeftStr((MyNumber + 'miles') * 1,6);
    km := ' and ' + ConvertMiles(Temp) + ' km';
  end;
end.


winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Measurement converter in Pascal
« Reply #1 on: March 16, 2022, 06:12:53 pm »
Hi!

A simple solution which exstracts the number from a string,
coverts miles to km an gives a output:

Code: Pascal  [Select][+][-]
  1. function MilesToKm (miles: string) : string;
  2. var i : integer;
  3.     mileVal, kmVal : single;
  4.     tmp : string;
  5. begin
  6.   tmp := '';
  7.   for i := 1 to length(miles) do
  8.       if miles[i] in ['0'..'9','.'] then tmp := tmp+miles[i];
  9.   mileVal := strToFloat(tmp);
  10.   kmVal := 1.609344 * mileVal;
  11.   // kmVal := 1.852 * mileVal; ==>nautic mile
  12.   result := FloatToStr(kmVal)+' Kilometer';
  13.   end;            
  14.  
  15.  

The decimal separator in Pascal is a dot!

Winni

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Measurement converter in Pascal
« Reply #2 on: March 16, 2022, 06:13:34 pm »
In Free pascal/Lazarus, the unit with measurement conversions is convutils, and is modelled after the same unit in Delphi (though not 100% compatible yet)

Winni: strtofloat is locale dependent (using either comma or dot as decimal separator), while your set check isn't.

dzoole

  • Guest
Re: Measurement converter in Pascal
« Reply #3 on: March 16, 2022, 06:36:03 pm »
Thank you for your fast answer. And yes in my system locale , coma is decimal separator.
I'll try the script now, to see does it work in Subtitle Workshop.

I don't why but I can't compile it. When I run Subtitle Workshop says error compiler expected closed bracket ]
In Lazarus syntax check OK, but can't compile.
« Last Edit: March 16, 2022, 06:45:14 pm by dzoole »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Measurement converter in Pascal
« Reply #4 on: March 16, 2022, 06:57:31 pm »
Hi!

For Lazarus error: show us the error message. We have no crystall balls.

For Subtitle Workshop: Ask there. This is a Lazarus/fpc Forum.

Winni

dzoole

  • Guest
Re: Measurement converter in Pascal
« Reply #5 on: March 16, 2022, 07:35:40 pm »
Like I said i'm newbie at this, so it's probably my mistake.



VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Measurement converter in Pascal
« Reply #6 on: March 16, 2022, 07:59:34 pm »
"result" is the return of a function. Make a function to call in your program.
"miles" is not defined.
A program is terminated with "."
« Last Edit: March 16, 2022, 08:07:44 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Measurement converter in Pascal
« Reply #7 on: March 16, 2022, 08:27:21 pm »
hi dzoole

you have messed up winni code;

if you look it is not the same..
you have implementation section in the function code.

the function code should be seperate to the implementation interface etc

Code: Pascal  [Select][+][-]
  1.  
  2. // declares these functions/procedures as they could be used before they have been defined
  3. // ie if you had a function/procedure that called another one that was further down your program code.
  4. // you must only add the function/procedure definition; it must match exactly the actual function/procedure definition
  5. // and have no actual code in it.
  6.  
  7. Function MilesToKm (miles: string) : string;
  8.  
  9. implementation
  10.  
  11. Function MilesToKm (miles: string) : string;
  12. var i : integer;
  13.     mileVal, kmVal : single;
  14.     tmp : string;
  15. begin
  16.   tmp := '';
  17.   for i := 1 to length(miles) do
  18.       if miles[i] in ['0'..'9','.'] then tmp := tmp+miles[i];
  19.   mileVal := strToFloat(tmp);
  20.   kmVal := 1.609344 * mileVal;
  21.   // kmVal := 1.852 * mileVal; ==>nautic mile
  22.   result := FloatToStr(kmVal)+' Kilometer';
  23. end;
  24.  
  25. // all other functions here
  26.  
  27.  
  28. {$R *.lfm}    
  29.  

can you see the difference
« Last Edit: March 16, 2022, 08:36:34 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

dzoole

  • Guest
Re: Measurement converter in Pascal
« Reply #8 on: March 16, 2022, 09:01:42 pm »
Like i said its probably my mistake. Now, in which file i should paste wiini's code?
In Lazarus ->File->New-> and then what? Pascal Unit?

In Subtitle Workshop, software for subtittles where I want to use the pascal script, i get this.

dzoole

  • Guest
Re: Measurement converter in Pascal
« Reply #9 on: March 16, 2022, 09:33:43 pm »
According to this, i'm not on the right path.
https://docwiki.embarcadero.com/Libraries/Sydney/en/System.ConvUtils

 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Measurement converter in Pascal
« Reply #10 on: March 16, 2022, 10:04:38 pm »
Code: Pascal  [Select][+][-]
  1. program doconvertmilestokm;
  2. {$mode delphi}
  3. uses
  4.   ConvUtils,stdconvs,sysutils;
  5.  
  6.  
  7. var
  8.   miles,kilometers : Double;
  9.  
  10. begin
  11.   // Define the miles value
  12.   miles := 1;
  13.  
  14.   // Convert to km
  15.   kilometers := Convert(miles, duMiles, duKilometers);
  16.  
  17.   // Display both values
  18.   writeln(format('%f miles  = %f kilometers',[miles,kilometers ]));
  19. end.
  20.  

dzoole

  • Guest
Re: Measurement converter in Pascal
« Reply #11 on: March 16, 2022, 10:26:14 pm »
Now says coudn't identify ConvUtils.
Never mind, thanks anyway for your time.

af0815

  • Hero Member
  • *****
  • Posts: 1288
Re: Measurement converter in Pascal
« Reply #12 on: March 17, 2022, 07:14:49 am »
Now says coudn't identify ConvUtils.
You are not using fpc/Lazarus. Are you using Subtitle workshop with pascalscript ?

In Lazarus you have to make a new program with 'Project->New Project->Simple Program' to create a new programm and insert the sample code.
« Last Edit: March 17, 2022, 07:17:55 am by af0815 »
regards
Andreas

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Measurement converter in Pascal
« Reply #13 on: March 17, 2022, 07:49:14 am »
dz, if all you want to do is convert miles to kilometers, just multiply by 1.609, does not need any special units at all. Its a simple floating point calc, ability to do that is there already.

Do you really need a Pascal Script ?  Or just a binary called, eg miles2km ? A line of code to check you have a just a number on the command line, a line of code to multiply it by 1.609, write that to std out.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dzoole

  • Guest
Re: Measurement converter in Pascal
« Reply #14 on: March 17, 2022, 11:00:19 am »
I'm not using Subtitle Workshop to compile. I'm using it for subtitles, but if there is some problem with pascal scripts, subtitle workshop gives an info.
Subtitle Workshop is write in Delphi and can use pascal scripts for bunch of things with subtitles. Most of them are for timings, FPS, etc...
So the idea was this.
I don't know how to write those in Pascal.
Find any number before word miles and than multiple that number to 1.6 and replace word miles with kilometres.
And the idea was to add this type of conversion for feet to meters, inches to centimetres etc, etc..

Too bad that i can't multiple in reg ex
In reg ex i can only mark "miles" and convert into text "x1.6 kilometres" which will be reminder to turn on calculator and calculate.
<ERROR Find="[^0-9](miles)" ReplaceBy="x1.6 kilometres">

Find some pascal script for conversions without System.ConvUnits.
In the attachment

I don't know how to adapt for my purpose.
« Last Edit: March 17, 2022, 11:04:55 am by dzoole »

 

TinyPortal © 2005-2018