C programming In summary just clean up the mess Specificall

----------------C programming-----------

In summary - just clean up the mess. Specifically, use functions where functions are appropriate.

#define _USE_MATH_DEFINES

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>

#ifndef M_PI
#define M_PI acos(-1.0)
#endif

// our first use of the function prototype to provide a declaration before use.
// note the function implementation is placed after usage in main.
// function prototype or declarations is considered good form, even though the language doesnt require such
void PrintExitBanner(const char *, const char *);

_Bool bHasError = false;

// note the use of the const specifier on these four strings
// these are literals that should not change during the lifetime of the program execution
//
// try adding this to the program and observe the output generated during compilation phase
// dueDate[0]=\'S\'; dueDate[1]=\'u\'; dueDate[2]=\'n\';
const char *assignmentID = \"Project 02\";
const char *classId = \"CSC-250 Computer Science II (a.k.a. C II) Spring 2017\";
const char *programmerName = \"Roy Fine\";
const char *dueDate = \"Monday AM, February 6, 2017\";

void print_usage(void)
{
printf(\"usage:\ \"
\" Project 02 compute area/volume for common geometric shapes\ \"
\" area calcualtors:\ \"
\" square width\ \"
\" circle radius\ \"
\" annulus inner_radius outer_radius\ \"
\" volume calculators:\ \"
\" square_prism width height\ \"
\" cylinder radius height\ \"
\" torus minor_radius major_radius\ \"
\" example:\ \"
\" square_prism 4 12\ \"
\" cylinder 32 320\ \"
\" torus 17 45\ \");
}

_Bool HasSilentFlag(int argc, char *argv[])
{
for (size_t n = 1; n < (unsigned int)argc; n++)
{
if (0 == strcmp(\"-quiet\", argv[n]))
return true;
}
  
return false;
}

void PrintEntryBanner(const char *sAssignID, const char *sClassID, const char *sDate, const char *sDeveloper)
{
// Is the multiline C string literal a new form?
// Adjacent string literal tokens are concatenated (during phase 6) into a single literal token
// See 6.4.5 String literals, paragraph 5 (Semantics) of C 2011 Language Standard (N1570.pdf draft)
  
// It is not until compilation phase 6 that the C string null terminator char is appended,
// and that is why the following produces one C string and not 5...
printf(\"\ --------------------------------------------------------------------------\ \"
\"Project ID: %s\ \"
\"Class ID: %s\ \"
\"Program Due Date: %s\ \"
\"Programmer ID: %s\ \ \", sAssignID, sClassID, sDate, sDeveloper);
}

// ===============================================================
// main - this is where all of the action occurs...
// ===============================================================
int main(int argc, char *argv[])
{
// dueDate[0] = \'S\'; dueDate[1] = \'u\'; dueDate[2] = \'n\';
  
if (argc < 2)
{
print_usage(); // if no args, then show usage and quit early
return 0;
}

if (!HasSilentFlag(argc, argv))
{
PrintEntryBanner(assignmentID, classId, dueDate, programmerName);
}

char *completionStatus = \"Success\"; // set this to Success here. on error we will reset it to appropriate messaging

// C is missing the case insensitive compare - so we coerce input arg to lower case, and compare on that
// OH MY - we are modifying a string that we do not have ownership of. In general, that is a very very very bad thing to do
// If the modifying of arguments stings would result in either undefined or unspecified behavior as covered in Annex J of C 2011 Language Standard (draft as of N1570), then you must fix this
// Hint -- check Section 5.1.2.2.1, paragraph 2 of C 2011 Language Standard (N1570.pdf draft)
for (char *p = argv[1]; *p; p++)
{
if (isalpha(*p) && isupper(*p))
{
*p = (char)tolower(*p);
}
}

// step throug hthe possible candidates - test for each
// if we find a request that we handle, we do so, else we move to the end
// if we get to the end and find we have not handled the request (e.g. the final else)
// we treat that as as error and report

// for each case that we handle, do a modest amount of input validation
// make sure width, widsth, radius are non-negative
// where appropriate, validate relationships

if (0 == strcmp(argv[1], \"square\") && (argc >= 3))
{
double base = atof(argv[2]);
if (base >= 0.0)
{
double area = base * base;
printf(\"%18s: base: %.2lf area: %.2f\ \", \"square\", base, area);
}
else
{
printf(\"%18s: base: %.2lf ***> error <*** Bad Data\ \", \"square\", base);
bHasError = true;
completionStatus = \"***> error <*** square(bad data)\";
}
}
else if (0 == strcmp(argv[1], \"circle\") && (argc >= 3))
{
double radius = atof(argv[2]);
if (radius >= 0.0)
{
double area = radius * radius * M_PI;
printf(\"%18s: radius: %.2lf area: %.2f\ \", \"circle\", radius, area);
}
else
{
printf(\"%18s: radius: %.2lf ***> error <*** Bad Data\ \", \"circle\", radius);
bHasError = true;
completionStatus = \"***> error <*** circle(bad data)\";
}
}
else if (0 == strcmp(argv[1], \"annulus\") && (argc >= 4))
{
double inner_radius = atof(argv[2]);
double outer_radius = atof(argv[3]);
if ((inner_radius >= 0.0) && (outer_radius >= 0.0) && (inner_radius <= outer_radius))
{
double area = (outer_radius*outer_radius - inner_radius*inner_radius) * M_PI;
printf(\"%18s: inner radius: %.2lf outer radius: %.2lf area: %.2f\ \", \"annulus\", inner_radius, outer_radius, area);
}
else
{
printf(\"%18s: inner_radius: %.2lf outer_radius: %.2lf ***> error <*** Bad Data\ \", \"annulus\", inner_radius, outer_radius);
bHasError = true;
completionStatus = \"***> error <*** annulus(bad data)\";
}
}
else if (0 == strcmp(argv[1], \"square_prism\") && (argc >= 4))
{
double width = atof(argv[2]);
double height = atof(argv[3]);
if ((width >= 0.0) && (height >= 0.0))
{
double volume = (width*width) * height;
printf(\"%18s: width: %.2lf height: %.2lf volume: %.2f\ \", \"square_prism\", width, height, volume);
}
else
{
printf(\"%18s: width: %.2lf height: %.2lf ***> error <*** Bad Data\ \", \"square_prism\", width, height);
bHasError = true;
completionStatus = \"***> error <*** square_prism(bad data)\";
}
}
else if (0 == strcmp(argv[1], \"cylinder\") && (argc >= 4))
{
double radius = atof(argv[2]);
double height = atof(argv[3]);
if ((radius >= 0.0) && (height >= 0.0))
{
double volume = height * radius * radius * M_PI;
printf(\"%18s: radius: %.2lf height: %.2lf volume: %.2f\ \", \"cylinder\", radius, height, volume);
}
else
{
printf(\" cylinder: radius: %.2lf height: %.2lf ***> error <*** Bad Data\ \", radius, height);
bHasError = true;
completionStatus = \"***> error <*** cylinder(bad data)\";
}
}
else if (0 == strcmp(argv[1], \"torus\") && (argc >= 4))
{
double minor_radius = atof(argv[2]);
double major_radius = atof(argv[3]);
if (minor_radius <= major_radius)
{
if ((minor_radius >= 0.0) && (major_radius >= 0.0))
{
double volume = (minor_radius*minor_radius * major_radius) * M_PI * M_PI * 2;
printf(\"%18s: minor radius: %.2lf major radius: %.2lf volume: %.2f\ \", \"torus\", minor_radius, major_radius, volume);
}
else
{
printf(\"%18s: minor radius: %.2lf major radius: %.2lf ***> error <*** Bad Data(negative data)\ \", \"torus\", minor_radius, major_radius);
bHasError = true;
completionStatus = \"***> error <*** torus(bad data)\";
}
}
else
{
printf(\" torus: minor radius: %.2lf major radius: %.2lf ***> error <*** Bad Data(invalid radii)\ \", minor_radius, major_radius);
bHasError = true;
completionStatus = \"***> error <*** torus(bad data)\";
}
}
else
{
printf(\" unknown or unhandled shape: %s\ \", argv[1]);
bHasError = true;
completionStatus = \"***> error <*** unknown shape\";
}

if (!HasSilentFlag(argc, argv) || bHasError)
PrintExitBanner(assignmentID, completionStatus);


return 0;
}

void PrintExitBanner(const char * sAssignID, const char * sComplStatus)
{
printf(\"\ Project ID: %s\ \"
\"Compeletion Status: %s\ \", sAssignID, sComplStatus);
}

Solution

#define _USE_MATH_DEFINES
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#ifndef M_PI
#define M_PI acos(-1.0)
#endif

// try adding this to the program and observe the output generated during compilation phase
// dueDate[0]=\'S\'; dueDate[1]=\'u\'; dueDate[2]=\'n\';
const char *assignmentID = \"Project 02\";
const char *classId = \"CSC-250 Computer Science II (a.k.a. C II) Spring 2017\";
const char *programmerName = \"Roy Fine\";
const char *dueDate = \"Monday AM, February 6, 2017\";
void print_usage(void) {
printf(\"usage:\ \"
\"Project 02 compute area/volume for common geometric shapes\ \"
\"area calcualtors:\ \"
\"square width\ \"
\"circle radius\ \"
\"annulus inner_radius outer_radius\ \"
\"volume calculators:\ \"
\"square_prism width height\ \"
\"cylinder radius height\ \"
\"torus minor_radius major_radius\ \"
\"example:\ \"
\"square_prism 4 12\ \"
\"cylinder 32 320\ \"
\" torus 17 45\ \");
}
_Bool HasSilentFlag(int argc, char *argv[]) {
for (size_t n = 1; n < (unsigned int)argc; n++){
if (0 == strcmp(\"-quiet\", argv[n])){
return true;
}
}
return false;
}
void PrintEntryBanner(const char *sAssignID, const char *sClassID, const char *sDate, const char *sDeveloper) {
printf(\"\ --------------------------------------------------------------------------\ \"
\"Project ID: %s\ \"
\"Class ID: %s\ \"
\"Program Due Date: %s\ \"
\"Programmer ID: %s\ \ \", sAssignID, sClassID, sDate, sDeveloper);
}

// ===============================================================
// main - this is where all of the action occurs...
// ===============================================================

int main(int argc, char *argv[]) {   
if (argc < 2) {
print_usage(); // if no args, then show usage and quit early
return 0;
}
if (!HasSilentFlag(argc, argv)) {
PrintEntryBanner(assignmentID, classId, dueDate, programmerName);
}
char *completionStatus = \"Success\"; // set this to Success here. on error we will reset it to appropriate messaging   
// C is missing the case insensitive compare - so we coerce input arg to lower case, and compare on that
for (char *p = argv[1]; *p; p++)
{
if (isalpha(*p) && isupper(*p))
{
*p = (char)tolower(*p);
}
}
// step throug hthe possible candidates - test for each
// if we find a request that we handle, we do so, else we move to the end
// if we get to the end and find we have not handled the request (e.g. the final else)
// we treat that as as error and report
// for each case that we handle, do a modest amount of input validation
// make sure width, widsth, radius are non-negative
// where appropriate, validate relationships
if (0 == strcmp(argv[1], \"square\") && (argc >= 3)) {
double base = atof(argv[2]);
if (base >= 0.0) {
double area = base * base;
printf(\"%18s: base: %.2lf area: %.2f\ \", \"square\", base, area);
} else {
printf(\"%18s: base: %.2lf ***> error <*** Bad Data\ \", \"square\", base);
bHasError = true;
completionStatus = \"***> error <*** square(bad data)\";
}
} else if (0 == strcmp(argv[1], \"circle\") && (argc >= 3)) {
double radius = atof(argv[2]);
if (radius >= 0.0) {
double area = radius * radius * M_PI;
printf(\"%18s: radius: %.2lf area: %.2f\ \", \"circle\", radius, area);
} else {
printf(\"%18s: radius: %.2lf ***> error <*** Bad Data\ \", \"circle\", radius);
bHasError = true;
completionStatus = \"***> error <*** circle(bad data)\";
}
} else if (0 == strcmp(argv[1], \"annulus\") && (argc >= 4)) {
double inner_radius = atof(argv[2]);
double outer_radius = atof(argv[3]);
if ((inner_radius >= 0.0) && (outer_radius >= 0.0) && (inner_radius <= outer_radius)) {
double area = (outer_radius*outer_radius - inner_radius*inner_radius) * M_PI;
printf(\"%18s: inner radius: %.2lf outer radius: %.2lf area: %.2f\ \", \"annulus\", inner_radius, outer_radius, area);
} else {
printf(\"%18s: inner_radius: %.2lf outer_radius: %.2lf ***> error <*** Bad Data\ \", \"annulus\", inner_radius, outer_radius);
bHasError = true;
completionStatus = \"***> error <*** annulus(bad data)\";
}
} else if (0 == strcmp(argv[1], \"square_prism\") && (argc >= 4)) {
double width = atof(argv[2]);
double height = atof(argv[3]);
if ((width >= 0.0) && (height >= 0.0)) {
double volume = (width*width) * height;
printf(\"%18s: width: %.2lf height: %.2lf volume: %.2f\ \", \"square_prism\", width, height, volume);
} else {
printf(\"%18s: width: %.2lf height: %.2lf ***> error <*** Bad Data\ \", \"square_prism\", width, height);
bHasError = true;
completionStatus = \"***> error <*** square_prism(bad data)\";
}
} else if (0 == strcmp(argv[1], \"cylinder\") && (argc >= 4)) {
double radius = atof(argv[2]);
double height = atof(argv[3]);
if ((radius >= 0.0) && (height >= 0.0)) {
double volume = height * radius * radius * M_PI;
printf(\"%18s: radius: %.2lf height: %.2lf volume: %.2f\ \", \"cylinder\", radius, height, volume);
} else {
printf(\" cylinder: radius: %.2lf height: %.2lf ***> error <*** Bad Data\ \", radius, height);
bHasError = true;
completionStatus = \"***> error <*** cylinder(bad data)\";
}
} else if (0 == strcmp(argv[1], \"torus\") && (argc >= 4)) {
double minor_radius = atof(argv[2]);
double major_radius = atof(argv[3]);
if (minor_radius <= major_radius) {
if ((minor_radius >= 0.0) && (major_radius >= 0.0)) {
double volume = (minor_radius*minor_radius * major_radius) * M_PI * M_PI * 2;
printf(\"%18s: minor radius: %.2lf major radius: %.2lf volume: %.2f\ \", \"torus\", minor_radius, major_radius, volume);
} else {
printf(\"%18s: minor radius: %.2lf major radius: %.2lf ***> error <*** Bad Data(negative data)\ \", \"torus\", minor_radius, major_radius);
bHasError = true;
completionStatus = \"***> error <*** torus(bad data)\";
}
} else {
printf(\" torus: minor radius: %.2lf major radius: %.2lf ***> error <*** Bad Data(invalid radii)\ \", minor_radius, major_radius);
bHasError = true;
completionStatus = \"***> error <*** torus(bad data)\";
}
} else {
printf(\" unknown or unhandled shape: %s\ \", argv[1]);
bHasError = true;
completionStatus = \"***> error <*** unknown shape\";
}
if (!HasSilentFlag(argc, argv) || bHasError){
PrintExitBanner(assignmentID, completionStatus);
return 0;
}
}
void PrintExitBanner(const char * sAssignID, const char * sComplStatus)
{
printf(\"\ Project ID: %s\ \"
\"Compeletion Status: %s\ \", sAssignID, sComplStatus);
}

----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF
----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF
----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF
----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF
----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF
----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF
----------------C programming----------- In summary - just clean up the mess. Specifically, use functions where functions are appropriate. #define _USE_MATH_DEF

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site