Directions: Remember, if I cannot READ your answer, then it is wrong; so write neatly and large enough for me to read. Good luck. Have a safe and happy summer.
<e> ::= + <e> <e> | * <e> <e> | <no> <no> ::= 2 | 3 | 4 | 5 a. + * 2 3 + 4 5 b. * 2 + + 3 4 5
public class Fraction { // a Fraction is stored as a pair of integers: // (num, denom) in lowest common denominator form; // ie, 2/4 is stored as (1, 2). int num; // numerator int denom; // denominator public Fraction (int n, int d) throws ArithmeticException { num = n; denom = d; normalize( ); } // Fraction public Fraction (Fraction frac) { num = frac.numerator( ); denom = frac.denominator( ); } // Fraction public int numerator( ) { return num; } public int denominator( ) { return denom; } public Fraction negated( ) { return new Fraction(-num, denom); } public Fraction add(Fraction frac) { return new Fraction (num * frac.denominator() + denom * frac.numerator( ), denom*frac.denominator()); } // add public Fraction subtract(Fraction frac) { return add(frac.negated( )); } public boolean equals(Fraction frac) { return compareTo(frac) == 0; } public int compareTo(Fraction frac) { // Answer -1 if receiver is less than frac, // 0 if equal to // 1 if greater Fraction tmp = subtract(frac); if (tmp.numerator() < 0) return -1; else if (tmp.numerator() > 0) return 1; return 0; } // compareTo } // Fraction
;;; A State is implemented as a list of ordered pairs, ;;; of the form (variable value). E.g. the initial state ;;; for int x; int y would be: ;;; "((x 0) (y 0)) ;;; we assume that EVERY variable occurs in the state ;;; Instructions are: ;;; (skip) ;;; (assign target source) ;;; (compound s0 s1 ... sn) ;;; (loop test body) ;;; (conditional test thenbr elsebr) (defun m-compound (lst state) (if (null lst) state (m-compound (rest lst) (m-instruction (first lst) state)) ) ) (defun m-instruction (instr state) (case (first instr) (skip state) (assign (put (second instr) (m-expression (third instr) state) state) (compound (m-compound (rest instr) state)) (loop (if (m-expression (second instr) state) (m-instruction (third instr) state) state)) (conditional (if (m-expression (second instr) state) (m-instruction (third instr) state) (m-instruction (fourth instr) state)) (otherwise nil) ) )
int i = 1; int a[4] = {0, 3, 5, 7}; void P (int x, int y) { x = x + 1; y = y + 1; if (i == x) x = x + 1; cout << x << y; // <===== } void main ( ) { P (i, a[i]); cout << i << a[0] << a[1] << a[2] << a[3]; // <===== }
Call by | x | y | i | a[0] | a[1] | a[2] | a[3] |
---|---|---|---|---|---|---|---|
Value | |||||||
Value-Result | |||||||
Reference | |||||||
Name |