Solved

re-post

Posted on 2004-09-21
5
213 Views
Last Modified: 2010-04-01
My apologies for the re-post, I need help on this.  I just got back online.  Del

# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'main'
# ---------------------------------------------------------------
# Note: If you want to use the g++ compiler on pegasus or helios
# uncomment the g++ lines and comment the Digital Unix lines

# the Digital Unix C++ compiler needs these lines:
CPP = cxx
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall

# the g++ compiler on pegasus and helios needs these lines:
# CPP = g++
# CFLAGS = -DPOSIX_4D9 -w -g

# link in the math library
LFLAGS = -lm

RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
#
# add suffix .cpp since it is not a default with make util
.SUFFIXES:     .cpp .o
#
# implicit rule for compilation only:
.cpp.o:
     ${CPP} -c ${CFLAGS} $<

OFILES=          main.o life.o util.o

HFILES=          life.h  util.h

# dependencies
#
default:     main    
#
main:           $(OFILES)
          ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

main.o:          main.cpp life.h util.h
life.o:          life.cpp life.h util.h
util.o:          util.cpp util.h

#
clean:
     ${RM} *.o
     ${RM} core
#
veryclean:     clean
     ${RM}  main  

// main.cpp
#include <stack>
#include <string>
#include <iostream>

using namespace std;

#define SIZE 100
typedef char item_t;

typedef struct {
     item_t Items[SIZE];
     int top;
}Stack_t;

typedef struct{
     int count,front, rear;
     item_t Item[SIZE];
}Queue_t;

void InitialiseStack (Stack_t *S)
{     S->top=0;     }

int emptystack (Stack_t S)
{     return (S.top==0);     }

int fullstack (Stack_t S)
{     return (S.top==SIZE);     }


void push (char x,Stack_t *S)
{
     if(fullstack(*S))
          printf("stack overflow\n");
     else{
               S->Items[S->top]=x;
               ++(S->top);
     }
}

void pop (Stack_t *S, char *x)
{
     if(emptystack(*S))
          printf("stack empty\n");
     else{
          --(S->top);
          *x=S->Items[S->top];
     }
}


template <class T> void stackMenu(T data);
void stackMenu();

int main()
{
//   int option;
 
   Stack_t S;
     // Queue_t Q;
   item_t input[SIZE] = { 0 };
   item_t Q_output[SIZE]="",S_output[SIZE]="";
     int i,length;

     InitialiseStack(&S);
     //InitialiseQueue(&Q);

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

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

     for(i=0;i<length;i++)
     {
          //while(!emptystack(S)){
               // Remove(&Q,&Q_output[i]);
               pop(&S,&S_output[i]);
          // }
     
          if(input[i]!=S_output[i])
          {
               printf("\nThe words are not palindrome\n");
               return 0;
          }

          else /* if(Q_output[i]==S_output[i]) */
               cout << S_output[i];
     }
     
     putchar('\n');
     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;
   }
}


This is the program behvior needed:

STL STACK PROGRAM: LAB 2

                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
Comment
Question by:edelossantos
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 500 total points
Comment Utility
FRom your requirement, it seems that u require a generic stack...thus using templates......but u should be using templates for the stack class itself

I would start something like this

template <class T>
class Stack {
    private :
        T stk ;
        int top ;
    public :
            Stack () ;
            T pop () ;
            int push ( T stack ) ;
            bool is_stack_empty () ;
            bool is_stack_full () ;            
                     
} ;  

Amit
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 500 total points
Comment Utility
While creating an insteance for Stack of type char, u say

Stack <char> char_stk ;


While creating an insteance for Stack of type int, u say

Stack <int> int_stk ;


Amit
0
 

Author Comment

by:edelossantos
Comment Utility
Hello, Amit.  Good to hear from you.  Del
0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
Same here Del....Did u get what i try to say

Amit
0
 

Author Comment

by:edelossantos
Comment Utility
yes I did.  Thank you.  Del
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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