Question

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

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In C language, the concept of struct is equvivalent to struct. Below is the code for the same.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// structure representing stack
struct Stack
{
int top;
unsigned size;
int* arr;
};

// creating stack
struct Stack* createStack(unsigned size)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->size = size;
stack->top = -1;
stack->arr = (int*) malloc(stack->size * sizeof(int));
return stack;
}

// Stack overflow condition
int IsFull(struct Stack* stack)
{ return stack->top == stack->size - 1; }

//Underflow of stack
int IsEmpty(struct Stack* stack)
{ return stack->top == -1; }

// to add an element to stack
void Push(struct Stack* stack, float ele)
{
if (IsFull(stack))
return;
stack->arr[++stack->top] = ele;
printf("%f pushed to stack\n", ele);
}

// Function to remove an item from stack. It decreases top by 1
float Pop(struct Stack* stack)
{
if (IsEmpty(stack))
return INT_MIN;
return stack->arr[stack->top--];
}
// Driver program to test above functions
int main()
{
struct Stack* stack = createStack(100);

Push(stack, 10.10);
Push(stack, 20.38);
Push(stack, 30.45);

printf("%f popped from stack\n", Pop(stack));

return 0;
}C 1 Need Help With The Cx C++Shell C cpp.sh C++ shel cpp.sh anline C++ compiler bout cpp.sh 12 13 creating stack 14 struct St

Add a comment
Know the answer?
Add Answer to:
I need help with the code below. It is a C program, NOT C++. It can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT