1 / 21

Today’s Agenda

Today’s Agenda. Putting things together …. Example Problem. Problem Definition: Design and implement a simple calculator. Requirements: The program should read expressions – one at a time – from the calculator. Evaluate the expression and print the result The expressions will be

milek
Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Putting things together … Sundar B. BITS, Pilani.

  2. Example Problem • Problem Definition: • Design and implement a simple calculator. • Requirements: • The program should • read expressions – one at a time – from the calculator. • Evaluate the expression and print the result • The expressions will be • Arithmetic operations (+,-,*,/) on integers and real numbers. Sundar B. BITS, Pilani.

  3. Calculate one expression Calculate one expression Calculate one expression Calculate expressions Calculate expressions Calculate expressions Design Calculate expressions No more expressions! Sundar B. BITS, Pilani.

  4. Algorithm repeat calculate one expression until (no more expressions) How do you calculate a single expression? Sundar B. BITS, Pilani.

  5. Read (and store) an expression Evaluate an expression Print the value Design Calculate one expression Sundar B. BITS, Pilani.

  6. Algorithm – Refinement 1 repeat read an expression and store it as E evalaute E – let the value be V print V until (no more expressions) Sundar B. BITS, Pilani.

  7. Design Questions • How do you read a single expression? • How do you read a number? • Are negative numbers allowed? • In what form do you store an expression? • How do you decide there are no more expressions? • How do you evaluate a (stored) expression? • What is the order of evaluation? • Can we mix integers and real numbers? Sundar B. BITS, Pilani.

  8. Design • Reading a number: • Read an integer value • If this is followed by a ‘.’ character, • read another integer value following that (and convert the two values into a float value). • Assumption: • Negative numbers not allowed! • Need to differentiate unary – (as in -5) from binary – (as in x - y). Sundar B. BITS, Pilani.

  9. Design • Representation of an expression: • An expression is either • A number (int OR float) OR • A sequence of numbers separated by operators (+, -, *, /) • Then an expression can be represented as a list of items: • Each item is a number or an operator • Each number is a integer or float value • Each operator is one of +, -, *, / Sundar B. BITS, Pilani.

  10. Design • The order of evaluation is left to right: • Easy to perform • If the list is just one number then • the value is the number • else • replace the sequence number op number with its value. • Example: • 5 is evaluated in one step to 5. • 3+5*7/2 is reduced in one step to 8*7/2 Sundar B. BITS, Pilani.

  11. Design • We can mix int values and float values under the assumption: • Mixed expressions are evaluated as float expressions • An int value in a mixed expression will be converted into an appropriate float value. Sundar B. BITS, Pilani.

  12. Algorithm (for reading) Declare an array Exp scan (i.e. read) a line of characters as a string repeat convert a prefix to a number N store N in the next location of Exp if (more characters) convert the next char to an operator O store O in the next location of Exp until (no more characters) Report error if last item stored is not a number. Sundar B. BITS, Pilani.

  13. Algorithm (for reading a number) convert a prefix to a integer I; inspect the next char c; If c is ‘.’ convert a prefix following it to a integer F; convert integers I and F into a float number, say N; Else let number N be I; Return N; Sundar B. BITS, Pilani.

  14. Algorithm (for evaluating) // Precondition: // LenExp > 0; LenExp is length of Exp val =Exp[0]; for (cur = 1; cur+1 < LenExp; cur+=2;) { val = evalOp(Exp[cur], val, Exp[cur+1]); } return val;

  15. Complexity of Algorithms • Algorithm for reading • Has to read characters one by one • Time Complexity is • O(N) where N is number of characters. • Algorithm for evaluating • Has to perform operations one by one • Time Complexity is • O(M) where M is number of operators in the expression. • Overall space complexity • O(M+N) which is O(N) • M operators and M+1 numbers in array Exp – O(M) • N characters in the input string Sundar B. BITS, Pilani.

  16. Type Definitions /* calcTypes.h */ typedef enum { PLUS, MINUS, MULT, DIV } OP; typedef enum { INT_T =0, FLOAT_T=1 } NUMTYPE; typedef struct { NUMTYPE iorf; union { int ival; float fval; } val; } NUM; typedef union { NUM n; OP o; } ExpItem; typedef ExpItem Exp[MAX_SIZE];

  17. Implementation - REPL void repl() /* Read-Eval-Print Loop */ { do { } while (TRUE); } et = readExp(nextExpr); if (validExpr(et)) printVal(eval(nextExpr)); if (inValid(et) printErr(nextExpr); if (terminator(et)) killCalc();

  18. Implementation - REPL int bufStart =0; /* Scan the string buf starting from the position bufStart and convert a prefix to an integer */ int scanInt(char buf[]){ return intVal; } int intVal = 0; char curChar = buf[bufStart]; while (curChar != '\0') { if (('0' > curChar) || ('9' < curChar)) break; intVal = intVal * 10 + curChar - '0'; curChar = buf[++bufStart]; }

  19. Implementation - REPL ExpKind readExpr(Exp e) { // … while (TRUE) { if (buf[bufStart] == '\0') { return INVALID_EXPR; } /* read an integer */ intVal = scanInt(buf); if (buf[bufStart] == '.') { bufStart++; /* read another integer*/ fracVal = scanInt(bufStart); curNum.val.fval = makeFloat(intVal, fracVal); curNum.iorf = FLOAT_T; } else { curNum.val.ival = intVal; curNum.iorf = INT_T; }

  20. Implementation - REPL e[expLen++].n = curNum; if (buf[bufStart] == '\0') { return VALID_EXPR; } else { switch (buf[bufStart]) { case '+' : e[expLen++] = PLUS; break; case '-' : e[expLen++] = MINUS; break; case '*' : e[expLen++] = MULT; break; case '/' : e[expLen++] = DIV; break; default : return INVALID_EXPR; } bufStart++; }

  21. e[expLen++].n = curNum; if (buf[bufStart] == '\0') { return VALID_EXPR; } else { switch (buf[bufStart]) { case '+' : e[expLen++] = PLUS; break; case '-' : e[expLen++] = MINUS; break; case '*' : e[expLen++] = MULT; break; case '/' : e[expLen++] = DIV; break; default : return INVALID_EXPR; } bufStart++; } } } Sundar B. BITS, Pilani.

More Related