Question: C++ Create Ingredient.cpp and Ingredient.h so that the client code complies and produces the output CalorieCounter.cpp #include #include #include #include Ingredient.h using namespace std; const

C++ Create Ingredient.cpp and Ingredient.h so that the client code complies and produces the output

CalorieCounter.cpp

#include

#include

#include

#include "Ingredient.h"

using namespace std;

const int MAX_NUM_INGREDIENTS = 100; // maximum number of ingredients

const string DEFAULT_INPUT_FILENAME = "drCsSpecialBreadIngredients.tab";

int main( ){

ifstream dataFS; // input file stream

Ingredient ingredients[ MAX_NUM_INGREDIENTS ]; // array of all ingredients read in from the file

int numberOfIngredients = 0; // counter

string name; // temp variable

int calories, grams; // temp variable

int totalCalories = 0, totalGrams = 0; // aggregates

// If just a return (' ') is entered, then use DEFAULT_INPUT_FILENAME.

// Otherwise, read in the input filename from STDIN.

string inputFilename;

cout << "Please enter the input filename (or simply press return to use " << DEFAULT_INPUT_FILENAME << ") ";

getline( cin, inputFilename);

if( inputFilename == ""){

inputFilename = DEFAULT_INPUT_FILENAME;

}

// open file specified on the command-line

dataFS.open( inputFilename.c_str() );

// verify that the file opened successfully

if( ! dataFS){

cerr << "Error opening \"" << inputFilename << "\"! ";

return 1;

}

// read in data from the file

cout << "Reading ingredients from " << inputFilename << " . . . ";

numberOfIngredients = 0;

while( dataFS >> name >> calories >> grams ){

ingredients[ numberOfIngredients ].setName( name );

ingredients[ numberOfIngredients ].setCalories( calories );

ingredients[ numberOfIngredients ].setGrams( grams );

totalCalories += calories;

totalGrams += grams;

numberOfIngredients++;

}

dataFS.close();

for( int i = 0; i < numberOfIngredients; i++){

ingredients[ i ].print();

}

Ingredients dish( "Total", totalCalories, totalGrams );

dish.print();

return 0;

}

Input File:

honey 683 227

oil 964 99

salt 0 26

water 0 1419

wheat 7862 2160

yeast 117 26

OUTPUT

Reading ingredients from drCsSpecialBreadIngredients.tab . . .

honey has 683 calories and weighs 227 grams

oil has 964 calories and weighs 99 grams

salt has 0 calories and weighs 26 grams

water has 0 calories and weighs 1419 grams

wheat has 7862 calories and weighs 2160 grams

yeast has 117 calories and weighs 26 grams

Total has 9626 calories and weighs 3957 grams

Problem set number 2.

The DynamicList class is a traditional pointer-based linked list implementation. Change the implementation to a doubly-linked list (next and prev pointers stored in every node) and add a copy constructor.

DynamicList.h

#ifndef DYNAMIC_LIST_H

#define DYNAMIC_LIST_H

typedef int ItemType;

class DynamicList{

public:

// Constructor

// Post: Empty list has been created

DynamicList();

/*

Action respnsibilities

*/

// Pre: List is not full and item is not in the list

// Post: item is in the list and length has been incremented

void insert( ItemType item);

// Post: item is not in the list

bool remove( ItemType item);

/*

Knowledge responsibilities

*/

// Post: Returns the length of the list

int getLength() const;

// Post: Returns true if list is empty; false otherwise

bool isEmpty() const;

// Post: Each item is printed

void printAll() const;

private:

// node for a linked list

struct Node{

ItemType data;

Node* next;

};

/*

data members

*/

int length;

Node* head;

};

#endif /* DYNAMIC_LIST_H */

DynamicList.cpp

#include

#include "DynamicList.h"

using namespace std;

// Constructor

DynamicList::DynamicList()

: length(0), head( NULL) {

}

/*

Action respnsibilities

*/

// Pre: The item is not in the list

// Post: Item is in the list and the list is sorted least to greatest value

void DynamicList::insert(ItemType item){

/* Find the location to insert item.

* Look for item in the list, or at least while items are less then item.

* curr is set to the node after the insertion point (potentially being NULL

* for the end of the list). prev is set to the node preceeding the

* insertion point. */

Node* newNode = new Node;

newNode->data = item;

newNode->next = NULL; // just in case

Node* prev = NULL;

Node* curr = head;

while( curr != NULL && item > curr->data ){

prev = curr;

curr = curr->next;

}

// Scenarios:

// + Empty list (head == NULL): set head to newNode

// + Insert into first position: set head to newNode, set the newNode's next

// + Insert into middle position (not first nor last): general case

// + Insert into last position: same as general case

if( head == NULL){

// empty list

head = newNode;

}else if( prev == NULL){

// insert into first position

newNode->next = curr;

head = newNode;

}else{

// general case

newNode->next = curr;

prev->next = newNode;

}

}

// Pre: None

// Post: Item is not in the list

bool DynamicList::remove(ItemType item){

/* Try to find item in the list. If item is found, the curr pointer is at the item to remove and the

prev pointer is pointing to the item preceeding it */

Node* prev = NULL;

Node* curr = head;

// Scenarios:

// + Empty list (head == NULL): return false

// + Single item list: same as first (which will set head to NULL)

// + First (of many) item: set head to whatever (if anything) the first item is pointing to

// + Middle (not first nor last) item: general case

// + Last (of many) item: same as general case

if( head == NULL){

// empty list

return false;

}

// look for the item

while( curr != NULL && item > curr->data ){

prev = curr;

curr = curr->next;

}

// Either

// 1) curr is pointing to the item

// 2) item was not found and curr is pointing to a valid entry

// or

// 3) item was not found and curr is NULL because we looked at everything in

// the list

if( curr == NULL ){

// item not found (exhausted the list)

return false;

}else if( item < curr->data ){

// item not found

return false;

}

/* remove item

* Remove node from the list by making the preceeding node point past

* the curr node */

if( prev == NULL){

// first item in the list

head = curr->next;

}else{

// general case

prev->next = curr->next;

}

delete curr;

curr = NULL;

length--;

return true;

}

/*

Knowledge responsibilities

*/

// Post: Returns the length of the list

int DynamicList::getLength() const {

return length;

}

// Post: Returns true if list is empty; false otherwise

bool DynamicList::isEmpty() const {

return (length == 0);

}

void DynamicList::printAll() const {

cout << "List contents: ";

Node* curr = head;

while( curr != NULL ){

cout << curr->data << endl;

curr = curr->next;

}

cout << endl;

}

DynamicList-client.cpp

#include

#include "DynamicList.h"

using namespace std;

void add( DynamicList& numbers, int num ){

cout << "Adding " << num << " ";

numbers.insert( num );

numbers.printAll();

}

void remove( DynamicList& numbers, int num ){

cout << "Trying to remove " << num << " ";

numbers.remove( num );

numbers.printAll();

}

int main(){

// Instantiate a DynamicList object

DynamicList numbers;

/*

add numbers

*/

add( numbers, 3 );

add( numbers, 42 );

add( numbers, 7 );

DynamicList numbersCopy( numbers ); // Note: calls copy constructor

/*

remove numbers

*/

remove( numbersCopy, 1 ); // never was in the list

remove( numbersCopy, 7 ); // the middle number

remove( numbersCopy, 7 ); // not in the list anymore

remove( numbersCopy, 42 ); // last number

remove( numbersCopy, 3 ); // first, now only number

remove( numbersCopy, -1 ); // never was in the list

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!