In: Computer Science
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that c is at least as large as the other two sides. 4. Please run your program with all of your test cases from HW2. The results should be identical to that from HW2. You must supply a listing of your program and sample output CODE right now #include <cstdlib> //for exit, atoi, atof #include <iostream> //for cout and cerr #include <iomanip> //for i/o manipulation #include <cstring> //for strcmp #include <cmath> //for fabs using namespace std; //triangle function void triangle(int a, int b, int c) { if (a + b <= c || a + c <= b || b + c <= a) //conditions for an invalid triangle cout << a << " " << b << " " << c << " " << "Not a triangle"; else //if above returned false, then it is a valid triangle { int temp; cout << a << " " << b << " " << c << " "; //print the side values //logic to find out the largest value and store it to c. if (a > b && a > c) { temp = a; a = c; c = temp; } else if (b > a && b > c) { temp = b; b = c; c = temp; } if (a == b && b == c) //condition for equilateral triangle { cout << "Equilateral triangle"; return; } else if (a * a == b * b + c * c || b * b == c * c + a * a || c * c == a * a + b * b) //condition for right triangle i.e. pythagoras theorem cout << "Right "; if (a == b || b == c || a == c) //condition for Isosceles triangle cout << "Isosceles triangle"; else //if both the above ifs failed, then it is a scalene triangle cout << "Scalene triangle"; } } //overloaded triangle function void triangle(double a, double b, double c) { //equality threshold value for absolute difference procedure const double EPSILON = 0.001; if (a + b <= c || a + c <= b || b + c <= a) //conditions for an invalid triangle cout << fixed << setprecision(5) << a << " " << b << " " << c << " " << "Not a triangle"; else //if above returned false, then it is a valid triangle { double temp; cout << fixed << setprecision(5) << a << " " << b << " " << c << " "; //print the side values //logic to find out the largest value and store it to c. if (a > b && a > c) { temp = a; a = c; c = temp; } else if (b > a && b > c) { temp = b; b = c; c = temp; } if (fabs(a - b) < EPSILON && fabs(b - c) < EPSILON) //condition for equilateral triangle { cout << "Equilateral triangle"; return; } else if (fabs((a * a) - (b * b + c * c)) < EPSILON || fabs((b * b) - (c * c + a * a)) < EPSILON || fabs((c * c) - (a * a + b * b)) < EPSILON) //condition for right triangle i.e. pythagoras theorem cout << "Right "; if (fabs(a - b) < EPSILON || fabs(b - c) < EPSILON || fabs(a - c) < EPSILON) //condition for Isosceles triangle cout << "Isosceles triangle"; else //if both the above ifs failed, then it is a scalene triangle cout << "Scalene triangle"; } } //main function int main(int argc, char **argv) { if (argc == 3 || argc > 5) //check argc for argument count { cerr << "Incorrect number of arguments. " << endl; exit(1); } else if (argc == 1) //if no command arguments found then do the following { int a, b, c; //declare three int cin >> a >> b >> c; //read the values if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit { cerr << "Data must be a positive integer. " << endl; exit(1); } triangle(a, b, c); //call the function if inputs are valid } else //if command arguments were found and valid { if (strcmp(argv[1], "-i") == 0) { int a, b, c, temp; //declare required variables if (argc == 5) { //convert char to int using atoi() a = atoi(argv[2]); b = atoi(argv[3]); c = atoi(argv[4]); } else { cin >> a >> b >> c; // read the values } if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit { cerr << "Data must be a positive integer. " << endl; exit(1); } //call the function triangle(a, b, c); } else if (strcmp(argv[1], "-d") == 0) { double a, b, c, temp; //declare required variables if (argc == 5) { //convert char to int using atoi() a = atof(argv[2]); b = atof(argv[3]); c = atof(argv[4]); } else { cin >> a >> b >> c; // read the values } if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit { cerr << "Data must be a positive double. " << endl; exit(1); } //call the overloaded function triangle(a, b, c); } } cout << endl; return 0; }
The below code has been modified to convert the functions return type from void to string. comment as //added has been included in the modified parts of the code.
#include <cstdlib> //for exit, atoi, atof
#include <iostream> //for cout and cerr
#include <iomanip> //for i/o manipulation
#include <cstring> //for strcmp
#include <cmath> //for fabs
#include <string>
using namespace std;
//triangle function
string triangle(int a, int b, int c)
{
string res;
if (a + b <= c || a + c <= b || b + c <= a) {//conditions for an invalid triangle
res = to_string(a) + " " +to_string(b)+ " "+to_string(c) + " "+ "Not a triangle"; //added
return res;
}
else //if above returned false, then it is a valid triangle
{
int temp;
res = to_string(a) + " " +to_string(b)+ " "+to_string(c) + " "; //added
//logic to find out the largest value and store it to c.
if (a > b && a > c)
{
temp = a;
a = c;
c = temp;
}
else if (b > a && b > c)
{
temp = b;
b = c;
c = temp;
}
if (a == b && b == c) //condition for equilateral triangle
{
res = res + "Equilateral triangle"; //added
return res;
}
else if (a * a == b * b + c * c ||
b * b == c * c + a * a ||
c * c == a * a + b * b) //condition for right triangle i.e. pythagoras theorem
res = res + "Right "; //added
if (a == b || b == c || a == c) //condition for Isosceles triangle
res = res + "Isosceles triangle";//added
else //if both the above ifs failed, then it is a scalene triangle
res = res + "Scalene triangle"; //added
}
return res;
}
//overloaded triangle function
string triangle(double a, double b, double c)
{
//equality threshold value for absolute difference procedure
const double EPSILON = 0.001;
string res;
if (a + b <= c || a + c <= b || b + c <= a) {//conditions for an invalid triangle
res = to_string(a) + " " +to_string(b)+ " "+to_string(c) + " "+ "Not a triangle"; //added
return res;
}
else //if above returned false, then it is a valid triangle
{
double temp;
res = to_string(a) + " " +to_string(b)+ " "+to_string(c) + " "; //added
//logic to find out the largest value and store it to c.
if (a > b && a > c)
{
temp = a;
a = c;
c = temp;
}
else if (b > a && b > c)
{
temp = b;
b = c;
c = temp;
}
if (fabs(a - b) < EPSILON &&
fabs(b - c) < EPSILON) //condition for equilateral triangle
{
res = res + "Equilateral triangle"; //added
//return;
}
else if (fabs((a * a) - (b * b + c * c)) < EPSILON ||
fabs((b * b) - (c * c + a * a)) < EPSILON ||
fabs((c * c) - (a * a + b * b)) < EPSILON) //condition for right triangle i.e. pythagoras theorem
// cout << "Right ";
res = res + "Right ";
if (fabs(a - b) < EPSILON ||
fabs(b - c) < EPSILON ||
fabs(a - c) < EPSILON) //condition for Isosceles triangle
res = res + "Isosceles triangle"; //added
else //if both the above ifs failed, then it is a scalene triangle
res = res + "Scalene triangle"; //added
}
return res;
}
//main function
int main(int argc, char **argv)
{
if (argc == 3 || argc > 5) //check argc for argument count
{
cerr << "Incorrect number of arguments. " << endl;
exit(1);
}
else if (argc == 1) //if no command arguments found then do the following
{
int a, b, c; //declare three int
string res; //added
cin >> a >> b >> c; //read the values
if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit
{
cerr << "Data must be a positive integer. " << endl;
exit(1);
}
//call the function if inputs are valid
res = triangle(a, b, c);
cout<< res ; //added
}
else //if command arguments were found and valid
{
if (strcmp(argv[1], "-i") == 0)
{
int a, b, c, temp; //declare required variables
string res;
if (argc == 5)
{
//convert char to int using atoi()
a = atoi(argv[2]);
b = atoi(argv[3]);
c = atoi(argv[4]);
}
else
{
cin >> a >> b >> c; // read the values
}
if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit
{
cerr << "Data must be a positive integer. " << endl;
exit(1);
}
//call the function
res = triangle(a, b, c);
cout<< res ; //added
}
else if (strcmp(argv[1], "-d") == 0)
{
double a, b, c, temp; //declare required variables
string res;
if (argc == 5)
{
//convert char to int using atoi()
a = atof(argv[2]);
b = atof(argv[3]);
c = atof(argv[4]);
}
else
{
cin >> a >> b >> c; // read the values
}
if (a <= 0 || b <= 0 || c <= 0) //if values are negative then exit
{
cerr << "Data must be a positive double. " << endl;
exit(1);
}
//call the overloaded function
res = triangle(a, b, c);
cout << fixed << setprecision(5) << res; //added
}
}
cout << endl;
return 0;
}
Output screenshot: