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

" يا أحباب" برنامج Queueإضافة عنصر حذف عنصر ..... الخ

بدأه السماك في 23 يونيو 2010 · 8 رد · 564 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

من فرج عن مسلم كربة من كرب الدنيا فرج الله عنه كربة من كرب يوم القيامة

يا أخوان هذا برنامج باستخدام المصفوفة يقوم بإظهار قائمة

1/ اضافة عنصر

2/حذف عنصر

3/ترتيب عنصار المصفوفة

4/بحث عن عنصر

5/ طباعة عنصار المصوصفة

6/خروج

وقد حاولت منذ البارحة وانا احاول اعمل Run للبرنامج يظهر لي اخطاء لا اعرف حلها

وهذي محاولتي :

const int MAX_ITEMS = 10;

class Queue
{
	public:
		void MakeEmpty();
		int Enqueue(int);
		int Dequeue();
		int sortid ();
                                          int IsFull();
		int IsEmpty();
		int search (int);
                                          void Print();

	private:
		int items[MAX_ITEMS];
		int front, rear;
};


#include <iostream.h>
#include "Queue.h"

void Queue::MakeEmpty()
{
	front = 0;
	rear = 0;
}

int Queue::Enqueue(int a)
{
	if (IsFull() == 0)
	{
		items[rear] = a;

		if ((rear+1) != MAX_ITEMS)
			rear = rear + 1;
	}

	return 0;
}

int Queue::Dequeue()
{
	if (IsEmpty() == 0)
	{
		front = front + 1;
		if (front == MAX_ITEMS)
			front = 0;

		return 1;
	}

	return -1;
}
int swap(int &x, int &y);

Int Queue :: sortid () 
{
        {	int temp;

	temp = x;
	x = y;
	y = temp;
         }
for (int k=0; k<MAX_ITEMS; k++)
	{
		for (int i=MAX_ITEMS-1; i>k; i--)
		{
			if (a < a[i-1])
			{
				swap(a, a[i-1]);
			}
		}
	}

}
int Queue::IsFull()
{
	return ( ((rear+1) % MAX_ITEMS) == front);
}

int Queue::IsEmpty()
{
	return (front == rear);
}
int Queue :: search (int a ){
        for ( int i =0 ; i < MAX_ITEMS; i++){
                       if ( items==a){
                               cout << i
               }
        }
   } 
void Queue::Print()
{
	if (IsEmpty() == 1)
	{
		cout << "Queue is empty!";
	}
	else
	{
		for (int i=front; i<rear; i++)
		{
			cout << items << endl;
		}
	}
}
void Queue :: Quit () {



} 

int main()
{
Queue 1 ;
      int n ;
while (n != 6){
       cout << "Enter one number from 1 to 6 >>> 1. Add number …<< endl.. 2. delete number …<< endl..3. sort number << endl ….4. search ….5. print …6.Quit "


cin >>n ;

     if ( n == 1){
        1. Enqueue(a) ;
        }
      else if (n==2){
         1.Dequeue ();	 
        }
     else if (n==3){
       1.sort ();	 
          }
     else if (n==4){
     1.search () ;	 
         }
   else if (n==5){
     1.print ();	 
           }
   }
}
#2

السلام عليكم

انا عدلت الكود ليك والحمدلله وصلت من 35 خطا الى 3

لكن ال3 هذه لم اعرف هناك متغيرات و arrays مش عارف اعمل فيهم ايه ؟

المهم

هاحددهم ليك وانت تشوفهم بيعملوا ايه ؟

وتعرفهم وبعد كدا الكود هيشتغل ان شاء الله

int swap(int &x, int &y); 

int Queue :: sortid () {
	int temp ; 
	temp = x; 
	x = y; 
	y = temp; 

	for(int k=0; k< MAX_ITEMS ; k++){ 
		for (int i=MAX_ITEMS-1; i>k; i--){ 
			if (a < a[i-1]){ 
				swap(a, a[i-1]); 
			} 
		}
	} 
}

المتغير x , y و الـ array a

غير متعرفين شوف هما بيعملوا ايه وعرفهم وهيشتغل

واكتب الكود بتاع الداله

