Lecture 11 Overview Programming Instructions 1. Create file p02.cpp that manages command line arguments and directs the process of creating a Cartesian product. 1.1. Start by entering the command line arguments program found lecture note 1 of CMSC 2613 Programming II http://cs2.uco.edu/~trt/cs2613/l001.pdf. 1.2. Modify the program so that it accepts three file names, two input file names and one output file name. 1.3. Make the manager function CartesianMgr as shown below. void CartesianMgr(istream& i1,istream& i2,ostream& o) { List L1(i1); L1.Print(o,"L1"); //Create List L1. Scan input file stream i1 into the list. //Print the list. List L2(i2); L2.Print(o,"L2"); // Create List L1. Scan input file stream i1 into the list. //Print the list. List C; //Create an empty list C for the Cartesian product. CartesianProduct(L1,L2,C); //Create the Cartesian product from lists L1 and L2. //Store the product in list C. C.Print(o,"C"); //Print the Cartesian product. 1.4. Create function CartesianProduct. void CartesianProduct(List& L1,List& L2,List& C) { for (L1.First();!L1.IsEol();L1.Next()) { for (L2.First();!L2.IsEol();L2.Next()) { C.Insert(Pair(L1.Member(),L2.Member())); 1.5. You will also need function Pair. string Pair(string x,string y){return "(" + x + "," + y + ")"; 1.6. File p02.cpp is organized as shown below. //File p02.cpp directs the process of creating a Cartesian product. //Author Identification block ---- //C++ include files and namespace std. #include <cstdlib> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <fstream> 1
Lecture 11 Overview using namespace std; //Application include files #include "List02.h" string Pair(string x,string y){ void CartesianProduct(List& L1,List& L2,List& C){ void CartesianMgr(istream& i1,istream& i2,ostream& o){ struct CommandLineException { ; struct FileException { ; int main(int argc, char* argv[]) { try { char ifn1[255],ifn2[255],ofn[255]; switch (argc) { case 1: case 2: //Prompt for 2 input file names and 1 output file name. //Copy input file name 1. Prompt for the input file name 2 and the //output file name. case 3: case 4: default: throw CommandLineException(3,argc-1); //end switch ifstream i1(ifn1); if (!i1) throw FileException(ifn1); ifstream i2(ifn2); if (!i2) throw FileException(ifn2); ofstream o(ofn); if (!o) throw FileException(ofn); CartesianMgr(i1,i2,o); o.close(); i1.close(); i2.close(); catch (... ) { return 0; //Read input file names 1 and 2. Prompt for the output file name. //Read input file names 1 and 2. Read the output file name. 2
Overview 2. Create file List02.h that contains class List. #ifndef List02 #define List02 1 //Author Identification Comment ---- //C++ include files and namespace std. #include <cstdlib> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; struct ListException { ; class List { int size; //Number of elements available in the list int count; //Actual number of elements occupied. //Index of the next available element string* L; //A pointer to a list of strings. int cursor; //Index of the current element public: List(int sz=100):size(sz),count(0),cursor(-1){l=new string[size]; List(istream& i,int sz=100):size(sz),count(0),cursor(0){l=new string[size];scan(i); ~List(){if (L) delete[] L; bool IsFull(void){ void Insert(string v){ void Scan(istream& i){ void Print(ostream& o,const char* title){ void First(void){cursor=0; void Next(void){if (cursor<count) cursor++; bool IsEol(void){return cursor>=count; string Member(void){return L[cursor]; ; #endif 3
Overview Submission Instructions 1. Create file p02.cpp in the root directory of your account for this class on the departmental computer cs.uco.edu. 2. Enter your author identification block consisting of 2.1. Your name, for example, Mr. Alan Turing 2.2. Your student identifier, for example, *00000000 2.3. You e-mail address, for example, aturing@uco.edu 2.4. This course Discrete Structures 2.5. The CRN, term, and year of the class in which you are enrolled, for example, 12599, Autumn, 2012 2.6. The project identifier p02 2.7. The date that the project is due, for example, October 10, 2012 2.8. The account number of the account that you were assigned tt000. 3. Design, code, and test your algorithm for producing the results of operations specified in the project assignment. 4. Download a copy of the Project Template (MS Word) from the class webpage http://cs2.uco.edu/~trt/cs2123.html changing the name of the file to crn-last-first-p02.docx where crn is the CRN of the class in which you are enrolled, last is your last name, and first is your first name. For example, if your name is Alan Turing and you are enrolled in CRN 12599, the name of the file you downloaded is 12599-Turing-Alan-p02.docx. 5. Complete the author identification block in the document you downloaded changing the name and other fields in the block to appropriate values. 6. Change the name in the header of the document to your name. 7. Create a new section and a new header in your document. 7.1. Select the Page Layout tab. 7.2. Select Breaks drop-down list. 7.3. Select the Next Page in the Section Breaks list. 7.4. In the Navigation Tab uncheck Link to Previous 7.5. Change Cover Page to File p02.cpp. 8. Employ WinSCP3 that can be obtained from Dr. McDaniel s web page to copy the source file p02.cpp from your account on the departmental computer to the PC having the document that you will submit to your instructor. 9. Insert File p02.cpp into the section having the header p02.cpp. 9.1. Select Insert. 9.2. In the Text tab, select the Object drop-down list. 9.3. Select Text from file. 9.4. Navigate to the folder containing file p02.cpp. 9.5. Select file p02.cpp to be inserted. 10. Submit the project to your instructor by sending the document to your instructor. The fields of your note contain: 10.1. To: trturner@uco.edu 10.2. Subject: crn-last-first-p02 where crn is the CRN of the class in which you are enrolled, last is your last name, and first is your first name. For example, if your name is Alan Turing and you are enrolled in CRN 12599, the name of the file you downloaded is 12599-Turing-Alan-p02 10.3. Attach the document containing your project template and source file. 4
File p02.cpp //File p02.cpp exercises class Stack // //Author: Ms. Petunia Perfect //Student ID: *00000001 //Course: - Discrete Structures //CRN: 11786, Autumn, 2014 //Project: p02 //Due: October 8, 2014 //Account: tt000 //Copyright October, 2014 by Ms. Petunia Perfect //Do not reproduce without permission from Ms. Petunia Perfect //C and C++ standard include files #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> using namespace std; //Application include files #include "Stack02.h" -- //Function ReverseMgr scans the input stream i for strings, pushes them on the //Stack, then pops them from the stack in reverse order. -- void ReverseMgr(istream& i,ostream& o) { Stack S; for (;;) { string c; i >> c; if (i.eof()) o << " " << c; S.Push(c); o << endl; while (!S.IsEmpty()) o << " " << S.Pop(); o << endl; // 5
File p02.cpp //FileException is thrown when too many arguments appear on the command line. struct FileException { FileException(const char* fn) { cout << "File " << fn << "could not be opened."; ; // //CommandLineException is thrown when too many arguments appear //on the command line. // struct CommandLineException { CommandLineException(int m,int a) { cout << "Too many arguments on the command line."; cout << "A maximum of " << m << " arguments may appear on the command line."; cout << a << " arguments were on the command line."; ; // //Function main processes command line arguments main(int argc,char* argv[]) { try { char ifn[255],ofn[255]; switch (argc) { case 1: cout << "Enter the input file name. "; cin >> ifn; cout << "Enter the output file name. "; cin >> ofn; case 2: strcpy(ifn,argv[1]); cout << "Enter the output file name. "; cin >> ofn; case 3: strcpy(ifn,argv[1]); strcpy(ofn,argv[2]); default: throw CommandLineException(2,argc-1); ifstream i(ifn); if (!i) throw FileException(ifn); ofstream o(ofn); if (!o) throw FileException(ofn); 6
File p02.cpp ReverseMgr(i,o); o.close(); i.close(); catch (...) { cout << "Program terminated!"; cout << "I won't be back!"; exit(exit_failure); return 0; 7
File Stack02.h #ifndef Stack02_h #define Stack02_h 1 //-- //File Stack02.h defines class Stack. Class Stack is implemented //using a dynamically allocated array of strings. //Author: Ms. Petunia Perfect //E-Mail: pperfect@uco.edu //Student ID: *00000001 //Course: - Discrete Structures //CRN: 11786, Autumn, 2014 //Project: p02 //Due: October 8, 2014 //Account: tt000 //Copyright October, 2014 by Ms. Petunia Perfect. //Do not reproduce without permission from Ms. Petunia Perfect. // //C++ Standard include files // #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> using namespace std; //class Stack contains the attributes and member functions of a //stack implemented using a dynamically allocated array class Stack { int size; int tos; string* S; public: Stack(int sz=100); ~Stack(); void Push(string v); bool IsFull(void); bool IsEmpty(void); string Pop(void); ; #endif //Number of available elements //Top Of Stack - index of the //element on top of //the stack //Points to an array of strings. //Constructor //Destructor //Push a string onto the stack //Is the stack full? //Is the stack empty? //Pop a string from the stack 8
File Stack02.cpp -- //File Stack02.cpp implements class Stack //Author: Ms. Petunia Perfect //Student ID: *00000001 //Course: - Discrete Structures //CRN: 11786, Autumn, 2014 //Project: p02 //Due: October 8, 2014 //Account: tt000 //Copyright October, 2014 by Ms. Petunia Perfect //Do not reproduce without permission from Ms. Petunia Perfect //C and C++ standard include files #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> using namespace std; //Application include files #include "Stack02.h" //A StackException is thrown when the stack is empty or full. struct StackException { StackException(const char* m) { cout << "I am the stack and I am " << m << "."; ; Stack::Stack(int sz):size(sz),tos(-1){s=new string[size]; -- //Destructor 9
File Stack02.cpp -- Stack::~Stack(){if (S) delete[] S; -- //Member function Push -- void Stack::Push(string v) { if (IsFull()) throw StackException("full"); S[++tos]=v; -- //Member function Pop -- string Stack::Pop(void) { if (IsEmpty()) throw StackException("empty"); return S[tos--]; -- //Member function IsFull -- bool Stack::IsFull(void){return tos>=size-1; -- //Member function IsEmpty -- bool Stack::IsEmpty(void){return tos<0; 10
File p02make # # File p02make contains instructions that create executable file # p02. # # Author: Ms. Petunia Perfect # Student ID: *00000001 # E-Mail: pperfect@uco.edu # Course: - Discrete Structures # CRN: 11786, Autumn, 2014 # Project: p02 # Due: Ocotber 8, 2014 # # Object files (no colon) # obj = p02.o Stack02.o # # Bind (Link) the object files and create executable p02 # p02: ${obj g++ -o p02 ${obj -lm # # Compile p02.cpp that exercises class Stack. # p02.o: p02.cpp Stack02.h g++ -g -c p02.cpp # # Compile Stack02.cpp that implements class Stack. # Stack02.o: Stack02.cpp Stack02.h g++ -g -c Stack02.cpp 11
File i02.dat One, two, buckle my shoe. 12
File o02.dat One, two, buckle my shoe. shoe. my buckle two, One, 13