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

palindrome with stack and queue

بدأه no yes في 2 يوليو 2011 · 9 رد · 1,027 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

حاولت في حل هذا السؤال

وقد نجحت في حل سؤال يشابهه قليلا في السابق. بفضل الله ثم الاعضاء الصبورين معي :)

Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack

the program should then verifty whether the line of text is a palindrome

حقا لم اعرف كيف اقسم كلمه من نوع سترينغ الى رموز (كركتر)

لم يقم السؤال بطلب مني انه اكتبه الكود كفنكشن او ممبر فنكشن

لكنني حاولت عليه كنون ممبر فنكشن :)

مثال:

ادخلت اسم رادار لأتأكد ان كانت palindrome ام لا

اولا اقوم بتجزئة الكلمه المعرفه سابقا من نوع سترينغ الى كركتر

ثم اقوم بوضع كل كركتر في stack و كذلك في queue

ثم اقوم بالمقارنه palindrome

#include <iostream>
#include<string>
using namespace std; 



class queue
{
private:
	char queueFront;
	char queueRear;
	int maxQueueSizs;
	int count;
	char *list;

public:
	bool isEmptyQueue()
	{
		return(count==0);
	}


		bool isFullQueue()
		{
			return(count == maxQueueSizs);
		}


		void initializeQueue()
	{
		queueFront=0;
	queueRear=maxQueueSizs-1;
	count=0;
	}

		char front()
		{
			return list[queueFront];
		}


		char back()
		{
			return list[queueRear];
		}



		void addQueue(const char& newElemnt)
		{
			if(! isFullQueue())
			{
			queueRear=(queueRear+1)%maxQueueSizs;
		count ++;
		list[queueRear]=newElemnt;
		}
			else
			cout<<" can not add to a full queue."<<endl; 
            		}


			void deleteQueue()
			{
				if(! isEmptyQueue())
				{
			count--;
			queueFront=(queueFront+1)%maxQueueSizs;
			}
				else
					cout<<"can not remove from empty queue."<<endl;
			}

};



class stackType
{
private:
char *list;
int stackTop;
int maxSize;

public:

stackType( int size=100)
{
if(size<=100)
maxSize=100;
else
maxSize=size;
stackTop=0;
list=new int[maxSize];
}

int isEmpty()
{
bool found=true;
if(stackTop==0)
return 1;
else
return 0;
}


int isFull()
{
bool found=true;
if(stackTop==maxSize)
return 1;
else
return 0;
}

char push(char item)
{
if(isFull())
cout<<"full stack";
else
{
list[stackTop]=item;
stackTop++;
}
}

void pop()
{
if(isEmpty())
cout<<"is Empty";
else
stackTop--;
}

char Top()
{
if(stackTop !=0)
return list [stackTop-1];
else
return -1;
}

};





void palindrome(string word,int length) 
{ 
bool found;
stackType s;
queue s1; 

for(int i=0;i<length; i++)
{
	s.push(word);
	s1.addQueue(word);
} 
while(!s.isEmpty()) 
{
	if(s.Top()!=s1.front()) 
{
found = false;
      break;
}
else 
{ 
s.pop(); 
s1.deleteQueue(); 
found= true;
} 
}// end foor loop
 if( true)
	   cout << "It is a palindrome";
 else
	 cout << "It is a not palindrome";
} //end function






int main()
{
string word;
int length;
cout<<"enter  length of the word."<<endl;
cin>>length;

cout<<"please enter your string word"<<endl;
cin>>word;


palindrome(word,length);

return 0;


}
#2

المسألة على كل تعتمد

هل السترينغ من نوع السترينغ القديم* C string -char

في هذه الحالة هي مجرد مصفوفة تفحص فيها الشرط != null

هل السترينغ من نوع string الخاص بالسي بلس بلس ، في هذه الحالة افضل ، لا تنسى ان المعامل [] مزايد في السي بلس بلس overloaded وهنا تستطيع الحصول على الحرف من خلال

string s = "this is a string";
char c = s [3];

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#3

انا اقصد من سترينغ المستخدمه في سي بلاس بلاس

