P R E L I M I N A R Y S P E C I F I C A T I O N REVISED 9/4/2016 Due 2:00 AM, Friday, 9 September 2016 CS-223 Homework #1 Any Two Five Elevennis? A Gentle Introduction to C REMINDER: Do not under any circumstances copy another student's code or give a copy of your code to another student. After discussing the assignment with another student, you may not take any written or electronic record away. Moreover, you must engage in a full hour of mind-numbing activity before you work on it again. Such discussions must be noted in your log file. The pianist and comedian, Victor Borge, proposed an inflationary language, in which numbers and their homophones in words would be incremented by one. See https://www.youtube.com/watch?v=bcGA4alhPas For example, "Anyone for tennis?" becomes "Anytwo five elevennis?" (40) Write a filter "Inflate" that copies a text from the standard input to the standard output, replacing each integer with its value incremented by 1. You do not need to handle fractions or scientific notation. It should process integers in decimal, binary, octal, and hexadecimal. For example, Inflate should make the following changes * 23 => 24 * -23 => -22 * A1 => A2 * 1A => 2A * 077 => 0100 * -0100 => -077 * 0b11 => 0b100 * -0b100 => -0b11 * 0XFF => 0x100 * -0X100 => -0xff * 0x1A => 0x1b * 12\ 34 => 1235 // destructively folds line splice * If a number occurs inside a string delimited by double quotes, it should NOT be changed. * If a number occurs inside a comment, it SHOULD be changed. * Handle line splices (i.e., when finding a backslash immediately followed by a newline in the input stream, behave as if they did not appear; note that splices do not nest and are recognized before any other processing takes place). ). * Fail "gracefully" (i.e., neither go into an infinite loop nor cause a memory dump) if any of the assumptions above is violated. Moreover, Inflate should not * Make ANY assumptions as to the maximum length of a line. * Use any global variables. * However, you can assume that no integer will be larger than the largest long (MAX_LONG per limits.h). That value is // MAX_LONG: 9223372036854775807 which is nearly 20 decimal digits. Note that octal or binary numbers could have more digits. The negative limit is similar. Use the submit command (see below) to turn in the source file(s) for Inflate, a Makefile, and your log file (see below). YOU MUST SUBMIT YOUR FILES (INCLUDING THE LOG FILE) AT THE END OF ANY SESSION WHERE YOU SPEND AT LEAST ONE-HALF HOUR WRITING OR DEBUGGING CODE, AND AT LEAST ONCE EVERY HOUR DURING LONGER SESSIONS. (All submissions are retained.) Notes ~~~~~ 1. When available, the public grading script will be /c/cs223/hw1/test.Inflate (and my solution will be /c/cs223/hw1/Inflatex). To run it, type % /c/cs223/hw1/test.Inflate (here % is the shell prompt). The script uses make to create Inflate. To run each test it redirects the test file (e.g., /c/cs223/hw1/Tests/t01.c for Test #01) to the standard input of Inflate and redirects the standard output to a temporary file. Then it compares this file with the expected output for that input (e.g., /c/cs223/hw1/Tests/t01.cs for Test #01). Your program passes the test only if the two files are identical. To run your program on the file for Test #01, type % ./Inflate < /c/cs223/hw1/Tests/t01.c To compare the output from your program with the expected output, type % ./Inflate < /c/cs223/hw1/Tests/t01.c | cmp - /c/cs223/hw1/Tests/t01.cs (cmp outputs the first character where the files differ) or % ./Inflate < /c/cs223/hw1/Tests/t01.c | diff - /c/cs223/hw1/Tests/t01.cs (diff outputs the lines where they differ but uses a looser definition for "identical") or % /c/cs223/hw1/test.Inflate 01 (you may specify more than one test here). If your output looks the same as what is expected, but your program still fails the test, there are probably some invisible characters in your output. To make all characters visible (except blanks), type % ./Inflate < /c/cs223/hw1/Tests/t01.c | cat -vet or % ./Inflate < /c/cs223/hw1/Tests/t01.c | od -bc 2. Keep track of how you spend your time in completing this assignment. Your log file should be of the general form (that below is fictitious): ESTIMATE of time to complete assignment: 10 hours Time Time Date Started Spent Work completed ---- ------- ---- -------------- 1/13 10:15pm 0:45 Read assignment and relevant material in K&R 1/16 4:45pm 1:15 Sketched solution using a finite-state machine with one-character look-ahead 1/19 9:00am 2:20 Wrote the program and eliminated compile-time errors; code passes eight tests 1/20 7:05pm 2:00 Discovered and corrected two logical errors; code now passes eleven tests 1/23 11:00am 1:35 Finished debugging; program passes all public tests ---- 7:55 TOTAL time spent I discussed my solution with: Peter Salovey, Ben Polak, Tamar Gendler, and Jonathan Holloway (and watched four episodes of The Simpsons). but MUST contain * your estimate of the time required (made prior to writing any code), * the total time you actually spent on the assignment, * the names of all others (but not members of the teaching staff) with whom you discussed the assignment for more than 10 minutes, and * a brief discussion (100 words MINIMUM) of the major conceptual and coding difficulties that you encountered in developing and debugging the program (and there will always be some). This log will generally be worth 5-10% of the total grade. N.B. To facilitate analysis, the log file MUST be the only file submitted whose name contains the string "log" and the estimate / total MUST be on the only line in that file that contains the string "ESTIMATE" / "TOTAL". 3. The submit program can be invoked in eight different ways: % /c/cs223/bin/submit 1 Makefile Inflate.c util.c time.log submits the named source files as your solution to Homework #1; % /c/cs223/bin/check 2 lists the files that you submitted for Homework #2; % /c/cs223/bin/unsubmit 3 error.submit bogus.solution deletes the named files that you submitted previously for Homework #3 (which is useful if you rename a file or accidentally submit the wrong one); % /c/cs223/bin/makeit 4 Inflate runs "make" on the files that you submitted previously for Homework #4; % /c/cs223/bin/testit 5 Inflate runs the public test script for Inflate using the files that you submitted previously for Homework #5; % /c/cs223/bin/protect 6 Inflate.c time.log protects the named files that you submitted previously for Homework #6 (so they cannot be deleted accidentally); % /c/cs223/bin/unprotect 7 util.c time.log unprotects the named files that you submitted previously for Homework #7 (so they can be deleted); and % /c/cs223/bin/retrieve 8 common.c time.log and % /c/cs223/bin/retrieve 8 -d"2016/01/21 20:00" util.c retrieve copies of the named files that you submitted previously for Homework #8 (in case you accidentally delete your own copies). The day and hour are optional and request the latest submission prior to that time (see the -d flag under "man co" for how to specify times). 4. When assignments are style graded, EVERY source file found in the submit directory will be reviewed. Thus prudence suggests using unsubmit to remove a file from the directory when you change its name or it ceases to be part of your solution. See http://zoo.cs.yale.edu/classes/cs223/doc/Style 5. Prudence (and a 5-point penalty for code that does not make) suggests that you run makeit ("makeit 1 Inflate") after you have submitted the final version of your source files. Better yet, run testit ("testit 1 Inflate"). 6. Inflate is easier to write if you can peek at the next character in the standard input without reading it. The macro #define ungetchar(c) ungetc(c,stdin) // Unread char read from stdin allows you to push a character back onto the standard input. That is, the character C "unread" will be the next character returned by getchar(). The value returned by ungetchar() is its argument, or EOF if the operation was unsuccessful. Note: Every ungetchar() must be preceded by a getchar(), and you can only do one ungetchar() between successive getchar()'s. You may find this macro useful in writing Inflate since it allows you to read the next character and then decide that you should not have. Example: while ((c = getchar()) != EOF) { if (c == 'C') { c = getchar(); if (c == 'S') printf ("I found a CS in the standard input!\n"); else ungetchar(c); } } 7. The function exit() allows your program to stop immediately, without having to terminate any surrounding loops or to return to main() from a function. (To use it you must #include the header file .) 8. Inflate reads from stdin and writes to stdout but does no other input/output. 9. You may wish to use atoi() and itoa() which are defined in K&R on pages 61 and 64, respectively. itoa() uses reverse() defined on page 62. A. Features of C99 (but not ANSI C) that may be useful: * The characters // begin a comment that ends at the end of the line. * Variable declarations can appear anywhere within a code block; no longer must variables be defined at the top of a code block or outside all functions. * The header file stdbool.h defines type bool (meaning boolean) and symbolic constants true and false. (To use it you must #include .) * Functions must declare a return value; no longer does the type default to int if no type is specified. B. Correct handling of line splices will be worth at most 10 points. C. Reading: Kernighan & Ritchie, Chapter 1 (introduction to C) and Chapter 3, especially atoi() (page 61) and itoa() (page 64) Summit: https://www.eskimo.com/~scs/cclass/krnotes/sx4.html (K&R 1) Kernighan & Pike, Chapter 1 (style), Chapter 5 (debugging) Matthew & Stones, pp. 377-387 (makefiles), 429-445 (debugging) Matloff: http://heather.cs.ucdavis.edu/~matloff/Debug/Debug.pdf Optional: Aspnes: http://zoo.cs.yale.edu/classes/cs223/doc/howto.html Matthew & Stones, pp. 1-8 (Linux), 17-23 (bash) D. My first solution was 172 lines long (100 lines ignoring comments, blank lines, brace-only, and else-only lines. If your solution looks to be much larger than this, you should talk to one of the instructional staff about your approach. CS-223-09/04/16