Solved

check

Posted on 2004-09-22
11
520 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
ID: 12130070
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
ID: 12130210
>>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
ID: 12130381
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
ID: 12130445
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
ID: 12130454
please advise on how to obtain behavior to previous posts.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 100 total points
ID: 12130468
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
ID: 12131178
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
ID: 12131603
>>> 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
ID: 12134055
[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
ID: 12134764
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
ID: 12140750
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

930 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

11 Experts available now in Live!

Get 1:1 Help Now