Skip to content

Instantly share code, notes, and snippets.

@AhmedMourad0
Created April 30, 2018 18:12
Show Gist options
  • Save AhmedMourad0/45f8cd215e9974f4517836afc4bb2b0d to your computer and use it in GitHub Desktop.
Save AhmedMourad0/45f8cd215e9974f4517836afc4bb2b0d to your computer and use it in GitHub Desktop.
A C++ report for College.
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
<component name="ProjectDictionaryState">
<dictionary name="DevAh">
<words>
<w>hany</w>
<w>mourad</w>
</words>
</dictionary>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ClassReport.iml" filepath="$PROJECT_DIR$/.idea/ClassReport.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
cmake_minimum_required(VERSION 3.8)
project(ClassReport)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES main.cpp Rect.cpp Rect.h Point.h Point.cpp)
add_executable(ClassReport ${SOURCE_FILES})
/**
* Ahmed Hany Mourad -> Section 2
*/
#include <iostream>
#include "Rect.h"
using namespace std;
void printRectResults(Rect &, Rect &);
int main() {
cout << "\n\n\t\tFirst Attempt\n\n";
Point lowerLeftPoint = {2, 10};
Point upperRightPoint = {50, 30};
Rect rect = {lowerLeftPoint, upperRightPoint};
lowerLeftPoint = {10, 5};
upperRightPoint = {100, 45};
Rect secondaryRect = {lowerLeftPoint, upperRightPoint};
rect.printVertices("rect");
secondaryRect.printVertices("secondaryRect");
printRectResults(rect, secondaryRect);
cout << "\n\t\tSecond Attempt\n\n";
lowerLeftPoint = {0, 0};
upperRightPoint = {70, 70};
rect = {lowerLeftPoint, upperRightPoint};
lowerLeftPoint = {10, 10};
upperRightPoint = {45, 56};
secondaryRect = {lowerLeftPoint, upperRightPoint};
rect.printVertices("rect");
secondaryRect.printVertices("secondaryRect");
printRectResults(rect, secondaryRect);
return 0;
}
void printRectResults(Rect &rect, Rect &secondaryRect) {
cout << "Rect results:"
<< "\n\tLength = " << rect.getLength()
<< "\n\tArea = " << rect.rectArea()
<< "\n\tPerimeter = " << rect.rectPeri()
<< "\n\tHas origin vertex = " << rect.rectOrg()
<< "\n\tIs perfect square = " << rect.rectPerf()
<< "\n\tArea Comparison Result = " << rect.rectComp(secondaryRect)
<< "\n\tContains the other rect = " << rect.rectCont(secondaryRect)
<< endl << endl;
}
#include "Point.h"
bool Point::isOrigin() {
return x == 0 && y == 0;
}
#ifndef POINT_H
#define POINT_H
struct Point {
double x;
double y;
bool isOrigin();
};
#endif
#include <iostream>
#include "Rect.h"
Rect::Rect(Point lowerLeftPoint, Point upperRightPoint) {
Rect::lowerLeftPoint = lowerLeftPoint;
Rect::upperRightPoint = upperRightPoint;
upperLeftPoint = {lowerLeftPoint.x, upperRightPoint.y};
lowerRightPoint = {upperRightPoint.x, lowerLeftPoint.y};
// Rect is drawn from bottom left corner (0,0) instead of top left corner for simplicity
width = upperRightPoint.x - upperLeftPoint.x;
height = upperLeftPoint.y - lowerLeftPoint.y;
}
/**
* The length is the largest of the rect's two dimensions
* @return the rect's length
*/
double Rect::getLength() {
return height > width ? height : width;
}
double Rect::rectPeri() {
return (width + height) * 2;
}
double Rect::rectArea() {
return width * height;
}
/**
* Compares this Rect to another
* @param rect The Rect to compare
* @return 1 if this Rect is larger in area, 2 if the given Rect is, 0 if they're equal
*/
int Rect::rectComp(Rect rect) {
if (rectArea() > rect.rectArea())
return 1;
else if (rectArea() < rect.rectArea())
return -1;
else
return 0;
}
/**
* Checks if the given Rect is totally contained inside this one
* @param rect The Rect to check
* @return 1 if the given Rect is totally contained inside this one, 0 otherwise
*/
int Rect::rectCont(Rect rect) {
bool top = rect.upperLeftPoint.y < upperLeftPoint.y;
bool bottom = rect.lowerLeftPoint.y > lowerLeftPoint.y;
bool left = rect.lowerLeftPoint.x > lowerLeftPoint.x;
bool right = rect.upperRightPoint.x < upperRightPoint.x;
bool isContained = top && bottom && right && left;
return isContained ? 1 : 0;
}
/**
* Checks if this Rect is a perfect square
* @return 1 if this Rect is a perfect square, 0 otherwise
*/
int Rect::rectPerf() {
return width == height ? 1 : 0;
}
/**
* Checks if one of the Rect's vertices is on the origin (0,0)
* @return 1 if one of the Rect's vertices is on the origin (0,0), 0 otherwise
*/
int Rect::rectOrg() {
return upperRightPoint.isOrigin() ||
lowerLeftPoint.isOrigin() ||
upperLeftPoint.isOrigin() ||
lowerRightPoint.isOrigin();
}
void Rect::printVertices(const std::string &name) {
std::cout << name << ":"
<< "\n\tLower left point = (" << lowerLeftPoint.x << "," << lowerLeftPoint.y << ")"
<< "\n\tLower right point = (" << lowerRightPoint.x << "," << lowerRightPoint.y << ")"
<< "\n\tUpper left point = (" << upperLeftPoint.x << "," << upperLeftPoint.y << ")"
<< "\n\tUpper right point = (" << upperRightPoint.x << "," << upperRightPoint.y << ")"
<< std::endl << std::endl;
}
#ifndef RECT_H
#define RECT_H
#include "Point.h"
#include <string>
class Rect {
private:
Point lowerLeftPoint;
Point upperRightPoint;
/* For the sake of simplicity */
Point lowerRightPoint;
Point upperLeftPoint;
double height;
double width;
public:
Rect(Point, Point);
double getLength();
double rectPeri();
double rectArea();
int rectComp(Rect);
int rectCont(Rect);
int rectPerf();
int rectOrg();
/* For demonstration */
void printVertices(const std::string &);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment