Skip to content

Commit

Permalink
fmk - adding Sessions4 and 5 assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
fmckenna committed Aug 2, 2024
1 parent ef2bdcc commit 9d453a4
Show file tree
Hide file tree
Showing 27 changed files with 2,795 additions and 0 deletions.
2,000 changes: 2,000 additions & 0 deletions assignments/C-Session4/ex1/big.txt

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions assignments/C-Session4/ex1/file3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

// program to read values from a file, each file a csv list of int and two double
// written: fmk

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

if (argc != 3) {
fprintf(stdout, "ERROR correct usage appName inputFile outputBinaryFile\n");
return -1;
}

//
// read from ascii file
//
FILE *filePtr = fopen(argv[1],"r");

int i = 0;
float float1, float2;
int maxVectorSize = 100;
double *vector1 = (double *)malloc(maxVectorSize*sizeof(double));
double *vector2 = (double *)malloc(maxVectorSize*sizeof(double));
int vectorSize = 0;

while (fscanf(filePtr,"%d, %f, %f\n", &i, &float1, &float2) != EOF) {
vector1[vectorSize] = float1;
vector2[vectorSize] = float2;
printf("%d, %f, %f\n",i, vector2[i], vector1[i]);
vectorSize++;

if (vectorSize == maxVectorSize) {

// create new arrys & copy contents
double *newVector1 = (double *)malloc(2*vectorSize*sizeof(double));
double *newVector2 = (double *)malloc(2*vectorSize*sizeof(double));
for (int i=0; i<vectorSize; i++) {
newVector1[i]=vector1[i];
newVector2[i]=vector2[i];
}

// release old memory, set vectors to point to new ones and update max vector size
free(vector1);
free(vector2);
vector1 = newVector1;
vector2 = newVector2;
maxVectorSize *= 2;
}
}
fclose(filePtr);

//
// write to binary file
//

FILE *filePtrB = fopen(argv[2],"wb");
fwrite(vector1, sizeof(double), vectorSize, filePtrB);
fwrite(vector2, sizeof(double), vectorSize, filePtrB);
fclose(filePtrB);
}
10 changes: 10 additions & 0 deletions assignments/C-Session4/ex1/small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0, 0.153779, 0.560532
1, 0.865013, 0.276724
2, 0.895919, 0.704462
3, 0.886472, 0.929641
4, 0.469290, 0.350208
5, 0.941637, 0.096535
6, 0.457211, 0.346164
7, 0.970019, 0.114938
8, 0.769819, 0.341565
9, 0.684224, 0.748597
19 changes: 19 additions & 0 deletions assignments/C-Session4/ex2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required (VERSION 2.6)
set (CMAKE_C_STANDARD 99)

project (Shapes)

include_directories(${PROJECT_SOURCE_DIR})
add_library(ShapeLib ColoredShape.cpp Rectangle.cpp Circle.cpp Ellipse.cpp)

add_executable(ex1 main1.cpp)
target_link_libraries (ex1 ShapeLib)

add_executable(ex2 main2.cpp)
target_link_libraries (ex2 ShapeLib)

add_executable(ex3 main3.cpp)
target_link_libraries (ex3 ShapeLib)

add_executable(ex4 main4.cpp)
target_link_libraries (ex4 ShapeLib)
22 changes: 22 additions & 0 deletions assignments/C-Session4/ex2/Circle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Circle.h"

Circle::~Circle() {
std::cout << "Circle Destructor\n";
}

Circle::Circle(double radius, string color)
:ColoredShape(color)
{
r = radius;
}

double
Circle::getArea(void) {
return r*r*getPI();
}

double
Circle::getPI(void) {
return 3.14159;
}

21 changes: 21 additions & 0 deletions assignments/C-Session4/ex2/Circle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _CIRCLE
#define _CIRCLE

#include "ColoredShape.h"

class Circle: public ColoredShape {
public:
Circle(double r, string color);
~Circle();
double getArea(void);

protected:

private:
double r;
double getPI(void);
};

#endif // _CIRCLE


23 changes: 23 additions & 0 deletions assignments/C-Session4/ex2/ColoredShape.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ColoredShape.h"

ColoredShape::ColoredShape(string col)
: color(col)
{

}

ColoredShape::~ColoredShape() {
std::cout << "ColoredShape Destructor\n";
}

const string &
ColoredShape::getColor() {
return color;
}

void
ColoredShape::printArea(std::ostream &s) {
s << "Color: " << color << " UNKOWN area: " << this->getArea() << "\n";
}


23 changes: 23 additions & 0 deletions assignments/C-Session4/ex2/ColoredShape.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _COLORED_SHAPE
#define _COLORED_SHAPE

#include <iostream>
#include <string>
using std::string;

class ColoredShape {
public:
ColoredShape(string color);
virtual ~ColoredShape();

const string &getColor();
virtual double getArea(void) =0;
virtual void printArea(std::ostream &s);

private:
string color;
};

#endif // _COLORED_SHAPE