لنفرض انني قمت بتعريف متغير من نوع سترينغ في المين فنكشن..

ثم قمت بعمل اتصال ( كول فنكشن)

والفنكشن عبارة عن

void palindrome(string word,int length)  
{  
bool found; 
stackType s; 
queue s1;  

for(int i=0;i<length; i++) 
{ 
        s.push(word); 
        s1.addQueue(word); 
}  
while(!s.isEmpty())  
{ 
        if(s.Top()!=s1.front())  
{ 
found = false; 
      break; 
} 
else  
{  
s.pop();  
s1.deleteQueue();  
found= true; 
}  
}// end foor loop 
 if( true) 
           cout << "It is a palindrome"; 
 else 
         cout << "It is a not palindrome"; 
} //end function

تعتبر طريقة تعاملي مع الكود صحيحة؟

وهنا تغيير بسيط في الكود

void palindrome(string word,int length)  
{  
bool found; 
stackType s; 
queue s1;  
 char c=word[];
for(int i=0;i<length; i++) 
{ 
        s.push(word); 
        s1.addQueue(word); 
}  
while(!s.isEmpty())  
{ 
        if(s.Top()!=s1.front())  
{ 
found = false; 
      break; 
} 
else  
{  
s.pop();  
s1.deleteQueue();  
found= true; 
}  
}// end foor loop 
 if( true) 
           cout << "It is a palindrome"; 
 else 
         cout << "It is a not palindrome"; 
} //end function

تم تعديل هذه المشاركة بواسطة no yes في 2 يوليو 2011 في 21:15

#4

بس علشان اكون فاهم ، المرة هذه بدل ما تقارن بين استاكين راح تقارن بين استاك و كيو؟

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#5

صحيح ماتعتقد

سأضع مثالا هنا

الكلمه التي اريد ادخالها في المين فنكشن فرضا هي (مبرمج) من نوع سترينغ

ثم اقوم بعمل اتصال للفنكشن الموجود في الخارج ( نون ممبر فنكشكن)

واجزء الكلمه من رادار الى ( م ب ر م ج) واضع هذه الرموز التي قسمتها انا في ستاك وفي كيويو

سيسهل المقارنه عليي

لانه الاندكس الموجود في الكيويو سيكون معكوس الاندكس الموجود في الستاك

واقارن

قمت بوضع تحرير في الاعلى :)

تم تعديل هذه المشاركة بواسطة no yes في 2 يوليو 2011 في 21:23

#6

حسنا جرب البرنامج اولا واذا لم يعمل نبدأ في تتبع الاخطاء ، بس الامر بيكون اسهل من البرنامج الاول لانك الان فقط تحتاج تعديلات بسيطة

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#7

للأسف لم يعمل

#8

سانظر فيه في الغد ، gotta sleep bro

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#9

أنظر هذا المثال:

#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
using std::string;
using std::cin;
using std::cout;

bool isPalindrome(string& str);

int main()
{
   string value;
   char c = 'n';

   do 
   {
      cout << "enter value: ";
      cin >> value;

      cout << "is string '" << value
          << "' palindrome: " << std::boolalpha
          << isPalindrome(value);

      cout << "\nquit (y/n):";
      cin >> c;
      cout << endl;

      c |= 0x20;

   }
   while (c != 'y');
}

bool isPalindrome(string& str)
{
   std::transform(str.begin(), str.end(), str.begin(), ::tolower);

   int count = str.length();
   int head = 0, tail = count - 1;
   count >>= 1;


   while(count--)
      if (str[head++] != str[tail--]) return false;

   return true;
}

و الله ولي التوفيق

تم تعديل هذه المشاركة بواسطة محمد علاء الدين في 3 يوليو 2011 في 01:55

مدونتي: C++ Tips and Tricks

#10

^^

اخي هذا السؤال موجود في الانترنت مثله تماما..لكنني لا استطيع اخذه

كل ماتمناه هو الاعتماد على نفسي ,,فقد قمت بحله ,, فقط تنقصه بعض الاضافات

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