Hello all, I am praying that I am now on the right track. I've read all of your postings (which have been extremely helpful) and read more into stacks over the past few days and modified my code. I wrote a simple stack and got it to work. My problem is that now I tried to combine it with my other functions but am getting errors. As long as someone can confirm that i am on the right track...then that will be good enough news for me. I will modify the main a little bit more but I am having problems on making sure that the stack takes the data from my primefactor() so that I can pop all of the factors to reverse the output.
Thanks in advance
Code:
#include <iostream.h>
const int STACK_MAX= 20;
typedef int StackElement;
class Stack
{
private:
StackElement StackArray[STACK_MAX];
int StackTop;
public:
bool empty();
int primefactor(int number);
bool isprime();
bool full( );
bool isprime(int number);
void push(int);
void pop();
Stack::Stack() //Constructor
{
StackTop=-1;
}
inline bool Stack::empty() const //Check for emptyness
{
return(StackTop==-1);
}
Stack::primefactor(int number)
{
bool isPrime = isprime();
int prime = number;
int i = 2, j;
double squareroot = sqrt(static_cast<double>(number));
int count = 0;
cout << "The prime factorization of the number " << number << " is:" << endl;
if(isPrime)
cout << space << number << " is a prime number." << endl;
else {
while((prime > 0) && (i <= number)) {
if((prime % i) == 0) {
count++;
for(j = 0; j < count; j++)
cout << space;
cout << i << " is a factor" << endl;
prime /= i;
} else
i++;
}
return true;
}
bool isprime(int number)
{
int i;
for(i = 2; i < number; i++) {
if((number % i) == 0)
return false;
}
void Stack::push(const StackElement & value) //Add value to the Stack
{
if(StackTop<STACK_MAX-1) //If Stack is not full add element
{
++StackTop;
StackArray[StackTop]=value;
}
else
cout<<"Stack is full. \n";
}
StackElement Stack::top()const //function to retireve value at top of Stack
{
if(StackTop>=0) //If Stack is not empty perform task
return StackArray[StackTop];
else
cout<<"Stack is empty\n";
}
void Stack::pop() //Function to discard value at top of Stack
{
if(StackTop>=0) //If Stack is not empty perform task
StackTop--;
else
cout<<"Stack is empty\n";
}
void Stack::display() //Function to write the entire stack
{
for(int i=StackTop;i>=0;i--)
cout<<StackArray[i]<<endl;
}
int main()
{
Stack factors;
int number;
cout << "Enter a number > 1000 ";
cin >> number;
bool isPrime = isprime();
int prime = number;
int i = 2, j;
double squareroot = sqrt(static_cast<double>(number));
int count = 0;
cout << "The prime factorization of the number " << number << " is:" << endl;
if(isPrime) //should this be factors.isPrime()
cout << space << number << " is a prime number." << endl;
else {
while((prime > 0) && (i <= number)) {
if((prime % i) == 0) {
count++;
for(j = 0; j < count; j++)
cout << space;
cout << i << " is a factor" << endl;
prime /= i;
} else
i++;
factors.top
factors.pop
factors.display
};