In: Computer Science
Programming Assignment 1: Representing, Managing and Manipulating Travel Options
You may NOT do any of the following:
change any of the function names or signatures (parameter lists and types and return type)
introduce any global or static variables
use any arrays or vectors inside the TravelOptions class! Not for this assignment!! (You may use them as you see fit in any driver/tester programs you write)
You MAY do any of the following as you see fit:
Introduce helper functions. But you should make them private.
Reminders:
Some functions eliminate list entries (e.g., prune_sorted). Don't
forget to deallocate the associated nodes (using the delete
operator).
Please read the below code and complete the missing parts.
#ifndef _TRVL_OPTNS_H
#define _TRVL_OPTNS_H
#include <iostream>
#include <vector>
#include <utility>
// using namespace std;
class TravelOptions{
public:
enum Relationship { better, worse, equal, incomparable};
private:
struct Node {
double price;
double time;
Node *next;
Node(double _price=0, double _time=0, Node* _next=nullptr){
price = _price; time = _time; next = _next;
}
};
/* TravelOptions private data members */
Node *front; // pointer for first node in linked list (or null if list is empty)
int _size;
public:
// constructors
TravelOptions() {
front = nullptr;
_size=0;
}
~TravelOptions( ) {
clear();
}
/**
* func: clear
* desc: Deletes all Nodes currently in the list
* status: DONE
*/
void clear(){
Node *p, *pnxt;
p = front;
while(p != nullptr) {
pnxt = p->next;
delete p;
p = pnxt;
}
_size = 0;
front = nullptr;
}
/**
* func: size
* desc: returns the number of elements in the list
* status: DONE
*/
int size( ) const {
return _size;
}
/**
* func: compare
* desc: compares option A (priceA, timeA) with option B (priceB, timeA) and
* returns result (see enum Relationship above):
*
* There are four possible scenarios:
* - A and B are identical: option A and option B have identical price and time:
* ACTION: return equal
* - A is better than B: option A and B are NOT equal/identical AND
* option A is no more expensive than option B AND
* option A is no slower than option B
* ACTION: return better
* - A is worse than B: option A and B are NOT equal/identical AND
* option A is at least as expensive as option B AND
* option A is no faster than option B
* ACTION: return worse
* NOTE: this means B is better than A
* - A and B are incomparable: everything else: one option is cheaper and
* the other is faster.
* ACTION: return incomparable
*
* COMMENTS: since this is a static function, there is no calling object.
* To call it from a client program, you would do something like this:
*
TravelOptions::Relationship r;
double pa, ta, pb, tb;
// some code to set the four price/time variables
r = TravelOptions::compare(pa, ta, pb, tb);
if(r == TravelOptions::better)
std::cout << "looks like option b is useless!" << std::endl;
// etcetera
*
* status: TODO
*/
static Relationship compare(double priceA, double timeA,
double priceB, double timeB) {
return incomparable; // placeholder
}
#ifndef TRVL_OPTNS_H_
#define TRVL_OPTNS_H_
#include <iostream>
#include <vector>
#include <utility>
#include <math.h>
// using namespace std;
class TravelOptions{
public:
enum Relationship { better, worse, equal, incomparable};
private:
struct Node {
double price;
double time;
Node *next;
Node(double _price=0, double _time=0, Node* _next=nullptr){
price = _price; time = _time; next = _next;
}
};
/* TravelOptions private data members */
Node *front; // pointer for first node in linked list (or null if list is empty)
int _size;
public:
// constructors
TravelOptions() {
front = nullptr;
_size=0;
}
~TravelOptions( ) {
clear();
}
/**
* func: clear
* desc: Deletes all Nodes currently in the list
* status: DONE
*/
void clear(){
Node *p, *pnxt;
p = front;
while(p != nullptr) {
pnxt = p->next;
delete p;
p = pnxt;
}
_size = 0;
front = nullptr;
}
/**
* func: size
* desc: returns the number of elements in the list
* status: DONE
*/
int size( ) const {
return _size;
}
/**
* func: compare
* desc: compares option A (priceA, timeA) with option B (priceB, timeA) and
* returns result (see enum Relationship above):
*
* There are four possible scenarios:
* - A and B are identical: option A and option B have identical price and time:
* ACTION: return equal
* - A is better than B: option A and B are NOT equal/identical AND
* option A is no more expensive than option B AND
* option A is no slower than option B
* ACTION: return better
* - A is worse than B: option A and B are NOT equal/identical AND
* option A is at least as expensive as option B AND
* option A is no faster than option B
* ACTION: return worse
* NOTE: this means B is better than A
* - A and B are incomparable: everything else: one option is cheaper and
* the other is faster.
* ACTION: return incomparable
*
* COMMENTS: since this is a static function, there is no calling object.
* To call it from a client program, you would do something like this:
*
TravelOptions::Relationship r;
double pa, ta, pb, tb;
// some code to set the four price/time variables
r = TravelOptions::compare(pa, ta, pb, tb);
if(r == TravelOptions::better)
std::cout << "looks like option b is useless!" << std::endl;
// etcetera
*
* status: TODO
*/
static Relationship compare(double priceA, double timeA,
double priceB, double timeB) {
double priceDiff = priceA - priceB;
double timeDiff = timeA - timeB ;
if(priceDiff < 0) // priceA < priceB
{
if(fabs(timeDiff) <= 0.001) //timeA <= timeB
return better;
else // timeA > timeB
return incomparable;
}else if(fabs(priceDiff) <= 0.001 ) //priceA = priceB
{
if(timeDiff < 0) // timeA < timeB
return better;
else if(fabs(timeDiff) <= 0.001) // timeA = timeB
return equal;
else //timeA > timeB
return worse;
}else // priceA > priceB
{
if(fabs(timeDiff) <= 0.001) // timeA < timeB
return incomparable;
else
return worse;
}
}
};
#endif /* TRVL_OPTNS_H_ */