This is a function of a calculator using GOTO and i wanted to know is it correct or not
ALSO how can i return 'NAN' if ( char op doesn't equal to - * / operators ) ??
float calcu(float num1, float num2, char op){
float R;
if (op ==' ') goto add;
if (op =='-') goto sou;
if (op =='*') goto mult;
if (op =='/') goto div;
add:
R=num1 num2;
goto end;
sou:
R=num1 -num2;
goto end;
mult:
R=num1 * num2;
goto end;
div:
R=num1 /num2;
goto end;
end:
return R;
}
CodePudding user response:
goto is generally not recommended as it leads to spaghetti code. Structured code is the de-facto standard in modern code. That being said in this thought experiment yes this works, except when an incorrect character is given it will do the add operation instead of the NaN. So if all the if statements fail it should direct to a NaN label that does what you want.
CodePudding user response:
Your code is semantically equivalent to this:
float calcu(float num1, float num2, char op){
float R;
if (op == ' ')
R = num1 num2;
else if (op == '-')
R = num1 - num2;
else if (op == '*')
R = num1 * num2;
else if (op == '/')
R = num1 / num2;
else
R = num1 num2; //All other characters execute code in add: from original code
return R;
}
As to whether it is correct, that depends on whether or not you expect R
to hold the sum of num1
and num2
in the case that op
is not ' '
, '-'
, '*'
, nor '/'
.
If you're doing an experiment just to see how goto
works in the C programming language, then don't let me scold you for your use of the keyword, but the use of this keyword in particular is controversial to say the least.
The overuse of goto
is associated with what is often called "spaghetti code", which is code with no well-defined structure to the control flow. "Spaghetti code" is harder to read, harder to maintain, and in some cases harder for the compiler to optimize. Just for example, I'd bet you missed that any op
that doesn't satisfy one of your if
checks will simply fall into add:
. If this is something other than an experiment to see how the keyword works, I highly recommend you use a different control flow method like if/else
or switch
.
CodePudding user response:
Based on your comment, this is for an assignment. I find that concerning, since it's highly unusual to use goto
in application code (it may be more common in the embedded world, but I can't speak to that).
If this is meant to be a lesson in how not to write C code, I can kind of see the justification, but it's still a waste of time.
Having said all that, your code is mostly okay for what it is; most of my criticism would be based on formatting and readability. To handle a bad operator, all you really need to do is add it after the last valid operator:
if ( op == ' ' )
goto add;
if ( op == '-' )
goto sub;
if ( op == '*' )
goto mul;
if ( op == '/' )
goto div;
// handle bad operator here
fprintf ( stderr, "Unrecognized operator '%c'!\n", op );
return NAN;
The NAN
macro defined in math.h
, so you'll need to add the line
#include <math.h>
to the beginning of your program, and if you're building on *nix you may have to link the math library explicitly. However, depending on the compiler and the version of C you're using, NAN
may not be available.
Again, using goto
like this leads to code that's difficult to understand and maintain, which is why its use is strongly discouraged. I've used a goto
in production code exactly once over the last 32 years, and that was fairly early in my career.