[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

check

Posted on 2004-09-22
11
Medium Priority
?
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 400 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 1200 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

650 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