Question number 1 (50 points) ----------------------------- For this assignment, you will write a simplified form of the unix program "tail". For those not familiar with tail, tail takes input from standard input or a file and returns the last N lines of the file where N is specified on the command line. Your SimpleTail will return the last 5 lines of a file specified on the command line. Requirements: 1. You can use structs or classes. 2. You can not use the std::string class. You must use arrays of char to hold strings. You can not use stl vectors etc. 3. You can not read the whole file into memory. You may hold at most six lines of text in memory at any one time. 4. Any array of char must be dynamically allocated and freed (new and delete []) as appropriate. 5. You must read one char at a time from the input stream using the method get(char & ch). (Example given below.) 6. Make appropriate use of functions to organize the program. 7. Use good coding style. 8. You may read the file only once. You may not read the file once and then close it and read it again. If your solution would not also work with std::cin, then it is an invalid solution. Example: Text file "test.txt": ------------------ start file This is a very simple text file. It consists of eight lines of text. I hope that is okay. ------------------ end file Usage: shellPrompt> SimpleTail test.txt of text. I hope that is okay. shellPrompt> Helpful info: 1) How to get the command line info: int main( int argc, char * argv[] ) // rather than int main() { int numArgsIncludingProgramName = argc ; // argv is an array of pointer to char that holds the // command line broken up based on whitespace. // // For example, the program name is always in argv[0]: std::cout << "\""<< argv[0] << "\" is the name of this program." << std::endl ; for ( int i = 1 ; i < argc ; ++i ) { std::cout << "Argument " << i << ": \"" << argv[i] << "\"" << std::endl; } } 2) If I assume filename is given in argv[1] (don't assume, make sure you have argc == 2), this is how to open the file: #include #include int main( int argc, char * argv[] ) { std::ifstream infile ; infile.open( argv[1] ) ; if ( !infile ) { std::cout << "unable to open file" << std::endl ; return 1 ; } } Now you can use infile just like cin. 3) How do I read a file one char at a time? // assuming infile is a ifstream: char ch ; while ( infile.get(ch) ) // while loops till end of file { // do something with ch } 4) What constitutes a line? Each '\n' character terminates a line or the EOF. What to hand in: 1. Print out of your source code. 2. Copy your source code to ~/cs197c/hw6/ on an edlab machine. The cs197c directory already exists for you, but you'll need to make the hw6 directory. Call all your source codes with the prefix hw6_1. Question 2 (50 points) ---------------------- In this assignment, you will build a stack based calculator that is designed to work only with fractions. (Unix has a stack based calculator called "dc" that this is modeled after. dc does not handle fractions as first class objects as your program will.) Reading from standard input (cin), your program will receive fractions and operators and when the end of file is reached, your program should output the result of the calculations via standard out (cout). As a fraction is read, it is pushed on the stack. When an operator is read, it pops the top two fractions off the stack, does the computation, and pushes the result back down on the stack. The final result is the fraction on the top of the stack. Here is an example input file, sample.txt: ----------------start 1/2 3/4 + ----------------end When your program is sent this file via stdin, it would output: ----------------start 5/4 ----------------end Here is a more complex example: ----------------start 1/1 1/4 + 1/2 * 1/4 / ----------------end When your program is sent this file via stdin, it would output: ----------------start 5/2 ----------------end The requirements of this program are: 1. The result should be simplified, i.e. 1/2 not 2/4. 2. If the input is malformed, output an error message and quit. This includes fractions that have a zero in their denominator. 3. If a divide by zero would occur, output an error message and quit. 4. You must use either a struct or a class to hold the fraction. I recommend this struct: struct Fraction { int numerator ; int denominator ; } ; The struct above will pass by value, pass by reference, be assigned to, etc. without complication since it is filled with only "plain old data", i.e. two simple ints. Note that the numerator and denominator are uninitialized by default. 5. You must handle the following operators: +,-,*,/ 6. Your stack must be able to grow without limit. 7. All numbers will be input as fractions. There will be no decimal numbers and no ordinary integers. 8. Do not use the STL container classes and do not use the std::string class. What to hand in: 1. Print out of your source code. 2. Copy your source code to ~/cs197c/hw6/ on an edlab machine. The cs197c directory already exists for you, but you'll need to make the hw6 directory. Name all your sources with the prefix hw6_2.