int swap(int &x, int &y);

الكود بعد التعديل

فى 3 ملفات

Queue.h

// Queue.h file
const int MAX_ITEMS = 10; 

class Queue { 
public: 
	void MakeEmpty(); 
	int Enqueue(int); 
	int Dequeue(); 
	int sortid (); 
	int IsFull(); 
	int IsEmpty(); 
	int search (int); 
	void Print(); 

private: 
	int items[MAX_ITEMS]; 
	int front, rear; 
};

Queue.cpp

// Queue.cpp file 
#include "Queue.h"
#include <iostream>
using namespace std;
void Queue::MakeEmpty(){ 
	front = 0; 
	rear = 0; 
} 

int Queue::Enqueue(int a){ 
	if (IsFull() == 0){ 
		items[rear] = a; 
		if ((rear+1) != MAX_ITEMS) 
			rear = rear + 1; 
	} 

	return 0; 
} 

int Queue::Dequeue() { 
	if (IsEmpty() == 0){ 
		front = front + 1; 
		if (front == MAX_ITEMS) 
			front = 0; 

		return 1; 
	} 

	return -1; 
} 
int swap(int &x, int &y); 

int Queue :: sortid () {
	int temp ; 
	temp = x; 
	x = y; 
	y = temp; 

	for(int k=0; k< MAX_ITEMS ; k++){ 
		for (int i=MAX_ITEMS-1; i>k; i--){ 
			if (a < a[i-1]){ 
				swap(a, a[i-1]); 
			} 
		}
	} 
}
int Queue::IsFull(){ 
	return ( ((rear+1) % MAX_ITEMS) == front); 
} 

int Queue::IsEmpty(){ 
	return (front == rear); 
} 
int Queue :: search (int a ){ 
	for ( int i =0 ; i < MAX_ITEMS; i++){ 
		if ( items==a){ 
			cout << i;
		} 
	} 
}  
void Queue::Print(){ 
	if (IsEmpty() == 1){ 
		cout << "Queue is empty!"; 
	} 
	else{ 
		for (int i=front; i<rear; i++){ 
			cout << items << endl; 
		} 
	} 
}

main.cpp

// main.cpp file 

#include<iostream>
#include"Queue.h"
using namespace std;
void main() { 
	Queue q ; 
	int n ; 
	while (n != 6){ 
		cout << "Enter one number from 1 to 6 >>> 1. Add number …"<<endl<< "2. delete number …"<< endl<<"3. sort number "<< endl <<"….4. search ….5. print …6.Quit " ;
		cin >>n ; 

		if ( n == 1){ 
			int a ; 
			cout <<"Enter number in queue ";
			cin>>a;
			q. Enqueue(a) ; 
		} 
		else if (n==2){ 
			q.Dequeue ();    
		} 
		else if (n==3){ 
			q.sortid();         
		} 
		else if (n==4){
			int searchItem;
			cout<<"Enter Search Item ";
			cin>>searchItem;
			q.search (searchItem) ;        
		} 
		else if (n==5){ 
			q.Print();          
		} 
	} 
}

اردت الافاده

واتمنى ان يكون ذلك

سلام عليكم

تم تعديل هذه المشاركة بواسطة Mahmoud Kelany في 23 يونيو 2010 في 16:47

1

Software Developer
Mahmoudkelany.com


 

#3

اخى الكريم

لقد حاولت مرار وتكرارر فى الكود بتاعك

لكن مش فاهم حاجه بصراحه تسمى المتغيرات برموز وخلافه

انا عملت لك كود زى بتاعك

ياريت تشوفه ولو فيه حاجه تقولى

الكود عباره عن template queue

يعنى اى نوع

ممكن تعمل int , double , float , string

اى حاجه تعملها queue

واتمنى اكون وفقت فى ذلك والحمدلله والفضل لله وحده

والمشروع بالمرفقات

و الكود اهووو

queue.h

template<class Type>
class queue
{
private:
	int Head,Tail,Size;
	Type * queueArray;
public:
	queue(int size=10);
	~queue();
	bool IsEmpty();
	bool IsFull();
	bool Add(Type Item);
	void Remove();
	void Print();
	void search(Type a );
	void sortid();
};

