diff options
author | zedarider <ymherklotz@gmail.com> | 2016-11-27 15:18:37 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-11-27 15:18:37 +0000 |
commit | 847ba8cd95371349df458f8029637064d5ddb892 (patch) | |
tree | 6e8b98423a4db64572f7b76d429ae08a73868c31 | |
parent | 4a41d23bc68c4fa55a76ea2b23ff9499d7f8e860 (diff) | |
download | second-year-computing-847ba8cd95371349df458f8029637064d5ddb892.tar.gz second-year-computing-847ba8cd95371349df458f8029637064d5ddb892.zip |
fixing git repo filesystem
m--------- | Lab Assessment | 0 | ||||
-rw-r--r-- | Lab Assessment/.atom-build.json | 3 | ||||
-rw-r--r-- | Lab Assessment/.clang_complete | 1 | ||||
-rw-r--r-- | Lab Assessment/.gitignore | 29 | ||||
-rw-r--r-- | Lab Assessment/Makefile | 34 | ||||
-rw-r--r-- | Lab Assessment/README.md | 1 | ||||
-rwxr-xr-x | Lab Assessment/bin/lab_assessment | bin | 0 -> 72568 bytes | |||
-rw-r--r-- | Lab Assessment/circles.txt | 2 | ||||
-rw-r--r-- | Lab Assessment/include/shape.hpp | 68 | ||||
-rw-r--r-- | Lab Assessment/src/main.cpp | 107 | ||||
-rw-r--r-- | Lab Assessment/src/shape.cpp | 31 | ||||
-rw-r--r-- | Lab Assessment/triangles.txt | 2 |
12 files changed, 278 insertions, 0 deletions
diff --git a/Lab Assessment b/Lab Assessment deleted file mode 160000 -Subproject 5973d6a630ccf93e580ed01bd347c2ffa3fb7b3 diff --git a/Lab Assessment/.atom-build.json b/Lab Assessment/.atom-build.json new file mode 100644 index 0000000..0193d03 --- /dev/null +++ b/Lab Assessment/.atom-build.json @@ -0,0 +1,3 @@ +{ + "cmd": "make -B" +} diff --git a/Lab Assessment/.clang_complete b/Lab Assessment/.clang_complete new file mode 100644 index 0000000..30679be --- /dev/null +++ b/Lab Assessment/.clang_complete @@ -0,0 +1 @@ +-Iinclude diff --git a/Lab Assessment/.gitignore b/Lab Assessment/.gitignore new file mode 100644 index 0000000..4581ef2 --- /dev/null +++ b/Lab Assessment/.gitignore @@ -0,0 +1,29 @@ +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app diff --git a/Lab Assessment/Makefile b/Lab Assessment/Makefile new file mode 100644 index 0000000..97ad5d9 --- /dev/null +++ b/Lab Assessment/Makefile @@ -0,0 +1,34 @@ +CC := g++ # this is the main compiler +# CC := clange --analyze # and comment out the linker last line +SRCDIR := src +BUILDDIR := build +TARGET := bin/lab_assessment + +SRCEXT := cpp +SOURCES := $(shell find $(SRCDIR) -type f -name "*.$(SRCEXT)") +OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o)) +CFLAGS := -g -Wall -Wextra -Wpedantic -std=c++14 +LIB := +INC := -I include + +$(TARGET): $(OBJECTS) + @echo " Linking..." + @echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) + +$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT) + @mkdir -p $(BUILDDIR) + @echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $< + +clean: + @echo " Cleaning..." + @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET) + +# Tests +tester: + @echo " $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester"; $(CC) $(CFLAGS) test/tester.cpp $(INC) $(LIB) -o bin/tester + +# Spikes +ticket: + @echo " $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket"; $(CC) $(CFLAGS) spikes/ticket.cpp $(INC) $(LIB) -o bin/ticket + +.PHONY: clean diff --git a/Lab Assessment/README.md b/Lab Assessment/README.md new file mode 100644 index 0000000..bd6b1e6 --- /dev/null +++ b/Lab Assessment/README.md @@ -0,0 +1 @@ +# Lab Assessment diff --git a/Lab Assessment/bin/lab_assessment b/Lab Assessment/bin/lab_assessment Binary files differnew file mode 100755 index 0000000..d86da0a --- /dev/null +++ b/Lab Assessment/bin/lab_assessment diff --git a/Lab Assessment/circles.txt b/Lab Assessment/circles.txt new file mode 100644 index 0000000..5866207 --- /dev/null +++ b/Lab Assessment/circles.txt @@ -0,0 +1,2 @@ +-2 1.5 10 +2 -1 5.3 diff --git a/Lab Assessment/include/shape.hpp b/Lab Assessment/include/shape.hpp new file mode 100644 index 0000000..34aedca --- /dev/null +++ b/Lab Assessment/include/shape.hpp @@ -0,0 +1,68 @@ +#ifndef SHAPE_HPP +#define SHAPE_HPP + +#include <cmath> +#include <stdexcept> +#include <cstdio> +#include <sstream> +#include <string> + +// abstract class shape +class Shape { +public: + virtual double get_perimeter()=0; +}; + +class Point { +public: + Point(double x_in, double y_in) : x(x_in), y(y_in) {} + + friend std::ostream& operator<<(std::ostream& out, const Point& p) { + std::stringstream ss; + ss << "(" << p.x << ", " << p.y << ")"; + out << ss.str(); + return out; + } + + double distance(Point p); +private: + double x, y; +}; + +class Triangle : public Shape { +public: + Triangle(Point p1_in, Point p2_in, Point p3_in); + + friend std::ostream& operator<<(std::ostream& out, const Triangle& t) { + std::stringstream ss; + ss << t.p1 << " " << t.p2 << " " << t.p3; + out << ss.str(); + return out; + } + + double get_perimeter(); +private: + Point p1, p2, p3; + + bool check_valid(); +}; + +class Circle : public Shape { +public: + Circle(Point centre_in, double radius_in); + + friend std::ostream& operator<<(std::ostream& out, const Circle& c) { + std::stringstream ss; + ss << c.centre << ", " << c.radius; + out << ss.str(); + return out; + } + + double get_perimeter(); +private: + const double PI = 3.14; + Point centre; + double radius; +}; + +#endif diff --git a/Lab Assessment/src/main.cpp b/Lab Assessment/src/main.cpp new file mode 100644 index 0000000..6b3537c --- /dev/null +++ b/Lab Assessment/src/main.cpp @@ -0,0 +1,107 @@ +#include "shape.hpp" + +#include <iostream> +#include <fstream> +#include <list> +#include <vector> +#include <stdexcept> + +typedef Shape* shape_ptr; + +using namespace std; + +int main(int argc, char* argv[]) { + // streams that open input files + ifstream t_file, c_file; + // sotres tmp variables in arrays + double x_tmp[3], y_tmp[3]; + // radius of circle + double radius_tmp; + + // lists in which the triangles and circles are stored + list<Triangle> triangle_list; + list<Circle> circle_list; + + // vector suitable for triangle and circle pointers + vector<shape_ptr> shape_vector; + + // try opening the files + try { + t_file.open(argv[1]); + c_file.open(argv[2]); + if(!t_file.is_open() || !c_file.is_open()) { + throw logic_error("Error opening files"); + } + } catch(logic_error e) { + cout << e.what() << endl; + exit(EXIT_FAILURE); + } + + // read from triangle file + while(t_file >> x_tmp[0] >> y_tmp[0] >> x_tmp[1] >> y_tmp[1] >> x_tmp[2] >> y_tmp[2]) { + Point p1_tmp(x_tmp[0], y_tmp[0]); + Point p2_tmp(x_tmp[1], y_tmp[1]); + Point p3_tmp(x_tmp[2], y_tmp[2]); + + try { + Triangle triangle_tmp(p1_tmp, p2_tmp, p3_tmp); + triangle_list.push_back(triangle_tmp); + } catch(logic_error e) { + cout << e.what() << endl; + t_file.close(); + c_file.close(); + exit(EXIT_FAILURE); + } + } + + // read from circle file + while(c_file >> x_tmp[0] >> y_tmp[0] >> radius_tmp) { + Point p_tmp(x_tmp[0], y_tmp[0]); + + try { + Circle circle_tmp(p_tmp, radius_tmp); + circle_list.push_back(circle_tmp); + } catch(logic_error e) { + cout << e.what() << endl; + t_file.close(); + c_file.close(); + exit(EXIT_FAILURE); + } + } + +/* + // prints out the triangles + for(list<Triangle>::iterator it = triangle_list.begin(); it != triangle_list.end(); ++it) { + cout << *it << endl; + } + + // prints out the cirlces + for(list<Circle>::iterator it = circle_list.begin(); it != circle_list.end(); ++it) { + cout << *it << endl; + } +*/ + + // adding Triangles and Circles to vector of Shapes + for(list<Triangle>::iterator it = triangle_list.begin(); it != triangle_list.end(); ++it) { + shape_ptr tmp; + tmp = &*it; + shape_vector.push_back(tmp); + } + + for(list<Circle>::iterator it = circle_list.begin(); it != circle_list.end(); ++it) { + shape_ptr tmp; + tmp = &*it; + shape_vector.push_back(tmp); + } + + // printing perimeters for all shapes + for(vector<shape_ptr>::iterator it = shape_vector.begin(); it != shape_vector.end(); ++it) { + cout << (*it)->get_perimeter() << endl; + } + + // close everything + t_file.close(); + c_file.close(); + + return 0; +} diff --git a/Lab Assessment/src/shape.cpp b/Lab Assessment/src/shape.cpp new file mode 100644 index 0000000..4b7ac9d --- /dev/null +++ b/Lab Assessment/src/shape.cpp @@ -0,0 +1,31 @@ +#include "shape.hpp" + +double Point::distance(Point p) { + return sqrt(pow(x-p.x, 2) + pow(y-p.y, 2)); +} + +Triangle::Triangle(Point p1_in, Point p2_in, Point p3_in) : p1(p1_in), p2(p2_in), p3(p3_in) { + if(!check_valid()) { + throw std::logic_error("Triangle Error: Couldn't create triangle, invalid coordiantes"); + } +} + +double Triangle::get_perimeter() { + return p1.distance(p2) + p1.distance(p3) + p2.distance(p3); +} + +bool Triangle::check_valid() { + return (p1.distance(p2) < p2.distance(p3) + p1.distance(p3) && + p1.distance(p3) < p2.distance(p3) + p1.distance(p2) && + p2.distance(p3) < p1.distance(p2) + p1.distance(p3)); +} + +Circle::Circle(Point centre_in, double radius_in) : centre(centre_in), radius(radius_in) { + if(radius <= 0) { + throw std::logic_error("Circle Error: Couldn't create circle with negative or 0 radius"); + } +} + +double Circle::get_perimeter() { + return 2*radius*PI; +} diff --git a/Lab Assessment/triangles.txt b/Lab Assessment/triangles.txt new file mode 100644 index 0000000..ee1b334 --- /dev/null +++ b/Lab Assessment/triangles.txt @@ -0,0 +1,2 @@ +1.5 2.5 3 3 2 2 +-0.5 1.2 1 1 -1 1 |