Reverse output Stack problem
Hello all, I wanted to say thank you so much for all the help. I've written the code and tried to use other functions that I've learned in past programming. Unfortunately I am still having problems. It seems that I am making a bunch of mistakes somewhere and I am not sure if I am getting close to correctly writing the program. Can you please let me know if I should just start from scratch and rewrite the program. I want to make sure that I come away from this understanding stacks and knowing where I wen wrong. Also does anyone have any suggestions on any good c++ programming books.
Thanks in advance for any help
Code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const int Stack_Size=12; //Initialization of Stack Size//
const string space="";
class Stack
{
public:
Stack(); //constructor//
void primefactor(int number);
bool isprime(int number);
void push(int);
int pop();
bool peek();
bool empty();
bool full();
private:
int count;
int data[Stack_Size];
};
Stack::Stack()
{
count=-1;
}
void primefactor(int n)
{
bool isPrime = isprime(n);
int prime = n;
int i = 2, j;
double squareRoot = sqrt(static_cast<double>(n));
int count = 0;
cout << "The prime factorization of " << n << " is:" << endl;
if(isPrime)
cout << space << n << " is a prime number." << endl;
else {
while((prime > 0) && (i <= n)) {
if((prime % i) == 0) {
count++;
for(j = 0; j < count; j++)
cout << space;
cout << i << " is a factor" << endl;
prime /= i;
} else
i++;
}
}
}
bool isprime(int n)
{
int i;
for(i = 2; i < n; i++) {
if((n % i) == 0)
return false;
}
return true;
}
void Stack::push(int value)
{
count++;
data[count]=value;
}
int Stack::pop()
{
int topvalue;
topvalue=data[count];
count--;
return(topvalue);
}
bool Stack::empty()
{
if (count==-1)
return 1;
else
return 0;
}
bool Stack::full()
{
if (count==Stack_Size-1)
return 1;
else
return 0;
}
int main()
{
Stack entries;
int number;
char answer;
cout<<" Please enter that is greater than 100\n ";
while(1)
{
cout<<"Digit:\n";
cin>>number;
if (number<100)
break;
if (entries.full())
{
cout<<"The stack is full and you do not have any space left\n";
break;
}
else
entries.push(number);
}
if (entries.empty()) // {
cout<<"The stack is empty\n";
}
else
while (!entries.empty())
{
cout<<" Do you want to pop an item from the stack? ";
cin>>answer;
if (answer=='y')
cout<<entries.pop()<<endl;
else
break;
}
return 0;
}
Reverse output (Stack)...almost there
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
};