Delphi calculate hours - windows

I have a problem with delphi, I created a program that calculates working hours, this is a piece of code:
procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c,d,e,f,g,h,i,l,m,n,o,p,q,r,s,t,u,v,z,a1,b1,c1,d1,e1,f1:TTime;
begin
a := StrToTime(Edit1.Text);
b := StrToTime(Edit2.Text);
c := StrToTime(Edit3.Text);
d := StrToTime(Edit4.Text);
e := StrToTime(Edit5.Text);
f := StrToTime(Edit6.Text);
g := StrToTime(Edit7.Text);
h := StrToTime(Edit8.Text);
i := StrToTime(Edit9.Text);
l := StrToTime(Edit10.Text);
m := StrToTime(Edit11.Text);
n := StrToTime(Edit12.Text);
o := StrToTime(Edit13.Text);
p := StrToTime(Edit14.Text);
q := StrToTime(Edit15.Text);
r := StrToTime(Edit17.Text);
s := StrToTime(Edit18.Text);
t := StrToTime(Edit19.Text);
u := StrToTime(Edit20.Text);
v := StrToTime(Edit21.Text);
z := StrToTime(Edit22.Text);
a1 := StrToTime(Edit23.Text);
b1 := StrToTime(Edit24.Text);
c1 := StrToTime(Edit25.Text);
d1 := StrToTime(Edit26.Text);
e1 := StrToTime(Edit27.Text);
f1 := StrToTime(Edit28.Text);
Memo1.Text:=TimeTostr(b-a+d-c+f-e+h-g+l-i+n-m+o-n+q-p+s-r+u-t+z-v+b1-a1+d1-c1+f1-e1);
end;
end.
But when I click on calculate, if the total exceeds 24 hours, back to 0, how do I fix this?
Thanks

One has to guess about what you really intend, but I think that a and b represent the start and the end of a working day (respectively), and so does c and d, and so on. Then you want, instead of b - a + d - c + ...,
HoursBetween(a, b) + HoursBetween(c, d) + ...
(uses DateUtils).
Update
In a comment, the OP made a request to also see the minutes. Do:
var
minspan: integer;
...
minspan := MinutesBetween(a, b) + MinutesBetween(c, d) + ...;
Memo1.Text := IntToStr(minspan div 60) + ':' + IntToStr(minspan mod 60);

This is how to resolve your immediate problem:
Declare a new variable TotalTime the way you've declared a,b,c, etc.
Then remove the last line, and replace it with this:
.
TotalTime := b-a+d-c+f-e+h-g+l-i+n-m+o-n+q-p+s-r+u-t+z-v+b1-a1+d1-c1+f1-e1;
Memo1.Text:= IntToStr(Trunc(TotalTime))+' day(s), '+ TimeTostr(TotalTime);
That should show something like this:
3 day(s), 07:04:45
Besides this change, I would rethink the entire approach if I were you. Learn from what others say here, and don't let yourself be affected by downvotes or negative comments.

Related

What is that the Error of Illegal assignment and how to correct it?

procedure tri_selection(t: tab; n: Integer);
var
i, j, min, aux: Integer;
begin
for i := 1 to n - 1 do
begin
min := i;
for j := i + 1 to n do
if t[j] < t[min] then
j := min;
if min <> i then
begin
aux := t[i];
t[i] := t[min];
t[min] := aux;
end;
end;
end;
That's supposed to be a correct and well-known code to arrange integers from inferior to superior but compiler still insists saying "illegal assignment to for loop 'j' variable".
What's the problem?
The problem is here:
for j := i + 1 to n do
if t[j] < t[min] then
j := min; // <-- Not allowed to assign to FOR loop variable j
You are not allowed to assign to the for loop variable.
Perhaps you meant to write
for j := i + 1 to n do
if t[j] < t[min] then
min := j;
you forgot var before t in the header of the procedure

Change from fmt.Printf to exec.Command

How can I change the code instead of showing it (fmt.Printf) to execute a command (exec.Command)
Now I have this:
// Print keys
fmt.Printf("%x %34s %34s\n", padded, uaddr.EncodeAddress(), caddr.EncodeAddress())
How to give a variable value to 'g' and 'h':
v := "cmd"
n := "/C"
a := "testcmd"
b := "-connect=127.0.0.1"
c := "-port=3333"
d := "-user=username"
e := "-password=password"
f := "importaddress"
g := "AddressHere"
h := "MoreInfo"
z := exec.Command(v, n, a, b, c, d, e, f, g, h)
if err := z.Run(); err != nil {
fmt.Println("Error: ", err)
}
I need to give this variable valie:
h := fmt.Printf("%x\n", padded)
g := fmt.Printf("%34s\n", uaddr.EncodeAddress())
g := fmt.Printf("%34s\n", caddr.EncodeAddress())
execute the command twice with different variables
You can use fmt.Sprintf()
Example:
g := fmt.Sprintf("%s", uaddr.EncodeAddress())
Sprintf formats according to a format specifier and returns the resulting string. You can then use this same value for your variables.

Delphi Changing Dynamic Value of the GrayScale Weight