queueImp.h

#include "queue.h"
template<class Type>
queue<Type>::queue(int size)
{
	Head = 0;
	Tail = 0;
	Size = size;
	queueArray = new Type[size];
}
template<class Type>
queue<Type>::~queue(void)
{
	delete [] queueArray;
}
template<class Type>
bool queue<Type>::IsEmpty(){
	return Head == Tail;
}
template<class Type>
bool queue<Type>::IsFull(){
	return (Tail+1) % Size == Head;
}
template<class Type>
bool queue<Type>::Add(Type item){
	if(!IsFull()){
		queueArray[Tail] = item;
		Tail = (Tail+1) % Size;
		return true;
	}
	else{
		cout<<"Sorry add failed : The queue is Full."<<endl;
		return false;
	}
}
template<class Type>
void queue<Type>::Remove(){
	if(!IsEmpty()){
		Type item = queueArray [Head];
		Head = (Head+1) % Size;
		cout<<"The removed item : "<< item <<endl ;
	}else{
		cout<<"Sorry remove failed : The queue is Empty."<<endl;
		}
}

///////

template<class Type>
void queue<Type>::sortid () {
	for(int i = Head ; i <  Tail ;  i++){
		for(int j = 0; j <  Tail; j++){

			if(queueArray<queueArray[j]){
				Type temp = queueArray;
				queueArray = queueArray[j];
				queueArray[j] = temp;
			}

		}
	}
} 

template<class Type>
void queue<Type>:: search (Type searchItem){ 
	for ( int i =Head ; i < Tail ; i++){ 
		if ( queueArray == searchItem){
			cout <<"The index of the searched item = " << i <<endl;
		}
		if ( i == Tail-1 && queueArray  != searchItem ){
			cout<<"Not found."<<endl;
		}
	}
}  

template<class Type>
void queue<Type>::Print(){ 
	if (!IsEmpty()){ 
		cout<<"The qeueu items is : "<<endl;
		for (int i = Head; i < Tail ; i++){ 
			cout << "item(" << i << ") = " << queueArray << endl; 
		} 
	} 
	else{
		cout<<"Queue empty."<<endl;
	}
}

main.cpp

#include<iostream>
#include<string>
#include"queueimp.h"  
using namespace std;
void main(){

	queue <int> myQueue(100) ; 

	int choise  = 0 ; 
	cout <<"1. Add number."<<endl<< "2. Delete number"<< endl<<"3. Sort number."
		<< endl <<"4. Search."<<endl<<"5. Print"<<endl<<"6. Quit."<<endl;

	while(choise != 6){
		cout<< "\t\t\t\t\tEnter your choise : ";
		cin >> choise; 

		switch(choise){
			case 1:
				int item ; 
				cout <<"Enter number in queue : ";
				cin>> item;
				myQueue.Add(item);
				break;

			case 2:
				myQueue.Remove();
				break;
			case 3:
				myQueue.sortid();         
				cout<<"Queue sorted : press 5 to print it."<<endl;
				break;
			case 4:
				int searchItem;
				cout<<"Enter Search Item : ";
				cin>>searchItem;
				myQueue.search (searchItem) ;
				break;
			case 5:
				myQueue.Print();          
				break;
		} 
	}
}

Queue.rar

1

Software Developer
Mahmoudkelany.com


 

#4

جزاك الله الف خير يا اخ محمود

لكن يا ليتك تعدل على الأولى وتسيبنا من الثانية لأنها بصراحة فيه أشياء ما اخذناها وشكلي حيكون بايخ مع الدكتور لما اجيب أشياء ما اخذتها

وأسال الله ان يحرم وجهك على النار وشكرا لك يا أخي على المساعدة والله في عون العبد ما كان العبد في عون أخيه

تم تعديل هذه المشاركة بواسطة السماك في 23 يونيو 2010 في 20:27

#5
السماك كتب:

جزاك الله الف خير يا اخ محمود

لكن يا ليتك تعدل على الأولى وتسيبنا من الثانية لأنها بصراحة فيه أشياء ما اخذناها وشكلي حيكون بايخ مع الدكتور لما اجيب أشياء ما اخذتها

