Thread: I have an assignament due tomorrow

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    7

    Post 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,462
    > (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.
    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
    Join Date
    Feb 2019
    Posts
    1,048
    Quote Originally Posted by Salem View Post
    > I have an assignament due tomorrow
    That's unfortunate.
    Indeed

  4. #4
    Registered User
    Join Date
    Nov 2022
    Posts
    7
    Quote Originally Posted by Salem View Post
    > (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. #5
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Quote Originally Posted by pythonary View Post
    the lecturer delayed it
    How far did you get? I'll be working on something similar soon.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,462
    > I think I a have an idea but I want some help from someone with more experience
    We await your attempt.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2022
    Posts
    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;
    }
    Last edited by Salem; 11-30-2022 at 06:43 AM. Reason: removed crayola

  8. #8
    Banned
    Join Date
    Jul 2022
    Posts
    112
    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. #9
    Banned
    Join Date
    Jul 2022
    Posts
    112
    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[2];
        
        printf("\nEnter Value %d: ", tag);
        scanf("%d", value);
        
        return value[0];
    }
    
    
    int enter_operator(void) {
        
        char operator[2];
        
        printf("\nEnter Operator: ");
        scanf("%s", operator);
        
        return operator[0];
    }
    
    
    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. #10
    Registered User
    Join Date
    Nov 2022
    Posts
    7
    Thank you for your effort, but Iím not allowed to use arrays or strings. And only use the stdio and math libraries.

  11. #11
    Banned
    Join Date
    Jul 2022
    Posts
    112
    This is the correct way to do it.

  12. #12
    Registered User
    Join Date
    Nov 2022
    Posts
    7
    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. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,462
    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
    }
    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.

  14. #14
    Registered User
    Join Date
    Nov 2022
    Posts
    7
    how do I build the paren_expr fuction?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,462
    Quote Originally Posted by pythonary View Post
    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 ')'
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello all,need help with test tomorrow
    By Freikorp in forum C Programming
    Replies: 10
    Last Post: 05-27-2011, 02:14 PM
  2. tomorrow is last day to submit
    By asad_fit786 in forum C++ Programming
    Replies: 14
    Last Post: 10-22-2009, 09:37 AM
  3. help me until tomorrow midnigt??
    By theblasphemous in forum C Programming
    Replies: 2
    Last Post: 04-19-2006, 06:23 PM
  4. Second MoA Demo Tomorrow
    By harryP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-30-2002, 07:22 PM

Tags for this Thread