#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
#include <algorithm>
// Define a struct to represent a 3D point with x, y, and z coordinates
struct Point
{
double x, y, z;
};
// Function to validate and parse a string as three double values into a Point struct
bool validateDoubleInput(const std::string &input, Point &point)
{
std::istringstream iss(input);
return (iss >> point.x >> point.y >> point.z) && !iss.fail();
}
// Function to convert a string to lowercase in place using std::transform
void toLower(std::string &str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
// Function to read a Point from user input with validation
Point readPoint(const std::string &prompt)
{
Point point;
std::string input;
std::cout << prompt;
while (true)
{
std::getline(std::cin, input);
if (validateDoubleInput(input, point))
break;
printErrorMessage("Invalid input. Please enter three valid numbers only.");
std::cout << prompt;
}
return point;
}
// Define an enum type for the distance calculation methods
enum Method
{
EUCLIDEAN,
MANHATTAN
};
// Function to read the distance calculation method from user input and return the corresponding enum value
Method readMethod()
{
std::string method;
while (true)
{
std::cout << "Enter the distance calculation method (euclidean or manhattan): ";
std::cin >> method;
toLower(method);
if (method == "euclidean")
{
return EUCLIDEAN;
}
else if (method == "manhattan")
{
return MANHATTAN;
}
printErrorMessage("Invalid method. Please enter 'euclidean' or 'manhattan'.");
}
}
// Function to calculate Euclidean distance between two points using std::hypot
double calculateEuclideanDistance(const Point &p1, const Point &p2)
{
return std::round(std::hypot(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z));
}
// Function to calculate Manhattan distance between two points using std::abs
double calculateManhattanDistance(const Point &p1, const Point &p2)
{
return std::abs(p2.x - p1.x) + std::abs(p2.y - p1.y) + std::abs(p2.z - p1.z);
}
// Function to print an error message using std::cerr
void printErrorMessage(const std::string &message)
{
std::cerr << message << std::endl;
}
int main()
{
// Read and validate coordinates for Point 1 and Point 2
Point point1 = readPoint("Enter coordinates for Point 1 in the format 'x y z': ");
Point point2 = readPoint("Enter coordinates for Point 2 in the format 'x y z': ");
// Read the distance calculation method
Method method = readMethod();
double distance;
// Calculate the distance based on the chosen method using switch statement
switch (method)
{
case EUCLIDEAN:
distance = calculateEuclideanDistance(point1, point2);
break;
case MANHATTAN:
distance = calculateManhattanDistance(point1, point2);
break;
default:
printErrorMessage("Invalid method.");
return 1;
}
// Display the calculated distance
std::cout << "The " << (method == EUCLIDEAN ? "euclidean" : "manhattan") << " distance between the two points is: " << distance << std::endl;
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}