وأسال الله ان يحرم وجهك على النار وشكرا لك يا أخي على المساعدة والله في عون العبد ما كان العبد في عون أخيه

خلاص ماشى هالغى ليك مووضوع الـ template دا

سهله اوى

ثوانى واعملها ان شاء الله

اسف ع التاخير الكهربا كانت فاصله

سلام عليكم

Software Developer
Mahmoudkelany.com


 

#6

لا تزعل يا اخى

هذا الكود مقارب جدا لتبعك

ولتحميل الكود

QueueTest.rar

queue.h file

const int Size = 10 ;
class queue
{
private:
	int Head,Tail;
	int queueArray[Size];
public:
	queue();
	bool IsEmpty();
	bool IsFull();
	bool Add(int Item);
	void Remove();
	void Print();
	void search(int a );
	void sortid();
};

queue.cpp file

#include "Queue.h"
#include<iostream>
using namespace std;
queue::queue()
{
	Head = 0;
	Tail = 0;
}


bool queue::IsEmpty(){
	return Head == Tail;
}

bool queue::IsFull(){
	return (Tail+1) % Size == Head;
}

bool queue::Add(int item){
	if(!IsFull()){
		queueArray[Tail] = item;
		Tail = (Tail+1) % Size;
		return true;
	}
	else{
		cout<<"Sorry add failed : The queue is Full."<<endl;
		return false;
	}
}

void queue::Remove(){
	if(!IsEmpty()){
		int item = queueArray [Head];
		Head = (Head+1) % Size;
		cout<<"The removed item : "<< item <<endl ;
	}else{
		cout<<"Sorry remove failed : The queue is Empty."<<endl;
		}
}

///////


void queue::sortid () {
	for(int i = Head ; i <  Tail ;  i++){
		for(int j = 0; j <  Tail; j++){

			if(queueArray<queueArray[j]){
				int temp = queueArray;
				queueArray = queueArray[j];
				queueArray[j] = temp;
			}

		}
	}
} 


void queue:: search (int searchItem){ 
	for ( int i =Head ; i < Tail ; i++){ 
		if ( queueArray == searchItem){
			cout <<"The index of the searched item = " << i <<endl;
		}
		if ( i == Tail-1 && queueArray  != searchItem ){
			cout<<"Not found."<<endl;
		}
	}
}  


void queue::Print(){ 
	if (!IsEmpty()){ 
		cout<<"The qeueu items is : "<<endl;
		for (int i = Head; i < Tail ; i++){ 
			cout << "item(" << i << ") = " << queueArray << endl; 
		} 
	} 
	else{
		cout<<"Queue empty."<<endl;
	}
}

main.cpp file

#include<iostream>
#include<string>
#include"Queue.h"  
using namespace std;
void main(){

	queue myQueue ; 

	int choise  = 0 ; 
	cout <<"1. Add number."<<endl<< "2. Delete number"<< endl<<"3. Sort number."
		<< endl <<"4. Search."<<endl<<"5. Print"<<endl<<"6. Quit."<<endl;

	while(choise != 6){
		cout<< "\t\t\t\t\tEnter your choise : ";
		cin >> choise; 

		switch(choise){
			case 1:
				int item ; 
				cout <<"Enter number in queue : ";
				cin>> item;
				myQueue.Add(item);
				break;

			case 2:
				myQueue.Remove();
				break;

			case 3:
				myQueue.sortid();         
				cout<<"Queue sorted : press 5 to print it."<<endl;
				break;

			case 4:
				int searchItem;
				cout<<"Enter Search Item : ";
				cin>>searchItem;
				myQueue.search (searchItem) ;
				break;

			case 5:
				myQueue.Print();          
				break;
		} 
	}
}

وان قدرت اعدل على بتاعك هاحاول فيه محاوله اخيره وربنا يسهل ان شاء الله

سلام عليكم

Software Developer
Mahmoudkelany.com


 

#7

تعديل على الكود تبعك

والحمدلله قدرت اعدله

ولتحميل كود البرنامج

QueueCustom.rar

queue.h file

const int MAX_ITEMS = 10;  

class Queue {  
public:  
	Queue();  
	int Enqueue(int);  
	int Dequeue();  
	void sortid ();  
	int IsFull();  
	int IsEmpty();  
	int search (int);  
	void Print();  

private:  
	int items[MAX_ITEMS];  
	int front, rear;  
};

queue.cpp file

#include "Queue.h"
#include <iostream> 
using namespace std; 
Queue::Queue(){  
	front = 0;  
	rear = 0;  
}  

// add element to the queue
int Queue::Enqueue(int a){  
	if (!IsFull()){  
		items[rear] = a;  
		rear = ( rear + 1 ) % MAX_ITEMS ;
		return 1; 
	}
	else{
		cout<<"Queue is Full."<<endl;
		return 0;  
	}
}  

// delete element from the queue
int Queue::Dequeue() {  
	if (!IsEmpty()){  
		int a = items [front] ;
		front = ( front + 1) % MAX_ITEMS ;
		cout<<"Removed Item : "<< a <<endl;
		return 1 ;
	}else{
		cout<<"Queue is empty."<<endl;
		return 0;  
	}
}  

int Queue::IsFull(){  
	return rear+1 % MAX_ITEMS == front ;  
}  

int Queue::IsEmpty(){  
	return front == rear;  
}  

// search function 
int Queue :: search (int a ){  
	for ( int i =front ; i < rear ; i++){  
		if ( items == a){ 
			cout <<"The index of the searched item = " << i <<endl; 
			return 1 ;
		} 
		if ( i == rear-1 && items  != a ){ 
			cout<<"Not found."<<endl; 
			return 0 ; 
		} 
	} 
	return 0 ;
}

// sort function 
void Queue :: sortid () { 
	for(int i = front ; i <  rear ;  i++){ 
		for(int j = 0; j <  rear; j++){ 

			if(items<items[j]){ 
				int temp = items; 
				items = items[j]; 
				items[j] = temp; 
			} 
		} 
	} 
}  
// print function 
void Queue::Print(){  
	if (!IsEmpty()){  
		cout<<"The qeueu items is : "<<endl; 
		for (int i = front; i < rear ; i++){  
			cout << "item(" << i << ") = " << items << endl;  
		}  
	}  
	else{ 
		cout<<"Queue empty."<<endl; 
	}
}

main.cpp file

#include<iostream> 
#include"Queue.h" 
using namespace std; 
void main() {  
	Queue q ;  
	int n = 0 ;

	cout <<"1. Add number."<<endl<< "2. Delete number"<< endl<<"3. Sort number." 
		<< endl <<"4. Search."<<endl<<"5. Print"<<endl<<"6. Quit."<<endl;

	while (n != 6){  
		cout<< "\t\t\t\t\tEnter your choise : "; 	
		cin >> n ;  
		if ( n == 1){  
			int a ;  
			cout <<"Enter number in queue "; 
			cin>>a; 
			q. Enqueue(a) ;  
		}  
		else if (n==2){  
			q.Dequeue ();     
		}  
		else if (n==3){  
			q.sortid();
			cout<<"Queue sorted : press 5 to print it."<<endl; 
		}  
		else if (n==4){ 
			int searchItem; 
			cout<<"Enter Search Item "; 
			cin>>searchItem; 
			q.search (searchItem) ;         
		}  
		else if (n==5){  
			q.Print();           
		}  
	}  
}
1

Software Developer
Mahmoudkelany.com


 

#8

جزاااااااااااااااااااااااااااااااااااك الله كل خيييييييييير

ماشاء الله علـــــــــــيك

المفروض ضمن المشرفين (:

والله انت سادعتني ووقفت معي الله ييسر لك كل خير

:rose:

#9
السماك كتب:

جزاااااااااااااااااااااااااااااااااااك الله كل خيييييييييير

ماشاء الله علـــــــــــيك

المفروض ضمن المشرفين (:

والله انت سادعتني ووقفت معي الله ييسر لك كل خير

:rose:

جزانا واياكم كل خير

الله يخليك لس لتك الدرجه

اللهم امين

ييسر لينا كل خير جميعا ان شاء الله

Software Developer
Mahmoudkelany.com


 

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