Skip to content

Instantly share code, notes, and snippets.

@rvente
Created February 15, 2019 19:18
Show Gist options
  • Save rvente/ca78ba7db3c3bb147460fc59fd5d0664 to your computer and use it in GitHub Desktop.
Save rvente/ca78ba7db3c3bb147460fc59fd5d0664 to your computer and use it in GitHub Desktop.
void TestPart1() {
Points2<int> a, b; // Two empty Points2 are created.
cout << a.size() << " " << b.size() << endl; // yields 0 0. const array<int,
// 2> a_point2{{7, 10}};
Points2<int> d{a_point2}; // Sequence (7, 10) should be created. cout << d;
// Should just print (7, 10)
cout << "Enter a sequence of points (integer)" << endl;
a.ReadPoints2(); // User enters a set of points in the form:
// 3 7 4 3 2 1 10 3 specifies number of points. Points are the pairs // (7, 4)
// (3, 2) and (1, 10)
cout << "Output1: " << endl;
cout << a; // Output should be what user entered.
cout << "Enter a sequence of points (integer)" << endl;
b.ReadPoints2(); // Enter another sequence.
cout << "Output2: " << endl;
cout << b;
Points2<int> c{a}; // Calls copy constructor for c.
cout << "After copy constructor1 c{a}: " << endl;
cout << c;
cout << a;
a = b; // Should call the copy assignment operator for a. cout << "After
// assignment a = b" << endl;
cout << a;
Points2<int> e = move(c); // Move constructor for d. cout << "After e =
// move(c) " << endl;
cout << e;
cout << c;
cout << "After a = move(e) " << endl;
a = move(e); // Move assignment operator for a.
cout << a;
cout << e;
}
########################################
##
## Makefile
## LINUX compilation
##
##############################################
#FLAGS
C++FLAG = -g -std=c++11 -Wall
#Math Library
MATH_LIBS = -lm
EXEC_DIR=.
#Rule for .cpp files
# .SUFFIXES : .cc.o
.cc.o:
g++ $(C++FLAG) $(INCLUDES) -c $< -o $@
#Including
INCLUDES= -I.
LIBS_ALL = -L/usr/lib -L/usr/local/lib $(MATH_LIBS)
#ZEROTH PROGRAM
ALL_OBJ0=test_points2.o
PROGRAM_0=test_points2
$(PROGRAM_0): $(ALL_OBJ0)
g++ $(C++FLAG) -o $(EXEC_DIR)/$@ $(ALL_OBJ0) $(INCLUDES) $(LIBS_ALL)
#Compiling all
all:
make $(PROGRAM_0)
#Clean obj files
clean:
(rm -f *.o; rm -f test_points2)
(:
// --> YOUR NAME here
// Few comments describing the class Points2
#ifndef CSCI335_HOMEWORK1_POINTS2_H_
#define CSCI335_HOMEWORK1_POINTS2_H_
#include <array>
#include <iostream>
#include <cstddef>
#include <string>
#include <sstream>
namespace teaching_project {
// Place comments that provide a brief explanation of the class,
// and its sample usage.
template<typename Object>
class Points2 {
public:
// Default "big five" -- you have to alter them for your assignment.
// That means that you will remove the "= default" statement.
// and you will provide an implementation.
// Zero-parameter constructor.
// Set size to 0.
Points2() = default;
// Copy-constructor.
Points2(const Points2 &rhs) = default;
// Copy-assignment. If you have already written
// the copy-constructor and the move-constructor
// you can just use:
// {
// Points2 copy = rhs;
// std::swap(*this, copy);
// return *this;
// }
Points2& operator=(const Points2 &rhs) = default;
// Move-constructor.
Points2(Points2 &&rhs) = default;
// Move-assignment.
// Just use std::swap() for all variables.
Points2& operator=(Points2 &&rhs) = default;
~Points2() = default;
// End of big-five.
// One parameter constructor.
Points2(const std::array<Object, 2>& item) {
// Provide code.
}
// Read a chain from standard input.
void ReadPoints2() {
// Part of code included (without error checking).
std::string input_line;
std::getline(std::cin, input_line);
std::stringstream input_stream(input_line);
if (input_line.empty()) return;
// Read size of sequence (an integer).
int size_of_sequence;
input_stream >> size_of_sequence;
// Allocate space for sequence.
// Add code here.
Object token;
for (int i = 0 ;input_stream >> token; ++i) {
// Read coordinates.
// Fill sequence_ here.
}
}
size_t size() const {
// Code missing.
}
// @location: an index to a location in the sequence.
// @returns the point at @location.
// const version.
// abort() if out-of-range.
const std::array<Object, 2>& operator[](size_t location) const {
// Code missing.
}
// @c1: A sequence.
// @c2: A second sequence.
// @return their sum. If the sequences are not of the same size, append the
// result with the remaining part of the larger sequence.
friend Points2 operator+(const Points2 &c1, const Points2 &c2) {
// Code missing.
}
// Overloading the << operator.
friend std::ostream &operator<<(std::ostream &out, const Points2 &some_points2) {
// Code missing.
}
private:
// Sequence of points.
std::array<Object, 2> *sequence_;
// Size of sequence.
size_t size_;
};
} // namespace teaching_project
#endif // CSCI_335_HOMEWORK1_POINTS2_H_
3 7 4 3 5 19 71
4 100 101 2 3 40 50 11 33
3 2.1 20.3 11.11 12.45 13.1 14.2
2 1.1 100.0 20.1 30.2
// Do not change this file other than adding header files
// if needed.
// You can also comment parts of the functions, and uncomment
// as you add more functionality.
#include <points2.h>
#include <array>
#include <iostream>
#include <string>
using namespace std;
using namespace teaching_project;
// Place stand-alone function in unnamed namespace.
namespace {
void TestPart1() {
Points2<int> a, b; // Two empty Points2 are created.
cout << a.size() << " " << b.size() << endl; // yields 0 0.
const array<int, 2> a_point2{{7, 10}};
Points2<int> d{a_point2}; // A Points2 containing (7, 10) should be created.
cout << d; // Should just print (7, 10).
cout << "Enter a sequence of points (integer)" << endl;
a.ReadPoints2(); // User enters a set of points in the form:
// 3 7 4 3 2 1 10
// 3 specifies number of points. Points are the pairs
// (7, 4) (3, 2) and (1, 10).
cout << "Output1: " << endl;
cout << a; // Output should be what user entered.
cout << "Enter a sequence of points (integer)" << endl;
b.ReadPoints2(); // Enter another sequence.
cout << "Output2: " << endl;
cout << b;
Points2<int> c{a}; // Calls copy constructor for c.
cout << "After copy constructor1 c{a}: " << endl;
cout << c;
cout << a;
a = b; // Should call the copy assignment operator for a.
cout << "After assignment a = b" << endl;
cout << a;
Points2<int> e = move(c); // Move constructor for d.
cout << "After e = move(c) " << endl;
cout << e;
cout << c;
cout << "After a = move(e) " << endl;
a = move(e); // Move assignment operator for a.
cout << a;
cout << e;
}
void TestPart2() {
Points2<double> a, b;
cout << "Enter a sequence of points (double)" << endl;
a.ReadPoints2(); // User provides input for Points2 a.
cout << a;
cout << "Enter a sequence of points (double)" << endl;
b.ReadPoints2(); // User provides input for Points2 b.
cout << b << endl;
cout << "Result of a + b" << endl;
cout << a + b << endl;
Points2<double> d = a + b;
cout << "Result of d = a + b" << endl;
cout << d;
cout << "Second element in a: " << endl;
cout << a[1][0] << ", " << a[1][1] << endl; // Should print the 2nd element.
}
} // namespace
int
main(int argc, char **argv) {
TestPart1();
TestPart2();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment