Hey guys I am trying to do the following.
2. Create a structure called ListNode that can hold an object of the car class as its value.
3. Create a Linked List to hold the following cars in inventory. Then use a loop to print all of the information about them for the user.
Make Model Color Year Mileage
1) Porsche 911 Silver 2005 45000
2) Ford Mustang Red 2007 12600
3) Voltzwagon Jetta Black 2006 20218
4) Jeep Cherokee White 2000 98322
5) Nisson Sentra red 2002 76046
6) Voltzwagon Beetle Black 2005 28031
4. Change the Jeep Cherokee’s year to be 2001. Change the Sentra’s mileage to be 80000. Use the loop to print out the information for the user.
5. You need to be able to see the average miles for all cars in inventory. Write the code to find the average miles and display it for the user by adding up the miles for all of the cars in inventory and dividing by the size of the Linked List holding inventory. Make sure to test if the Linked List holding the inventory has nothing in it yet to prevent a divide by zero problem!
6. Add the following cars to inventory. Then use a loop to print all of the information about them for the user.
Make Model Color Year Mileage
1) Chevrolet Corvette Black 2003 11903
6) Ford Explorer Grey 2004 73922
7) Honda Civic White 2007 12045
7. Delete the following car out of inventory. Then use a loop to print all of the information about them for the user.
Voltzwagon Jetta Black 2006 20218
(Hint: This is not the last car! You will have to delete this node and set the previous node’s pointer to reference the next object in the list.)
Does anyone know how i can do this with the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
//Car Class
class Car
{
protected:
string make; //make
string model; // model
string color; // color
int year; // year
int mileage; // miles on car
public:
//Constructor that will set information for a new car
void New_vehicle (string a, string b, string c, int d, int e)
{make = a; model = b; color = c; year = d; mileage = e;}
Car(); //Default constructor
Car(string, string, string, int, int);
//mutator and accessor functions
void setMake(string);
void setModel(string);
void setColor(string);
void setYear(int);
void setMileage(int);
string getMake();
string getModel();
string getColor();
int getYear();
int getMileage();
//Check mileage to see if valid
void valid_mileage(int);
void car_details();
string string_car_details();
};
//Sets to default values
Car::Car() {
make = " ";
model = " ";
color = " ";
year = 0;
mileage = 0;
}
// My Vehicle set up(Make, model, color, year, mileage)
Car::Car(string make, string model, string color, int year, int mileage) {
this->make = make;
this->model = model;
this->color = color;
this->year = year;
valid_mileage(mileage);
}
void Car::setMake(string make) {
Car::make = make;
}
void Car::setModel(string model) {
Car::model = model;
}
void Car::setColor(string color) {
Car::color = color;
}
void Car::setYear(int year) {
Car::year = year;
}
void Car::setMileage(int mileage) {
valid_mileage(mileage);
}
string Car::getMake() {
return make;
}
string Car::getModel() {
return model;
}
string Car::getColor() {
return color;
}
int Car::getYear() {
return year;
}
int Car::getMileage() {
return mileage;
}
void Car::valid_mileage(int mileage) {
if (mileage>=0)
Car::mileage=mileage;
else {
Car::mileage=0;
cout << "WARNING! You have entered invalid mileage!\n";
}
}
void Car::car_details() {
cout << "The current car is a " << year << ' ' << color << ' '
<< make << ' ' << model << " with " << mileage << " miles.\n\n";
}
string Car::string_car_details() {
stringstream buf;
buf << "The current car is a " << year << ' ' << color << ' '
<< make << ' ' << model << " with " << mileage << " miles.\n\n";
return buf.str();
}
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include "CarClass.h"
using namespace std;
int main() {
//Array of 6 cars
Car Car_array[SIZE] = { Car("Porsche", "911", "Silver", 2005, 45000),
Car("Ford", "Mustang", "Red", 2007, 12600),
Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
Car("Jeep", "Cherokee", "White", 2000, 98322),
Car("Nissan", "Sentra", "Red", 2002, 76046),
Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};
| |