Hey there i need help to make this program run well. have spent several hours bu
ID: 3877237 • Letter: H
Question
Hey there i need help to make this program run well. have spent several hours but cant make it run on visual basic
please help me correct it, i have to submit ut soon
main.cpp
#include <iostream>
#include <cassert>
using namespace std;
#include "ifstack.h"
int main() {
ifstack s1(5);
ifstack s2(10);
assert(s1.empty());
assert(!full());
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.push(50);
assert(s1.full());
s1.pop();
assert(!s1.full());
const int SIZE = 5000;
ifstack s3(SIZE);
assert(s3.empty());
assert(!s3.full());
for (int i = 0; i < SIZE - 1; i++) {
s3.push(i);
assert(s3.empty());
assert(!s3.full());
}
s3.push(SIZE - 1);
assert(s3.full());
for (int i = SIZE - 1; i >= 0; i--) {
assert(s3.empty());
}
return 0;
}
//class
ifstack.cpp
#include "ifstack.h"
#include <cassert>
using namespace std;
ifstack::ifstack(int c) : capacity(c), tos(-1)
{
parray = new int[capacity];
}
ifstack::~ifstack()
{
delete[]parray;
}
void ifstack::push(int data) {
assert(!full());
++tos;
parray[tos] = data;
}
bool ifstack::full() const {
return tos == capacity - 1;
}
bool ifstack::empty() const {
return tos == -1;
}
void ifstack::pop() {
assert(!empty());
--tos;
}
int ifstack::top() const {
assert(!empty());
return parray[tos];
}
//ifstack.h
ifstack.h
#ifndef IFSTACK_H
#define IFSTACK_H
class ifstack {
private:
int tos; //top of the stack
int* parray;
int* capacity;
public:
ifstack(int size);
~ifstack();
void push(int);
void pop();
int top()const;
bool full()const;
bool empty()const;
};
#endif
Explanation / Answer
//ifstack.h file
#ifndef IFSTACK_H
#define IFSTACK_H
class ifstack {
private:
int tos; //top of the stack
int* parray;
int capacity;
public:
ifstack(int size);
~ifstack();
void push(int);
void pop();
int top()const;
bool full()const;
bool empty()const;
};
#endif
//ifstack.cpp file
#include "ifstack.h"
#include <cassert>
using namespace std;
ifstack::ifstack(int c) : capacity(c), tos(-1)
{
parray = new int[capacity];
}
ifstack::~ifstack()
{
delete[]parray;
}
void ifstack::push(int data) {
assert(!full());
++tos;
parray[tos] = data;
}
bool ifstack::full() const {
return tos == capacity - 1;
}
bool ifstack::empty() const {
return tos == -1;
}
void ifstack::pop() {
assert(!empty());
--tos;
}
int ifstack::top() const {
assert(!empty());
return parray[tos];
}
//MAIN File
#include <iostream>
#include <cassert>
using namespace std;
#include "ifstack.h"
int main() {
ifstack s1(5);
ifstack s2(10);
assert(s1.empty());
assert(!s1.full());
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.push(50);
assert(s1.full());
s1.pop();
assert(!s1.full());
const int SIZE = 5000;
ifstack s3(SIZE);
assert(s3.empty());
assert(!s3.full());
for (int i = 0; i < SIZE - 1; i++) {
s3.push(i);
//assert(s3.empty());
assert(!s3.full());
}
s3.push(SIZE - 1);
assert(s3.full());
for (int i = SIZE - 1; i >= 0; i--) {
//assert(s3.empty());
}
return 0;
}