Area Calculator
This is a simple ish code to calculate the area of the chosen shape I'll add any formula/shape that people ask me to but for now I'll add the basic ones.
This is in C++ btw also if anyone know how I can syntax highlight these that'd be great.
#include <iostream>
#include <cmath>
int main(){
int input;
std::cout << "Curently available:\n"<< "1. Triangle\n" << "2. Triangle2\n" << "3. Rectangle\n"<< "4. Square\n" << "5. Regular Pentagon\n" << "Please input the desired shape:\n";
std::cin >> input;
switch(input) {
case 1:
float area, p, a, b, c;
std::cout << "Please input the lenghts of the triangle: ";
std::cin >> a >> b >> c;
p = (a+b+c)/2;
if(0>a || 0>b || 0>c || 0>p*(p-a)*(p-b)*(p-c)){
std::cout << "This is an invalid triangle.\n";
} else {
area = sqrt(p*(p-a)*(p-b)*(p-c));
std::cout << area << "\n";
}
break;
case 2:
float area2, a2, b2;
std::cout << "Please input the height and base of the triangle: ";
std::cin >> a2 >> b2;
area2 = a2*b2/2;
if(0>a2 || 0>b2){
std::cout << "This is an invalid triangle.\n";
}else {
std::cout << area2 << "\n";
}
break;
case 3:
float area1, a1, b1;
std::cout << "Please input the lenghts of the rectangle: ";
std::cin>>a1>>b1;
area1 = a1*b1;
if(0>a1 || 0>b1){
std::cout << "This is an invalid rectangle.\n";
} else{
std::cout <<area1<<"\n";
}
break;
case 4:
float area4, a4;
std::cout << "Please input the lenght of the side: ";
std::cin >> a4;
area4 = a4*a4;
if(a4>0){
std::cout << area4 << "\n";
} else {
std::cout << "This is an invalid square.\n";
}
break;
case 5:
float area3, a3;
std::cout << "Please input the lenght of the side: ";
std::cin >> a3;
area3 = 1.72*a3*a3;
if(a3>0){
std::cout << area3 << "\n";
} else{
std::cout << "This is an invalid regular pentagram, please re-check it. \n";
}
default:
break;
}
return 0;
}
#include <iostream>
#include <cmath>
int main(){
int input;
std::cout << "Curently available:\n"<< "1. Triangle\n" << "2. Triangle2\n" << "3. Rectangle\n"<< "4. Square\n" << "5. Regular Pentagon\n" << "Please input the desired shape:\n";
std::cin >> input;
switch(input) {
case 1:
float area, p, a, b, c;
std::cout << "Please input the lenghts of the triangle: ";
std::cin >> a >> b >> c;
p = (a+b+c)/2;
if(0>a || 0>b || 0>c || 0>p*(p-a)*(p-b)*(p-c)){
std::cout << "This is an invalid triangle.\n";
} else {
area = sqrt(p*(p-a)*(p-b)*(p-c));
std::cout << area << "\n";
}
break;
case 2:
float area2, a2, b2;
std::cout << "Please input the height and base of the triangle: ";
std::cin >> a2 >> b2;
area2 = a2*b2/2;
if(0>a2 || 0>b2){
std::cout << "This is an invalid triangle.\n";
}else {
std::cout << area2 << "\n";
}
break;
case 3:
float area1, a1, b1;
std::cout << "Please input the lenghts of the rectangle: ";
std::cin>>a1>>b1;
area1 = a1*b1;
if(0>a1 || 0>b1){
std::cout << "This is an invalid rectangle.\n";
} else{
std::cout <<area1<<"\n";
}
break;
case 4:
float area4, a4;
std::cout << "Please input the lenght of the side: ";
std::cin >> a4;
area4 = a4*a4;
if(a4>0){
std::cout << area4 << "\n";
} else {
std::cout << "This is an invalid square.\n";
}
break;
case 5:
float area3, a3;
std::cout << "Please input the lenght of the side: ";
std::cin >> a3;
area3 = 1.72*a3*a3;
if(a3>0){
std::cout << area3 << "\n";
} else{
std::cout << "This is an invalid regular pentagram, please re-check it. \n";
}
default:
break;
}
return 0;
}
Comments
Post a Comment