/** File: P3.c* Copy: Copyright (c) 2021 Gregory C. Myers* BlazerID: wpires24* Vers: 1.0.0 02/20/2021 GCM - Original Coding* Desc: Utilizing functions build a command line utility that will* calculate various geometric properties*/
#include #include #include #include
/*Function prototypes*/int filled_rectangle(double b, double h);int hollow_rectangle(double b_outer, double h_outer, double b_inner, double h_inner);int filled_circle(double r);int hollow_circle(double r_outer, double r_inner);int filled_right_triangle(double b, double h);int help(void);
/** Name: int main(int argc, char** argv)* Desc: Used to drive other functions for the purpose to demo features in* class. Function calls are commented/uncommented depending on which features* are to be tested.*/int main(int argc, char** argv) { //Declaring variables int index; double b; double h; double b_outer; double h_outer; double b_inner; double h_inner; double r; double r_outer; double r_inner; //initializing b = 0; h = 0; b_outer = 0; h_outer = 0; b_inner = 0; h_inner = 0; r = 0; r_outer = 0; r_inner = 0; //Switches printf("argv[0]: %s\n", argv[0]); //name of executable for (index = 0; index printf("argv[%d]: %s\n", index, argv[index]); } //for if (argc help(); //implicit call for help } else { //handles scenarios where we have 2 or more arguments if (strcmpi(argv[1], "/h") == 0) { //explicit call for help } else if ((argc == 3) && (strcmpi(argv[1], "/i") == 0) && (strcmpi(argv[2], "/filled_rectangle") == 0)) { //filled_rectangle printf("Using interactive mode.\n"); printf("Please enter the base in inches:\n"); scanf("%lf", &b); printf("Please enter the height in inches:\n"); scanf("%lf", &h); filled_rectangle(b, h); } else if ((argc == 3) && (strcmpi(argv[1], "/i") == 0) && (strcmpi(argv[2], "/hollow_rectangle") == 0)) { //hollow_rectangle printf("Using interactive mode.\n"); printf("Please enter the outer base in inches:\n"); scanf("%lf", &b_outer); printf("Please enter the outer height in inches:\n"); scanf("%lf", &h_outer); printf("Please enter the inner base in inches:\n"); scanf("%lf", &b_inner); printf("Please enter the inner height in inches:\n"); scanf("%lf", &h_inner); hollow_rectangle(b_outer, h_outer, b_inner, h_inner); } else if ((argc == 3) && (strcmpi(argv[1], "/i") == 0) && (strcmpi(argv[2], "/filled_circle") == 0)) { //filled_circle printf("Using interactive mode.\n"); printf("Please enter the radius in inches:\n"); scanf("%lf", &r); filled_circle( r ); } else if ((argc == 3) && (strcmpi(argv[1], "/i") == 0) && (strcmpi(argv[2], "/filled_right_triangle") == 0)) { //filled_right_triangle //initializing just in case b = 0; h = 0; printf("Using interactive mode.\n"); printf("Please enter the base in inches:\n"); scanf("%lf", &b); printf("Please enter the height in inches:\n"); scanf("%lf", &h); filled_right_triangle(b, h); } else if ((argc == 5) && (strcmpi(argv[1], "/p") == 0) && (strcmpi(argv[2], "/filled_rectangle") == 0)) { //filled_rectangle printf("Using parametric mode.\n"); b = atof( argv[3] ); // 4th arg h = atof( argv[4] ); // 5th arg filled_rectangle(b, h); } else if ((argc == 7) && (strcmpi(argv[1], "/p") == 0) && (strcmpi(argv[2], "/hollow_rectangle") == 0)) { //hollow_rectangle printf("Using parametric mode.\n"); b_outer = atof( argv[3] ); // 4th arg h_outer = atof( argv[4] ); // 5th arg b_inner = atof( argv[5] ); // 6th arg h_inner = atof( argv[6] ); // 7th arg hollow_rectangle(b_outer, h_outer, b_inner, h_inner); } else if ((argc == 4) && (strcmpi(argv[1], "/p") == 0) && (strcmpi(argv[2], "/filled_circle") == 0)) { //filled_circle printf("Using parametric mode.\n"); r = atof( argv[3] ); // 4th arg filled_circle( r ); } else if ((argc == 5) && (strcmpi(argv[1], "/p") == 0) && (strcmpi(argv[2], "/hollow_circle") == 0)) { //hollow_circle printf("Using parametric mode.\n"); r_outer = atof( argv[3] ); // 4th arg r_inner = atof( argv[4] ); // 5th arg hollow_circle( r_outer, r_inner); } else if ((argc == 5) && (strcmpi(argv[1], "/p") == 0) && (strcmpi(argv[2], "/filled_right_triangle") == 0)) { //filled_right_triangle printf("Using parametric mode.\n"); b = atof( argv[3] ); // 4th arg h = atof( argv[4] ); // 5th arg filled_right_triangle(b, h); } else { help(); } //else } //else return (EXIT_SUCCESS);} //int main
/** Name: int filled_rectangle(double b, double h);* Desc: this displays the input args and also calculates the area, perimeter,* x_bar (x coordinate of centroid), y_bar (y coordinate of centroid),* Args:* b - base* h - height */int filled_rectangle(double b, double h){ //Declaring variables double area; double perimeter; double x_bar; double y_bar; double I_x; double I_y; //states name printf("filled_rectangle\n"); //Calculations area = b * h; perimeter = 2*b + 2*h; x_bar = b/2; y_bar = h/2; I_x = (b * pow(h,3)) / 12; I_y = (pow(b,3) * h) / 12; //output printf("Base: %f [in]\n", b); printf("Height: %f [in]\n", h); printf("Area: %f [in^2]\n", area); printf("Perimeter: %f [in]\n", perimeter); printf("x_bar: %f [in]\n", x_bar); printf("y_bar: %f [in]\n", y_bar); printf("I_x: %f [in^4]\n", I_x); printf("I_y: %f [in^4]\n", I_y); return (EXIT_SUCCESS);}
int help(void) { printf("============================================\n"); printf("File: P3.exe\n"); printf("Name: Walt Pires\n"); printf("BlazerID: wpires24\n"); printf("=============================================\n"); printf("p3.exe /h ...calls help\n"); printf("p3.exe /i /filled_rectangle ...gets info interactively, calculates properties\n"); printf("p3.exe /i /hollow_rectangle ...gets info interactively, calculates properties\n"); printf("p3.exe /i /filled_circle ...gets info interactively, calculates properties\n"); printf("p3.exe /i /hollow_circle ...gets info interactively, calculates properties\n"); printf("p3.exe /i /filled_right_triangle ...gets info interactively, calculates properties\n"); printf("p3.exe /p /filled_rectangle 4.0 2.0 ...gets info parametrically, calculates properties\n"); printf("p3.exe /p /hollow_rectangle 4.0 2.0 2.0 1.0 ...gets info parametrically, calculates properties\n"); printf("p3.exe /p /filled_circle 3.0 ...gets info parametrically, calculates properties\n"); printf("p3.exe /p /hollow_circle 3.0 2.0 ...gets info parametrically, calculates properties\n"); printf("p3.exe /p /filled_right_triangle 1.0 2.0 ...gets info parametrically, calculates properties\n"); printf("==============================================\n"); return (EXIT_SUCCESS);}