Engr. 4892 Assignment 1

Due: 0900 Thursday, May 15, 2003.

In this assignment you are to implement two unrelated functions as described below. Please submit each in a separate .cpp file as indicated. For both parts of this assignment you should use one or more instances of STL stack.

  1. Base Conversion
  2. Your are to implement the function mulitbaseOutput, as declared in assign1_a.h, to convert a positive integer to a string representation in the given base. The following are some test cases (note the result should not contain quote characters, they're used in this table to make it clear that these are strings):

    numbresult
    1210"12"
    122"1100"
    127"15"
    1216"C"

    Note that the string representation returned should be only the number. No whitespace or extra characters should be included.

    Submit an, appropriately commented, .cpp file called assign1_a.cpp containing only your implementation of multibaseOutput (i.e., not main).

    Hints:

    1. You may find it helpful to declare a local character array or string variable that contains the character representation of the digits from 0 - F (hex). For example
      static const char *digits = "0123456789ABCDEF";
      Thus digits[5] is '5', and digits[10] is 'A'.
    2. Note that, for integers n and b, the modulus operator, n % b will give the lowest-order digit in the base b representation of n and n/b will give the number to convert for the higher order digits. For example,
      12 % 2 = 0, 12 / 2 = 6,
      6 % 2 = 0, 6 / 2 = 3,
      3 % 2 = 1, 3 / 2 = 1,
      1 % 2 = 1, 1 / 2 = 0.
      So 12 in base 2 is "1100".

  3. Expression Evaluation
  4. Write a function evaluate as declared in assign1_b.h to compute the value of a "fully parenthesized numeric expression" using integer math.

    A "fully parenthesized numeric expression" consists of only the following:

    Such that the digits form positive integers (i.e., no unary minus), and every operator and its operands are contained within a matched pair of parentheses. For example, the following is a fully parenthesized numeric expression:
    (((6 + 9)/3)*(6 - 4))
    The value returned by evaluate for this expression should be 10.

    Submit an, appropriately commented, .cpp file called assign1_b.cpp containing only your implementation of evaluate (i.e., not main).

    Hint: Use two stacks: one to hold operands, and one to hold operators.

Be sure to include appropriate comments, including file and function header blocks (see Assignment Policies), and to use good style as outlined in Programming With Style. Submit your source code using Web Submit.


Back to 4892 homepage

Last modified: Thu 2003.05.08 at 21:57 NDT by Dennis Peters