r/badcode • u/Petrov_Maxim • Nov 20 '21
other language One of my first programs. It is written in Pascal and allows you to count the number of days before a birthday.
439
Upvotes
r/badcode • u/Petrov_Maxim • Nov 20 '21
23
u/Petrov_Maxim Nov 20 '21 edited Nov 20 '21
If you want to try
Example of input:
23.10
20.11.2021
Code:
var j,a1,a2,b,b1,b2,b3:string; d,d1,m,m1,c,i,n,n1,l,h,o:integer; g,g1:integer; a:array[0..12] of integer = (0,31,28,31,30,31,30,31,31,30,31,30,31); begin readln(j); readln(b); a1:=copy(j,1,2); a2:=copy(j,4,2); b1:=copy(b,1,2); b2:=copy(b,4,2); b3:=copy(b,7,4); Val(a1,d,c); Val(a2,m,c); Val(b1,d1,c); Val(b2,m1,c);Val(b3,g,c);
if ((g mod 4)=0) or (((g mod 4)=0) and ((g mod 100)=0) and ((g mod 400)=0)) then n:=1 else n:=0; g1:=g+1; if ((g1 mod 4)=0) or (((g1 mod 4)=0) and ((g1 mod 100)=0) and ((g1 mod 400)=0)) then n1:=1 else n1:=0;
m:=m-1; m1:=m1-1;
if (m>m1) or ((m=m1) and (d>=d1)) then begin for i:=0 to m do l:=l+a[i]; d:=d+l; for i:=0 to m1 do h:=h+a[i]; d1:=d1+h; o:=d-d1; end;
if (m<m1) or ((m=m1) and (d<d1)) then if (n=0) and (n1=0) then begin for i:=0 to m do l:=l+a[i]; d:=d+l; for i:=0 to m1 do h:=h+a[i]; d1:=d1+h; o:=365-d1+d; end else if (n=1) and (n1=0) then begin for i:=0 to m do l:=l+a[i]; d:=d+l; a[2]:=29; for i:=0 to m1 do h:=h+a[i]; d1:=d1+h; o:=366-d1+d; end else if (n=0) and (n1=1) then begin a[2]:=29; for i:=0 to m do l:=l+a[i]; d:=d+l; a[2]:=28; for i:=0 to m1 do h:=h+a[i]; d1:=d1+h; o:=365-d1+d; end;
writeln(o);
end.
130
u/CopperyMarrow15 Nov 20 '21
the indentation gods would like to know your location
102
u/keddir Nov 20 '21
To be fair, calendars are hard. Just look at Python's implementation of datetime. Pretty much any code related to dates is somewhere between "mediocre" and "Hans, get the flamethrower"
18
u/ContinuousPuppy Nov 20 '21
Here is a transcription of the program:
12
u/wwelna Nov 20 '21
This is a work of art, you should print this out and frame it, and watch those that try to comprehend it have their brains melt out of their ears.
7
u/pumpkinplight Nov 20 '21
I never knew PL/SQL borrowed it's syntax from Pascal.
3
u/Ahmed4star Nov 20 '21
I wish I was blind
2
u/sad_developer Nov 20 '21
man!! imagine running a linter on this bad boy....
2
u/DelusionalPianist Nov 20 '21
I really wish I would still have my first program/game written in Turbo Pascal: It was essentially Tetris, but I didn't know how to read a char without waiting for the input, and I didn't know that you need to initialise the random generator :) It had multiple "levels" and each level was just copy past of the previous level with different and more symbols....
2
u/ContinuousPuppy Nov 20 '21 edited Nov 20 '21
You forgot to check for leap year (n=1) in the same year case (m>m1).
And the leap year check should be:
1
u/[deleted] Nov 20 '21
no wonder why common people think programming is hard
1
u/ywBBxNqW Nov 21 '21
Wow, /r/badcode
How dare you.
1
u/exintrovert Nov 21 '21
I was concerned about leap years there for a minute. You had me in the first half, ngl...
1
u/thanatica Nov 21 '21
Doing some custom leap year and days per month calculation, are we? 😀
Honestly, I can't remember if Pascal had a date library. I imagine there's a chance it probably maybe did.
1
u/yaminokaze4 Nov 21 '21
tounsi?
1
u/yepMad Nov 21 '21
Programming is art
1
u/BrownThunder95 Nov 21 '21
Well we all have to start somewhere ya know
1
u/TheCakeWasNoLie Nov 21 '21
Pascal was the language they taught me in the first programmer's courses in university. Haven't touched it since. The := sign and the end. at the end are downright nostaligic to me.