I know Delphi for almost a month,
I have found a function code that change the color of the gray-scale by changing the color weights, I would like to ask, is there a faster way than this code for changing color or weighting the color?
function tform1.changecolorweighting(coloredbmp:tbitmap):tbitmap;
Var
X, Y: Integer;
P : TColor;
r,g,b: byte;
RP,GP,BP:single;
changegray:tbitmap;
changecolor:tbitmap;
begin
x:=RedWeight.value+GreenWeight.value+BlueWeight.value;
RP:=RedWeight.value/x;
GP:=Greenweight.value/x;
BP:=BlueWeight.value/x;
changegray := tbitmap.Create;
changegray.Width := coloredbmp.Width;
changegray.Height := coloredbmp.Height;
changecolor.Assign(coloredbmp);
For X := 0 to changecolor.Width do
begin
For y := 0 to changecolor.Height do
begin
P := changecolor.Canvas.Pixels[X, Y];
r := (P and $00000FF);
g := (P and $00FF00) shr 8;
b := (P and $FF0000) shr 16;
changegray.Canvas.Pixels[X, Y] := round(r * RP + g * GP + b*BP) * $010101;
end;
end;
result := changegray;
end;
if there is someone of you has a faster way of changing the color weights, please correct the code that I have found in the internet, or if you have something to offer faster than that code, please help.
The code above, it takes 1 second before the gray-scale applied with the color weighting.
thank you
This is the answer that I'm looking for, it's from Embarcadero:
https://community.embarcadero.com/blogs/entry/converting-to-grayscale-with-tbitmapscanline-property-39051
procedure ToGray(aBitmap: Graphics.TBitmap; redweightvalue,greenweightvalue,blueweightvalue:integer);
var w, h: integer; CurrRow, OffSet: integer;
x: byte; pRed, pGreen, pBlue: PByte;
function RGBToGray(R, G, B: byte): byte;
var x:integer;
RP,GP,BP:single;
begin
x:=redweightvalue+greenweightvalue+blueweightvalue;
RP:=redweightvalue/x;
GP:=greenweightvalue/x;
BP:=blueweightvalue/x;
//Result := round(0.2989*R + 0.5870*G + 0.1141*B); // coeffs from Matlab
Result := round(rp*R + gp*G + bp*B);
end;
begin
if aBitmap.PixelFormat <> pf24bit then exit;
CurrRow := Integer(aBitmap.ScanLine[0]);
OffSet := Integer(aBitmap.ScanLine[1]) - CurrRow;
for h := 0 to aBitmap.Height - 1 do
begin
for w := 0 to aBitmap.Width - 1 do
begin
pBlue := pByte(CurrRow + w*3);
pGreen := pByte(CurrRow + w*3 + 1);
pRed := pByte(CurrRow + w*3 + 2);
x := RGBToGray(pRed^, pGreen^, pBlue^);
pBlue^ := x;
pGreen^ := x;
pRed^ := x;
end;
inc(CurrRow, OffSet);
end;
end;

Why do floats and ints = Nan? in go

package main
import (
"fmt"
"math"
)
func main() {
// x= +- sqrtB-4ac/2a
cal()
}
func cal() {
b := 3
a := 4
c := 2
b2 := float64(b*b)
ac := float64(4)*float64(a)*float64(c)
q := math.Sqrt(b2-ac)
fmt.Print(q)
}
This will output a NaN, but why. I am trying to make a quadratic calculator. All I want is for this to output the number.
Because you're trying to take the square root of a negative number which isn't a valid operation (not just in Go, in math) and so it returns NaN which is an acronym for Not A Number.
b := 3
a := 4
c := 2
b2 := float64(b*b) // sets b2 == 9
ac := float64(4)*float64(a)*float64(c) // ac == 32
q := math.Sqrt(b2-ac) // Sqrt(9-32) == Sqrt(-23) == NaN
fmt.Print(q)
q = math.Sqrt(math.Abs(b2-ac)) // suggested in comments does Sqrt(23) == ~4.79
// perhaps the outcome you're looking for.
EDIT: please don't argue semantics on the math bit. If you want to discuss square roots of negative numbers this isn't the place. Generally speaking, it is not possible to take the square root of a negative number.
Since you're taking the square root of a negative number, you've got an imaginary result (sqrt(-9) == 3i). This is assuredly NOT what you're trying to do. Instead, do:
func main() {
b := float64(3)
a := float64(4)
c := float64(2)
result := [2]float64{(-b + math.Sqrt(math.Abs(b*b - 4*a*c))) / 2 * a,
(-b - math.Sqrt(math.Abs(b*b - 4*a*c))) / 2 * a)}
fmt.Println(result)
}
You try Sqrt Negative Number for this reason return always NaN ( Not a Number )
I run you code and print the results:
b := 3
a := 4
c := 2
b2 := float64(b*b)
fmt.Printf("%.2f \n", b2)
ac := float64(4)*float64(a)*float64(c)
fmt.Printf("%.2f \n", ac)
fmt.Printf("%.2f \n", b2-ac)
q := math.Sqrt(b2-ac)
fmt.Print(q)
Console:
9.00
32.00
-23.00
NaN
Sqrt in Golang : https://golang.org/pkg/math/#Sqrt

Misunderstanding the usage of := in Go

I was reading this doc and saw the following fragment:
The := syntax is shorthand for declaring and initializing a variable, e.g. for var f string = "short" in this case.
f := "short"
fmt.Println(f)
The point is: is it only for strings? Or is it dymanic enough to understand what datatype should it store?
And plus: isn't it the same of var f = "short"?
Of course it infers the obvious type(s) returned by the expression on the right side.
The specification gives those examples :
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
r, w := os.Pipe(fd) // os.Pipe() returns two values
_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
Note that it's not dynamic : everything happens at compile time, the type(s) being given by the right part expression.

Resources