28 changes: 28 additions & 0 deletions assignments/C-Session4/ex2/Ellipse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Ellipse.h"

Ellipse::~Ellipse() {
std::cout << "Ellipse Destructor\n";
}

Ellipse::Ellipse(double a, double b, string color)
:ColoredShape(color)
{
ax1 = a;
ax2 = b;
}

double
Ellipse::getArea(void) {
return ax1*ax2*getPI();
}

double
Ellipse::getPI(void) {
return 3.14159;
}

void
Ellipse::printArea(std::ostream &s) {
s << "Ellipse: color: " << this->getColor() << ", area: " << this->getArea() << "\n";
}

23 changes: 23 additions & 0 deletions assignments/C-Session4/ex2/Ellipse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _ELLIPSE_H
#define _ELLIPSE_H

#include "ColoredShape.h"

class Ellipse: public ColoredShape {
public:
Ellipse(double a, double b, string color);
~Ellipse();
double getArea(void);
void printArea(std::ostream &s);

protected:

private:
double ax1;
double ax2;
double getPI(void);
};

#endif // _ELLIPSE_H


26 changes: 26 additions & 0 deletions assignments/C-Session4/ex2/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "Rectangle.h"

int Rectangle::numRect = 0;

Rectangle::~Rectangle() {
numRect--;
std::cout << "Rectangle Destructor " << this->getColor() << "\n";
}

Rectangle::Rectangle(double w, double d, string color)
:ColoredShape(color), width(w), height(d)
{
numRect++;
}

double
Rectangle::getArea(void) {
return width*height;
}

void
Rectangle::printArea(std::ostream &s) {
s << "Rectangle: color: " << this->getColor() << ", area: "
<< width * height << " numRect: " << numRect << "\n";
}

22 changes: 22 additions & 0 deletions assignments/C-Session4/ex2/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef _RECTANGLE
#define _RECTANGLE

#include "ColoredShape.h"

class Rectangle: public ColoredShape {
public:
Rectangle(double w, double h, string color);
~Rectangle();
double getArea(void);
void printArea(std::ostream &s);

protected:

private:
double width, height;
static int numRect;
};

#endif // _RECTANGLE


20 changes: 20 additions & 0 deletions assignments/C-Session4/ex2/main1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Rectangle.h"
#include "Ellipse.h"

int main(int argc, char **argv) {
Rectangle s1(1.0, 2.0, "red");
ColoredShape *s2 = new Rectangle(3.0, 1.0, "blue");
ColoredShape *s3 = new Ellipse(3.0,2.0,"purple");
// Shape *s4 = new Square(3.0, "pink");

s1.printArea(std::cout);
s2->printArea(std::cout);
s3->printArea(std::cout);
// s4->printArea(std::cout);

delete s2;
delete s3;
return 0;
}


18 changes: 18 additions & 0 deletions assignments/C-Session4/ex2/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "Rectangle.h"
#include "Circle.h"

int main(int argc, char **argv) {
Circle s1(2.0, "red");
ColoredShape *s2 = new Rectangle(1.0, 2.0, "blue");
ColoredShape *s3 = new Rectangle(3.0,2.0, "green");

s1.printArea(std::cout);
s2->printArea(std::cout);
s3->printArea(std::cout);

delete s2;
delete s3;
return 0;
}


27 changes: 27 additions & 0 deletions assignments/C-Session4/ex2/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Rectangle.h"
#include "Circle.h"

#include <list>

int main(int argc, char **argv) {
Circle s1(2.0, "red");
ColoredShape *s2 = new Rectangle(1.0, 2.0, "blue");
ColoredShape *s3 = new Rectangle(3.0,2.0, "green");

std::list<ColoredShape*> theShapes;

theShapes.push_front(&s1);
theShapes.push_front(s2);
theShapes.push_front(s3);

std::list<ColoredShape *>::iterator it;
for (it = theShapes.begin(); it != theShapes.end(); it++) {
(*it)->printArea(std::cout);
}

delete s2;
delete s3;
return 0;
}


37 changes: 37 additions & 0 deletions assignments/C-Session4/ex2/main4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Rectangle.h"
#include "Circle.h"
#include "Ellipse.h"

#include <list>
#include <vector>

typedef std::list<ColoredShape*> Container;
//typedef std::vector<ColoredShape*> Container;

typedef Container::iterator Iter;

int main(int argc, char **argv) {
Container theShapes;

Circle s1(2.0, "red");
ColoredShape *s2 = new Rectangle(1.0, 2.0, "blue");
ColoredShape *s3 = new Rectangle(3.0,2.0, "green");
ColoredShape *s4 = new Ellipse(3.0,2.0, "purple");

theShapes.push_front(&s1);
theShapes.push_front(s2);
theShapes.push_front(s3);
theShapes.push_front(s4);

Iter it;
for (it = theShapes.begin(); it != theShapes.end(); it++) {
(*it)->printArea(std::cout);
}

delete s2;
delete s3;
delete s4;
return 0;
}


Loading

0 comments on commit 9d453a4

Please sign in to comment.