Solved

re-post

Posted on 2004-09-21
5
224 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
ID: 12118200
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
ID: 12118209
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
ID: 12118216
Hello, Amit.  Good to hear from you.  Del
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12119247
Same here Del....Did u get what i try to say

Amit
0
 

Author Comment

by:edelossantos
ID: 12119519
yes I did.  Thank you.  Del
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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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.
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.

920 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