السبت، 12 مارس 2011

كود سي بلس بلس يحدد اذا ما كان العدد يقبل القسمة على ثلاث

كود سهل وبسيط بالسي بلس بلس لتحديد اذا كان الرقم المدخل من قبل المستخدم يقبل القسمة على العدد ثلاث  او لا


#include<iostream.h>
main()
{
float a ;
cout <<" enter the number you want "<<endl ;
cin >>a ;
if(a%3==0)
cout<<"it is ok! "<<endl ;
else
cout<<" false " << endl;
return 0 ;
}

الخميس، 10 مارس 2011

برنامج الضرب بالسي بلس بلس

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


# include <iostream >
# include <conio>


int main()
{
     cout << "A multiplication table:"  <<endl<<endl;
       cout<< "   1  \t 2\t 3\t 4\t 5\t 6\t 7\t 8\t 9"   << "" << endl;


     for(int c = 1; c < 10; c++)
     {
          //cout << c << "|";
          for(int i = 1; i < 10; i++)
          {
          cout <<i<<"*"<<c<<"="<<i*c<<'\t';
          }
          cout << endl;
     }

     getch();
     return 0;
}

الأربعاء، 9 مارس 2011

كود سي بلس بلس لتمثيل الكيو لنك لست

برنامج اكثر من رائع لن تجده الا هنا لتمثيل الكيو لنك لست بلغة السي بلس بلس فهذا البرنامج يقوم ببناء  الكيو لنك لست باستخدام خاصية التمبليت التي تمكنك من اضافة اي نوع من المتغيرات الى الكيو   يحتوي البرنامج على كل الدوال التي تخص الكيو فقد تم بناء دالة الاضافة append بطريقة مبسطة وسهلة للفهم ودالة الحذف serve لحذف اول قيمة فالكيو فالكيو المميز فيها انها تتعامل ب FIFO بمعنى اول قيمة تمت اضافتها هيا اول قيمة يتم التعامل معها 

يوجد في هذا البرنامج دالة موف MOVE  التي تمكنك من تبديل اي قيمة بالكيو ووضعها على الفرونت


# include <iostream>
# include <conio>
# include <stdlib>

template <class T>
struct node
{

  T num;
  node<T> *next;

};

template <class T>
class queue
{

 private:
          node<T> *front;
          node<T>* rear;
          int count;

 public:
           queue();
          void append(int num);
          void move(int n);
          void print();
          void serve();
          bool isempty();

};

template <class T>
queue<T>::queue()
{
   front=NULL;
   rear=NULL;
   count=0;
 }

   template <class T>
bool queue<T>::isempty()
{
   if (front==NULL)

     return 1;
     else
     return 0;
 }
 template <class T>
void queue<T>::append(int num)
{
     node<T> *newnode;
     newnode=new node<T>;
     newnode->num=num;
     newnode->next=NULL;

     if(front==NULL)
     {
       front=newnode;
       rear=newnode;
     }
     else
     {
        rear->next=newnode;
        rear=newnode;

      }
      count++;

 }

 template <class T>
void queue<T>::move(int n)
{
   if(n<count && n!=0)
   {
   node<T>*current;
   current=front;
   int c= front->num;
   for(int i=1;i<=n;i++)
   {
     current=current->next;
     if(i==n)
     {
      front->num=current->num;
     }
   }

   current->num=c;
   }
   else
   {
   cout<<" \t \n The index not correct ."<<endl;
   }
 }

 template <class T>
 void queue<T>::serve()
 {

     node <T> *temp;
     temp=front;
     front=front->next;
     delete temp;
     count--;


 }

  template <class T>
  void queue<T>::print()
  {
     textcolor(90);
    cprintf("Queue is :");
    node <T> * current;
    current=front;
    while(current != NULL)
    {
      cout<<current->num<<"    ";
      current=current->next;
    }


    cout<<endl<<endl;
   }

 int main()
 {
    int n ,x,item,m,z;
    queue<int> q;

    cout<<"======  QUEUELINKLIST =========="<<endl<<endl;
    cout<<"  1: append item to the queue  "<<endl;
    cout<<"  2: move element to the front  "<<endl;
    cout<<"  3: serve from queue           "<<endl;
    cout<<"  press ( 0) to exist           "<<endl<<endl;
    cout<<"==============================="<<endl;
    cout<<" ===== PART ONE ( INTEGER )  ======";

      cout<<endl<<endl;

    do
    {

      cout<<" \t Enter your choice : ";
      cin>>x;
       cout<<endl;
       switch(x)
       {
            case 1 :
                   {
                       cout<<"Enter number of item to append :";
                       cin>>n;
                       for(int i=1;i<=n;i++)
                       {
                             cout<<"Enter The item  :";
                             cin>>item;
                             q.append(item);
                             cout<<endl;
                        }
                        q.print();

                     break;
                    }

            case 2 :
                 {
                      if(q.isempty())
                      {
                        cout<<"Queu is empty >"<<endl;
                      }
                      else
                      {
                    cout<<"Enter index of item to move :";
                    cin>>m;
                    q.move(m);
                    cout<<endl;
                    q.print();
                    cout<<endl;
                    }


                  break;
                  }
           case 3 :
                 {
                 if(q.isempty())
                 {
                    cout<<"Queue is empty ."<<endl;
                  }
                  else
                  {

                   cout<<"Enter number of item to serve :";
                   cin>>z;
                   if( z>n)
                   { cout<<"The#of item to sreve is larger tham # tem in queue ";
                     cout<<endl;
                   }
                   else
                   {
                   for(int i=0;i<z;i++)
                   {
                      q.serve();

                    }
                       q.print();
                       cout<<endl;
                  }

                 }
                    break;
                   }


         }
      }
      while( x != 0);


      return 0;
}

