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

1. ## 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. What about statements with parenthesis such as T+(T*F)?

3. No no, without "()", cant use those.
just help me with a way to solve the logical
expression itself plz.

4. 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()
}```

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();
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. 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. 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. oh i didnt see that stipulation, sorry.

9. 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. works like a charm, thanks nonpuz