Question: I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is

I need help with the code below. It is a C program, NOT C++.

It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help.

//

// main.c

// float_stack_class_c_9_29

//

//

/* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C).

*/

#include

#include

#include

#include

header file to read and print the output on console

using namespace std; // Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially

//when your code base includes multiple libraries.

class float_stack

{

public:

float_stack();

size_t size() const;

bool empty() const;

float peek() const;

void pop();

void push(float value);

friend std::ostream& operator<<(std::ostream& os, const float_stack& st);

private:

void check_empty() const; // exit if true (throw exception in C++)

void check_overflow() const; // ditto

private:

static const size_t maxStack_ = 30;

size_t size_;

float data_[maxStack_];

};

float_stack::float_stack()

{

size_ = 0;

}

size_t float_stack::size() const

{

return size_;

}

bool float_stack::empty() const

{

return size_ == 0;

}

float float_stack::peek() const

{

return data_[size_-1];

}

void float_stack::pop()

{

size_--;

}

void float_stack::push(float value)

{

if(size_ < maxStack_)

{

data_[size_] = value;

size_++;

}

}

std::ostream& operator<<(std::ostream& os, const float_stack& st)

{

size_t size = st.size();

for(int i = size-1; i >= 0; i--)

os << st.data_[i] << " ";

os << endl;

return os;

}

void float_stack::check_empty() const // exit if true (throw exception in C++)

{

if(empty())

exit(1);

}

void float_stack::check_overflow() const // ditto

{

if(size_ == maxStack_)

exit(1);

}

int main(int argc, const char * argv[])

{

float_stack stack;

stack.push(23.2);

stack.push(3.12);

stack.push(54.3);

stack.push(98.0);

cout<

stack.pop();

cout<

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!