# Thread: I have an assignament due tomorrow

1. ## I have an assignament due tomorrow

the assignament is basically for begginers, we have to build a logical statement calculator:
the statement is from the form (variable2 operation variable1)

the operations in the statements are : '>', '<', '='
and between two or more statement the logical operations could be :
'|' (or) , '&' (and) '~' (not)
if the input was ((a>b)&(1>2)|(5=3)) the function should return 0.

and therse no restriction on how many statements there could be
PS: we are not allowed to use strings or arrays.

examples for outputs:
~(a>b)The statement is true.
(a>b)&(2<3)The statement is false.
(2>3)|~(a>b)&(C>d)The statement is false.
(200>100)The statement is true.
(b=B)The statement is true. 2. > (a>b)&(2<3)The statement is false.
Do a and b have values, in the usual sense of a variable name has a value.
In which case, the truth value depends on those values.

> I have an assignament due tomorrow
That's unfortunate. 3. Originally Posted by Salem > I have an assignament due tomorrow
That's unfortunate.
Indeed  4. Originally Posted by Salem > (a>b)&(2<3)The statement is false.
Do a and b have values, in the usual sense of a variable name has a value.
In which case, the truth value depends on those values.

so the input has to be a char, so a and b are technically the a and b in the ascii table
I think I a have an idea but I want some help from someone with more experience

> I have an assignment due tomorrow
That's unfortunate.
the lecturer delayed it  5. Originally Posted by pythonary the lecturer delayed it How far did you get? I'll be working on something similar soon. 6. > I think I a have an idea but I want some help from someone with more experience 7. So i thought of creating two different function, the first one is gonna be statement scanning
and the second one is gonna be calculating the statements .

so this is my statement receiving function: I hope you can provide me with any shorter substitute or any improvement :]

Code:
```int Statement()
{
char in, op;                  // in as in input and op as in operation
int result, not = 0, val1 = 0, val2 = 0;
scanf("%c", &in);
if (in == '~') {
not = 1;
scanf("%c", &in);
if (in != '(') {
printf("You have a syntax error in your statement\n");
return 0;
}
}
//first value
scanf("%c", &in);
if ((in <= 'f' && in >= 'a') || (in <= 'F' && in >= 'A')) {
val1 = (int) in;
scanf("%c", &in);
if (in == '<' || in == '>' || in == '=') {
op = in;
} else {
printf("You have a syntax error in your statement\n");
return 0;
}

}
if (in <= '9' && in >= '0') {
do {
val1 *= 10;
val1 += (int) in;
scanf("%c", &in);
} while (in <= '9' && in >= '0');
op = in;
} else {
printf("You have a syntax error in your statement\n");
return 0;
}

// second value
scanf("%c", &in);
if ((in <= 'f' && in >= 'a') || (in <= 'F' && in >= 'A')) {
val2 = (int) in;
scanf("%c", &in);
if (in == '<' || in == '>' || in == '=') {
op = in;
} else {
printf("You have a syntax error in your statement\n");
return 0;
}

}
if (in <= '9' && in >= '0') {
do {
val2 *= 10;
val2 += (int) in;
scanf("%c", &in);
} while (in <= '9' && in >= '0');
op = in;
} else {
printf("You have a syntax error in your statement\n");
return 0;
}
if (op == '=') {
result = val1 == val2;
}
if (op == '>') {
result = val1 > val2;
}
if (op == '<') {
result = val1 < val2;
}
if (not == 1) {
return !result;
} else
return result;
}``` 8. due tomorrow
Cheap trick to force someone else to do your stuff..

Code:
`in <= '9' && in >= '0'`
Remove this.
Keep it simple.

Code:
`printf("You have a syntax error in your statement\n");`
Remove this.
For now, don't worry about error checking.

There is a risk of int32 overflow.
I don't expect you to handle int32 overflow. 9. Code:
```/* main.m, Simple Calculator, Created by Great on 10/30/22. */

#import <Foundation/Foundation.h>

int enter_value(int);
int enter_operator(void);
int calculate(int, int, char);

int enter_value(int tag) {

int value;

printf("\nEnter Value %d: ", tag);
scanf("%d", value);

return value;
}

int enter_operator(void) {

char operator;

printf("\nEnter Operator: ");
scanf("%s", operator);

return operator;
}

int calculate(int value1, int value2, char operator) {

if ('=' == operator && value1 == value2) {
printf("\nResult: True.\n\n");

} else if ('>' == operator && value1 > value2) {
printf("\nResult: True.\n\n");

} else if ('<' == operator && value1 < value2) {
printf("\nResult: True.\n\n");

} else {
printf("\nResult: False.\n\n");

}

return 0;
}

int main(int argc, const char *argv[]) {

int value1 = 0;
int value2 = 0;
char operator = 0;

value1 = enter_value(1);
value2 = enter_value(2);
operator = enter_operator();
calculate(value1, value2, operator);

return 0;
}``` 10. Thank you for your effort, but Im not allowed to use arrays or strings. And only use the stdio and math libraries. 11. This is the correct way to do it. 12. Im really thankful. And I know that this the correct way, but Im simply not allowed to use those stuff. If I use them imma get 0/100 13. TBH, you need to define the grammar before you write the code.

I mean it might work for your examples at the moment, but something like
((((((a>b))))))
is going to blow it up.

compiler construction - How to define a grammar for a programming language - Stack Overflow
Introduction to Programming Languages/Grammars - Wikibooks, open books for an open world

Like
log_op ::= "|" | "&"
paren_expr ::= "(" rel_expr ")"
rel_expr ::= symbol relop symbol
relop ::= "<" | "=" | ">"
symbol ::= "0" | "1" ... "Y" | "Z"

Which you turn into functions
Code:
```int symbol() {
int ch = getchar();
if ( ch >= '0' && ch <= '9' ) return ch;
if ( ch >= 'a' && ch <= 'z' ) return ch;
}

int rel_expr( ) {
int s1 = symbol();
int op = relop();
int s2 = symbol();
if ( op == '<' ) return s1 < s2;
// etc
}``` 14. how do I build the paren_expr fuction? 15. Originally Posted by pythonary how do I build the paren_expr fuction?
You first call getchar() and expect that it's a '('
Then you call rel_expr to handle the relational expression.
Then you call getchar() and expect that it's a ')' Popular pages Recent additions assignament, form, no array, no strings, statement 