Thread: Function to check operators...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Function to check operators...

    Hi,

    I want to write a function which takes two operators as characters and check if one has a higher, lower or equal precendece than the other. Operators are only going to be + * / -. I thought of one way: assign numbers to the operatos, number one to plus and numbers 2 to * and /. Then check if one is greater than the other. Can anyone think of some other way?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    If that is all the program does, then ur approach is fine.

    You cud simply put all the operator chars in a char array...

    char a[]={'*','/','+','-',......}

    Have some schema as above that 0,1 elements have same precedence, 2,3 have same, etc.etc.

    Now find out the difference in indices of the inputted chars(ops), and display result based on that.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Another appraoch..

    char a[]={'*','/',' ' (BLANK), '+','-'....}

    Separate all "levels" of precedence by a BLANK. Now just find out whether u traversed through a ' ' between the positions of the two ops in the array. If u did, then display "op 1 is higher", otherwise display "both have same precdence".

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Yep thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM