Calendar - for "REAL" programmers: Difference between revisions

Content added Content deleted
m (BaCon and BBC BASIC moved to the BASIC section.)
imported>Arakov
Line 1,046: Line 1,046:
30 </pre>
30 </pre>
=={{header|Elena}}==
=={{header|Elena}}==
ELENA 5.0 :
ELENA 6.x :
<syntaxhighlight lang="elena">import system'text;
<syntaxhighlight lang="elena">import system'text;
import system'routines;
import system'routines;
Line 1,053: Line 1,053:
import extensions'routines;
import extensions'routines;


const MonthNames = new string[]{"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER",
const MonthNames = new string[]{"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
"NOVEMBER","DECEMBER"};
const DayNames = new string[]{"MO", "TU", "WE", "TH", "FR", "SA", "SU"};
const DayNames = new string[]{"MO", "TU", "WE", "TH", "FR", "SA", "SU"};


class CalendarMonthPrinter
class CalendarMonthPrinter
{
{
Date theDate;
Date _date;
TextBuilder theLine;
TextBuilder _line;
int theMonth;
int _month;
int theYear;
int _year;
ref<int> theRow;
Reference<int> _row;
constructor(year, month)
constructor(year, month)
{
{
theMonth := month;
_month := month;
theYear := year;
_year := year;
theLine := new TextBuilder();
_line := new TextBuilder();
theRow := 0;
_row := 0;
}
}


writeTitle()
writeTitle()
{
{
theRow.Value := 0;
_row.Value := 0;
theDate := Date.new(theYear, theMonth, 1);
_date := Date.new(_year, _month, 1);

DayNames.forEach:(name)
{ theLine.print(" ",name) }
DayNames.forEach:(name)
{ _line.print(" ",name) }
}
}
writeLine()
writeLine()
{
{
theLine.clear();
_line.clear();

if (theDate.Month == theMonth)
if (_date.Month == _month)
{
{
var dw := theDate.DayOfWeek;
var dw := _date.DayOfWeek;

theLine.writeCopies(" ",theDate.DayOfWeek == 0 ? 6 : (theDate.DayOfWeek - 1));
_line.writeCopies(" ",_date.DayOfWeek == 0 ? 6 : (_date.DayOfWeek - 1));

do
do
{
{
theLine.writePaddingLeft(theDate.Day.toPrintable(), $32, 3);
_line.writePaddingLeft(_date.Day.toPrintable(), $32, 3);

theDate := theDate.addDays:1
_date := _date.addDays:1
}
}
until(theDate.Month != theMonth || theDate.DayOfWeek == 1)
until(_date.Month != _month || _date.DayOfWeek == 1)
};
};

int length := theLine.Length;
int length := _line.Length;
if (length < 21)
if (length < 21)
{ theLine.writeCopies(" ", 21 - length) };
{ _line.writeCopies(" ", 21 - length) };

theRow.append(1)
_row.append(1)
}
}
indexer() = new Indexer
indexer() = new Indexer
{
{
bool Available = theRow < 7;
bool Available = _row < 7;


int Index
int Index
{
{
get() = theRow.Value;
get() = _row.Value;
set(int index)
set(int index)
{
{
if (index <= theRow)
if (index <= _row)
{ self.writeTitle() };
{ self.writeTitle() };
while (index > theRow)
while (index > _row)
{ self.writeLine() }
{ self.writeLine() }
}
}
}
}


appendIndex(int index)
appendIndex(int index)
{
{
this self.Index := theRow.Value + index
this self.Index := _row.Value + index
}
}
get int Length() { ^ 7 }
get int Length() { ^ 7 }


get() = self;
get Value() = self;
set(o) { NotSupportedException.raise() }
set Value(o) { NotSupportedException.raise() }
};
};
printTitleTo(output)
printTitleTo(output)
{
{
output.writePadding(MonthNames[theMonth - 1], $32, 21)
output.writePadding(MonthNames[_month - 1], $32, 21)
}
}
printTo(output)
printTo(output)
{
{
output.write(theLine.Value)
output.write(_line.Value)
}
}
}
}


class Calendar
class Calendar
{
{
int theYear;
int _year;
int theRowLength;
int _rowLength;
constructor new(int year)
constructor new(int year)
{
{
theYear := year;
_year := year;
theRowLength := 3
_rowLength := 3
}
}
printTo(output)
printTo(output)
{
{
output.writePadding("[SNOOPY]", $32, theRowLength * 25);
output.writePadding("[SNOOPY]", $32, _rowLength * 25);
output.writeLine();
output.writeLine();
output.writePadding(theYear.toPrintable(), $32, theRowLength * 25);
output.writePadding(_year.toPrintable(), $32, _rowLength * 25);
output.writeLine().writeLine();
output.writeLine().writeLine();
var rowCount := 12 / theRowLength;
var rowCount := 12 / _rowLength;
var months := Array.allocate(rowCount).populate:(i =>
var months := Array.allocate(rowCount).populate:(i =>
Array.allocate(theRowLength)
Array.allocate(_rowLength)
.populate:(j =>
.populate:(j =>
new CalendarMonthPrinter(theYear, i * theRowLength + j + 1)));
new CalendarMonthPrinter(_year, i * _rowLength + j + 1)));
months.forEach:(row)
months.forEach:(row)
{
{
var r := row;
var r := row;
row.forEach:(month)
row.forEach:(month)
{
{
month.printTitleTo:output;
month.printTitleTo:output;
output.write:" "
output.write:" "
};
};
output.writeLine();
output.writeLine();
ParallelEnumerator.new(row).forEach:(line)
ParallelEnumerator.new(row).forEach:(line)
{
line.forEach:(printer)
{
{
line.forEach:(printer)
printer.printTo:output;
{
printer.printTo:output;


output.write:" "
output.write:" "
};
};


output.writeLine()
output.writeLine()
}
}
}
}
}
}
}
}


public program()
public program()
{
{
var calender := Calendar.new(console.write:"ENTER THE YEAR:".readLine().toInt());
var calender := Calendar.new(console.write:"ENTER THE YEAR:".readLine().toInt());
calender.printTo:console;
calender.printTo:console;

console.readChar()
console.readChar()
}</syntaxhighlight>
}</syntaxhighlight>
{{out}}
{{out}}
Line 1,246: Line 1,246:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
</pre>

=={{header|Fortran}}==
=={{header|Fortran}}==
Alas, the header "FORTRAN" is not recognised - REAL programmers were absent that day? Even upon the apperance of lower case, I have preferred to use shouting for programme source, and normal upper/lower case for commentary. Aside from petty details such as 1 and l being nowhere as distinct as 1 and L, this allows the two sorts of blather to be identifiably different without ratiocination as the hours drag past. Further, the names of variables can easily be distinguished from the same word in discussion, as in ... the text in TEXT will be printed as the subtitle to the text in TITLE ... Anyway, in the spirit of old, herewith the source without tedious commentary:
Alas, the header "FORTRAN" is not recognised - REAL programmers were absent that day? Even upon the apperance of lower case, I have preferred to use shouting for programme source, and normal upper/lower case for commentary. Aside from petty details such as 1 and l being nowhere as distinct as 1 and L, this allows the two sorts of blather to be identifiably different without ratiocination as the hours drag past. Further, the names of variables can easily be distinguished from the same word in discussion, as in ... the text in TEXT will be printed as the subtitle to the text in TITLE ... Anyway, in the spirit of old, herewith the source without tedious commentary: