Go Assignment 2

This Again

Assigned
Due

Feb 4
80 pts
Feb 14
Create a program which outputs a document based on the input described below. Read from standard input, and write to standard output. Standard input may be provided from a file using the < symbol on the command line, but do not attempt to open any file from inside your program. Read only from standard input.

Input Specification

The input file is a sequence of specifications separated by white space. “White space” means a run of one or more of any of the characters Go considers spaces, including space, tab and (usually) line break.

Each specification has the following form:

[(rpt|srpt)n]c
Where n is a positive integer repeat count, and c is any non-blank string. The c value may not be rpt or srpt when the repeat part is omitted. The output for each specification is as follows:
  1. The item c is printed. Generally, c stands for itself, except for the special items sp, which stands for a single space, and nl which stands for a line break (new line). For either of the two special items, print what they stand for, otherwise, just print the value of c.
  2. If there is no repeat count specified (if the specification consists only of c), then print c once.
  3. If a specification uses the rpt marker, the item c should be printed n times.
  4. If a specification uses the srpt marker, the item c should also be printed n times, but with a single space between each repetition.

Some some example specifications:

fred
This just prints the string fred once.
sp
This prints a single space.
rpt 5 joe
This prints joejoejoejoejoe
srpt 5 joe
This prints joe joe joe joe joe
rpt 3 sp
This prints three spaces.
The special items only match exactly. So, the specification ssp just prints ssp, and the input s p is two successive specifications, which together print sp.

For instance, the input

This sp is sp some sp input. nl Hope sp you sp srpt 3 really sp like sp it. nl It sp is sp nice rpt 2 ! sp sp Also, sp here sp is sp an sp s p . nl
produces the output
This is some input. Hope you really really really like it. It is nice!! Also, here is an sp.

For another example,

srpt 9 :: nl :: rpt 8 sp rpt 3 __ sp rpt 7 sp rpt 2 : nl : : rpt 7 sp / rpt 2 + rpt 2 = rpt 2 + \ rpt 7 sp : : nl rpt 2 : rpt 6 sp |+++==+++| rpt 6 sp : : nl : : rpt 6 sp | rpt 4 + sp rpt 2 sp < rpt 7 sp rpt 2 : nl :: rpt 6 sp | rpt 3 sp C rpt 4 sp _ \ rpt 5 sp :: nl : : sp rpt 4 sp sp | rpt 7 sp = rpt 4 sp sp rpt 2 sp rpt 2 : nl rpt 2 : rpt 3 sp rpt 4 sp \ rpt 5 sp _ ) rpt 7 sp :: nl : : rpt 8 sp | sp sp sp | rpt 9 sp :: nl rpt 1 : : sp srpt 11 sp :: nl srpt 9 :: nl rpt 17 sp Uncle sp Max nl
produces this bit of ascii art:
:: :: :: :: :: :: :: :: :: :: ______ :: :: /++==++\ :: :: |+++==+++| :: :: |++++ < :: :: | C _\ :: :: | = :: :: \ _) :: :: | | :: :: :: :: :: :: :: :: :: :: :: :: Uncle Max

You may assume the input is correctly formatted; you don't need to check for input errors.

Implementation Notes

Write in Go and use the fmt package. To read in a specification, begin by reading the first word into a string variable. If it is not rpt or srpt, then you've read the whole specification, which was only a c value.

If your first read does find either rpt or srpt, then read the repeat count (into an integer variable) and read the next word as a string, which is c.

Now that you have read one complete specification, print c either once or n times, as required. If c is special (either sp or nl), print its translation. Otherwise, print what you read in for c.

The above procedure reads and obeys one specification. Place it in a loop to read the entire input to end-of-file.

Programming Style

Please indent your code properly. You should indent the body of a control constructs (such as if or while), and indent function bodies. The amount of the indent should be proportional to the nesting depth of code indented, and the amount should be consistent throughout the program. That is, any statement nested three levels should be indented three times as much as a statement indented one level, and the same as any other statement nested three levels.

Comment your code. The meaning of each variable should be clear; use good variable names and/or comment the declaration to be sure of this. Each program should have a comment at the top describing what the program does, and what its input must look like. Code bodies should be commented so that they are easy to understand by another programmer. A program is usually long-lived and will have to be understood by many persons other than its author. Good comments describe what's going on at a higher level than the code. For instance, this is remarkably useless:

// Increment counter in_count++;
But this might be better:
// Record that we read the current line of input. in_count++;

The on-line code examples use an acceptable programming style.

Running Your Program

Your program should read from standard input and write to standard output. Since you will not usually want to type the entire test input, you will need to supply it from a file. To do this, use the standard input redirection, <, to provide a file for standard input. Suppose we copy the first input example above input into a text file called a2in.txt. If the program is compiled to an executable a2 (or a2.exe on Windows), you can run it like this:
[tom@laptop asst]$ a2 < a2in.txt This is some input. Hope you really really really like it. It is nice!! Also, here is an sp. [tom@laptop asst]$
Here, the file a2in.txt holds the input data, and the < makes standard input the contents of the file instead of the console.

This works fine with CodeBlocks on Windows. Create a text file containing the input data, either copied from this page or data you create yourself. Save the data file and your program in the same folder. You may compile your program from the GUI, then bring the command window up, and change to the folder where all the files are. Then run your program as shown above. Your program should not open any files. It should just read from standard input, which can then be supplied from a file using the method described here.

Submission

When your program works and is properly formatted and commented, submit it online here.