辅导案例-CSI 1440

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
CSI 1440
Project 7
“Study of Programming Paradigms”
Deliverables:
• proj7-sequentialMain3.cpp
• proj7-modularMain3.cpp
• proj7-oopMain5.cpp
You are not explicitly required to turn in the example code that is presented throughout this document. I do highly
encourage you to type and run each of these examples as you progress through the document. The actual deliverables
listed above do require you to start off with a previous example. Also, make sure that you add header comments,
function comments, and meaningful inline comments to the deliverables.
We are going to try to experience three major programming paradigms (sequential programming, modular programming,
and object oriented programming) with this project.
First, let’s write some code. We have been asked to write some code that will store information about people. We need
to store, manipulate, and retrieve a person’s name and age. We will ask the user to input their name and age then we will
display it back to the user. Here’s the code for our simple program.
sequentialMain1.cpp:
int main() {
string name;
int age;

cout << "What is your name? " << endl;
getline( cin, name );

cout << “How old are you? " << endl;
cin >> age;

cout << "Your name is: " << name << endl;
cout << "You are " << age << “ years old” << endl;

return 0;
}
This looks like something you would have implemented early last semester. For such a simple task it makes since for us
to implement the solution in such a simple way. Ideas such as modular programming and object oriented programming
start to influence our implementations as the complexity of the problem increases.
After we completed our task, our customers came back and articulated that our program was good, but it lacked some of
the functionality they wanted. They indicated that they would need to store this information for multiple people. So, we
went back to our trusty IDE and rewrote our solution. Here’s the code for our second program.
sequentialMain2.cpp:
int main() {
string name;
int age;

string choice = "yes";

while( choice != "no" ) {
cout << "What is your name? " << endl;
getline( cin, name );

cout << “How old are you? " << endl;
cin >> age;

cout << "Your name is: " << name << endl;
cout << "You are " << age << “ years old” << endl;

cout << "Do you want to read in another person (yes/no)? " << endl;
cin >> choice;
cin.ignore(3, '\n');
}
return 0;
}
At this point, let’s see how modular programming can make things a little easier from a readability standpoint.
modularMain1.cpp:
void readPerson( istream &in, string &name, int &age ) {
getline( in, name );
in >> age;
}
void requestInfo( ostream &out ) {
out << "Please enter your name followed by your age." << endl;
}
void displayPerson( ostream &out, string name, int age ) {
out << "Your name is: " << name << endl;
out << "You are " << age << “ years old” << endl;
}
string readAgain( ostream &out, istream &in ) {
string choice;

out << "Do you want to read in another person (yes/no)? " << endl;
in >> choice;
in.ignore(3, '\n');

return choice;
}
int main() {
string name;
int age;
string choice = “yes”;

while( choice != "no" ) {
requestInfo( cout );
readPerson( cin, name, age );
displayPerson( cout, name, age );
choice = readAgain( cout, cin );
}
return 0;
}
You can see that it makes it easier to understand the logic of the loop. In addition, it allows us the opportunity to make a
change to the algorithm of reading a person independently of the loop algorithm.
The customers have come back with another change. They have decided that in addition to reading from the keyboard it
would be nice to be able to read the data from a file. In addition, there is no need to display all names to the screen.
Instead, they indicated that we should just write the names to a file. Here’s the modular designed code with those
additions.
You need to write the non modular version of this solution. Start with the implementation provided in
sequentialMain2.cpp and implement the functionality found in modularMain2.cpp. Name your source code
sequentialMain3.cpp and turn it in on the upload site.
modularMain2.cpp:
void readPerson( istream &in, string &name, int &age ) {
getline( in, name );
in >> age;
in.ignore(3, '\n');
}
void requestInfo( ostream &out ) {
out << "Please enter your name followed by your age." << endl;
}
void writePerson( ostream &out, string name, char age ) {
out << name << endl;
out << age << endl;
}
string readAgain( ostream &out, istream &in ) {
string choice;

out << "Do you want to read in another person (yes/no)? " << endl;
in >> choice;
in.ignore(3, '\n');

return choice;
}
int main() {
string name, choice;
int age;
bool fileRead = false;
ifstream pfile;
ofstream opfile;

cout << "Would you like to read the data from a file? " << endl;
cin >> choice;
cin.ignore(3, '\n');

if( choice == "yes" ) {
fileRead = true;
pfile.open("iperson.txt");
if( !pfile ) {
cerr << "Can't open person.txt for read." << endl;
return -1;
}
}

opfile.open("operson.txt");

choice = "yes";

if( fileRead ) {
readPerson( pfile, name, age );
if( !pfile ) {
choice = "no";
}
}

while( choice != "no" ) {
if( !fileRead ) {
requestInfo( cout );
readPerson( cin, name, age );
}

writePerson( opfile, name, age );

if( !fileRead ) {
choice = readAgain( cout, cin );
} else {
readPerson( pfile, name, age );
if( !pfile ) {
choice = "no";
}
}
}
if( fileRead ) {
pfile.close();
}

opfile.close();
return 0;
}
This customers are happy with the changes, but want us to allow for searching through a list of persons that have been
stored in a file or entered via the keyboard. For this addition, we will need to create an array of persons. I’m going to
move to an object oriented design.
oopMain1.cpp:
const int PLIST_SIZE = 10;
class Person {
string name;
int age;
public:
string getName() { return name; }
void readPerson( istream & );
void writePerson( ostream & );
};
void Person::readPerson( istream &in ) {
getline( in, name );
in >> age;
in.ignore(3, '\n');
}
void Person::writePerson( ostream &out ) {
out << name << endl;
out << age << endl;
}
void requestInfo( ostream &out ) {
out << "Please enter your name followed by your age." << endl;
}
string readAgain( ostream &out, istream &in ) {
string choice;

out << "Do you want to read in another person (yes/no)? " << endl;
in >> choice;
in.ignore(3, '\n');

return choice;
}
int findPerson( Person pList[], string name ) {
int pos = -1, index = 0;

while( pos == -1 && index < PLIST_SIZE ) {
if( pList[index].getName() == name ) {
pos = index;
}
index++;
}

return pos;
}
int main() {
Person pList[PLIST_SIZE];
string choice;
int index = 0;
bool fileRead = false;
ifstream pfile;
ofstream opfile;

cout << "Would you like to read the data from a file (yes/no)? " << endl;
cin >> choice;
cin.ignore(3, '\n');

if( choice == "yes" ) {
fileRead = true;
pfile.open("iperson.txt");
if( !pfile ) {
cerr << "Can't open person.txt for read." << endl;
return -1;
}
}

opfile.open("operson.txt");

choice = "yes";

if( fileRead ) {
pList[index].readPerson( pfile );
if( !pfile ) {
choice = "no";
}
}

while( choice != "no" && index < PLIST_SIZE ) {
if( !fileRead ) {
requestInfo( cout );
pList[index].readPerson( cin );
}

pList[index].writePerson( opfile );

index++;
if( !fileRead ) {
choice = readAgain( cout, cin );
} else {
pList[index].readPerson( pfile );
if( !pfile ) {
choice = "no";
}
}
}
if( fileRead ) {
pfile.close();
}
opfile.close();

cout << "Which person are you looking for? " << endl;
getline( cin, choice );

if( (index = findPerson( pList, choice )) != -1 ) {
cout << "Found your person: " << endl;
pList[index].writePerson( cout );
} else {
cout << "Couldn't find your person: " << choice << endl;
}
return 0;
}
Even our customer admits that he did not originally articulate that there are actually multiple types of Persons within their
organization. So, he has asked us to make one more change. He wants us to add the idea of a customer.
At this point, you need to think about the additional content that would typically be required to store the information for the
new and old types we have created to get a true appreciation for OOP. We’re leaving out many of the details that a typical
business would want to record for brevity. But, here’s where our object oriented approach will help us. It allows for more
rapid and easy changes to be made without redeveloping portions of code. Now, of course, we aren’t going to see the
final product quite yet. Here’s our next iteration of the project. This iteration introduces the concept of inheritance. We
are inheriting a Customer class from the Person class to reuse portions of the Person class.
You need to write the modular version of this solution. Start with the implementation provided in
modularMain2.cpp and implement the functionality found in oopMain2.cpp. Name your source code
modularMain3.cpp and turn it in on the upload site.
oopMain2.cpp:
const int PLIST_SIZE = 10;
class Person {
protected:
string name;
int age;
public:
string getName() { return name; }
void readPerson( istream & );
void writePerson( ostream & );
};
void Person::readPerson( istream &in ) {
getline( in, name );
in >> age;
in.ignore(3, '\n');
}
void Person::writePerson( ostream &out ) {
out << name << endl;
out << age << endl;
}
void requestInfo( ostream &out, string msg ) {
out << msg << endl;
}
string readAgain( ostream &out, istream &in ) {
string choice;
out << "Do you want to read in another person (yes/no)? " << endl;
in >> choice;
in.ignore(3, '\n');
return choice;
}
class Customer : public Person {
protected:
double shippingRate;
public:
Customer();
double getShippingRate();
void readPerson( istream & );
void writePerson( ostream & );
};
Customer::Customer() {
shippingRate = 5.0;
}
double Customer::getShippingRate() {
return shippingRate;
}
void Customer::readPerson( istream & in ) {
Person::readPerson(in);
in >> shippingRate;
cin.ignore(3, '\n');
}
void Customer::writePerson( ostream & out ) {
Person::writePerson(out);
out << shippingRate << endl;
}
int findPerson( Person pList[], string name ) {
int pos = -1, index = 0;
while( pos == -1 && index < PLIST_SIZE ) {
if( pList[index].getName() == name ) {
pos = index;
}
index++;
}
return pos;
}
int findCustomer( Customer pList[], string name ) {
int pos = -1, index = 0;
while( pos == -1 && index < PLIST_SIZE ) {
if( pList[index].getName() == name ) {
pos = index;
}
index++;
}
return pos;
}
int main() {
Person pList[PLIST_SIZE];
Customer cList[PLIST_SIZE];
string choice, pTypeChoice;
int index = 0;
bool fileRead = false;
ifstream pfile;
ofstream opfile;
cout << "Would you like to read the data from a file (yes/no)? " << endl;
cin >> choice;
cin.ignore(3, '\n');
if (choice == "yes") {
fileRead = true;
pfile.open("iperson.txt");
if (!pfile) {
cerr << "Can't open person.txt for read." << endl;
return -1;
}
pfile >> pTypeChoice;
pfile.ignore(3, '\n');
} else {
cout << "What type of Person? (person/customer)" << endl;
cin >> pTypeChoice;
cin.ignore(3, '\n');
}
if( pTypeChoice == "person" ) {
opfile.open("operson.txt");
choice = "yes";
if (fileRead) {
pList[index].readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
while (choice != "no" && index < PLIST_SIZE) {
if (!fileRead) {
requestInfo(cout, "Please enter your name followed by your age.");
pList[index].readPerson(cin);
}
pList[index].writePerson(opfile);
index++;
if (!fileRead) {
choice = readAgain(cout, cin);
} else {
pList[index].readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
}
if (fileRead) {
pfile.close();
}
opfile.close();
cout << "Which person are you looking for? " << endl;
getline(cin, choice);
if ((index = findPerson(pList, choice)) != -1) {
cout << "Found your person: " << endl;
pList[index].writePerson(cout);
} else {
cout << "Couldn't find your person: " << choice << endl;
}
} else {
opfile.open("ocustomer.txt");
choice = "yes";
if (fileRead) {
cList[index].readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
while (choice != "no" && index < PLIST_SIZE) {
if (!fileRead) {
requestInfo(cout, "Please enter your name followed by your age then shipping rate");
cList[index].readPerson(cin);
}
cList[index].writePerson(opfile);
index++;
if (!fileRead) {
choice = readAgain(cout, cin);
} else {
cList[index].readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
}
if (fileRead) {
pfile.close();
}
opfile.close();
cout << "Which person are you looking for? " << endl;
getline(cin, choice);
if ((index = findCustomer(cList, choice)) != -1) {
cout << "Found your person: " << endl;
cList[index].writePerson(cout);
} else {
cout << "Couldn't find your person: " << choice << endl;
}
}
return 0;
}
That worked well, but you have probably noticed that we had to deal with the two different classes separately. If we add
the idea of polymorphism in with our inheritance, we get this.
oopMain3.cpp:
const int PLIST_SIZE = 10;
class Person {
protected:
string name;
int age;
public:
string getName() { return name; }
virtual void readPerson(istream &);
virtual void writePerson(ostream &);
};
void Person::readPerson(istream &in) {
getline(in, name);
in >> age;
in.ignore(3, '\n');
}
void Person::writePerson(ostream &out) {
out << name << endl;
out << age << endl;
}
void requestInfo(ostream &out, string msg) {
out << msg << endl;
}
string readAgain(ostream &out, istream &in) {
string choice;
out << "Do you want to read in another person (yes/no)? " << endl;
in >> choice;
in.ignore(3, '\n');
return choice;
}
class Customer : public Person {
protected:
double shippingRate;
public:
Customer();
double getShippingRate();
void readPerson(istream &);
void writePerson(ostream &);
};
Customer::Customer() {
shippingRate = 5.0;
}
double Customer::getShippingRate() {
return shippingRate;
}
void Customer::readPerson(istream &in) {
Person::readPerson(in);
in >> shippingRate;
cin.ignore(3, '\n');
}
void Customer::writePerson(ostream &out) {
Person::writePerson(out);
out << shippingRate << endl;
}
int findPerson(Person *pList[], string name) {
int pos = -1, index = 0;
while (pos == -1 && index < PLIST_SIZE) {
if (pList[index]->getName() == name) {
pos = index;
}
index++;
}
return pos;
}
int main() {
Person **pList = new Person *[PLIST_SIZE];
string choice, pTypeChoice, msg = "Please enter your name followed by your age.";
int index = 0;
bool fileRead = false;
ifstream pfile;
ofstream opfile;
if (choice == "yes") {
fileRead = true;
pfile.open("iperson.txt");
if (!pfile) {
cerr << "Can't open person.txt for read." << endl;
return -1;
}
pfile >> pTypeChoice;
pfile.ignore(3, '\n');
} else {
cout << "What type of Person? (person/customer)" << endl;
cin >> pTypeChoice;
cin.ignore(3, '\n');
}
if (pTypeChoice == "person") {
for (int i = 0; i < PLIST_SIZE; i++) {
pList[i] = new Person;
}
} else {
for (int i = 0; i < PLIST_SIZE; i++) {
pList[i] = new Customer;
}
msg = "Please enter your name followed by your age then shipping rate.";
}
cout << "Would you like to read the data from a file (yes/no)? " << endl;
cin >> choice;
cin.ignore(3, '\n');
opfile.open("operson.txt");
choice = "yes";
if (fileRead) {
pList[index]->readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
while (choice != "no" && index < PLIST_SIZE) {
if (!fileRead) {
requestInfo(cout, msg);
pList[index]->readPerson(cin);
}
pList[index]->writePerson(opfile);
index++;
if (!fileRead) {
choice = readAgain(cout, cin);
} else {
pList[index]->readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
}
if (fileRead) {
pfile.close();
}
opfile.close();
cout << "Which person are you looking for? " << endl;
getline(cin, choice);
if ((index = findPerson(pList, choice)) != -1) {
cout << "Found your person: " << endl;
pList[index]->writePerson(cout);
} else {
cout << "Couldn't find your person: " << choice << endl;
}
return 0;
}
Our customer is extremely happy with our new version. He has now decided to introduce a special customer category.
The customer pays a flat yearly rate to receive free shipping. We’re going to name that new customer type
MegaCustomer. Here’s our addition of the MegaCustomer class.
oopMain4.cpp:
const int PLIST_SIZE = 10;
class Person {
protected:
string name;
int age;
public:
string getName() { return name; }
virtual void readPerson(istream &);
virtual void writePerson(ostream &);
};
void Person::readPerson(istream &in) {
getline(in, name);
in >> age;
in.ignore(3, '\n');
}
void Person::writePerson(ostream &out) {
out << name << endl;
out << age << endl;
}
void requestInfo(ostream &out, string msg) {
out << msg << endl;
}
string readAgain(ostream &out, istream &in) {
string choice;
out << "Do you want to read in another person (yes/no)? " << endl;
in >> choice;
in.ignore(3, '\n');
return choice;
}
class Customer : public Person {
protected:
double shippingRate;
public:
Customer();
double getShippingRate();
void readPerson( istream & );
void writePerson( ostream & );
};
Customer::Customer() {
shippingRate = 5.0;
}
double Customer::getShippingRate() {
return shippingRate;
}
void Customer::readPerson(istream &in) {
Person::readPerson(in);
in >> shippingRate;
cin.ignore(3, '\n');
}
void Customer::writePerson(ostream &out) {
Person::writePerson(out);
out << shippingRate << endl;
}
class MegaCustomer : public Customer {
public:
MegaCustomer();
void readPerson( istream & );
};
MegaCustomer::MegaCustomer() {
shippingRate = 0;
}
void MegaCustomer::readPerson( istream & in ) {
Person::readPerson(in);
}
int findPerson(Person *pList[], string name) {
int pos = -1, index = 0;
while (pos == -1 && index < PLIST_SIZE) {
if (pList[index]->getName() == name) {
pos = index;
}
index++;
}
return pos;
}
int main() {
Person **pList = new Person *[PLIST_SIZE];
string choice, pTypeChoice, msg = "Please enter your name followed by your age.";
int index = 0;
bool fileRead = false;
ifstream pfile;
ofstream opfile;
cout << "Would you like to read the data from a file (yes/no)? " << endl;
cin >> choice;
cin.ignore(3, '\n');
if (choice == "yes") {
fileRead = true;
pfile.open("iperson.txt");
if (!pfile) {
cerr << "Can't open person.txt for read." << endl;
return -1;
}
pfile >> pTypeChoice;
pfile.ignore(3, '\n');
} else {
cout << "What type of Person? (person/customer/mega)" << endl;
cin >> pTypeChoice;
cin.ignore(3, '\n');
}
if (pTypeChoice == "person") {
for (int i = 0; i < PLIST_SIZE; i++) {
pList[i] = new Person;
}
} else if(pTypeChoice == "customer"){
for (int i = 0; i < PLIST_SIZE; i++) {
pList[i] = new Customer;
}
msg = "Please enter your name followed by your age then shipping rate.";
} else {
for( int i = 0; i < PLIST_SIZE; i++ ) {
pList[i] = new MegaCustomer;
}
}
opfile.open("operson.txt");
choice = "yes";
if (fileRead) {
pList[index]->readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
while (choice != "no" && index < PLIST_SIZE) {
if (!fileRead) {
requestInfo(cout, msg);
pList[index]->readPerson(cin);
}
pList[index]->writePerson(opfile);
index++;
if (!fileRead) {
choice = readAgain(cout, cin);
} else {
pList[index]->readPerson(pfile);
if (!pfile) {
choice = "no";
}
}
}
if (fileRead) {
pfile.close();
}
opfile.close();
cout << "Which person are you looking for? " << endl;
getline(cin, choice);
if ((index = findPerson(pList, choice)) != -1) {
cout << "Found your person: " << endl;
pList[index]->writePerson(cout);
} else {
cout << "Couldn't find your person: " << choice << endl;
}
for( int i = 0; i < PLIST_SIZE; i++ ){
delete pList[i];
}
delete [] pList;
return 0;
}
Hopefully, you can see how inheritance paired with polymorphism made this last addition easy.
You need to write a version of the previous solution avoiding inheritance and polymorphism. Start with the
implementation provided in ooplMain1.cpp and implement the functionality found in oopMain4.cpp. Name your
source code oopMain5.cpp and turn it in on the upload site.

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468