Can i have some guidance in the following task, plz?

This is a discussion on Can i have some guidance in the following task, plz? within the C Programming forums, part of the General Programming Boards category; '*' means && (AND) '+' means || (OR) 'T' and 'F' mean true and false (1 & 0). '*' (&&) ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Can i have some guidance in the following task, plz?

    '*' means && (AND)
    '+' means || (OR)
    'T' and 'F' mean true and false (1 & 0).
    '*' (&&) comes before '+' (||),
    between 2 values (F or T) will always be an operator ('+' or '*')
    example: T*F+F*T*T*F=(true&&false)|| (false&&true&&true&&false)

    The program will receive a number of strings, containing logic expressions.
    the character '=' separates between expressions and there could be spaces
    inside the input strings and between them.
    The number of strings in the input file is unknown. The program shall read until
    the end of the file.

    The program will:

    1. print each expression on a different row, reducing spaces (if they exist). In the end
    of each expression it will print '='.

    2. calculate the result of each expression and print it after the '=' character.
    example: for the stream "T*F+F*T*T*F=T+F+F+T*T*T+T=F+T+F*F+T=", the program
    should print:

    T*F+F*T*T*F=F
    T+F+F+T*T*T+T=T
    F+T+F*F+T=T

    There is no need to check if the input is ok (except cropping spaces). No need for interpreters, tokens, data types and recursion. I can only use loops/ifs and functions...
    cant come up with a decent way to solve and save the logic expression.
    Till now i've just substituted all the "F"s and "T"s with
    1's and 0's in a temp string, so now im able to use C's && and
    || operators on the input string. but what to do next?
    help plz....

    btw, i use borland turbo cpp 3.0 as an ide and a compiler.

  2. #2
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11
    What about statements with parenthesis such as T+(T*F)?

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    No no, without "()", cant use those.
    just help me with a way to solve the logical
    expression itself plz.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Take this as an example
    Code:
    T*F+F*T*T*F=F
    Which is basically two 'and' sub-expressions combined with an 'or'

    The simplest form of 'and' expression is a single value 'T' or 'F'
    So
    Code:
    T+F=T
    are two (really simple) 'and' expressions combined with an 'or'

    So you might have functions which work a bit like this
    Code:
    int do_and ( ) {
    }
    int do_or ( ) {
      return do_and() | do_and();
    }
    int do_expression ( ) {
      result = do_or()
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Here's what i've done

    using yiur guidance, salem, i've made this.
    but i cant see the logic behind (OR) operation, and
    the program treats (+) expressions just like it treats
    (*) ones, thus f+t=f, t+f=f, t+t=t, f+f=f. everything
    else works, but it ruins the result

    thanks for your help... can you try to fix it and
    explain me the mistake plz?

    #include <stdio.h>
    #include <conio.h>

    char make_char(int x)
    {
    if (x) return 't';
    else return 'f';
    }
    int make_int(char x)
    {
    if (x=='f') return 0;
    if (x=='t') return 1;
    }

    int do_and(int op1,int op2)
    {
    return op1&&op2 ;
    }

    int do_or(int op1, int op2)
    {
    return do_and(op1,op2) | do_and(op1,op2);
    }

    int do_exp(int op1,int op2)
    {
    int result;
    result=do_or(op1,op2);
    return result;
    }

    int main()
    {
    char stream[256];
    int i=0,result;

    clrscr();
    printf("Enter your logic expression:\n");
    gets(stream);
    printf("\n");
    result=make_int(stream[0]);
    while (stream[i])
    {
    while (stream[i]!='=')
    {
    if (stream[i]!=' ') printf("%c",stream[i]);
    if ((stream[i]=='*')||(stream[i]=='+'))
    result=do_exp(result,make_int(stream[i+1]));
    i++;
    }
    printf("%c %c\n",stream[i],make_char(result));
    result=make_int(stream[i++]);
    }

    return 0;
    }

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    if do_and(opt1,opt2) is just return opt1&&opt2;

    obviously you can do or the same way, ie "do_or(opt1,opt2) { return opt1 || opt2; }"

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    Quote Originally Posted by nonpuz
    if do_and(opt1,opt2) is just return opt1&&opt2;

    obviously you can do or the same way, ie "do_or(opt1,opt2) { return opt1 || opt2; }"
    but (AND) expressions should be executed before (OR) ones.
    if i do what you say, will loose the precedense of AND operand...

    not that it works now, anyway. just treats OR exactly like AND.
    think i know why, but dunno what exactly to change

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    oh i didnt see that stipulation, sorry.

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    this works...if you tweak it a bit to read from a file, and write a function to remove spaces

    the if condition is wear all the logic is checked....the outer loop just processes the whole string while the inner for loop processes each run of ANDs..
    also you'll need to add support for things like a string consiting of only "T" or "F" (by itself)

    Code:
    #include <stdio.h>
    
    int main()
    {
        char *p, test[]="F+T+F*F+T";
        unsigned int flag = 0, res=1, lres=0;
        
        p=test;
        while (*p) {
            for (;*p != '+' && *p;p++) { /* process and expressions */
                if (*p == '*' && (*(p-1) == 'F' || *(p+1) == 'F') || 
                                   (*p == 'F' && *(p-1) == '+' || *(p+1) == '+') ) {
                    if (flag)
                        res = lres || 0;
                    else 
                        res = 0;
                }
            } /* end and processing loop */
            if (*p == '+') {
                flag = 1;
                lres = res;
                res = 1;
                p++;
            }
        
            if (*p == '\0') {
                printf("%s=%c\n",test,(res == 1 ? 'T' : 'F'));
                break;
            }
        }
        
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    works like a charm, thanks nonpuz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. Where do a task get "wakeuped", blocked, preempted?
    By micke_b in forum Linux Programming
    Replies: 4
    Last Post: 12-28-2007, 03:49 PM
  3. A Task Buffer for storing socket descriptors
    By cloudy in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-09-2006, 01:08 PM
  4. a very small task but seems very hard....
    By alokdotnet in forum C Programming
    Replies: 4
    Last Post: 08-18-2006, 12:56 AM
  5. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21