EE150 - Fall 1996

Programming
Assignment #4
Last Updated: Monday October 21, 1996


------

This assignment is designed to give you experience with character processing, binary numbers, and C's various operators and statement types.

As with the previous assignment, this assignment will give you experience in writing your own functions.

The due date is: Monday, Nov. 4th, 1996 at 2.00pm (before class). There will be no extensions or exceptions. You should expect to spend between ten to fifteen hours of computer time over the next two weeks. You should also expect to spend three to four hours of thinking time. As usual, we will not accept late assignments.

What To Do

Part 1: Create a file named base1.c. This program will read in a binary number and print the result of converting this number to decimal number. Here is the sample input and output for our solution. The italics are our input, the monospace font is computer output.

1001
1001 is 9

You can assume that the user will be well-behaved, that the user will always enter the input in the form of a digit character , some more spaces, and a carriage return.

Part 2: Now that your program works on the basics, this part of the program base2.c is to extend your program to ensure that it reads in some binary numbers with some spaces seperating them and print their decimal values.

Here is some example input and output.

1001 01 10 10001
9, 1, 2, 17

0001 0101 100 000
1, 5 , 4 , 0

111 0010 100 1000
7 , 2 , 4 , 8

Part 3: For this part you will be given a main() function which calls two functions (called BinToDec(char c), getNondelimiter() )to help you to solve the above problem. What you should do is to write two functions' files (called BinToDec.c and getNondelimiter.c) that converts the binary number into decimal number and get the first digit character of every binary number.

In this part, any two of the binary numbers may be seperated not only by a space but also by valid delimiters (i.e. " " "," ";" ".")

Here is some sample I/O that you must be able to accomodate.

..., 1011,01;1000 101 ..100.00
11,1,8,5,4,0,

0001,1010. . 100 ; 10
1, 10 , 4 , 2

. 110, 01. 01
6 , 1 , 1

Here's the main function:
   #include
   
   main()
  {
   int BinToDec(char c);
   char getNondelimiter();
   char c;
   int n, END;

    printf("Please input the binary numbers(press return to exit): \n");

   /* get the first digit character then convert the binary number*/

     while( (END != 1) && ((c = getNondelimiter()) != '\n') )
   {

    n = BinToDec(c);

    if( n < 0 )
      {
       n = (-1) * n;
       END = 1;
       }
    printf(" %i  ", n);
    }          
  }

Your functions should look something like this:

  /*
    This function reads chars until a non-delimiter is encountered,
     and returns the non-delimiter found.
  */
    char getNondelimiter()
   {
    ...
    }

  /*
    This function reads a binary number starting with the second
     character/digit and passes the first character/digit through the 
     function parameter.
    The value returned is the decimal value of the whole binary number
     or else the negative value when this binary number followed by a '\n'
  */
    int BinToDec(char c)
   {
    ...
    }

You may assume that your program is given binary numbers and valid delimiters as input, so you don't have to worry about error checking.

Part 4: Copy the program from the previous part of the assignment into a file named caculate.c. Now you'll need to extend it to do addition and multiplication .

The following I/O (which you must be able to handle) demonstrates the necessary additions for this part of the assignment.

100 * 10 + 001 + 1010
4 * 2 + 1 + 10 = 19

1001 + 10 * 1111 * 01 + 01
9 + 2 * 15 * 1 + 1 = 40

you can assume the user is well-behaved and will enter some (i.e. at least one) space between operator and operands.

What To Turn In

To get credit for this assignment, you must electronically turn in the five files you created in Parts 1 through 4: base1.c, base2.c, BinToDec.c, getNondelimiter.c and caculate.c.

As usual, to turn in your program, you need to use the grade command on wiliki. For this assignment, you should turn in your program using the command:

grade -4s2,ee150 base1.c base2.c BinToDec.c getNondelimiter.c calculate.c
The -4s2 tells the grade command which assignment and section you are turning in (assignment #4), the ee150 tells it your class (EE150, so the programs get sent to the EE150 professor and not to the professor of some other class), and the ".c" files are the source files you are actually turning in. Please make sure you type in the command exactly as it shown above.