The purpose of these questions is to have you use the UNIX system and environment.
Finally, wait at least 15 minutes, and retype the command
Reminder: Be sure you change your password by Monday of the second week, or the system staff will disable your account, and you will need to go to them to get it reinstated!
(100 points) The purpose of this question is to get your feet wet with respect to programming in C and the C compiler. You also will need to use an input function, a control loop, and printing functions (and some ingenuity).
Input Specifications
Read from the standard input. The input is supposed to be a C program. You need not check that it is, in fact, a legal C program; your program should simply assume it is.
If the first non-whitespace character on the line is a "#" and the line is not in a comment, the line is a preprocessor directive. The next maximal sequence of alphanumeric characters and underscores "_" is the command to the preprocessor. (For example, the line
#include <stdio.h>is a preprocessor directive, and the preprocessor command is "include".)
Output Specifications
Write to the standard output. In the first 6 columns of each output line, print a line number followed by a period ".". Print the input line beginning in column 9 (one tab from the left margin)..
If the line is a preprocessor directive, the next output line should contain "*"s in the first 8 columns, followed by the phrase "Preprocessor directive: " and the directive only on the same line.
Example Input
/* next line is a preprocessor directive */ #include <stdio.h> /* next line is not: #include <something.h> because the comment is 3 lines long */ int main(void) { #ifdef mips printf("2/3 = %d, 2.0/3.0 = %18.14f, 2.0F/3.0F = %18.14f\n", 2/3, 2.0/3.0, 2.0F/3.0F); #endif }Example Output
1. /* next line is a preprocessor directive */ 2. #include <stdio.h> ********Preprocessor directive: include 3. /* next line is not: 4. #include <something.h> 5. because the comment is 3 lines long */ 6. 7. int main(void) 8. { 9. #ifdef mips ********Preprocessor directive: ifdef 10. printf("2/3 = %d, 2.0/3.0 = %18.14f, 2.0F/3.0F = %18.14f\n", 11. 2/3, 2.0/3.0, 2.0F/3.0F); 12. #endif ********Preprocessor directive: endif 13. }
Compile and execute the program in the example input. Explain why the results of the divisions are all different, especially the last two. It is not sufficient to note the middle one is double precision, and the last one floating point; you need to explain how that causes a difference.
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562