lab08/build.bat
@echo off
cls
set DRIVE_LETTER=%1:
set PATH=%DRIVE_LETTER%\MinGW\bin;%DRIVE_LETTER%\MinGW\msys\1.0\bin;%DRIVE_LETTER%\MinGW\gtkmm3\bin;%DRIVE_LETTER%\MinGW\gtk\bin;c:\Windows;c:\Windows\system32
set PROJECT_PATH=.
make DRIVE_LETTER="%DRIVE_LETTER%" PROJECT_DIR="%PROJECT_PATH%"
lab08/CSC2110/CD.h
#if !defined CD_H
#define CD_H
#include "Song.h"
#include "Text.h"
using CSC2110::String;
#include "ListArray.h"
using CSC2110::ListArray;
namespace CSC2110
{
class CD
{
private:
String* artist;
String* title;
int year;
int rating;
int num_tracks;
ListArray<Song>* songs;
public:
CD(String* artist, String* title, int year, int rating, int num_tracks);
virtual ~CD();
String* getKey();
void addSong(String* title, String* length);
void displayCD();
static ListArray<CD>* readCDs(const char* file_name);
static int compare_items(CD* one, CD* two);
static int compare_keys(String* sk, CD* cd);
static char getRadixChar(CD* cd, int index); //1-based
};
}
#endif
lab08/CSC2110/Double.h
#if !defined (DOUBLE_H)
#define DOUBLE_H
namespace CSC2110
{
class Double
{
private:
double value;
public:
Double(double val);
~Double();
double getValue();
};
}
#endif
lab08/CSC2110/HighPerformanceCounter.h
#if !defined (HIGHPERFORMANCECOUNTER_H)
#define HIGHPERFORMANCECOUNTER_H
namespace CSC2110
{
class HighPerformanceCounter
{
private:
double micro_spt; //micro_seconds per tick
HighPerformanceCounter();
static HighPerformanceCounter* hpc;
static int getTicksPerSecond();
public:
virtual ~HighPerformanceCounter();
static HighPerformanceCounter* getHighPerformanceCounter();
int getCurrentTimeInTicks();
double getTimeDifferenceInMicroSeconds(int start_time, int end_time);
};
}
#endif
lab08/CSC2110/Integer.h
#if !defined (INTEGER_H)
#define INTEGER_H
namespace CSC2110
{
class Integer
{
private:
int value;
public:
Integer(int val);
virtual ~Integer();
int getValue();
};
}
#endif
lab08/CSC2110/Keyboard.h
#if !defined KEYBOARD_H
#define KEYBOARD_H
#include "Text.h"
using CSC2110::String;
#include <string>
using namespace std;
namespace CSC2110
{
class Keyboard
{
private:
Keyboard();
public:
virtual ~Keyboard();
static Keyboard* getKeyboard();
//pre: the string (character literal) that will prompt the user for input
//post: the input read from the keyboard interpreted as an int is returned
int readInt(string prompt);
int getValidatedInt(string prompt, int min, int max);
//pre: the string that will prompt the user for input
//post: the input read from the keyboard interpreted as a double is returned
double readDouble(string prompt);
double getValidatedDouble(string prom ...
2. #include "Text.h"
using CSC2110::String;
#include "ListArray.h"
using CSC2110::ListArray;
namespace CSC2110
{
class CD
{
private:
String* artist;
String* title;
int year;
int rating;
int num_tracks;
ListArray<Song>* songs;
3. public:
CD(String* artist, String* title, int year, int rating, int
num_tracks);
virtual ~CD();
String* getKey();
void addSong(String* title, String* length);
void displayCD();
static ListArray<CD>* readCDs(const char* file_name);
static int compare_items(CD* one, CD* two);
static int compare_keys(String* sk, CD* cd);
static char getRadixChar(CD* cd, int index); //1-based
};
}
8. #include "Text.h"
using CSC2110::String;
#include <string>
using namespace std;
namespace CSC2110
{
class Keyboard
{
private:
Keyboard();
public:
virtual ~Keyboard();
static Keyboard* getKeyboard();
//pre: the string (character literal) that will prompt the user for
input
9. //post: the input read from the keyboard interpreted as an int is
returned
int readInt(string prompt);
int getValidatedInt(string prompt, int min, int max);
//pre: the string that will prompt the user for input
//post: the input read from the keyboard interpreted as a double
is returned
double readDouble(string prompt);
double getValidatedDouble(string prompt, double min, double
max);
//pre: the string that will prompt the user for input
// the string to store the user input and the length of the input
storage string
//post: the text read from the keyboard is copied into the storage
string
String* readString(string prompt);
};
}
13. ListArray<T>::ListArray()
{
max_size = 10;
items = new T*[max_size];
sz = 0;
}
template < class T >
ListArray<T>::~ListArray()
{
delete[] items; //the items themselves are not deleted
}
template < class T >
bool ListArray<T>::isEmpty()
{
return (sz == 0);
}
14. template < class T >
int ListArray<T>::size()
{
return sz;
}
template < class T > //1-based
T* ListArray<T>::get(int index)
{
T* item = NULL;
if (index >= 1 && index <= sz)
{
item = items[index - 1];
}
return item;
15. }
template < class T >
void ListArray<T>::add(T* item)
{
add(sz + 1, item); //add the item to the end of the array list
}
template < class T >
void ListArray<T>::add(int index, T* item)
{
if (index < 1 || index > sz + 1)
{
return;
}
//need more room in the array list
if (sz == max_size)
16. {
arrayResize(2*max_size);
}
for (int i = sz; i >= index; i--)
{
items[i] = items[i - 1];
}
items[index - 1] = item;
sz++;
}
template < class T >
void ListArray<T>::remove(int index)
{
if (index < 1 || index > sz)
{
17. return;
}
for (int i = index; i < sz; i++)
{
items[i - 1] = items[i];
}
items[sz - 1] = NULL;
sz--;
/*
if (sz < max_size/2 - 1) //halve the size of the array, smallest
size of max_size should be 2
{
arrayResize(max_size/2);
}
*/
}
18. template < class T >
ListArrayIterator<T>* ListArray<T>::iterator()
{
ListArrayIterator<T>* iter = new ListArrayIterator<T>(items,
sz);
return iter;
}
template < class T >
void ListArray<T>::set(int index, T* item)
{
//could use other methods already written, but this is more
efficient
if (index >= 1 && index <= sz)
{
items[index - 1] = item; //overwrite contents at that
location
}
19. }
template < class T >
void ListArray<T>::arrayResize(int new_max_size)
{
max_size = new_max_size;
T** temp = new T*[max_size];
for (int i = 0; i < sz; i++)
{
temp[i] = items[i];
}
delete[] items;
items = temp;
}
template < class T >
20. void ListArray<T>::removeAll()
{
delete[] items;
max_size = 10;
items = new T*[max_size];
sz = 0;
}
template < class T >
T** ListArray<T>::toArray()
{
int num_items = size();
T** to_array = new T*[num_items];
for (int i = 0; i < num_items; i++)
{
to_array[i] = items[i];
}
22. private:
int index;
int sz;
T** items;
public:
ListArrayIterator(T** items, int size);
~ListArrayIterator();
bool hasNext();
T* next();
};
template < class T >
ListArrayIterator<T>::ListArrayIterator(T** itms, int size)
{
items = new T*[size];
for (int i = 0; i < size; i++)
{
23. items[i] = itms[i]; //snapshot of the data
}
index = 1;
sz = size;
}
template < class T >
ListArrayIterator<T>::~ListArrayIterator()
{
delete[] items;
}
template < class T >
bool ListArrayIterator<T>::hasNext()
{
return (index <= sz);
}
24. template < class T >
T* ListArrayIterator<T>::next()
{
T* item = NULL;
if (hasNext())
{
item = items[index - 1];
index++;
}
return item;
}
}
#endif
25. lab08/CSC2110/Matrix.h
#if !defined MATRIX_H
#define MATRIX_H
//the indices are 1-based!!
namespace CSC2110
{
class Matrix
{
private:
int rows;
int cols;
double* mat;
public:
Matrix(int rows, int cols); //constructor
~Matrix(); //destructor
26. void displayMatrix();
int getNumRows();
int getNumCols();
double getElement(int row, int col);
void setElement(int row, int col, double val);
Matrix* add(Matrix* other);
Matrix* multiply(Matrix* other);
static Matrix* readMatrix(const char* file_name); //discuss
static
void writeMatrix(const char* file_name);
};
}
#endif
lab08/CSC2110/Permutation.h
27. #if !defined (PERMUTATION_H)
#define PERMUTATION_H
#include "ListArray.h"
using CSC2110::ListArray;
#include "Integer.h"
using CSC2110::Integer;
#include "Random.h"
using CSC2110::Random;
namespace CSC2110
{
class Permutation
{
private:
int r;
ListArray<Integer>* numbers;
Random* random;
28. public:
Permutation(int r, int n);
virtual ~Permutation();
int next();
};
}
#endif
lab08/CSC2110/Poly.h
#if !defined (POLY)
#define POLY
namespace CSC2110
{
class Poly
{
private:
35. const char* text;
int sz; //length of string not including null terminator
public:
String(const char* char_array);
virtual ~String();
void displayString();
int length();
const char* getText();
//add this member function
char charAt(int index);
int a_to_i();
float a_to_f();
static String* i_to_a(int number);
static String* f_to_a(float number);
36. //find the location of a particular character in a String and
return the index if found
//preconditions:
// str is the String being examined for the character delimiter
(str must point to a valid String)
// delimiter is the character being searched for
// start is the index to start the search at (the first index of
the String is 0, start cannot exceed the length of the String)
//postconditions:
// if the preconditions are met, the index of the first delimiter
encountered at or after the start index is returned
// if the delimiter is not present in the String at index start or
later, -1 is returned
// if the preconditions are not met, no guarantees on output
are made
int find(char delimiter, int start);
//creates a new String that is extracted from an existing String
with characters specified by the start and end indices
//preconditions:
// str is the String from which the substring will be extracted
(str must point to a valid String)
37. // start and end are the indices used to create the substring
// start must be less than or equal to end, start must be >= 0,
end must be >= 0, end < the length of the String
//postconditions:
// if the preconditions are met, the String extracted from the
parameter String
// that starts at index start and ends at index end is created
and returned
// the original string is unaffected
String* substr(int start, int end);
//need to document that this compare only has three possible
return values (-1, 0, 1)
int compare(String* other);
};
}
#endif
38. lab08/CSC2110/Tokens.h
#if !defined TOKENS_H
#define TOKENS_H
#include "Text.h"
using CSC2110::String;
namespace CSC2110
{
class Tokens
{
private:
String** tokens;
int max_tokens;
int sz;
void addToken(String* str); //requires a resizing check
void resize();
39. public:
Tokens(String* str, char delimiter);
~Tokens(); //Tokens is not responsible for deleting each
token
void displayTokens();
String* getToken(int index); //returns a specifically
requested token
int getNumTokens();
};
}
#endif
lab08/CSC2110/WriteFile.h
#if !defined WRITE_FILE
#define WRITE_FILE
#include "Text.h"
using CSC2110::String;
49. private:
Color* color;
double line_width;
public:
Line(Color* color, double line_width);
~Line();
void draw(Cairo::RefPtr<Cairo::Context> cr, int x1, int y1,
int x2, int y2);
};
#endif
lab08/GUI/Rect.h
#ifndef RECT_H
#define RECT_H
#include "Color.h"
#include <gtkmm.h>
50. class Rect
{
private:
int width;
int height;
Color* color;
public:
Rect(Color* color, int width, int height);
virtual ~Rect();
void draw(Cairo::RefPtr<Cairo::Context> cr, int x, int y);
};
#endif
lab08/Makefile
55. bool Maze::solve()
{
bool done = traverse(1, 1);
return done;
}
bool Maze::traverse(int row, int col)
{
bool done = false; //assume we are not done unless proven
otherwise
//DO THIS
//test that the current grid location is a space (i.e. not a wall
or already tried)
if ( )
{
//DO THIS
//now it has been tried so mark it as tried
56. Sleep(75); //slow down the maze traversal
gui->update();
//DO THIS
//check to see if we have arrived at the bottom right corner
of the maze
int height = maze->getNumRows();
int width = maze->getNumCols();
if ( )
{
done = true;
}
else
{
57. //DO THIS
//make recursive calls that consider all four orthogonal
directions
//basically, we will try all possible paths until a solution
is found
//IMPORTANT!!
//don't use row++ or column++ use row + 1 or col + 1,
etc.
//IMPORTANT: make use of the boolean that is returned
every time you call traverse
58. }
//if we are done, on the way back recursively we must mark
the path that we took as the solution path
if (done)
{
//DO THIS
//mark the path taken as the solution path
60. void Maze::mouseClicked(int x, int y)
{}
void Maze::draw(Cairo::RefPtr<Cairo::Context> cr, int width,
int height)
{
int rows = maze->getNumRows();
int cols = maze->getNumCols();
int cell_width = (int) (((double) width)/cols + 0.5);
int cell_height = (int) (((double) height)/rows + 0.5);
Color red(1.0, 0.0, 0.0);
Rect redRect(&red, cell_width, cell_height);
Color green(0.0, 1.0, 0.0);
Rect greenRect(&green, cell_width, cell_height);
Color blue(0.0, 0.0, 1.0);
Rect blueRect(&blue, cell_width, cell_height);
61. Color white(1.0, 1.0, 1.0);
Rect whiteRect(&white, cell_width, cell_height);
Color black(0.0, 0.0, 0.0);
Rect blackRect(&black, cell_width, cell_height);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
int val = (int) maze->getElement(i, j);
int x_pixel = (j - 1) * cell_width + cell_width/2;
int y_pixel = (i - 1) * cell_height + cell_height/2;
if (val == WALL)
{
blackRect.draw(cr, x_pixel, y_pixel);
63. }
}
lab08/Maze.h
#if !defined (MAZE_H)
#define MAZE_H
#include "Matrix.h"
using CSC2110::Matrix;
#include "Drawable.h"
#include "Update.h"
class Maze : public Drawable
{
private:
Matrix* maze;
Update* gui;
64. int WALL;
int SPACE;
int TRIED;
int BACKTRACK;
int PATH;
bool traverse(int row, int col);
public:
Maze(Matrix* mz);
virtual ~Maze();
bool solve();
virtual void draw(Cairo::RefPtr<Cairo::Context> cr, int
width, int height);
virtual void mouseClicked(int x, int y);
void addListener(Update* gui);
};