Solved

check

Posted on 2004-09-22
11
518 Views
Last Modified: 2008-03-17
#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 `>'


0
Comment
Question by:edelossantos
11 Comments
 

Author Comment

by:edelossantos
Comment Utility
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


0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
Comment Utility
>>main.cpp:67: implicit declaration of function `int stackMenu(...)'


You need to put the stackMenu function, before the main() function.
0
 

Author Comment

by:edelossantos
Comment Utility
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 `>'

0
 

Author Comment

by:edelossantos
Comment Utility
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?
0
 

Author Comment

by:edelossantos
Comment Utility
please advise on how to obtain behavior to previous posts.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 100 total points
Comment Utility
template<class T> void stackMenu(T);
int main() {
...
}

try #include <stdio.h> in addition to <cstdio>. but i don't think it would make any difference.

   else  
     return 0;
} <<<====== remove this brace; it closes main()

   const int SIZE = 100;
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 300 total points
Comment Utility
If you want to test palindromes you have to remove the Stack functionality from main()
 
   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;

Regards, Alex
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 300 total points
Comment Utility
>>> what do I need to do to get this behavior?

#include <iostream>  
using namespace std;

#define CHUNK_SIZE 100

template <class item_t>
class Stack_t
{
    item_t*  items;
    int      top;

public:
    Stack_t() : items(NULL), top(0) {}
    ~Stack_t() { delete [] items; }
    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)
        {
            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;
    }                            
    bool empty() { return top == 0; }
};


int main()
{
    const int SIZE = 100;
   
    Stack_t<char> S;
    char input[SIZE]    = { 0 };
    char S_output[SIZE] = { 0 };
    int i,length;
   
    cout << "Enter a palindrome> ";
    gets(input);
    for(i=0;input[i]!=NULL;i++)
    {
        S.push(input[i]);
    }
   
    length = strlen(input);  
    for(i=0;i<length;i++)
    {
        S.pop(S_output[i]);
       
        if(input[i]!=S_output[i])
        {
            cout << endl << "The words are not palindrome" << endl;
            break;  
        }
       
        else // if(Q_output[i]==S_output[i])
            cout << S_output[i];
    }
    if (i >= length)
        cout << " is a palindrome" << endl;
   
    putchar('\n');

    Stack_t<int> Si;
    int inp;
    cout << "Enter a list of integers terminated by -1" << endl;
    for (i = 1; ; ++i)
    {
        cin >> inp;
        if (inp == -1)
            break;
        Si.push(inp);
    }

    int sum = 0;
    while (!Si.empty())
    {
        Si.pop(inp);
        sum += inp;
    }
    cout << "The sum of the numbers entered is " << sum << endl;
    return 0;
}


Regards, Alex

p. s. you don't need any includes beside <iostream>

 
0
 

Author Comment

by:edelossantos
Comment Utility
[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?
0
 

Author Comment

by:edelossantos
Comment Utility
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
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now