//stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer {
char fullname[35];
double payment;
};
typedef customer Item;
class Stack {
private:
static double total;
enum{ MAX = 5 };
Item items[MAX];
int top;
public:
Stack();
Stack(const char**,double arr[],int value);
~Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item& item);
bool pop(Item& item);
};
#endif // !STACK_H_
//stack.cpp
#include"stack.h"
#include<iostream>
double Stack::total = 0.0;
Stack::Stack()
{
total = 0.0;
top = 0;
for (int i = 0; i < MAX; i++)
{
//strcpy(items[i].fullname,name[i]);
strcpy(items[i].fullname, "\0");
items[i].payment = i * 3.14;
}
}
Stack::Stack(const char** str,double arr[],int value) // value == lesser num elements in str and arr
{
top = value;
for (int i = 0; i < MAX; i++)
{
items[i].payment = arr[i];
strcpy(items[i].fullname,*(str+i));
}
}
Stack::~Stack()
{}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push(const Item& item)
{
if (!isfull())
{
items[top++] = item; //top , above the toppest data
return true;
}
else
return false;
}
bool Stack::pop(Item& item)
{
if (!isempty())
{
item = items[--top];
total = total + item.payment;
return true;
}
else
return false;
}
//stack_main.cpp
#include<iostream>
#include"stack.h"
int main()
{
const char* name[] = { "hello","hi","how are u","good by","thank u" };
double arr[]{ 2.3,23.4,74.7,25.8,3.53, };
Item one = {"hahahah",23.32};
Item temp;
const char** ppt = name;
Stack obj1;
Stack obj2(ppt,arr,5);
if (!obj1.isfull())
{
std::cout << "obj1 is full";
obj1.pop(one);
std::cout << one.fullname << " " << one.payment << std::endl;
}
std::cout << "next obj2\n";
for (int i = 0; i < 5; i++)
{
if (!obj2.isempty())
{
obj2.pop(one);
std::cout << one.fullname << " " << one.payment << std::endl;
}
if (!obj1.isfull())
{
obj1.push(one);
}
}
std::cout << "done\n";
return 0;
}