Solved

code check

Posted on 2004-09-22
5
269 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

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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