Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions complex/complex.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
// complex.cpp
// Multiplying complex numbers

#include <iostream>
#include <complex> // required for the complex class
#include <complex>

using namespace std;

int main()
{
complex<float> num1{ 2.0, 2.0 }; // using C++11 uniform initialisation syntax
complex<float> num2{ 4.0, -2.0 }; // old syntax: complex<float> num2(4.0,-2.0);
using Comp = complex<double>;

int main() {

Comp a = 0.0;
Comp b = 0.0;
Comp c = 0.0;
char Continue;

while(Continue != 'q'){

cout << "Please enter a :" << endl;
cin >> a;
cout << "Please enter b :" << endl;
cin>> b;
cout << "Please enter c :" << endl;
cin >> c;


auto answer = num1 * num2; // using C++11 auto keyword,
// answer is of type: complex<float>
Comp Root = sqrt(pow(b,2) -4.0*a*c);

cout << "The answer is: " << answer << endl;
cout << "Or in a more familiar form: " << answer.real() << " + " << answer.imag() << "j" << endl;
Comp answer1 = (-b + Root)/(2.0*a);

// answer++;
Comp answer2 = (-b - Root)/(2.0*a);

cout << "The roots are: " << answer1.real() << " + " <<answer1.imag() << "j" << endl<< answer2.real() << " + " <<answer2.imag() << "j" << endl;


cout<< "Would you like to perform another calculation?"<< endl;
cout << "Enter y to continue or enter q to quit" << endl;

cin >> Continue;


}

return 0;
}

48 changes: 48 additions & 0 deletions guessing-game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;

int main() {

int UserGuess = 0 ;
int RandNum = 0;
int TurnCounter = 0;

srand(time(0));

RandNum =rand() % 100+1;


cout <<"Please enter your guess."<<endl;
while(TurnCounter<5){

cin >> UserGuess;

if (TurnCounter ==4 && UserGuess!=RandNum){
cout<< "You lose"<<endl;
break;
}


if(UserGuess>RandNum){
cout << "Guess lower" <<endl;
}

if (UserGuess<RandNum){
cout<< "Guess higher"<<endl;
}

if (UserGuess==RandNum){
cout<< "You win"<<endl;
break;
}



TurnCounter ++;
}

return 0;
}
61 changes: 38 additions & 23 deletions screen/scr_main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Exercising the Screen class
// Making the initial A
#include "screen.h"
#include <iostream>

Expand All @@ -7,27 +7,42 @@ using namespace std;
int main()
{
Screen myScreen{6,6};

myScreen.forward();
myScreen.set('*');
myScreen.down();

myScreen.clear(' ');
myScreen.move(1,3);
myScreen.set('*');
myScreen.move(3,3);
myScreen.set("---");

myScreen.display();
cout << endl;

myScreen.reSize(16,16);
myScreen.display();
myScreen.clear(' ');

myScreen.move(7,7);
myScreen.set("BIG");
myScreen.move(8,5);
myScreen.set("SCREEN");
myScreen.display();

return 0;
myScreen.forward();
myScreen.set('*');
myScreen.move(2,2);
myScreen.set('*');
myScreen.move(2,5);
myScreen.set('*');
myScreen.move(3,6);
myScreen.set('*');
myScreen.move(3,1);
myScreen.set('*');
myScreen.move(4,1);
myScreen.set('*');
myScreen.forward();
myScreen.set('*');
myScreen.forward();
myScreen.set('*');
myScreen.forward();
myScreen.set('*');
myScreen.forward();
myScreen.set('*');
myScreen.forward();
myScreen.set('*');
myScreen.move(5,1);
myScreen.set('*');
myScreen.move(6,1);
myScreen.set('*');
myScreen.move(5,6);
myScreen.set('*');
myScreen.move(6,6);
myScreen.set('*');

myScreen.display();

return 0;
}

116 changes: 114 additions & 2 deletions screen/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,49 @@ Screen::Screen(string::size_type height, string::size_type width, char bkground)
// character value of bkground
{ /* all the work is done with the member initialization list */ }

void Screen::EmptySquare(unsigned int x,unsigned int y, unsigned int length){ // x and y are the cords of the top left corner with length

if(x + (length-1) > _width){

cerr<<"Not enough space for entered dimensions" << endl;
return;
}

if(y + (length-1) > _height){

cerr<<"Not enough space for entered dimensions" << endl;
return;

}


clear(' ');
move(x,y); // sets original starting position
set('*');

for(int j =0; j <length -1 ;j++){ // moves cursor forward right continously printing stars
forward();
set('*');
}

for(int j =0; j <length -1 ;j++){ // moves cursor down continously printing stars
down();
set('*');
}

for(int j =0; j <length -1 ;j++){ // moves cursor back left continously printing stars
back();
set('*');
}

for(int j =0; j <length -2 ;j++){ // moves cursor up continously printing stars
up();
set('*');
}
display();

}

void Screen::forward()
{ // advance _cursor one screen element
++_cursor;
Expand All @@ -38,7 +81,7 @@ void Screen::up()
{ // move _cursor up one row of screen
// do not wrap around
if ( row() == 1 ) // at top?
cerr << "Screen::up - Cannot wrap around in a vertical direction" << endl;
_cursor = _cursor + (_width*(_height-1));
else
_cursor -= _width;

Expand All @@ -49,7 +92,7 @@ void Screen::down()
{ // move _cursor down one row of screen
// do not wrap around
if ( row() == _height ) // at bottom?
cerr << "Screen::down - Cannot wrap around in a vertical direction" << endl;
_cursor = _cursor - (_width*(_height-1));
else
_cursor += _width;

Expand All @@ -69,6 +112,32 @@ void Screen::move( string::size_type row, string::size_type col )
return;
}

void Screen::move(Direction dir)
{
if(dir == Direction::FORWARD){
forward();
}
if(dir == Direction::BACK){
back();
}
if(dir == Direction::UP){
up();
}
if(dir == Direction::DOWN){
down();
}

if(dir == Direction::HOME){
home();
}


if(dir == Direction::END){
end();
}

return;
}
char Screen::get( string::size_type row, string::size_type col )
{
// position _cursor
Expand Down Expand Up @@ -180,3 +249,46 @@ string::size_type Screen::row() const
return (_cursor + _width)/_width;
}

/*Three different instances of the const type in use include:

--const string::size_type TOP_LEFT = 0;
--This is used so that the user is unable to change the position of the top left corner of the screen

--string::size_type Screen::remainingSpace() const
--This is used to make sure that this member function can not change the member variable of the class

-- void Screen::set( const string& s )
-- This is used to make sure that the fucntion "set" cannot modify the variable "s" within the function "set"

The "at" function of string class used in function "reSize" returns a reference to the character at specified location "pos".
Bounds checking is performed, exception of type std::out_of_range will be thrown on invalid access
reference to the requested character.


* 4.3

No this is not a necessity because all of the functions called within the overloaded move function
already exist and it just lumps them all together in one function when they all exist separately anyway.
The function is just a copy of existing code.

/* 4.5

Yes you do need access to the private variables of the Screen class in order to implement the function as the width and height
need to be used for error checking as well as calling functions that utlise the private variables.
No a function like this does not, in our opinion, form part of the Screen class responsibilities as it would make more intuitive sense
to create another class purely for drawing shpaes as each class should have its own focus on a task and not get
complicated with functions that dont fit the purpose of the class.

*/

/*4.6

Yes instead of using a string type as the screen displayed you could implement a 2D character array to perform the same function which
would be more intutive as it it basically a grid just like a screen should be interperated.

It is easier to change the implementation rather than the interface because if the way the fucntions work are changed (for example using a 2D character array)
its less work than writing all new functions to work with the changes the developer wishes to make as the interface already perfoms the correct purpose.



*/
5 changes: 4 additions & 1 deletion screen/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ using namespace std;
// *size* of any string that can be held by the string class as well as any index into
// the string.

enum class Direction {HOME,FORWARD,BACK,UP,DOWN,END};

class Screen {
public:
// Screen's constructor
Expand All @@ -26,6 +28,7 @@ class Screen {
void home() { _cursor = 0; return; }
// place the cursor at the bottom-right corner of the screen
void end() { _cursor = _width * _height - 1; return; }
void EmptySquare(unsigned int x,unsigned int y, unsigned int length);
// move the cursor one position to the right
void forward();
// move the cursor one position to the left
Expand All @@ -36,7 +39,7 @@ class Screen {
void down();
// move the cursor to the specified row and column
void move(string::size_type row, string::size_type col);

void move(Direction dir);
// get the character at the cursor's current position
char get() const { return _screen[_cursor]; }
// get the character at the specified row and column
Expand Down
32 changes: 29 additions & 3 deletions stopwatch/StopWatch.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
#include "StopWatch.h"
#include <ctime>

using namespace std;

// returns the amount of time in seconds that has passed since the process (i.e. your program) started executing
// returns the amount of time in seconds that has passed since the process (i.e. your program) started executing
double getProcessTime()
{
clock_t time = clock();
return static_cast<double>(time)/CLOCKS_PER_SEC;
clock_t time = clock();
return static_cast<double>(time)/CLOCKS_PER_SEC;
}

StopWatch::StopWatch( double startTime, double endTime): _startTime(startTime), _endTime(endTime){};

void StopWatch::StartWatch()
{
_startTime = getProcessTime();
return;
}

void StopWatch::EndWatch()
{
_endTime = getProcessTime();
}

void StopWatch::ResetWatch()
{
_startTime = 0;
_endTime = 0;
}

double StopWatch::getFunctionTime() // calculate the lapsed time in calculating function
{
return _endTime - _startTime;
}

Loading