102 lines
1.8 KiB
C
102 lines
1.8 KiB
C
/* Area of a Triangle
|
||
*
|
||
* A prgorgam that cauclatales the area of a
|
||
* triangle based on user-provided parameters.
|
||
*/
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <math.h>
|
||
|
||
int main() {
|
||
int o; /* Option */
|
||
|
||
/* Print the options, then prompt */
|
||
printf("(1) ASA\n");
|
||
printf("(2) SAS\n");
|
||
printf("(3) SSS\n");
|
||
printf("(1|2|3) ");
|
||
scanf("%d", &o);
|
||
|
||
/* Sanity Check
|
||
*
|
||
* If the user provides an argument that
|
||
* is not 1, 2, or 3, exit the program.
|
||
*/
|
||
if (o != 1 && o != 2 && o != 3) {
|
||
printf("Error: Invalid Argument\n");
|
||
|
||
exit(1);
|
||
}
|
||
|
||
/* ASA */
|
||
if (o == 1) {
|
||
float a1, s, a2;
|
||
|
||
/* Prompt user for variables */
|
||
printf("Enter the first angle: ");
|
||
scanf("%f", &a1);
|
||
|
||
printf("Enter the side: ");
|
||
scanf("%f", &s);
|
||
|
||
printf("Enter the second angle: ");
|
||
scanf("%f", &a2);
|
||
|
||
/* Convert degrees to radians */
|
||
a1 = a1 * (M_PI / 180);
|
||
a2 = a2 * (M_PI / 180);
|
||
|
||
/* Print the area */
|
||
printf("Area = %.2f", (s * s * sin(a1) * sin(a2)) / (2 * sin(a1 + a2)));
|
||
|
||
}
|
||
|
||
/* SAS */
|
||
if (o == 2) {
|
||
float s1, a, s2;
|
||
|
||
/* Prompt user for variables */
|
||
printf("Enter the first side: ");
|
||
scanf("%f", &s1);
|
||
|
||
printf("Enter the angle: ");
|
||
scanf("%f", &a);
|
||
|
||
printf("Enter the second side: ");
|
||
scanf("%f", &s2);
|
||
|
||
/* Convert degrees to radians */
|
||
a = a * (M_PI / 180);
|
||
|
||
/* Print the area */
|
||
printf("Area = %.2f", 0.5 * s1 * s2 * sin(a));
|
||
}
|
||
|
||
/* SSS */
|
||
if (o == 3) {
|
||
float s1, s2, s3;
|
||
|
||
/* Prompt user for variables */
|
||
printf("Enter the first side: ");
|
||
scanf("%f", &s1);
|
||
|
||
printf("Enter the second side: ");
|
||
scanf("%f", &s2);
|
||
|
||
printf("Enter the third side: ");
|
||
scanf("%f", &s3);
|
||
|
||
/* Calculate the value of the additional variable, 𝑧 */
|
||
float z = (s1 + s2 + s3) / 2; /* 𝑧 */
|
||
|
||
/* Print the area */
|
||
printf("Area = %.2f", sqrt(z * (z - s1) * (z - s2) * (z - s3)));
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
|
||
|
||
|