Thread: problem with relational operators

  1. #1
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Question problem with relational operators

    Hi,

    I am working on a scientific program. For better usability I want to make it quite flexible when it comes to user appliable "filters", cut off values and so on. All user defined commands are passed via the command line.
    My problem are the filters. Lots of independent data sets will be processed and the results of each set will only be written to the output file if the (choosen) value (int or double) is "<, <=, ==, >= or > than x" (customizable via command line). So, do I really have to program a big "if ... else if ..." thingy or is there a more elegant, flexible way? Perhaps something similar to function pointers?

    Before I forget it: More than one filter can be applied to the results of a data set!

    Thank you for your help.

    Sargnagel

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you want things like "AND", "OR", "NOT" as well?

    Do you have operator precedence (like in C), and do you have parentheses to override the precedence?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    >>Do you want things like "AND", "OR", "NOT" as well?

    No, just "<, <=, ==, >=, >".

    >>Do you have operator precedence (like in C), and do you have parentheses to override the precedence?

    It's just a simple command line interface.
    Here is an example:
    Code:
    program.exe -srcfile input.txt -function1 >7 -function2 >=2.5
    Don't worry about tokenizing the strings ">7" and ">=2.5". I can handle that. Just assume it's done.

    [edit]
    Each data set is used as an input for every function. If the point of writing the results to output-files is reached, the filters are applied. (although I could apply the filters directly after a function to check if the results meet the requirements (e.g. >7) and if not I could leave out the other calculations and start with the next data set)
    Only a few functions need the filter-information for some calculations.
    [/edit]
    Last edited by Sargnagel; 10-12-2002 at 05:39 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Here's something
    Code:
    #include<stdio.h>
    
    #define MAX_TERMS   20
    
    typedef int (*fnptr)(float,float);
    
    // comparison functions
    int cmp_lt ( float a, float b ) {
        return a < b;
    }
    int cmp_le ( float a, float b ) {
        return a <= b;
    }
    int cmp_eq ( float a, float b ) {
        return a == b;
    }
    int cmp_ge ( float a, float b ) {
        return a >= b;
    }
    int cmp_gt ( float a, float b ) {
        return a > b;
    }
    
    // contains the functions and data to compare with
    struct {
        fnptr   func;
        float   data;
    } terms[MAX_TERMS];
    int maxterms;
    
    void test ( void ) {
        float   a;
        for ( a = 0.0 ; a <= 10.0 ; a += 0.2 ) {
            int i, ok = 1;
            for ( i = 0 ; i < maxterms && ok ; i++ ) {
                ok = ok && terms[i].func( a, terms[i].data );
            }
            if ( ok ) {
                printf( "%f passes filter\n", a );
            }
        }
    }
    
    int main ( ) {
        // fill the array with
        // >= 2.5 AND < 7.0
        terms[0].func = cmp_ge;
        terms[0].data = 2.5;
        terms[1].func = cmp_lt;
        terms[1].data = 7.0;
        maxterms = 2;
        test();
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Many, many thanks for your help!

    Your solution is awesome! I hope the days will come when I am able to work out such a solution myself ... still a long way to go from newbie to intermediate ... and I thought I already took this step. (well, at least partly)

    Now, back to work! *fires up text editor and starts torturing the keyboard*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. problem with relational operators
    By manoncampus in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2005, 07:15 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM