|
Lectionary C: 2019, 2016, 2013
Secret Decoder Ring
Submit: Turn in two C source files called "encoder-ring.c" and "decoder-ring.c" using the turnin command on the Systems Lab computers, as noted below.
Points for this project will be assigned primarily based upon
automated testing of your submitted source code. It is imperative,
therefore, that you name your files properly as directed above, (no
changes in name, spelling, or capitalization,) and that your programs
are ready to be compiled and run using gcc on Morbius. You
should make liberal use of debugging output during development, but
your submitted version should not be embellished with unspecified
output such as additional prompts or commentary.
This assignment is to be completed individually.
Ring of Xinu
Whenever Lord Xinu (Dark Lord of the Mips) is communicating with his minions over long distances, he uses his super-secret, Wormhole X-Treme! cereal box decoder ring to properly encipher his messages.
Your task is to write two simple programs that automate the process of enciphering and deciphering messages without relying on the cheap plastic decoder ring.
You should use the getchar library function to read console
input one character at a time. The use of any other library functions
for input is not recommended at this time.
We have provided example programs for your reference, executable
on Morbius as ~brylow/os/Projects/encoder-ring and
~brylow/os/Projects/decoder-ring.
Simple Autokey Cipher
Autokey ciphers are a class of polyalphabetic cipher algorithms that incorporate the text of the plaintext message into the cipher key.
Envision our decoder ring as a disk with the 26 upper-case alphabet
letters arrayed around its edge in order, with a 27th place between
'Z' and 'A' containing a space. Each of the 27 slots has a number associated
with it: the space is 0, 'A' is 1, 'B' is 2, and so on, up to 'Z' = 26.
To encipher a message, Lord Xinu first chooses a positive integer
key value. His minions will use the same key value later to decode
the enciphered message. Next, he adds the key value to the value of
the first letter of his message, which is equivalent to counting that
many slots around the edge of the decoder ring. The symbol he lands
on will be the first character he sends as his encrypted message.
If Lord Xinu followed this same algorithm -- adding the key value
to each letter in turn -- he would have what is called a shift cipher,
such as the
infamous Caesar
cipher or ROT13.
This is too easy to break; to make this a more
difficult autokey
cipher, he instead adds the next letter value and key to
his current position on the decoder ring.
For example:
Assume a key value of zero, and a message of "Hello world!". The first
letter 'H' has value 8. The key is 0, and there is no previous value, so 'H' will be encoded as 'H' (8 + 0 + 0 = 8 = 'H'). The second letter is 'e', which has value 5. The previous value plus 'e' plus the key is 'M' (8 + 5 + 0 = 13 = 'M'.) The third letter of the message is 'L'. (Note that we immediately convert lower-case letters to upper-case for this code.) Previous value 13 plus 'L' (12) plus key (0) = 25 = 'Y'. The fourth character is also 'L'. Previous value (25) plus 'L' (12) plus key (0) = 37, which when wrapped around the decoder ring (modulo 27) is 10, or 'J'.
The enciphered result for "Hello world!" is thus
"HMYJYYUI LPP" with a key of 0, or "IOANCDAQIV A"
with a key of 1, or "JQDRHJHYREKM" with a key of 2, and so
on.
Newline characters ('\n') are transmitted unencoded.
Note that we treat spaces -- and all other non-alphabetic characters
-- as value 0. They will be decoded as blank spaces.
This assignment can be completed using simple loop and conditional
constructs, along with basic integer operations like addition and modulus.
Your encoder-ring and decoder-ring programs should take an optional
key value as a command-line argument. (See textbook section 5.10.) If no
argument is given, the key is assumed to be 0. You may use the C library
function atoi (textbook section B5), or parse the command-line
integer yourself. The encoder-ring reads in plaintext messages line
by line and outputs ciphertext encrypted with the specified key. The
decoder-ring reads in ciphertext as input and outputs the plaintext
message decrypted with the specified key.
Notes
- The Systems Lab (CU 310) is the primary development environment
for COSC 3250 and COEN 4820. It is expected that you will learn to use
the required tools for developing, testing, and submitting your
projects. The
are available locally in Cudahy 310,
and can be accessed remotely via ssh using your MSCS password. Free
secure shell (ssh) clients are readily available for other platforms,
including Windows machines at home or in the dorms. See How to Get and Use Secure Shell
(SSH)
-
The
run the Linux operating system. If
you need a refresher on using the UNIX environment, please see
the
UNIX Tutorial. See also How to Get and Use
Secure Shell (SSH), How to Run an
X-Windows Session, or Brief UNIX
intro
- This project can seem deceptively complex, but is quite tractable
if you first take the time to design suitable helper functions,
and test those function thoroughly before moving on to the
overall project. We encourage the disciplined practice of
test-driven development.
- The reference implementation is considered an integral part of the
specifications for this assignment. Project grades will be based in
significant part upon an automated comparison of behavior against the
reference implementation. Devise test cases to discover the expected
behavior, and do your best to match it precisely. Creativity will be
valued in later assignments, but first one must master the tools.
- Use the electronic turnin system to submit your work.
The command is "turnin", followed by the name(s) of files(s) you
wish to submit. The command "turnin -v" will print a listing
verifying the contents of your submission. See "turnin -h" for
a list of other relevant options. The system does not accept work
after the deadline, and neither does the professor.
We will return to encryption in Chapter 15 of the text in April.
Lectionary B: 2018, 2015, 2012, 2010
All Your Base Are Belong To Us
Geek cultural reference, circa 2001, (en.wikipedia.org/wiki/All_your_base_are_belong_to_us)
Submit: Source code is to be submitted electronically using the turnin command on the Systems Lab computers, as noted below.
This assignment is to be completed individually.
The Multi-Base Calculator
Write a calculator program that reads in expressions consisting of integers and the operators +, -, *, /, %, ^ (exponentiation), & (bitwise AND), | (bitwise OR), << (left shift), and >> (right shift), and prints the results when evaluated in simple left-to-right order.
Your calculator should understand positive integers in binary (starting with "0b",) octal, decimal, and hexadecimal, but all output will be in decimal (base-10).
You should use the getchar library function to read console input one character at a time. The use of any other library functions for input is not recommended at this time.
We have provided an example program for your reference, executable on Morbius as ~brylow/os/Projects/basecalc.
Notes
- The Systems Lab (CU 310) is the primary development environment for COSC 3250 and COEN 4820. It is expected that you will learn to use the required tools for developing, testing, and submitting your projects. The
are available locally in Cudahy 310, and can be accessed remotely via ssh using your MSCS password. Free secure shell (ssh) clients are readily available for other platforms, including Windows machines at home or in the dorms.
- The
run the Linux operating system. If you need a refresher on using the UNIX environment, please see the UNIX Tutorial.
- This project can seem deceptively complex, but is quite tractable if you first take the time to design suitable helper functions, and test those function thoroughly before moving on to the overall calculator. We encourage the disciplined practice of test-driven development.
- The internal data representation of the calculator values can be assumed to be a signed int. You are not required to deal with overflow and underflow issues.
- Parsing input is always tedious, particularly when the input is not rigidly constrained with rules like, "all operators must be surrounded by space on both sides," (which is NOT a constraint for this assignment). Powerful tools for parsing are covered in COSC 4400 Compiler Construction and to a much lesser extent, COSC 3410 Programming Languages. In lieu of a parser-generator, consider drawing simple state diagrams for how to recognize an integer in octal, or how to recognize an integer in hexadecimal. In many cases with input of this type, it is useful to be able to put back a character of input once you realize you have read too far. Useful C routines for implementing this kind of buffering are presented in Section 4.3 of K&R, (getch() and ungetch()).
- Consider various operational and parsing errors that can take place. See the reference implementation for appropriate error messages.
- The reference implementation is considered an integral part of the specifications for this assignment. Project grades will be based in significant part upon an automated comparison of behavior against the reference implementation. Devise test cases to discover the expected behavior, and do your best to match it precisely. Creativity will be valued in later assignments, but first one must master the tools.
- Use the electronic turnin system to submit your work. The command is "turnin", followed by the name(s) of files(s) you wish to submit. The command "turnin -v" will print a listing verifying the contents of your submission. See "turnin -h" for a list of other relevant options. The system does not accept work after the deadline, and neither does the professor.
Lectionary A: 2017, 2014
Euros and Rupees and Yen, Oh My!
Submit: Source code is to be submitted electronically using the turnin command on the Systems Lab computers, as noted below.
This assignment is to be completed individually.
International Currency Converter
Whenever Lord Xinu (Dark Lord of the Mips) returns from an international business trip, he finds his pockets are full of international currency that cannot be used to purchase wireless routers in the U.S.
Your task is to write a simple international currency converter. The converter will take an unformatted list of five allowable currencies, convert each to their equivalent in U.S. dollars, and print a final total when done.
Your converter should understand dollars ('$'), Euros ('€'), British pounds sterling ('£'), the Japanese Yen ('¥'), and the Indian Rupee ('₹').
You should use the getchar library function to read console input one character at a time. The use of any other library functions for input is not recommended at this time.
We have provided an example program for your reference, executable on Morbius as ~brylow/os/Projects/money.
Conversions
Conversion rates between international currencies change each daily. For the sake of simplicity, we will use the rates that were in effect the day this assignment specification was written:
$1000 == £813 or €936 or ¥113415 or ₹68163.
You may assume that only integer values of currency will be entered, and no decimal points will appear in the input. Likewise, please use only integer arithmetic operations to perform your conversion, and output only truncated integer dollars. (Note that this means an answer of $0.99 is still just output as $0.)
Representation
The dollar sign ('$') is a regular ASCII character, a single byte in the system used nearly universally by computers to represent plain text. The other currency symbols, however, are all later additions to computer character sets. The Unicode Standard contains assignments for all of the currency symbols used in this project:
Table 1 Currency symbol codes |
Symbol | Unicode | Meaning | UTF-8 Encoding |
$ | U+0024 | Dollar Sign | 0x24 |
£ | U+00A3 | Pound Sign | 0xC2, 0xA3 |
¥ | U+00A5 | Yen Sign | 0xC2, 0xA5 |
€ | U+20AC | Euro Sign | 0xE2, 0x82, 0xAC |
₹ | U+20B9 | Indian Rupee Sign | 0xE2, 0x82, 0xB9 |
|
* - All table values in hexadecimal |
While all of these symbols can be represented by a simple two-byte encoding, the more common solution is to use a representation called UTF-8. This is the encoding actually used by Linux (as well as most web servers and e-mail clients) to store extended character values. The first 127 ASCII remain the same for reverse compatibility, but higher-numbered Unicode values are encoded with between two and four bytes.
What does all of this mean for you? When your program is reading or writing the currency symbols, you're actually going to use the byte combinations in the column marked "UTF-8" in Table 1 above.
Notes
| |