What do you think?
My views and discoveries in the technology world, my experiences and some programming codes. Besides, I share some inspiration flavored with humor. I also share random thoughts.
Wednesday, 22 May 2013
Tuesday, 14 May 2013
C++ Stack - Part 3
Below is the manipulation of the objects created with the stack data type inside the main function. This final C++ stack source code is used in collaboration with part 1 and part 2.
#include<iostream>
#include "part1_filename.h"
using namespace std;
int main()
{
int choice;
stack Abacus;
int element;
while(1)
{
cout <<"Select a number for an action you want to perform";
cout<<endl;
cout<<endl;
cout <<"1 - Insert an element into the stack";
cout<<endl;
cout<<"2 - Remove an element from the stack";
cout<<endl;
cout<<"3 - View the elements of the stack";
cout<<endl;
cout<<"4 - See the size of the stack";
cout<<endl;
cout<<"5 - End";
cout<<endl;
cout<<endl;
cout <<"Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1: cout <<"Input a number: ";
cin >> element;
cout <<"\n";
Abacus.Push(element);
break;
case 2:
Abacus.Pop();
break;
case 3:
Abacus.Print();
break;
case 4:
Abacus.getSize();
break;
case 5: exit(0);
}
}
system("pause");
return 0;
}
The code and its associates are fully functional. There are a few changes to be made and the code can be run on any c++ compiler.
Feel free to suggest some changes on the source codes.
C++ Stack - Part 2
The stack's function definitions and implementations. To view the stack interface go to part 1.
#include<iostream>
#include "part1_filename.h"
using namespace std;
stack::stack()
{
top = -1;
}
stack::~stack(){ }
bool stack::Push(int x)
{
if(IsFull())
{
cout <<"The stack is full!"<<endl;
return 0;
}
arrayStack[++top] = x;
}
bool stack::Pop()
{
if( IsEmpty() )
{
cout <<"The stack is empty";
cout<<endl;
return 0;
}
else
cout<<arrayStack[top--]<<" has been removed from the stack"<<endl;
}
bool stack::IsEmpty()
{
return (top == -1);
}
bool stack::IsFull()
{
return (top > 8);
}
void stack::Print()
{
for(int z=top; z>=0; z--){
cout <<arrayStack[z] <<" ";
}
cout<<endl;
}
int stack::getSize()
{
top = top + 1;
cout << top;
}
Save this block of code in the same folder as the header file in part one. Part three will cover the main function.
C++ Stack - Part 1
Part one covers only the class interface of the stack. Basically, a stack is a data type which follows Last-In-First-Out rule.
#ifndef STACK_H
#define STACK_H
using namespace std;
class stack
{
public:
stack(); // Constructor
~stack(); // Destructor
bool Push(int x); // A Function that inserts an element into the stack
bool Pop(); // A Function that deletes an element from the stack
bool IsEmpty(); // A function that determines if the stack is empty
bool IsFull(); // A function that determines if the stack is full
void Print(); // A Function that prints all elements in the stack
int getSize(); // A function that returns the number of elements in the stack
private:
int top; // A variable that indicates the position of the last element
int arrayStack[10]; // An array that contains the elements of the stack
};
#endif
Your file name is very important here, because in Part two, which will be the function definitions and implementation will be used.
Saturday, 4 May 2013
9 Lies and The Truth
1. I’m too busy
2. Everybody hates me
3. I tried over and over and over again
4. I’m not meant for this
5. I’m such a fool
6. I've already failed
7. I’m not a genius
8. I’m too young
9. It has always been this way
1. You’re fearfully and wonderfully made. Stop.
Read that again.
Subscribe to:
Posts (Atom)