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

أريد ايقاف اللوب في هذا الكود

بدأه hayatech في 17 مارس 2012 · 1 رد · 374 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم .

في هذا البرنامج

#include <iostream>
#include <stack>
#include <queue>
#include <string>

/*	Stack ADT Type Defintions 
	   Written by: F & G
	   Date:       2/98

	   Revised:    4/99--converted to C++  

	Brooks/Cole 
	A division of Thomson Learning
	Copyright 2001 All Rights Reserved 
*/
//	Node Declaration

	template<class TYPE>
	struct Node 
	   {
	    TYPE  data;
	    Node<TYPE> *next;
	   };

//	Class Declaration
	template<class TYPE>
	class Stack
	{
	 private: 
	    int         count; 
	    Node<TYPE> *top; 

	 public:
	         Stack      (void);
	        ~Stack      (void);
	   bool  pushStack  (TYPE  dataIn);
	   bool  popStack   (TYPE& dataOut);
	   bool  stackTop   (TYPE& dataOut);
	   bool  emptyStack (void);
	   bool  fullStack  (void);
	   int   stackCount (void);  
	};  // class Stack

/*	=============== Constructor ==============	
	This algorithm creates an empty stack.
	   Pre  Nothing
	   Post Stack created and initialized
*/

template<class TYPE>
Stack<TYPE> :: Stack (void)
{
//	Statements  
	top   = NULL;
	count = 0;
}	// Constructor 

/*	=================== pushStack =================== 
	This function pushes an item onto the stack.
	   Pre     dataIn contains data to be inserted
	   Returns true if success; false if overflow
*/

template<class TYPE> 
bool Stack<TYPE> ::  pushStack (TYPE dataIn)
{
//	Local Definitions 
	bool         success;
	Node<TYPE>  *newPtr;

//	Statements 
	if (!(newPtr =  new Node<TYPE>))
	    success  = false;
	else
	   {
	    newPtr->data = dataIn; 
	    newPtr->next = top; 
	    top          = newPtr; 
	    count ++;
	    success      = true;
	   } // else 
	return success;
}	// pushStack 

/*	=============== popStack ============== 
	This function pops the item on the top of the stack.
	   Pre     dataOut is variable to receive data
	   Post    popped data in dataOut 
	   Returns true if successful, false if underflow
*/

template<class TYPE>
bool Stack<TYPE> :: popStack (TYPE& dataOut) 
{
//	Local Definitions
	Node<TYPE>  *dltPtr;
	bool         success;

//	Statements 
	if (count == 0)
        success = false;
	else
	   {
	    dltPtr  = top;
	    dataOut = top->data;
	    top     = top->next;
	    count--;
	    delete dltPtr;
	    success = true;
	   } //  else 
	return success;
}	// popStack 

/*	==================== stackTop =================== 
	This function retrieves the data from the top of the 
	stack without changing the stack.
	   Pre     dataOut is variable to receive data
	   Post    data in dataOut 
	   Returns true if successful, false if underflow
*/

template<class TYPE> 
bool Stack<TYPE> :: stackTop (TYPE& dataOut) 
{
//	Local Definitions 
	bool success;

//	Statements 
	if (count == 0)
	   success = false;
	else
	   {
	    dataOut = top->data;
	    success = true;
	   } // else
	return success;
}	// stackTop 

/*	================= emptyStack ================ 
	This function determines if a stack is empty. 
	   Pre     nothing
	   Returns true if empty, false if data in stack 
*/

template<class TYPE>
bool Stack<TYPE> :: emptyStack (void) 
{
//	Statements 
	return (count == 0);
}	// emptyStack 

/*	=================== fullStack =================== 
	This function determines if a stack is full.
	Full is defined as heap full.
	    Pre     nothing
	    Returns true if heap full, false if room 
*/

template<class TYPE> 
bool Stack <TYPE> :: fullStack (void) 
{
//	Local Definitions  
	Node<TYPE>  *temp;

//	Statements 
	temp = new Node<TYPE>;
   if (temp != NULL)
	   {
	    delete  temp;
	    return false;
	   } // if

	// allocation failed 
	return true;
}	// fullStack 

/*	==================== stackCount =================== 
	Returns the number of elements in the stack.
	   Pre   nothing
	   Post  count returned
*/

template<class TYPE> 
int Stack <TYPE> :: stackCount(void) 
{
//	Statements 
	return count;
}	//  stackCount 

/*	=============== Destructor  ============== 
	This function releases all nodes to the heap.
	   Pre   stack being destroyed
	   Post  stack and data deleted
*/

template<class TYPE>
Stack<TYPE> :: ~Stack (void) 
{
//	Local Definitions 
	Node<TYPE>  *temp;

//	Statements 
	//  Delete all nodes in stack 
	while (top != NULL) 
	   {
	    temp = top;
	    top  = top->next; 
	    delete temp; 
	   } //  while 
}	// Destructor 


/*	Queue ADT Type Defintions 
	   Written by: F & G
	   Date:       2/98

	   Revised:    4/99--converted to C++  

	Brooks/Cole 
	A division of Thomson Learning
	Copyright 2001 All Rights Reserved 
*/
#include <iostream>
#include <fstream>                                                                      
#include <cstring>
using namespace std;

/*****************************QUEUE ABSTRACT DATA TYPE *******************/

template <class TYPE>
	struct NODE  
	{
	 TYPE        data;
	 NODE<TYPE> *next;
	};

//	Class Declaration
	template <class TYPE>
	class Queue 
	{
	 private:
	    NODE<TYPE>  *front; 
	    int          count; 
	    NODE<TYPE>  *rear; 

	 public:
	       Queue      (void);
	      ~Queue      (void);
	 bool  dequeue    (TYPE& dataOut);
	 bool  enqueue    (TYPE  dataIn);
	 int   queueCount (void);
	 bool  emptyQueue (void);
	 bool  fullQueue  (void);
	}; // class Queue
	template <class TYPE>
Queue<TYPE> :: Queue (void)
{
//	Statements 
	front  = NULL;
	rear   = NULL;
	count  = 0;
}	// Constructor 

template <class TYPE>
bool Queue<TYPE> :: enqueue (TYPE dataIn)
{
//	Local Definitions 
	NODE<TYPE>  *newPtr;

//	Statements 
	if (!(newPtr = new NODE<TYPE>))
	    return false;

	newPtr->data = dataIn; 
	newPtr->next = NULL; 

	if (count == 0)
	    // Inserting into empty queue 
	    front  = newPtr;
	else
        rear->next = newPtr; 

	count++;
	rear = newPtr;
	return true;
}	//enqueue


template<class TYPE>
bool Queue<TYPE> :: dequeue (TYPE& dataOut) 
{
//	Local Definitions 
	NODE<TYPE>  *deleteLoc;

//	Statements 
	if (count == 0)
	    return false;

	dataOut   = front->data;
	deleteLoc = front;
	if (count == 1)
	    // Deleting the only item in queue 
	    rear = front = NULL;
	else
	    front = front->next;
	count--;
	delete deleteLoc;

	return true;
}	// dequeue 



template <class TYPE> 
bool Queue<TYPE> ::  emptyQueue (void) 
{
// Statements 
	return (count == 0);
}	// emptyQueue 


template <class TYPE>
bool Queue<TYPE> :: fullQueue (void) 
{
//	Local Definitions  
	NODE<TYPE>  *temp;

//	 Statements 
	temp = new NODE<TYPE>;
	if (temp != NULL)
	   {
	    delete temp;
	    return false;
	   } // if 

	// Heap full 
	return true;
}	//  fullQueue 


template <class TYPE> 
int Queue<TYPE> :: queueCount(void) 
{
//	Statements 
	return count;
}	// queueCount 


template <class TYPE> 
Queue<TYPE> :: ~Queue (void) 
{
//	Local Definitions 
	NODE<TYPE>   *deletePtr;

//	Statements 
	while (front != NULL) 
	   {
	    deletePtr = front;
	    front = front->next; 
	    delete deletePtr; 
	   } //  while 
}	// Destructor

using namespace std;

bool operator==(const stack<char>&, const queue<char>&);
bool operator!=(const stack<char>&, const queue<char>&);

int main(int argc, char *argv[]) {
    stack<char> *s;
    queue<char> *q;
    string input;
    string::iterator i;

    while (true) {
        s = new stack<char>;
        q = new queue<char>;

        cout << "Enter the text:\n ";
        getline(cin,input);
        for (i = input.begin(); i != input.end(); i++) {
            s->push(*i);
            q->push(*i);
        }
        cout << input << " is ";
        if (*s != *q) {
            cout << "not ";
        }
        cout << "a palindrome." << endl;

        delete s;
        delete q;
    }

    return 0;
}

bool operator==(const stack<char> &s1, const queue<char> &q1) {
    bool eq = true;
    stack<char> s = s1;
    queue<char> q = q1;

    if (s.size() == q.size()) {
        while ((s.empty() == false) && (eq == true)) {
            eq = (s.top() == q.front());
            s.pop();
            q.pop();
        }
    } else {
        eq = false;
    }
    return eq;
}

bool operator!=(const stack<char> &s, const queue<char> &q) {
    return !(s == q);
}

ابغاه يوقف يعني لما يطبع لي الاوت بوت ابغاه يطلع لي plaindrome or not بدون مايقول لي اطبعي نص جديد , ياريت تساعدوني فيه

#2

استبدل do بــ بالتالي:

تكتب n للأنهاء

 do 
    {
        s = new stack<char>;
        q = new queue<char>;

        cout << "Enter the text:\n ";
        getline(cin, input);
        for (i = input.begin(); i != input.end(); i++)
        {
            s->push(*i);
            q->push(*i);
        }
        cout << input << " is ";
        if (*s != *q)
        {
            cout << "not ";
        }
        cout << "a palindrome." << endl;

        delete s;
        delete q;
        cout << "Try Again Y/n : ";
        getline(cin, input);
    } while(input != "n");

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

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