Solved

code check

Posted on 2004-09-22
5
276 Views
Last Modified: 2010-04-01
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stack>

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()
{
   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> ");
   for(i=0,input[i]=getchar();input[i]!='\n';i++,input[i]=getchar())
   {
      S.push(input[i]);
   }

   length=strlen(input)-1;   // we don't need final '/n'

   for(i=0;i<length;i++)
   {
      S.pop(S_output[i]);
 
      if(input[i]!=S_output[i])
      {
           printf("\nThe words are not palindrome\n");
           cin >> i;
           return 0;
      }

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

output:

[edeloss2@pegasus lab2]$ g++ main.cpp
main.cpp: In function `int main()':
main.cpp:67: implicit declaration of function `int printf(...)'
main.cpp:68: implicit declaration of function `int getchar(...)'
main.cpp:91: implicit declaration of function `int putchar(...)'



0
Comment
Question by:edelossantos
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12126840
You need to add

#include <stdio.h>

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 12126851
BTW, to elaborate - these functions are part of the CRT (C Runtime Library), not of the STL
0
 

Author Comment

by:edelossantos
ID: 12126899
please explain palindrome, the code stops that this is not a palindrome.  Del
0
 

Author Comment

by:edelossantos
ID: 12126907
[edeloss2@pegasus lab2]$ a.out
Enter a words to reverse> del

The words are not palindrome
0
 
LVL 86

Expert Comment

by:jkr
ID: 12126961
Instead of

  printf("Enter a words to reverse> ");
  for(i=0,input[i]=getchar();input[i]!='\n';i++,input[i]=getchar())
  {
     S.push(input[i]);
  }


  length=strlen(input)-1;   // we don't need final '/n'

use

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


0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

770 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