chore: Add course material

This commit is contained in:
2026-10-05 11:27:21 +02:00
parent 971813c351
commit e8241fd1ff
660 changed files with 26658 additions and 1 deletions
+22
View File
@@ -0,0 +1,22 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Ex51", "Ex51.vcxproj", "{AFB7826A-449B-436C-A7FC-4401601F082B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AFB7826A-449B-436C-A7FC-4401601F082B}.Debug|Win32.ActiveCfg = Debug|Win32
{AFB7826A-449B-436C-A7FC-4401601F082B}.Debug|Win32.Build.0 = Debug|Win32
{AFB7826A-449B-436C-A7FC-4401601F082B}.Release|Win32.ActiveCfg = Release|Win32
{AFB7826A-449B-436C-A7FC-4401601F082B}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
+95
View File
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{AFB7826A-449B-436C-A7FC-4401601F082B}</ProjectGuid>
<RootNamespace>Ex51</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>C:\course\397\DOS_Unix;</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\DOS_Unix\Keyboard_DOS.cpp" />
<ClCompile Include="..\DOS_Unix\Screen_DOS.cpp" />
<ClCompile Include="Light.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Pelican.cpp" />
<ClCompile Include="PelicanTimer.cpp" />
<ClCompile Include="Pole.cpp" />
<ClCompile Include="Subject.cpp" />
<ClCompile Include="Timer.cpp" />
<ClCompile Include="TimerSet.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\DOS_Unix\Keyboard.h" />
<ClInclude Include="..\DOS_Unix\Screen.h" />
<ClInclude Include="Light.h" />
<ClInclude Include="Observer.h" />
<ClInclude Include="Pelican.h" />
<ClInclude Include="PelicanTimer.h" />
<ClInclude Include="Pole.h" />
<ClInclude Include="Subject.h" />
<ClInclude Include="Timer.h" />
<ClInclude Include="TimerSet.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+80
View File
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="DOS_Unix">
<UniqueIdentifier>{075c2eca-b76b-43df-8022-827ab031c5e2}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Light.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Pelican.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PelicanTimer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Pole.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Subject.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="TimerSet.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\DOS_Unix\Keyboard_DOS.cpp">
<Filter>DOS_Unix</Filter>
</ClCompile>
<ClCompile Include="..\DOS_Unix\Screen_DOS.cpp">
<Filter>DOS_Unix</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Light.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Observer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Pelican.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PelicanTimer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Pole.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Subject.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Timer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TimerSet.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\DOS_Unix\Keyboard.h">
<Filter>DOS_Unix</Filter>
</ClInclude>
<ClInclude Include="..\DOS_Unix\Screen.h">
<Filter>DOS_Unix</Filter>
</ClInclude>
</ItemGroup>
</Project>
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
+21
View File
@@ -0,0 +1,21 @@
#include "Light.h"
#include "Pelican.h"
#include "Screen.h"
#include <iostream>
Light::Light(std::string name, int x, int y, int color) : x(x), y(y), c(color), name(name) {}
Light::~Light() {
std::cout << "Deleting light (" << name << ", " << x << ", " << y << ", " << c << ")"
<< "\n";
}
void Light::set(LightState newState) {
state = newState;
Screen::write(name.c_str(), x, y, state == FLASH ? (128 + c) : (state == OFF ? 0 : c));
// 128 + color was used to make a character flash (but not any more)
}
Light::LightState Light::get(void) { return state; }
+30
View File
@@ -0,0 +1,30 @@
// ====================================================
//
// Represents a light, implemented as a string written
// to a console window
//
// ======================================================
#ifndef LIGHT_H
#define LIGHT_H
#include <string>
class Light {
public:
Light(std::string name, int x, int y, int color);
~Light();
enum LightState { ON, OFF, FLASH };
void set(LightState new_state);
LightState get(void);
private:
int c; // light colour
int x, y;
std::string name;
LightState state;
};
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef OBSERVER_H
#define OBSERVER_H
#include "Subject.h"
class Observer {
public:
virtual void update(const Hint &) = 0;
};
#endif
+59
View File
@@ -0,0 +1,59 @@
#include "Pelican.h"
#include "PelicanTimer.h"
#include "Pole.h"
Pelican::Pelican() : pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), state(GREEN_IDLE) {
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
}
void Pelican::press(void) {
if (state == GREEN_IDLE) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
pressed = true;
notify(WaitHint(pressed));
notify(LightHint(state));
} else if (state != RED_STATE) {
pressed = true;
notify(WaitHint(pressed));
}
}
void Pelican::timerExpired(void) {
switch (state) {
case RED_STATE:
state = YELLOW_FLASHING;
myTimer->timeoutRequest(FLASH_TIME);
break;
case YELLOW_FLASHING:
state = GREEN_TIMING;
myTimer->timeoutRequest(GREEN_TIME);
break;
case GREEN_TIMING:
if (pressed) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
} else {
state = GREEN_IDLE;
}
break;
case GREEN_IDLE:
// should never happen
break;
case YELLOW_STATE:
state = RED_STATE;
pressed = false;
notify(WaitHint(pressed));
myTimer->timeoutRequest(RED_TIME);
break;
}
notify(LightHint(state));
}
+65
View File
@@ -0,0 +1,65 @@
// ======================================================
//
// Represents a Pelican crossing
//
// ======================================================
#ifndef PELICAN_H
#define PELICAN_H
#include "Subject.h"
class PelicanTimer;
class Pelican : public Subject {
public:
Pelican();
// call when the WAIT button has been pressed
void press(void);
// states that a Pelican can be in
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
bool isPressed() const { return pressed; }
PelicanState getState() const { return state; }
private:
// state that this Pelican is in
PelicanState state;
// called by PeleicanTimer when the lights need to change
void timerExpired(void);
const unsigned int RED_TIME;
const unsigned int YELLOW_TIME;
const unsigned int GREEN_TIME;
const unsigned int FLASH_TIME;
// indicates whether the WAIT light should be on
bool pressed;
// Timer that indicates when the lights should change
PelicanTimer *myTimer;
private:
// prevent general use of copy ctor and operator=
Pelican(const Pelican &);
Pelican &operator=(const Pelican &);
};
class LightHint : public Hint {
public:
LightHint(Pelican::PelicanState s) : state(s) {}
Pelican::PelicanState state;
};
class WaitHint : public Hint {
public:
WaitHint(bool p) : pressed(p) {}
bool pressed;
};
#endif
+7
View File
@@ -0,0 +1,7 @@
#include "PelicanTimer.h"
PelicanTimer::PelicanTimer(Pelican &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {}
void PelicanTimer::doExpired() { (myTarget.*myTargetFunc)(); }
+27
View File
@@ -0,0 +1,27 @@
// ======================================================
//
// This class specializes the Timer to enable it to
// stimulate a Pelican
//
// ======================================================
#ifndef PelicanTimer_H
#define PelicanTimer_H
#include "Pelican.h"
#include "Timer.h"
class PelicanTimer : public Timer {
public:
typedef void (Pelican::*TargetFunc)();
PelicanTimer(Pelican &target, TargetFunc targetFunc);
private:
void doExpired();
Pelican &myTarget;
TargetFunc myTargetFunc;
};
#endif
+79
View File
@@ -0,0 +1,79 @@
#include "Pole.h"
#include "Screen.h"
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
myPelican.attach(*this);
using namespace Screen; // we will discuss namespaces in the course
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
write(" _____ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" |____| | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" _||__ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write("========||===========", x, ++y, GRAY);
write(" / || / ", x, ++y, GRAY);
write("_ /_________ /_ ", x, ++y, GRAY);
write("/ / ", x, ++y, GRAY);
// the only time we need to ask the Pelican its state is at start up
update(Hint());
}
Pole::~Pole() {
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
delete lights[i];
delete waitLight;
}
void Pole::setTrafficLights(Pelican::PelicanState state) {
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
// red yellow green green man red man
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
};
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
lights[i]->set(LightStates[state][i]);
}
}
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
void Pole::update(const Hint &h) {
const WaitHint *waitHintPtr;
const LightHint *lightHintPtr;
if ((waitHintPtr = dynamic_cast<const WaitHint *>(&h)) != 0) {
setWaitLight(waitHintPtr->pressed);
} else if ((lightHintPtr = dynamic_cast<const LightHint *>(&h)) != 0) {
setTrafficLights(lightHintPtr->state);
} else {
// don't know what's changed, so update everything
setTrafficLights(myPelican.getState());
setWaitLight(myPelican.isPressed());
}
}
+29
View File
@@ -0,0 +1,29 @@
#ifndef POLE_H
#define POLE_H
#include "Light.h"
#include "Observer.h"
#include "Pelican.h"
class Pole : public Observer {
public:
Pole(int x, int y, Pelican &p);
~Pole();
void setTrafficLights(Pelican::PelicanState state);
void setWaitLight(bool pressed);
void update(const Hint &);
private:
Pelican &myPelican;
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
Light *lights[RED_MAN + 1];
Light *waitLight;
};
#endif
+12
View File
@@ -0,0 +1,12 @@
#include "Subject.h"
#include "Observer.h"
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
void Subject::notify(const Hint &h) const {
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
(*iter)->update(h);
}
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef SUBJECT_H
#define SUBJECT_H
class Observer;
#include <set>
class Hint {
public:
virtual ~Hint() {}
};
class Subject {
public:
typedef std::set<Observer *> ObsList;
void attach(Observer &obs);
protected:
void notify(const Hint &) const;
private:
ObsList myObservers;
};
#endif
+23
View File
@@ -0,0 +1,23 @@
#include "Timer.h"
#include "TimerSet.h"
void Timer::timeoutRequest(int seconds) { timeToGo = seconds; }
Timer::Timer() : timeToGo(-1) { TimerSet::Instance().addATimer(this); }
void Timer::tick() {
if (isExpired()) {
doExpired();
}
}
bool Timer::isExpired() {
if (timeToGo > 0) {
if (--timeToGo == 0) {
return true;
}
}
return false;
}
+32
View File
@@ -0,0 +1,32 @@
// ======================================================
//
// A general-purpose countdown timer
// To use this class, specialize it
// and override tick()
//
// ======================================================
#ifndef timer_H
#define timer_H
#include <ctime>
class Timer {
public:
Timer();
void timeoutRequest(int seconds);
// template method
void tick();
protected:
bool isExpired();
private:
virtual void doExpired() = 0;
int timeToGo;
};
#endif
+37
View File
@@ -0,0 +1,37 @@
#include "TimerSet.h"
#include "Timer.h"
#include <iostream>
// Member functions
TimerSet::TimerSet()
: previousTime(time(&previousTime)), pollingThread([this]() { poll10Hz(); }) // Exercise 5.1 start thread
{
pollingThread.detach(); // let it die on its own, without join
}
// Exercise 5.1 new function
void TimerSet::poll10Hz() {
std::chrono::milliseconds pause(100);
while (true) {
std::this_thread::sleep_for(pause);
pollAllTimers();
}
}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
void TimerSet::pollAllTimers() {
std::time_t timeNow;
if (time(&timeNow) > previousTime) {
previousTime = timeNow;
for (auto it = timers.begin(); it != timers.end(); it++) {
(*it)->tick();
}
}
}
TimerSet &TimerSet::Instance() {
static TimerSet pInstance;
return pInstance;
}
+33
View File
@@ -0,0 +1,33 @@
#ifndef TIMERSET_H
#define TIMERSET_H
#include <ctime>
#include <thread>
#include <vector>
class Timer;
class TimerSet {
public:
void pollAllTimers();
void addATimer(Timer *);
private:
TimerSet();
std::vector<Timer *> timers;
std::time_t previousTime;
// Exercise 5.1 Threading two new members
std::thread pollingThread;
void poll10Hz();
// Meyer's Singleton
public:
static TimerSet &Instance();
private:
TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &);
};
#endif
+35
View File
@@ -0,0 +1,35 @@
#include "Keyboard.h"
#include "Pelican.h"
#include "Pole.h"
#include "TimerSet.h"
#include <cctype> // provides toupper()
int main() {
Keyboard kbd;
Pelican thePelican;
Pole pole1(20, 0, thePelican);
Pole pole2(40, 0, thePelican);
char c = 0;
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
// Exercise 5.1 no longer needed to poll TimerSet from
// while (!kbd.isCharAvail())
//{
// TimerSet::Instance().pollAllTimers(); // no key press to process, poll the timers
//}
c = kbd.getChar(); // get the character
if (c == ' ') {
// it's a space, so press the WAIT button
thePelican.press();
}
}
return 0;
}