Link to home
Start Free TrialLog in
Avatar of edelossantos
edelossantos

asked on

check

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <cstdio>

using namespace std;

typedef char item_t;
#define CHUNK_SIZE 100

class Stack_t
{
    item_t*  items;
    int      top;

public:
    Stack_t() : items(NULL), top(0) {}
    void push(item_t item)
    {
        if (top%CHUNK_SIZE == 0)
        {
            item_t* nitems = new item_t[top + CHUNK_SIZE];
            if (items != NULL)
            {
                for (int i = 0; i < top; i++)
                    nitems[i] = items[i];
            }
            delete []items;
            items = nitems;
        }
        items[top++] = item;
    }
    bool pop(item_t& item)
    {
        if (top == 0)
        {
            cout << "Stack is empty";
            return false;
        }
        item  = items[--top];
        if (top%CHUNK_SIZE == 0)
        {  
            item_t* nitems = NULL;
            if (top > 0)
            {
                nitems = new item_t[top];
                for (int i = 0; i < top; i++)
                    nitems[i] = items[i];
            }
            delete []items;
            items = nitems;
        }
        return true;
    }                            
};


int main()
{
   int option;
 
   cout << "Create a Stack for integers[1] or characters[2]?\n";
   cin >> option;
   if (option == 1)  {
       int i = 0;
       stackMenu(i);
   }
   else if (option == 2) {
        char c = 'a';
        stackMenu(c);
    }
   else  
     return 0;
}

   const int SIZE = 100;

   Stack_t S;
   item_t input[SIZE]    = { 0 };
   item_t S_output[SIZE] = { 0 };
   int i,length;

   printf("Enter a words to reverse> ");
  gets(input);
  for(i=0;input[i]!=NULL;i++)
  {
     S.push(input[i]);
  }

  for(i=0;i<length;i++)
   {
      S.pop(S_output[i]);
 
      if(input[i]!=S_output[i])
      {
            printf("\nThe words are not palindrome\n");
               return 0;   // <- This returns from main() and therefore quite the program
      }

      else // if(Q_output[i]==S_output[i])
           cout << S_output[i];
     }
     printf(" is a palindrome\n");
     
     putchar('\n');
     cin >> i;
     return 0;
}

void stackoptions()
{
   cout << "1: Push\n";
   cout << "2: Pop\n";
   cout << "3: Top\n";
   cout << "4: Quit\n\n";
   cout << "Enter Option: ";
}


template <class T> void stackMenu(T data)
{
   stack<T> mystack;  // create the right stack
   stackoptions();
   int option;
   cin >> option;

   while (option != 4)
   {
      if (option == 1)
         {
            cout << "enter data";
            cin >> data;
            mystack.push(data);
            cout << "Stack Added Value " << data << " to Top\n";
         }
      if (option == 2)
         {
            if (!mystack.empty())
          {
               mystack.pop();
               cout << "Stack Successfully Poped\n";
            }
            else
               cout << "Stack is Empty, Cannot Pop\n";
         }
      if (option == 3)
         {
            if (!mystack.empty())
               cout << "Value at Top is: " << mystack.top() << "\n";
            else
               cout << "Stack is Empty, Cannot Top\n";
         }
      if (option > 3 || option < 1)
         {
            cout << "Invalid Option\n";
         }
      stackoptions();
      cin >> option;
   }
}

