Skip to content

Commit

Permalink
feat: added c++ grader example
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrieleMaurina committed Dec 15, 2024
1 parent c7aa55a commit 30ab8c0
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Example grading scripts for RamDesk
* C assignment with Python grader
* [Sum](c_assignment_python_grader/sum/README.md)
* [Factorial](c_assignment_python_grader/factorial/README.md)
* C++ assignment with C++ grader
* [Triangle](c++_assignment_c++_grader/triangle/README.md)
* C++ assignment with table grader
* [Reverse String](c++_assignment_table_grader/reverse_string/README.md)
* Java assignment with Java grader
Expand Down
34 changes: 34 additions & 0 deletions c++_assignment_c++_grader/triangle/README.md
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".
2 changes: 2 additions & 0 deletions c++_assignment_c++_grader/triangle/grader/Makefile
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
94 changes: 94 additions & 0 deletions c++_assignment_c++_grader/triangle/grader/main.cpp
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;
}
28 changes: 28 additions & 0 deletions c++_assignment_c++_grader/triangle/grader/triangle.h
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
29 changes: 29 additions & 0 deletions c++_assignment_c++_grader/triangle/solution/triangle.cpp
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;
}

0 comments on commit 30ab8c0

Please sign in to comment.