The 2nd line is an integer stating the number of dimensions, followed by 2 strings: a sender, and receiver. Then, according to the number of dimensions of the package, the respective measurements of each dimension of the package. and finally, for 3D and 4D packages, a weight measure.
Example of input and output:
Input:
4
2
Zapp
Leela
10 20
3
Dr.Zoidberg
Mom
14 28 30 18
4
Proff.Fansworth
Gunter
18 16 18 20 5
2
Fry
Nibbler
5 5
Output:
#0: From Zapp to Leela
$10.00
#1: From Dr.Zoidberg to Mom
$273.90
#2: From Proff.Fansworth to Gunter
$1094.92
#3: From Fry to Nibbler
$1.25
TOTAL PROFIT = $1380.07
AVERAGE = $345.01
We need to use a dynamically allocated data structure to hold the packages and I chose a Linked List. However, I am having trouble with inserting objects of a different type into a linked list. Here is my code so far:
/******************************************************
Author:
Class: Data Structures
Date:
Description: This codes reads package descriptions
such as how many of what type (2D,3D, or 4D), who
sent them and to whom they are going. It also outputs
the charge, tallies a total, and calculates an average.
*******************************************************/
#include <iostream>
#include <string>
using namespace std;
//-------------------------------------------
class Package
{
protected:
int length, width; // All packages
string sender, receiver; // have these variables
Package* next; // points to next package of list
float charge; //
public:
virtual ~Package(){}
//Default constructor not sure if done right
Package() : sender(""), receiver(""), length(0), width(0), next(NULL){}
//Auxiliary constructor to be used for inserting objects
Package(string s, string r, int l, int w, Package* p) : sender(s),receiver(r),length(l),width(w),next(p){ }
//These were supposed to insert the package type into the list of packages
//I'm not sure how to create a link list of different objects
virtual void insert_two(string s, string r, int l, int w){}
virtual void insert_three(string s, string r, int l, int w, int h, int m){}
virtual void insert_four(string s, string r, int l, int w, int h, int d, int m){}
//Gets the sender and receiver data in a package
virtual string getsender(){return sender;}
virtual string getreceiver(){return receiver;}
//Used to get the charge for each package type in the list
virtual float getcharge(){return charge;}
//supposed to be used to help locate packages with getAtPackage
Package* getFirstPackage();
//Used to locate packages in linked list
Package* getAtPackage(int i);
};
Package* Package::getFirstPackage()
{
Package* tmp;
tmp = new Package(sender,receiver,length,width,next);
return tmp;
}
Package* Package::getAtPackage(int i)
{
if(i == 0)
return getFirstPackage();
if(i == 1)
return next;
Package* p = next;
int counter = 1;
while (p->next->next != NULL && counter < i)
{
counter++;
p = p->next;
}
return p;
}
//-------------------------------------------
class two_D : public Package
{
protected:
int area;
public:
two_D(string s, string r, int l, int w, Package* p) : Package(s,r,l,w,p){}
void insert_two(string s, string r, int l, int w)
{
two_D* tmp;
tmp = new two_D(s,r,l,w,next);
next = tmp;
}
float getCharge()
{
area = length * width;
charge = area * .05;
return charge;
}
};
//-------------------------------------------
class three_D : public two_D
{
protected:
int height, volume, weight;
public:
three_D(string s, string r, int l, int w, Package* p, int h, int m) : two_D(s,r,l,w,p)
{
height = h;
weight = m;
}
void insert_three(string s, string r, int l, int w, int h, int m)
{
Package* tmp;
tmp = new three_D(s,r,l,w,next,h,m);
next = tmp;
}
float getCharge()
{
volume = area * height;
charge = (volume*.02)+(weight*2.15);
return charge;
}
};
//-------------------------------------------
class four_D : public three_D
{
private:
int depth, h_volume;
public:
four_D(string s, string r, int l, int w, Package* p, int h, int m, int d) : three_D(s,r,l,w,p,h,m)
{
depth = d;
}
void insert_four(string s, string r, int l, int w, int h, int d, int m)
{
Package* tmp;
tmp = new four_D(s,r,l,w,next,h,m,d);
next = tmp;
}
float getCharge()
{
h_volume = volume * depth;
charge = (h_volume*.01)+(weight*3.14)+42.42;
return charge;
}
};
/*********************************************************************
Inputs for main are designed to be read directly from a file.
I want the program to create a new empty package list and then
read the first input (number of packages).
Then a loop is created and runs for the number of packages there are.
The first part reads the type,sender,receiver,length, and width
since all packages have these values.
Next an if else series of statements is used to determine what to do
if a package is of a certain type.
If the type is 2 (2D) I wanted it to insert a package of type 2D
into the package list.
If it is 3 (3D) it reads in the height and weight and creates
a new package in the package list of type 3D.
If its is 4 (4D) it reads in height,depth, and weight and creates
a new package in the package list of type 4D.
However, I dont know create a list of different objects and so
can't properly construct a list of the packages.
******************************************************************/
int main()
{
Package package_list; //Should be new list that all packages will be stored in
Package* p;
//declares all the variables that are used to describe packages
int number,type,length,width,height,depth,weight, totalprofit = 0, average;
int counter = 0;
string sender, receiver;
cin>>number;
while ( counter < number )
{
cin>>type,sender,receiver,length,width;
if (type == 2)
package_list.insert_two(sender,receiver,length,width);
else
{
cin>>height;
if (type == 3)
{
cin>>weight;
package_list.insert_three(sender,receiver,length,width,height,weight);
}
else
{
cin>>depth,weight;
package_list.insert_four(sender,receiver,length,width,height,depth,weight);
}
}
counter++;
}
counter = 0;
//This while loop is supposed to get the the package at the first position of the linked package list
//It is supposed to print out the position and then get the sender and receiver data from that list position
//It repeats this for all the packages using the first input (number).
while ( counter < number)
{
p = package_list.getAtPackage(counter);
cout<<"#"<<counter<<":"<<" From "<<p->getsender()<<" to "<<p->getreceiver()<<endl;
cout<<"$ "<<p->getcharge()<<endl;
totalprofit = totalprofit + p->getcharge();//adds the charge from each package to total profit, not sure if you can use p->get charge() directly in math operations.
counter++;
}
cout<<"Total Profit = "<<totalprofit<<endl;
cout<<"Average = "<<totalprofit/number<<endl;
return 0;
}

New Topic/Question
Reply



MultiQuote




|