[edeloss2@pegasus lab2]$ g++ main.cpp
main.cpp: In function `int main()':
main.cpp:67: implicit declaration of function `int stackMenu(...)'
main.cpp: At top level:
main.cpp:84: ANSI C++ forbids declaration `printf' with no type
main.cpp:84: `int printf' redeclared as different kind of symbol
/usr/local/lib/gcc-lib/alphaev56-dec-osf4.0d/2.95.3/include/stdio.h:179: pr
s declaration of `int printf(const char *, ...)'
main.cpp:84: initialization to `int' from `const char *' lacks a cast
main.cpp:85: ANSI C++ forbids declaration `gets' with no type
main.cpp:85: `int gets' redeclared as different kind of symbol
/usr/local/lib/gcc-lib/alphaev56-dec-osf4.0d/2.95.3/include/stdio.h:210: pr
s declaration of `char * gets(char *)'
main.cpp:85: initialization to `int' from `char *' lacks a cast
main.cpp:86: parse error before `for'
main.cpp:86: syntax error before `!='
main.cpp:86: syntax error before `++'
main.cpp:91: parse error before `;'
main.cpp:91: syntax error before `++'
main.cpp:104: ANSI C++ forbids declaration `printf' with no type
main.cpp:104: redefinition of `int printf'
main.cpp:84: `int printf' previously defined here
main.cpp:104: initialization to `int' from `const char *' lacks a cast
main.cpp:104: multiple initializations given for `printf'
main.cpp:106: parse error before `--'
main.cpp:107: syntax error before `>'


Avatar of edelossantos
edelossantos

ASKER

what do I need to do to get this behavior?

Enter a palindrome: madam

                The word reversed is: madam

                Enter a list of integers followed by -1: 1 2 3 4 5 -1

                The sum of the numbers on the stack is: 15


SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
void stackMenu();

int main() {

-------------------

}

output:

[edeloss2@pegasus lab2]$ g++ main.cpp
main.cpp: In function `int main()':
main.cpp:58: too many arguments to function `void stackMenu()'
main.cpp:68: at this point in file
main.cpp:58: too many arguments to function `void stackMenu()'
main.cpp:72: at this point in file
main.cpp: At top level:
main.cpp:85: ANSI C++ forbids declaration `printf' with no type
main.cpp:85: `int printf' redeclared as different kind of symbol
/usr/local/lib/gcc-lib/alphaev56-dec-osf4.0d/2.95.3/include/stdio.h:179: previ
s declaration of `int printf(const char *, ...)'
main.cpp:85: initialization to `int' from `const char *' lacks a cast
main.cpp:86: ANSI C++ forbids declaration `gets' with no type
main.cpp:86: `int gets' redeclared as different kind of symbol
/usr/local/lib/gcc-lib/alphaev56-dec-osf4.0d/2.95.3/include/stdio.h:210: previ
s declaration of `char * gets(char *)'
main.cpp:86: initialization to `int' from `char *' lacks a cast
main.cpp:87: parse error before `for'
main.cpp:87: syntax error before `!='
main.cpp:87: syntax error before `++'
main.cpp:92: parse error before `;'
main.cpp:92: syntax error before `++'
main.cpp:105: ANSI C++ forbids declaration `printf' with no type
main.cpp:105: redefinition of `int printf'
main.cpp:85: `int printf' previously defined here
main.cpp:105: initialization to `int' from `const char *' lacks a cast
main.cpp:105: multiple initializations given for `printf'
main.cpp:107: parse error before `--'
main.cpp:108: syntax error before `>'

public class stack{
  public static void main(String[] args){
    String s = "Hello";
          Stack stk = new Stack();

    for( int i = 0; i < s.length(); i++ )
            stk.push(new Character(s.charAt(i)));
          s = "";
          while( !stk.empty() )
            s = s + (Character)(stk.pop());
          System.out.println(s);
  }
}

// can this code be incorporated into the above code to fascilitate palindrome operation?
please advise on how to obtain behavior to previous posts.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
[edeloss2@pegasus lab2]$ make veryclean
rm -f *.o
rm -f core
rm -f  main
[edeloss2@pegasus lab2]$ make
:{CPP} -c -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall  main.cpp
/bin/sh: :{CPP}: not found
make: *** [main.o] Error 1

what does this mean?
I fixed one of the errors in the makefile and now I have this error:

[edeloss2@pegasus lab2]$ make
:{CPP} -c -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall  main.cpp
/bin/sh: :{CPP}: not found
make: *** [main.o] Error 1
Del,

simlply take the makefile of the last question http:Q_21141770.html and put in i a directory that contains only the makefile and main.cpp, nothing else.

If the makefile doesn't work, use makemake to create a new one. Type 'man makemake' if you need help on this.

Regards, Alex