-2

So my code will ask the user for a input of what operation he wants to perform (+ - * /) and then two numbers.

Based on that, a bunch of if statements will figure out what type of operation the user wants and them calculate the result.

I'm trying to reduce all of that to a single function, how can I make that?

This is what I've tried but it did not work:R is the result, N1 and N2 are the numbers, and the char OP is the user input on what type of math operation he wants.

This is my old code: I want to reduce all of those if functions to something like the image, a single line that could do all of them.

float N1, N2, R;
char OP;

printf("Type the type of operation you want(+ - * /):");
scanf("%c",&OP);
fflush(stdin);

printf("Type your first number:");
scanf("%f",&N1);
fflush(stdin);

printf("Type your second number:");
scanf("%f",&N2);
fflush(stdin);

if (OP=='+') //ADDITION
{
    R=N1+N2;
    
    if (R==(int)R)
    {
      printf("Your result is: %i \n" ,(int)R);  
    }
    else
    {
        printf("Your result is: %.2f \n",R); 
    }
}

else if (OP=='-') //SUBTRACTION
{
    R=N1-N2;
    
    if (R==(int)R)
    {
      printf("Your result is: %i \n",(int)R);  
    }
    else
    {
        printf("Your result is: %.2f \n",R); 
    }
}

else if (OP=='*') //MULTIPLICATION
{
    R=N1*N2;

    if (R==(int)R)
    {
      printf("Your result is: %i \n",(int)R);  
    }
    else
    {
        printf("Your result is: %.2f \n",R); 
    }
}

else if (OP=='/') //DIVISION
{
    R=N1/N2;

    if (R==(int)R)
    {
      printf("Your result is: %i \n",(int)R);  
    }
    else
    {
        printf("Your result is: %.2f \n",R); 
    }
}

else
{
    printf("Error. Please try again.");
}

system("pause");
return 0;
horsse
  • 107
  • 5
  • Showing just one line of code is inadequate for anyone to diagnose the problem. And you need to describe the problem in more detail than "did not work". Please provide a [mre] as well as the exact input, expected result and actual result. Also, please be sure to show the code as text and not as an image. – kaylum Sep 11 '22 at 04:39
  • Thank you for the feedback, I've included some more info on the question. – horsse Sep 11 '22 at 04:42
  • That is still not a [mre] and you still have not explained what the exact problem is beyond "does not work". In what specific way does it not work? Where is the attempt to put it into a function and what specific problem did you encounter when you tried? – kaylum Sep 11 '22 at 04:45
  • The user will type one of these: + - * / I would like to use the user input to calculate a math operation without using if statements. Instead of having a if for each case, I will one line of code, where the math sign between the two numbers is whatever the user input was – horsse Sep 11 '22 at 04:50
  • `fflush(stdin)` isn't portable. Don't compare floating points by equality, instead do `fabsf(R - (int) R) < delta` in your case `delta = 0.01` perhaps? – Allan Wind Sep 11 '22 at 05:09
  • The only difference between all your if branches is the operator and expression. Define a function for each of operators then map the OP to the function. The natural fit would be a switch(OP). You can a tertiary to make it a single line: R = (OP == '+' ? add(N1, N2) : OP == '-' ? subtract(N1, N2) : .... If you build a table of these functions float (*ops[])(float,float) = { add, subtract, ...} you use a function to map + to 0, - to 1 etc. Any of these, of course, can be hidden in a function call: R = op(OP, N1, N2). – Allan Wind Sep 11 '22 at 05:15
  • Here is pretty much a duplicate: https://stackoverflow.com/questions/252748/how-can-i-use-an-array-of-function-pointers – Allan Wind Sep 11 '22 at 05:16
  • @AllanWind suggesting `fabsf(R - (int) R) < delta` in this code is not a good choice for OP's code here. (potential UB with cast, using `double` constant, delta better with about 1/2 that value). IAC OP likely better with using `"%g"` for printing. – chux - Reinstate Monica Sep 11 '22 at 11:27
  • @chux-ReinstateMonica I am sure you are right... but why do you conclude there is a ub? – Allan Wind Sep 11 '22 at 21:27
  • @chux-ReinstateMonica `%g` is tricky here... as precision is number of significant number of digits `%.2g` of `1.01` will print `1`. – Allan Wind Sep 11 '22 at 21:56
  • @AllanWind UB occurs with `(int) R` when `R` is much outside `int` range. If OP wants 2 digits after the `.` for 1.01, use `"%.3g"`. It will output `"1"` for 1.00. – chux - Reinstate Monica Sep 11 '22 at 22:37
  • @chux-ReinstateMonica It sure does. Thx. – Allan Wind Sep 11 '22 at 22:38

1 Answers1

2

You can’t. There needs to be a branching structure somewhere.

A common (and good) way to do it is to build a lookup table for operatorfunction.

For really fast stuff ( O(1) ), you can build yourself a perfect hash map.

For a homework over four operators, just use an O(n) lookup.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39