0

Here is the function (I hope the logic is fairly obvious).

Let x be one of the '<' or '>' operators and a and b are the terms.

int rationalCheck(x, a, b){

    if ( x == '<' && a < b && b < a ){
        printf( "a is less than b\n" );
    }

    if ( x != '>' && a > b && b > a ){
        printf( " a is greater than b\n" );
    }

    return 0;
}

The input into the function would be

(4 < 4) > (3 > 3)

This would evaluate to

(4 < 4) > (3 > 3) is false

Or input into the function would be

(4 < 6) > (2 > 1)

This would evaluate to

(4 < 6) > (2 > 1) is true
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
RandomNumberFun
  • 638
  • 2
  • 8
  • 25
  • 1
    and what do you find to be your problem? – atk Mar 17 '13 at 02:04
  • Notice that in *C* or *C++* you have `false < true` – Basile Starynkevitch Mar 17 '13 at 02:05
  • 1
    `a < b && b < a` is false, also `a > b && b > a` is false – Musa Mar 17 '13 at 02:08
  • Your function only compares two numbers (numbers? Where are the parameter types?) - do you want a full-blown expression parser? – Koterpillar Mar 17 '13 at 02:09
  • Note that in C, the expression `(4 < 6) > (2 > 1)` evaluates to 0 (false). `4 < 6` is true, so it evaluates to 1; `2 > 1` is true and also evaluates to 1; therefore, the middle operator is evaluating `1 > 1` which is false and evaluates to 0. – Jonathan Leffler Mar 17 '13 at 02:52
  • Sorry, the logic is not obvious at all. Bothe of the conditions are always false, so the function always returns 0. `a < b && b < a` and `a > b && b > a` evaluate to 0 or false whatever the variables a and b hold. It doesn't matter what else is in the condition. – UncleO Mar 17 '13 at 05:25

2 Answers2

1

You can't pass operators/operations to functions in C. I suggest considering Haskell.

Alternatively, you can pass operations to macros, so this can be implemented as a macro, hence the definition of the assert macro being something like:

#include <stdio.h>

#define assert(cond) if (!(cond) && printf("Assertion failed: " # cond " at " __FILE__ ":%d\n", __LINE__) != 0) abort()

int main(void) {
    assert(1 > 1);
}

Perhaps you want something like:

#include <stdio.h>

#define rational_check(cond) printf(# cond " is %s\n", (cond) == 0 ? "false" : "true")

int main(void) {
    rational_check((4 > 4) > (3 > 3));
    rational_check((4 < 6) > (2 > 1)); // (4 < 6) > (2 > 1) is 1 > 1, by the way... false
}

I can't be certain, however, whether this suits your needs. A function pointer can't be derived from rational_check, and it can't work with strings that represent expressions formed during runtime; You'd need to write a translator for any use cases that require those... Otherwise, this should be suitable.

autistic
  • 1
  • 3
  • 35
  • 80
-1

This works for me. I was over thinking it.

int rationalCheck(x, a, b){


if ( x == '<')
{
    if (a >= b)
    {
        return 99;
    }

    if (b <= a) {
        return 99;
    }
}

if (x == '>')
{
    if (a <= b)
    {
        return 99;

    }
    if (b >= a)
    {
        return 99;
    }

}


return 1;
}

Thanks for everyone's input.

RandomNumberFun
  • 638
  • 2
  • 8
  • 25
  • Please learn to write in C89 at least. A function written like that with no types for the parameters at all is appallingly antiquated. Such code would not be acceptable in industry. It was barely tolerable before the original C standard was published; it certainly isn't acceptable in modern C. – Jonathan Leffler Mar 17 '13 at 02:47
  • Under what circumstances do you think your second `return 99;` will be executed? There are none I can think of! Similarly with the fourth. – Jonathan Leffler Mar 17 '13 at 02:49
  • @JonathanLeffler willing to learn here. Could you offer feedback with a code example? – RandomNumberFun Mar 17 '13 at 03:08
  • @JonathanLeffler also thanks for pointing out the second return will never run! \ – RandomNumberFun Mar 17 '13 at 03:09
  • The function should be defined with types for the arguments (in prototype notation) such as: `int rationalCheck(char x, int a, int b) { ... }`. The implicit `int` notation you were using is not part of standard C99 (though compilers still recognize it, usually with generating a warning). You aren't using Turbo C are you, by any mischance? If you're using GCC, using the `-Wall` option is a good starting point. – Jonathan Leffler Mar 17 '13 at 03:50