-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7aa55a
commit 30ab8c0
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Triangle (C++) | ||
|
||
[< Home](../../README.md) | ||
|
||
Given the file `triangle.h`, write the implementation for the `Triangle` class inside `triangle.cpp`. | ||
|
||
|
||
### Examples | ||
|
||
```bash | ||
./triangle | ||
# Triangle | ||
# Sides: 37.7156, 31.4852, 25.288 | ||
# Perimeter: 94.4888 | ||
# Area: 394.677 | ||
# Equilateral: 0 | ||
# Isosceles: 0 | ||
# Scalene: 1 | ||
# Test passed | ||
# ... | ||
``` | ||
|
||
|
||
### Grade command | ||
|
||
This is the command to set up the grader in RamDesk. | ||
|
||
```bash | ||
./triangle | ||
``` | ||
|
||
### RamDesk Grade Configuration | ||
|
||
Make sure to enble the task "Make" with location "Root". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
triangle: triangle.h sub/triangle.cpp | ||
g++ -I. main.cpp sub/triangle.cpp -o triangle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "triangle.h" | ||
|
||
#include <cmath> | ||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
|
||
double get_perimeter(double s1, double s2, double s3) { | ||
return s1 + s2 + s3; | ||
} | ||
|
||
double get_area(double s1, double s2, double s3) { | ||
double s = (s1 + s2 + s3) / 2.0; | ||
return std::sqrt(s * (s - s1) * (s - s2) * (s - s3)); | ||
} | ||
|
||
bool is_equilateral(double s1, double s2, double s3) { | ||
return s1 == s2 && s2 == s3; | ||
} | ||
|
||
bool is_isosceles(double s1, double s2, double s3) { | ||
return s1 == s2 || s1 == s3 || s2 == s3; | ||
} | ||
|
||
bool is_scalene(double s1, double s2, double s3) { | ||
return s1 != s2 && s1 != s3 && s2 != s3; | ||
} | ||
|
||
double random_side() | ||
{ | ||
return 1.0 + (double) rand() / RAND_MAX * 99.0; | ||
} | ||
|
||
bool prob(double p) | ||
{ | ||
return (double) rand() / RAND_MAX < p; | ||
} | ||
|
||
int test(){ | ||
double s1; | ||
double s2; | ||
double s3; | ||
double area; | ||
do { | ||
s1 = random_side(); | ||
s2 = random_side(); | ||
s3 = random_side(); | ||
if (prob(0.3)) { // make it isosceles | ||
s2 = s1; | ||
} | ||
if (prob(0.3)) { // make it equilateral | ||
s3 = s1; | ||
} | ||
area = get_area(s1, s2, s3); | ||
} while (area != area); // make sure it's a valid triangle | ||
|
||
Triangle t(s1, s2, s3); | ||
t.print(); | ||
|
||
int score = 0; | ||
if (t.get_perimeter() == get_perimeter(s1, s2, s3)) { | ||
score += 1; | ||
} | ||
if (t.get_area() == get_area(s1, s2, s3)) { | ||
score += 1; | ||
} | ||
if (t.is_equilateral() == is_equilateral(s1, s2, s3)) { | ||
score += 1; | ||
} | ||
if (t.is_isosceles() == is_isosceles(s1, s2, s3)) { | ||
score += 1; | ||
} | ||
if (t.is_scalene() == is_scalene(s1, s2, s3)) { | ||
score += 1; | ||
} | ||
|
||
if (score == 5) { | ||
std::cout << "Test passed" << std::endl; | ||
} else { | ||
std::cout << "Test failed" << std::endl; | ||
} | ||
return score; | ||
} | ||
|
||
|
||
int main() { | ||
srand(time(0)); | ||
int score = 0; | ||
for (int i = 0; i < 20; i++) { | ||
score += test(); | ||
} | ||
std::cout << score << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef TRIANGLE_H | ||
#define TRIANGLE_H | ||
|
||
#include <iostream> | ||
|
||
class Triangle { | ||
private: | ||
double side1, side2, side3; | ||
|
||
public: | ||
Triangle(double s1, double s2, double s3); | ||
double get_perimeter() const; | ||
double get_area() const; | ||
bool is_equilateral() const; | ||
bool is_isosceles() const; | ||
bool is_scalene() const; | ||
void print() const { | ||
std::cout << "Triangle" << std::endl; | ||
std::cout << "Sides: " << side1 << ", " << side2 << ", " << side3 << std::endl; | ||
std::cout << "Perimeter: " << get_perimeter() << std::endl; | ||
std::cout << "Area: " << get_area() << std::endl; | ||
std::cout << "Equilateral: " << is_equilateral() << std::endl; | ||
std::cout << "Isosceles: " << is_isosceles() << std::endl; | ||
std::cout << "Scalene: " << is_scalene() << std::endl; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "triangle.h" | ||
|
||
#include <cmath> | ||
#include <iostream> | ||
|
||
|
||
Triangle::Triangle(double s1, double s2, double s3) | ||
: side1(s1), side2(s2), side3(s3) {} | ||
|
||
double Triangle::get_perimeter() const { | ||
return side1 + side2 + side3; | ||
} | ||
|
||
double Triangle::get_area() const { | ||
double s = get_perimeter() / 2.0; | ||
return std::sqrt(s * (s - side1) * (s - side2) * (s - side3)); | ||
} | ||
|
||
bool Triangle::is_equilateral() const { | ||
return side1 == side2 && side2 == side3; | ||
} | ||
|
||
bool Triangle::is_isosceles() const { | ||
return side1 == side2 || side1 == side3 || side2 == side3; | ||
} | ||
|
||
bool Triangle::is_scalene() const { | ||
return side1 != side2 && side1 != side3 && side2 != side3; | ||
} |