تمثيل الستاك لنك لست باستخدام ++c

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

يحتوي البرنامج على واجهة بالين تظهر فيهات كل العمليات التي يستطيع المستخدم تنفيذها من اضافة عناصر وحذف عناصر ومعرفة التوب في الستاك


# include <iostream>
# include <conio>

template <class T>
struct node
{
  T info;
  node<T> *link;

};

 template <class T>
 class stacktype
 {
    private:
              node<T> *stacktop;
              int count;



    public:
              stacktype();
              void inti();
              void push(const T& item);
              void pop();
              T top();
              bool isempty();
             int c_item();
             void print();


 };


  template <class T>
  int stacktype<T>::c_item()
  {
    return count;

  }

  template <class T>
  T stacktype<T>::top()
  {
  if(!isempty())
    return stacktop->info;
    else
    return false ;

  }



    template <class T>
  bool stacktype<T>::isempty()
  {
     if(stacktop==NULL)
     return true;
     else
     return false;

  }


  template <class T>
  stacktype<T>::stacktype()
  {

     stacktop=NULL;
     count =0;


  }

    template <class T>
  void stacktype<T>:: inti()
  {
         node<T> *temp;
      while(stacktop != NULL)
      {
          temp=stacktop;
          stacktop=stacktop->link;
          delete temp;
      }

  }

  template <class T>
  void stacktype<T>:: push(const T& item)
  {

            node<T> *newnode;
         newnode=new node<T>;
         newnode->info=item;
         newnode->link=stacktop;
         stacktop=newnode;
         count++;


  }

    template <class T>
  void stacktype<T>:: pop()
  {
           node<T> *temp;
      if(stacktop != NULL)
      {
          temp=stacktop;
          stacktop=stacktop->link;
          delete temp;
          count--;
          cout<<" you pop from  stack."<<endl;

      }
      else
         cout<<"cannot pop from empty stack."<<endl;

  }

      template <class T>
  void stacktype<T>::print()
  {
      node<    T> *current;
      current=stacktop;
      while(current != NULL)
      {
        cout<<current->info<< " ";
        current=current->link;
      }
  }

  int main()
  {

       stacktype<int> stack_link;
       int x,item,n;

       textcolor(43);
       cprintf("        case 1: push item in stack link.");
       cout<<endl;
       cprintf("        case 2:pop item from stack link.");
       cout<<endl;
       cprintf("        case 3: top of stack link.");
       cout<<endl;
       cprintf("        case 4:print item in stack.");
       cout<<endl;
       cprintf("       press zero (0) to end.");
       cout<<endl<<endl;


       stack_link.inti();

       do
       {
          cprintf("Enter your choise :");
          cin>>x;
       switch(x)
       {
           case 1:
                 {
                    cout<<"Enter the number of item that u want push : ";
                    cin>>n;
                    for(int i=0;i<n;i++)
                    {
                         cprintf("     Enter item");cprintf("(");cout<<i;cprintf(")");cprintf(":");
                                     cin>>item;
                       stack_link.push(item);

                     }
                     cout<<endl;
                   break;
                 }

           case 2:
                {

                       stack_link.pop();

                       cout<<endl;
                     break;
                 }
           case 3:
                 {
                           if( stack_link.top()==0)
                           cout<<" the stack is empty"<<endl;
                           else
                         cout<<"stacktop is : "<<stack_link.top();
                         cout<<endl;

                  break;
                 }
           case 4:
                 {
                         
                           if( stack_link.top()==0)
                           {
                           cout<<" the stack is empty"<<endl;
                           }
                           else
                           {
                         cprintf("The stacklink is :");
                             stack_link.print();

                            }
                    cout<<endl;
                  break;
                }

           }// end switch
             } // end while

               while(x !=0);

   return 0;
  }