الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ارجو افادتي في Stack

مغلق
بدأه alqali_4u في 10 ديسمبر 2003 · 3 رد · 505 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

يوقوم هذا بإدخال ارقام أو حروف

عند التنفيذ يظهر لي مشكلة لا اعرف سببها مع العلم ان كتابة البرنامج صحيحة ومنقولة من المدرس نفسة ...

stackarr.h + Implemention + test.cpp

#ifdef stackarr.h
#define stackarr.h
#include<iostream.h>
#include <stdexcept>

using namespace std;

const int defMaxStackSize = 10;   // Default maximum stack size

template < class DT >
class Stack
{
  public:

    // Constructor
    Stack ( int maxNumber = defMaxStackSize ) throw ( bad_alloc );

    // Destructor
    ~Stack ();

    // Stack manipulation operations
    void push ( const DT &newDataItem )    // Push data item
        throw ( logic_error );
    DT pop ()                              // Pop data item
        throw ( logic_error );
    void clear ();                         // Clear stack

    // Stack status operations
    bool isEmpty () const;                 // Stack is empty
    bool isFull () const;                  // Stack is full

    // Output the stack structure -- used in testing/debugging
    void showStructure () const;

  private:

    // Data members
    int maxSize,    // Maximum number of data items in the stack
        top;        // Index of the top data item
    DT *dataItems;  // Array containing the stack data items
};

//**********************************************

template < class DT >
Stack<DT>::Stack ( int maxNumber ) throw ( bad_alloc )
{
	top = -1;
	maxSize = 0;
	dataItems = new DT [ maxNumber ];
}



template < class DT >
Stack<DT>::~Stack ()
{
	delete [ ] dataItems;
}


template < class DT >
void Stack<DT>::push ( const DT &newDataItem )    
        throw ( logic_error )
{
	if ( ! isFull () )
	{
	top++;
	dataItems [ top ] = newDataItem;
	}
	else 
  cout <<" Stack is Full" << endl;
}



template < class DT >
DT Stack<DT>::pop () throw ( logic_error )
{
	DT item;

	if ( ! isEmpty () )
	{
	item = dataItems [ top ];
	top--;
	return item;
	}
	else
  cout <<"Stack is Empty " << endl;
}




template < class DT >
void Stack<DT>::clear ()
{
	for ( int i = 0; i < defMaxStackSize; i++ )
  dataItems [ i ] = 0;
	top = -1;
	maxSize = 0;
}



template < class DT >
bool Stack<DT>::isFull () const
{
	return ( top == ( defMaxStackSize - 1 ) );
}



template < class DT >
bool Stack<DT>::isEmpty () const
{
	return ( top == -1 );

}



template < class DT >
void Stack<DT>:: showStructure () const

// Array implementation. Outputs the data items in a stack. If the
// stack is empty, outputs "Empty stack". This operation is intended
// for testing and debugging purposes only.

{
    int j;   // Loop counter

    if ( top == -1 )
       cout << "Empty stack" << endl;
    else
    {
       cout << "top = " << top << endl;
       for ( j = 0; j < maxSize; j++ )
           cout << j << "\t";
       cout << endl;
       for ( j = 0; j <= top ; j++ )
           cout << dataItems[j] << "\t";
       cout << endl;
    }
}
#endif

والمشكلة على ما اعتقد تكم في ملف main >> test.cpp

#2

هذا برنامج ال Test

include <iostream>

#include "stackarr.cpp"
//#include "stackdwn.cpp"

//--------------------------------------------------------------------

void print_help()
{
    cout << endl << "Commands:" << endl;
    cout << "  H  : Help (displays this message)" << endl;
    cout << "  +x : Push x" << endl;
    cout << "  -  : Pop" << endl;
    cout << "  C  : Clear" << endl;
    cout << "  E  : Empty stack?" << endl;
    cout << "  F  : Full stack?" << endl;
    cout << "  Q  : Quit the test program" << endl;
    cout << endl;
}

void main()
{
    Stack<char> testStack(8);     // Test stack
    char testDataItem;            // Stack data item
    char cmd;                     // Input command

    print_help();

    do
    {
        testStack.showStructure();                    // Output stack

        cout << endl << "Command: ";                  // Read command
        cin >> cmd;
        if ( cmd == '+' )
           cin >> testDataItem;

        switch ( cmd )
        {
          case 'H' : case 'h':
               print_help();
               break;

          case '+' :                                  // push
               cout << "Push " << testDataItem << endl;
               testStack.push(testDataItem);
               break;

          case '-' :                                  // pop
               cout << "Popped " << testStack.pop() << endl;
               break;

          case 'C' : case 'c' :                       // clear
               cout << "Clear the stack" << endl;
               testStack.clear();
               break;

          case 'E' : case 'e' :                       // isEmpty
               if ( testStack.isEmpty() )
                  cout << "Stack is empty" << endl;
               else
                  cout << "Stack is NOT empty" << endl;
               break;

          case 'F' : case 'f' :                       // isFull
               if ( testStack.isFull() )
                  cout << "Stack is full" << endl;
               else
                  cout << "Stack is NOT full" << endl;
               break;

          case 'Q' : case 'q' :                   // Quit test program
               break;

          default :                               // Invalid command
               cout << "Inactive or invalid command" << endl;
        }
    }
    while ( cmd != 'Q'  &&  cmd != 'q' );

}
#3

السلام عليكم ,,

أخي الكريم البرنامج كان فيه مشكلتين ,,

الاولى يبدو أنك لم تضع الملفات كلها في المشروع بالطريقة الصحيحة,,عموما اليك المثال المرفق معدل وهو يعمل ,,

المشكلة الحقيقية الثانية ,, أن أضفت العبارات

#ifdef

#define

في بداية ملف ال .H الذي به تعريف الفئة class ,, بطريقة خاطئة ,, كان ينبغي كتابة

#ifndef

بدلا من

#ifdef

وجعلتهم في الملف الذي به ال mian يعني ال cpp ,,

بالتوفيق ,,

Stac.zip

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#4

مشكور اخوي على الرد

لكن ممكن ممكن شوية مساعدة؟؟ ماهي طريقة تحويل stackarr

إلى stackdwn

يعني بالعكس...

هذا الموضوع مغلق.

مواضيع مشابهة