Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int countOfShapes;
struct vertex{
int x;
int y;
};
struct shape{
int layer;
char fillCharacter;
char shapeCode[20];
char identifier[20];
vertex coordinates[4];
};
int processData(char []);
int layerValidation(shape[], int, int);
void layerValidation(shape[], int, char[], int);
int shapeCodeValidation(char[]);
int identifierValidation(char[13], shape[50], int, int);
int vertexValidation(int[], int[], char[20], int);
void moveUp(shape[50], int, int);
void moveDown(shape[50], int, int);
void sortVertexSet(int[], int[], char[]);
void drawTheShapes(shape[50], int);
void drawSquare(struct vertex[], char);
void drawRectangle(struct vertex[], char);
void drawTrapezoid(struct vertex[], char);
void drawTriangle(struct vertex[], char);
void initService()
/*
This program is called from the main. It initialzes the values on any global arrays or countOfShapes.
Input: Static boolean value which verifies whether it is the first time this function is called. Also given access to any global variables.
Output: No direct output. Global variables and arrays are reset and initialized to 0.
Processing Steps:
1) If myFirstTimeCalled = true
a) Display what the function does
b) set myFirstTimeCalled = false
2) Initialize countOfShapes to 0
3) End Function and return to the calling function
*/
{
static bool myFirstTimeCalled = true;
if (myFirstTimeCalled)
{
cout << "You have choosen to initialize the arrays. This function will now auto-initialize the global variables.";
myFirstTimeCalled = false;
}
countOfShapes = 0;
}
void refreshBuffer()
{
}
char getBufferXY(int, int)
{
}
int processData(const char file[])
{
fstream fin;
char buffer[100];
char command[20], shapeCode[20], identifier[40];
int layer, x[3], y[3];
int countOfShapes = 0;
int hasAnError = 0;
int shapeOverload = 0;
int extraVertix = 0;
struct shape shapeArray[50];
fin.open(file, ios::in);
if (fin.fail())
countOfShapes = -1;
else
{
while(!fin.eof() && strcmp(command, "Exit"))
{
for(int i = 0; i < 4; i++) {
x[i] = -1;
y[i] = -1;
}
identifier[12] = 0;
hasAnError = 0;
fin >> command;
if(!strcmp(command, "Translate"))
{
fin >> identifier;
identifierValidation(identifier, shapeArray, countOfShapes, 2);
fin.getline(buffer, 100);
}
else if (!strcmp(command, "Layer"))
{
fin >> identifier;
fin >> layer;
fin.getline(buffer, '\n');
identifierValidation(identifier, shapeArray, countOfShapes, 2);
layerValidation(shapeArray, layer, identifier, countOfShapes);
}
else if (!strcmp(command, "Fill"))
{
int n = 0;
char fillCharacter = '0';
hasAnError = 0;
fin >> identifier;
fin.getline(buffer, 100);
n = sscanf(buffer, "%1s", &fillCharacter);
if (n < 1)
{
cout << "***Error: Missing, or invalid shape code specified.*** \n";
hasAnError = 1;
}
if (!hasAnError)
{
for (int i = 0; i < countOfShapes; i++)
{
if (shapeArray[i].identifier == identifier)
shapeArray[i].fillCharacter = fillCharacter;
}
}
}
else if (!strcmp(command, "Display"))
{
fin.getline(buffer, 100);
drawTheShapes(shapeArray, countOfShapes);
}
else if (!strcmp(command, "Delete"))
{
hasAnError = 0;
int indexOfDelete = 0;
fin >> identifier;
fin.getline(buffer, 100);
hasAnError = identifierValidation(identifier, shapeArray, countOfShapes, 2);
if (!hasAnError)
{
drawTriangle(shapeArray[i].coordinates,
if (shapeArray[i].identifier == identifier)
indexOfDelete = i;
moveDown(shapeArray, countOfShapes, indexOfDelete);
}
countOfShapes--;
}
else if (!strcmp(command, "Exit"))
{
cout << "***Error: Exit command processed.*** \n";
}
else if (!strcmp(command, "Create"))
{
int n = 0;
extraVertix = -1;
fin >> shapeCode;
fin >> identifier;
fin >> layer;
fin.getline(buffer, 100);
n = sscanf(buffer, "%d %d %d %d %d %d %d %d %d", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2], &x[3], &y[3], &extraVertix);
sortVertexSet(x, y, shapeCode);
if (countOfShapes == 50)
{
cout << "***Error: Unable to add shape: shape structure is full.*** \n";
shapeOverload = 1;
}
else
{
hasAnError += identifierValidation(identifier, shapeArray, countOfShapes, 1);
hasAnError += layerValidation(shapeArray, layer, countOfShapes, 1);
hasAnError += shapeCodeValidation(shapeCode);
hasAnError += vertexValidation(x, y, shapeCode, n);
if (!hasAnError)
{
int indexOfInsert = 0;
for (int i = 0; i < countOfShapes; i++)
{
if (layer > shapeArray[i].layer)
indexOfInsert = i + 1;
}
moveUp(shapeArray, indexOfInsert, countOfShapes);
shapeArray[indexOfInsert].layer = layer;
strcpy(shapeArray[indexOfInsert].shapeCode, shapeCode);
strcpy(shapeArray[indexOfInsert].identifier, identifier);
for (int i = 0; i < 3; i++)
{
shapeArray[indexOfInsert].coordinates[i].x = x[i];
shapeArray[indexOfInsert].coordinates[y].y = y[i];
}
countOfShapes++;
}
}
}
else
{
cout << "***Error: Invalid Command.*** \n";
fin.getline(buffer, 100);
}
}
}
fin.close();
if (shapeOverload)
{
countOfShapes = -2;
}
return countOfShapes;
}
void moveDown(shape shapeArray[50], int countOfShapes, int indexOfDelete)
{
for (int i = indexOfDelete; i < countOfShapes; i++)
{
shapeArray[i].layer = shapeArray[i + 1].layer;
shapeArray[i].fillCharacter = shapeArray[i + 1].fillCharacter;
strcpy(shapeArray[i].shapeCode, shapeArray[i + 1].shapeCode);
strcpy(shapeArray[i].identifier, shapeArray[i + 1].identifier);
for (int j = 0; j < 4; j++)
{
shapeArray[i].coordinates[j].x = shapeArray[i + 1].coordinates[j].x;
shapeArray[i].coordinates[j].y = shapeArray[i + 1].coordinates[j].y;
}
}
}
void moveUp(shape shapeArray[50], int indexOfInsertion, int countOfShapes)
{
for (int i = countOfShapes; i > indexOfInsertion; i--)
{
shapeArray[i].layer = shapeArray[i - 1].layer;
shapeArray[i].fillCharacter = shapeArray[i - 1].fillCharacter;
strcpy(shapeArray[i].shapeCode, shapeArray[i - 1].shapeCode);
strcpy(shapeArray[i].identifier, shapeArray[i - 1].identifier);
for (int j = 0; j < 4; j++)
{
shapeArray[i].coordinates[j].x = shapeArray[i - 1].coordinates[j].x;
shapeArray[i].coordinates[j].y = shapeArray[i - 1].coordinates[j].y;
}
}
}
void sortVertexSet(int x[4], int y[4], char shapeCode[20])
{
int lowest = 0;
int indexOfSwitch = 0;
int xHolder = 0;
if (strcmp(shapeCode, "triangle"))
{
for (int i = 0; i < 4; i++)
{
cout << " " << x[i] << " " << y[i];
}
for (int i = 0; i < 4; i++)
{
lowest = y[i];
indexOfSwitch = i;
xHolder = 0;
for (int j = i + 1; j < 4; j++)
{
if (y[j] < lowest)
{
lowest = y[j];
indexOfSwitch = j;
}
else if (y[j] == lowest)
{
if (x[j] < x[indexOfSwitch])
{
lowest = y[j];
indexOfSwitch = j;
}
}
}
y[indexOfSwitch] = y[i];
y[i] = lowest;
xHolder = x[i];
x[i] = x[indexOfSwitch];
x[indexOfSwitch] = xHolder;
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << " " << x[i] << " " << y[i];
}
cout << endl;
}
if (!strcmp(shapeCode, "triangle"))
{
for (int i = 0; i < 3; i++)
{
cout << " " << x[i] << " " << y[i];
}
for (int i = 0; i < 3; i++)
{
lowest = y[i];
indexOfSwitch = i;
xHolder = 0;
for (int j = i + 1; j < 3; j++)
{
if (y[j] < lowest)
{
lowest = y[j];
indexOfSwitch = j;
}
else if (y[j] == lowest)
{
if (x[j] < x[indexOfSwitch])
{
lowest = y[j];
indexOfSwitch = j;
}
}
}
y[indexOfSwitch] = y[i];
y[i] = lowest;
xHolder = x[i];
x[i] = x[indexOfSwitch];
x[indexOfSwitch] = xHolder;
}
cout << endl;
for (int i = 0; i < 3; i++)
{
cout << " " << x[i] << " " << y[i];
}
cout << endl;
}
}
int layerValidation(shape shapeArray[50], int layer, int countOfShapes)
{
int hasAnError = 0;
if (layer < 0 || layer > 100)
{
cout << "***Error: Invalid layer number specified.*** \n";
hasAnError = 1;
}
for (int i = 0; i < countOfShapes; i++)
{
if (layer == shapeArray[i].layer)
{
cout << "***Error: Shape created or moved to occupied layer.*** \n";
hasAnError = 1;
}
}
return hasAnError;
}
void layerValidation(shape shapeArray[50], int layer, char identifier[20], int countOfShapes)
{
if (layer < 0 || layer > 100)
cout << "***Error: Invalid layer number specified.*** \n";
for (int i = 0; i < countOfShapes; i++)
{
if (layer == shapeArray[i].layer)
{
if(strcmp(identifier, shapeArray[i].identifier))
cout << "***Error: Shape created or moved to occupied layer.*** \n";
}
}
}
int shapeCodeValidation(char shapeCode[20])
{
int hasAnError = 0;
if (strcmp(shapeCode, "triangle") && strcmp(shapeCode, "rectangle") && strcmp(shapeCode, "trapezoid") && strcmp(shapeCode, "square"))
{
cout << "***Error: Missing, or invalid shape code specified.*** \n";
hasAnError = 1;
}
return hasAnError;
}
int identifierValidation(char identifier[13]), shape shapeArray[50], int countOfShapes, int sender)
{
int hasAnError = 0;
int hasBeenUsed = 0;
if (identifier[12] > 0)
{
hasAnError = 1;
cout << "***Error: Missing, or invalid identifier specified.*** \n";
}
else
{
for (int i = 0; i < int(strlen(identifier)); i++)
{
if (!isalpha(identifier[i]))
{
hasAnError = 1;
cout << "***Error: Missing, or invalid identifier specified.*** \n";
}
}
}
for (int i = 0; i <countOfShapes; i++)
{
if (!strcmp(identifier, shapeArray[i].identifier))
hasBeenUsed = 1;
}
if (sender == 1 && hasBeenUsed)
{
cout << "***Error: Shape identifier already exists.***\n";
hasAnError = 1;
}
if (sender == 2 && !hasBeenUsed)
{
cout << "***Error: Shape with given identifier does not exist.***\n";
hasAnError = 1;
}
return hasAnError;
}
int vertexValidation(int x[3], int y[3], char shapeCode[20], int vertexCount)
{
int hasAnError = 0;
if ((!strcmp(shapeCode, "square") || !strcmp(shapeCode, "rectangle") || !strcmp(shapeCode, "trapezoid")) && vertexCount != 8)
{
cout << "***Error: Invalid vertex count for given shape code.***\n";
hasAnError = 1;
}
else if(!strcmp(shapeCode, "triangle") && (vertexCount !=6))
{
cout << "***Error: Invalid vertex count for given shape code.***\n";
hasAnError = 1;
}
if (!strcmp(shapeCode, "triangle"))
{
x[3] = 0;
y[3] = 0;
}
if (x[0] > 78 || y[0] > 24 || x[0] < 0 || y[0] < 0 || x[1] > 78 || y[1] > 24 || x[1] < 0 || y[1] < 0 || x[2] > 78 || y[2] > 24 || x[2] < 0 || y[2] < 0 || x[3] > 78 || y[3] > 24 || x[3] < 0 || y[3] < 0)
{
cout << "***Error: Missing, or specified coordinates fall outside of plot area.***\n";
hasAnError = 1;
}
if (!strcmp(shapeCode, "square"))
{
if (!(x[0] == x[2] && x[1] == x[3] && y[0] == y[2] & (x[3] - x[2]) == (y[2] - y[0]) && (x[3]-x[2]) > 0))
{
cout << "***Error: Invalid vertex set for the given shape code.***" << endl;
hasAnError = 1;
}
}
if (!strcmp(shapeCode, "rectangle"))
{
if (!(x[0]==x[2] && x[1] ==x[3] && y[0]==y[1] && y[2]==y[3] && x[3]-x[2]>0 && y[2]-y[0]>0))
{
cout << "***Error: Invalid vertex sset for the given shape code.***" << endl;
hasAnError = 1;
}
}
if (!strcmp(shapeCode, "trapezoid"))
{
if (0)
{
cout << "***Error: Invalid vertex set for the given shape code.***" << endl;
hasAnError = 1;
}
}
if (!strcmp(shapeCode, "triangle"))
{
if (!((y[0]==y[1] && y[2]-y[0]>0 && (x[2]==x[0] || x[2]==x[1]) && (y[2]-y[0] == x[1]-x[0])) || (y[1]==y[2] && y[2]-y[0]>0 && (x[0]==x[1] || x[0]==x[2]) && (y[2]-y[0] == x[2]-x[1]))))
{
cout << "***Error: Invalid vertex set for the given shape code.***" << endl;
hasAnError = 1;
}
}
return hasAnError;
}
void drawTheShapes(shape shapeArray[50], int countOfShapes)
{
for (int i = 0; i < countOfShapes; i++)
{
if (!strcmp(shapeArray[i].shapeCode, "square"))
drawSquare(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
if (!strcmp(shapeArray[i].shapeCode, "rectangle"))
drawRectangle(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
if (!strcmp(shapeArray[i].shapeCode, "trapezoid"))
drawTrapezoid(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
if (!strcmp(shapeArray[i].shapeCode, "triangle"))
drawTriangle(shapeArray[i].coordinates, shapeArray[i].fillCharacter);
}
}
void drawSquare(vertex coordinates[4], char fillCharacter)
{
}
void drawRectangle(vertex coordinates[4], char fillCharacter)
{
}
void drawTrapezoid(vertex coordinates[4], char fillCharacter)
{
}
void drawTriangle(vertex coordinates[4], char fillCharacter)
{
}