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

** مساعدة في تعديل برنامج في ال List ..

مغلق
بدأه :: إسـتبرق C++ :: في 29 أكتوبر 2005 · 5 رد · 649 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

أعزائي أصحابي الكِـرام ..

لقد عملت برنامج ، و صادفتني عدة مشكلات فيه ، :(

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

مع خالص الشكر والإحترام ..

و منكم دائماً نستفيد :rolleyes:

هذا هو البرنامج ....

#include<iostream.h>
#include<string.h>

typedef struct node *ptrType;
typedef int itemType;

struct node{
	char code;
	int grade;
	int nCredits;
	ptrType next;
};

class course{
protected:
	ptrType head;

public:
	course(){ head=NULL;};
	int isFull(ptrType temp){ return (temp==NULL);};
	int isEmpty(){ return (head==NULL);};

	int addFront(itemType newItem)
	{
  ptrType temp=new node;
  if (isFull(temp))
 	 return 0;

  else{	
 	 temp->code= newItem;
 	 temp->grade= newItem;
 	 temp->nCredits= newItem;
 	 temp->next= head;
 	 head= temp;
 	 return 1;
  }
	};

	int deleteFront(){
  node* temp=head;
  head= head->next;

  delete temp;
	}

  int getHead (char item,struct node c ){
 	 ptrType cur;
 	 for (cur=head;cur!=NULL && cur->code!=item;cur=cur->next)
    if (cur==NULL){
   	 return 0;}
                else{
          strcpy(c.code,cur->code);
       strcpy(c.grade,cur->grade);
       c.nCredits=cur->nCredits;
                   cout<<cur->code
        <<"\n"<<cur->grade
        <<"\n"<<cur->nCredits;
                   }
      return 1;
  };
  int set (char item, struct node c){
 	 ptrType cur;
 	 for (cur=head;cur!=NULL && cur->code!=item;cur=cur->next )
             if (cur == NULL ){
                return 0;}
             else{ 
                strcpy(cur->code ,c.code);
                strcpy(cur->grade ,c.grade);
                cur->nCredits=c.nCredits;
                   }
             return 1;
                  };


        int print(){
             if (head == NULL){
             cout<<"there is no data";
             return 0;}
             else{
               for (ptrType cur=head; cur != NULL; cur = cur -> next)
                 cout<<cur->code
      <<"\n"<<cur->grade
      <<"\n"<<cur->nCredits;
                   }
      }
};


int main (){
	class course R;
	struct node N;
	char Ccode;
	int choice;

  cout<<"\n1.Add course details"<<"\n2.Remove course details"
 	 <<"\n3.Set course details"<<"\n4.Get course details"
      <<"\n5.Print course details"
 	 <<"\n6.Calculate GPA"
 	 <<"\n7.Exit";
  cin>>choice;
       
	while (choice !=7)
  {
   switch(choice){
         case 1:
           cout<<"Enter the code of the course:";
           cin>>N.code;
           cout<<"Enter the grade of the course:";
           cin>>N.grade;
           cout<<"Enter the number of credits of the course:";
           cin>>N.nCredits;
           R.addFront(N);
     break;
        
  case 2:
           cout<<"Enter the code of the course you want to delete :";
           cin>>Ccode;
           R.deleteFront (Ccode);
     break;
        
     case 3:
           cout<<"Enter the code of the course you want to set :";
           cin>>Ccode;
           cout<<"Enter the code of the course:";
           cin>>N.code;
           cout<<"Enter the grade of the course:";
           cin>>N.grade;
           cout<<"Enter the number of credits of the course:";
           cin>>N.nCredits;
           N.setHead (Ccode,N);
           break;

      case 4:
           cout<<"Enter the code of the course you want to get :";
           cin>>Ccode;
           R.getHead (Ccode,N);
           break;

      case 5:
           R.print();
           break;
        }
           
   cout<<"\n1.Add course details"<<"\n2.Remove course details"
    <<"\n3.Set course details"<<"\n4.Get course details"
       <<"\n5.Print course details"<<"\n6.Exit";
         cin>>choice;

   case 6:
    float sum=0.0;
    int total=0;
    for(ptrType cur=R.getHead();cur!=NULL;cur=cur->next)
     if(cur->grade=='A')
      sum+=4*cur->nCredits;

     else if{
      (cur->grade=='B')
      sum+=3*cur->nCredits;
     }

     else if{
      (cur->grade=='C')
      sum+=2*cur->nCredits;
     }

     else if{
      (cur->grade=='D')
      sum+=1*cur->nCredits;
     }

     else 
      return 0;
     
	}
           
	return 0;
                  

}

بسم الله الرحمن الرحيم

" أولئك لهم جنات عدن تجري من تحتهم الأنهار

يحلون فيها من أساور من ذهب ويلبسون ثيابا خضرا

من سندس و إسـتـبـرق متكئين فيها على الأرائك

نعم الثواب وحسنت مرتفقاً "

صدق ذو الجلال والإكرام

#2

االعضو الكريم

توجد اخطاء كثيرة معظمها بسيط جداً. لدرجة انى اعتقد انها اخطاء فى النقل من كتاب. لكن انت قلت انك عملته :D

الاخطاء 21 اصلحت منها 18 اما الثلاثة الاخرين فتركتهم لك لحد ما تعرف انت عايز ايه من البرنامج يعنى دالة اسمها deleteFront

تديها رقم ليه ما هى بتمسح الاول.

يعنى

المهم انا ليا بعض الملاحظات ممكن تفيد

راعى استعمال الاقواس حتى لو البلوك فيه جمله واحدة. البلوك يعنى زى الشروط و الحلقات.

استعمال اسطر عملاقة غير مطلوب

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

و لذلك ضع كل الاقواس و راعى سطولة القراءة الى اقصى مدى.

هنا الكود الجديد

التغيرات مضاف معها تعليق // Change و كذلك ما يحتاج الى تغير // need change

#include<iostream.h>
#include<string.h>

typedef struct node * ptrType;
typedef int itemType;

struct node{
	char code;
	int grade;
	int nCredits;
	ptrType next;
};

class course{
protected:
	ptrType head;

public:
	course(){ head=NULL;};
	int isFull(ptrType temp){ return (temp==NULL);};
	int isEmpty(){ return (head==NULL);};

	int addFront(node newItem)   	 // Change
	{
  ptrType temp=new node;
  if (isFull(temp))
 	 return 0;
  else{
 	 temp->code= newItem.code; 	 // Change	
 	 temp->grade= newItem.grade; 	 // Change
 	 temp->nCredits= newItem.nCredits;	// Change
 	 temp->next= head;
 	 head= temp;
 	 return 1;
  }
	};

	int deleteFront(){
  node* temp=head;
  head= head->next;
  delete temp;
	}

	int getHead (char item,struct node c ){
  ptrType cur;
  for (cur=head;cur!=NULL && cur->code!=item;cur=cur->next)
 	 if (cur==NULL){
    return 0;
 	 }else{
    strcpy(&c.code, &cur->code);	// Change
    c.grade = cur->grade; 	 // Change
    c.nCredits=cur->nCredits;
    cout<<cur->code	<<"\n"<<cur->grade<<"\n"<<cur->nCredits;
 	 }
 	 return 1;
	};
	int set (char item, struct node c){
  ptrType cur;
  for (cur=head;cur!=NULL && cur->code!=item;cur=cur->next )
            if (cur == NULL ){
    return 0;
 	 }
            else{
    strcpy(&cur->code , &c.code);	// Change
    cur->grade  = c.grade; 	 // Change
    cur->nCredits=c.nCredits;
 	 }
            return 1;
	};

	int print(){
  if (head == NULL){
            cout<<"there is no data";
            return 0;
  }else{
 	 for (ptrType cur=head; cur != NULL; cur = cur -> next)
    cout<<cur->code<<"\n"<<cur->grade<<"\n"<<cur->nCredits;
  }
	}
};


int main ()
{
	class course R;
	struct node N;
	char Ccode;
	int choice;

	cout<<"\n1.Add course details"
  <<"\n2.Remove course details" 
  <<"\n3.Set course details"
  <<"\n4.Get course details"
  <<"\n5.Print course details"
  <<"\n6.Calculate GPA"
  <<"\n7.Exit";

	cin>>choice;

	while (choice !=7){
  switch(choice){
        case 1:
 	 cout<<"Enter the code of the course:";
 	 cin>>N.code;
 	 cout<<"Enter the grade of the course:";
 	 cin>>N.grade;
 	 cout<<"Enter the number of credits of the course:";
 	 cin>>N.nCredits;
 	 R.addFront(N);  // Change
 	 break;
  case 2:
 	 cout<<"Enter the code of the course you want to delete :";
 	 cin>>Ccode;
 	 R.deleteFront (Ccode);  // need change
 	 break;
  case 3:
 	 cout<<"Enter the code of the course you want to set :";
 	 cin>>Ccode;
 	 cout<<"Enter the code of the course:";
 	 cin>>N.code;
 	 cout<<"Enter the grade of the course:";
 	 cin>>N.grade;
 	 cout<<"Enter the number of credits of the course:";
 	 cin>>N.nCredits;
 	 N.setHead (Ccode,N);  // need change
 	 break;
  case 4:
 	 cout<<"Enter the code of the course you want to get :";
 	 cin>>Ccode;
 	 R.getHead (Ccode,N);
 	 break;
  case 5:
 	 R.print();
 	 break;
      // change
//change (This section is repeated)
/*  
  cout<<"\n1.Add course details"<<"\n2.Remove course details"
 	 <<"\n3.Set course details"<<"\n4.Get course details"
 	 <<"\n5.Print course details"<<"\n6.Exit";
        cin>>choice;
*/
  case 6:
 	 float sum=0.0;
 	 int total=0;
 	 for(ptrType cur=R.getHead();cur!=NULL;cur=cur->next){	// need Change
    if(cur->grade=='A'){ 	 // change
   	 sum+=4*cur->nCredits;
    }else{     	 //change
   	 if(cur->grade=='B'){
      sum+=3*cur->nCredits;
   	 }else{      //change
      if	(cur->grade=='C'){
     	 sum+=2*cur->nCredits;
      }else{   	 //Change
     	 if(cur->grade=='D'){
        sum+=1*cur->nCredits;
     	 }else{
        return 0;
     	 }   	 // change
      }      // change
   	 }     	 // change
    }        // change
 	 }
  }
	}
	return 0;
}
#3

أهـــلاً ..

شكراً جزيلاً أخي على تقديم يد المساعده ..

و ربي يسعدك دنيا و آخره .. :rolleyes:

و شكراً على التوضيح كمان ، معلش أخي أحياناً الواحد بيصير كذا :wacko:

لقد أضفت بعض التعديلات وضبط معاي ،

ما عدا إيرور واحد في الـ getHead()

case 6:
   float sum=0.0;
   int total=0;
   for(ptrType cur=R.getHead(Ccode,N);cur!=NULL;cur=cur->next)
    if(cur->grade=='A')
     sum+=4*cur->nCredits;

'initializing' : cannot convert from 'int' to 'struct node *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

حاولت فيه ، أشيل و أحط بس ما يضبط وياي .. :( ...؟؟!!!!

بسم الله الرحمن الرحيم

" أولئك لهم جنات عدن تجري من تحتهم الأنهار

يحلون فيها من أساور من ذهب ويلبسون ثيابا خضرا

من سندس و إسـتـبـرق متكئين فيها على الأرائك

نعم الثواب وحسنت مرتفقاً "

صدق ذو الجلال والإكرام

#4

هذه نسخة تعمل ان شاء الله

قمت ببعض التعديلات و عليها كلمة // Modified

#include<iostream.h>
#include<string.h>

typedef struct node * ptrType;
typedef int itemType;

struct node{
	char code;
	int grade;
	int nCredits;
	ptrType next;
};

class course{
protected:
	ptrType head;

public:
	course(){ head=NULL;};
	int isFull(ptrType temp){ return (temp==NULL);};
	int isEmpty(){ return (head==NULL);};

	int addFront(node newItem)   	 // Change
	{
  ptrType temp=new node;
  if (isFull(temp))
 	 return 0;
  else{
 	 temp->code= newItem.code; 	 // Change	
 	 temp->grade= newItem.grade; 	 // Change
 	 temp->nCredits= newItem.nCredits;	// Change
 	 temp->next= head;
 	 head= temp;
 	 return 1;
  }
	};

	void deleteFront(){
  node* temp=head;
  head= head->next;
  delete temp;
	}

	ptrType getHead(){// Modified
  return(head);
	}

	int get(char item,struct node c ){// Modified
  ptrType cur;
  for (cur=head;cur!=NULL && cur->code!=item;cur=cur->next)
 	 if (cur==NULL){
    return 0;
 	 }else{
    strcpy(&c.code, &cur->code);	// Change
    c.grade = cur->grade; 	 // Change
    c.nCredits=cur->nCredits;
    cout<<cur->code	<<"\n"<<cur->grade<<"\n"<<cur->nCredits;
 	 }
 	 return 1;
	};
	int set (char item, struct node c){
  ptrType cur;
  for (cur=head;cur!=NULL && cur->code!=item;cur=cur->next )
            if (cur == NULL ){
    return 0;
 	 }
            else{
    strcpy(&cur->code , &c.code);	// Change
    cur->grade  = c.grade; 	 // Change
    cur->nCredits=c.nCredits;
 	 }
            return 1;
	};

	int print(){ // Modified
  if (head == NULL){
            cout<<"there is no data";
            return 0;
  }
  
  for (ptrType cur=head; cur != NULL; cur = cur -> next){
    cout<<cur->code<<"\n"<<cur->grade<<"\n"<<cur->nCredits;
  }
  return (1);
	}
};


int main ()
{
	class course R;
	struct node N;
	char Ccode;
	int choice;

	cout<<"\n1.Add course details"
  <<"\n2.Remove course details" 
  <<"\n3.Set course details"
  <<"\n4.Get course details"
  <<"\n5.Print course details"
  <<"\n6.Calculate GPA"
  <<"\n7.Exit";

	cin>>choice;

	while (choice !=7){
  switch(choice){
        case 1:
 	 cout<<"Enter the code of the course:";
 	 cin>>N.code;
 	 cout<<"Enter the grade of the course:";
 	 cin>>N.grade;
 	 cout<<"Enter the number of credits of the course:";
 	 cin>>N.nCredits;
 	 R.addFront(N);  // Change
 	 break;
  case 2:
 	 cout<<"Enter the code of the course you want to delete :";
 	 cin>>Ccode;
 	 R.deleteFront ();  // need change
 	 break;
  case 3:
 	 cout<<"Enter the code of the course you want to set :";
 	 cin>>Ccode;
 	 cout<<"Enter the code of the course:";
 	 cin>>N.code;
 	 cout<<"Enter the grade of the course:";
 	 cin>>N.grade;
 	 cout<<"Enter the number of credits of the course:";
 	 cin>>N.nCredits;
 	 R.set(Ccode,N);  // need change
 	 break;
  case 4:
 	 cout<<"Enter the code of the course you want to get :";
 	 cin>>Ccode;
 	 R.get (Ccode,N);
 	 break;
  case 5:
 	 R.print();
 	 break;
      // change
//change (This section is repeated)
/*  
  cout<<"\n1.Add course details"<<"\n2.Remove course details"
 	 <<"\n3.Set course details"<<"\n4.Get course details"
 	 <<"\n5.Print course details"<<"\n6.Exit";
        cin>>choice;
*/
  case 6:
 	 float sum=0.0;
 	 int total=0;
 	 for(ptrType cur=R.getHead();cur!=NULL;cur=cur->next){	// need Change
    if(cur->grade=='A'){ 	 // change
   	 sum+=4*cur->nCredits;
    }else{     	 //change
   	 if(cur->grade=='B'){
      sum+=3*cur->nCredits;
   	 }else{      //change
      if	(cur->grade=='C'){
     	 sum+=2*cur->nCredits;
      }else{   	 //Change
     	 if(cur->grade=='D'){
        sum+=1*cur->nCredits;
     	 }else{
        return 0;
     	 }   	 // change
      }      // change
   	 }     	 // change
    }        // change
 	 }
  }
	}
	return 0;
}

بس لازم تراجع الاداء بنفسك. بالبرنامج قد ىكون خالى من ااخطاء اللغوية و لكن لا يؤدى المطلوب

بالتوفيق ان شاء الله

#5

تسلم أخي PointerVC ، شغال الآن البرنامج ..

الله يعطيك العافيه إن شاء الله ..

و الله يقدرنا و نرد لك جمايلك علينا .. :rolleyes:

وشكراً لتقبلك كل إستفساراتي برحابة صدر بالإضافه للمعامله الطيبه :rolleyes: ..

شكراً ...

بسم الله الرحمن الرحيم

" أولئك لهم جنات عدن تجري من تحتهم الأنهار

يحلون فيها من أساور من ذهب ويلبسون ثيابا خضرا

من سندس و إسـتـبـرق متكئين فيها على الأرائك

نعم الثواب وحسنت مرتفقاً "

صدق ذو الجلال والإكرام

#6

شكراً على الكلام الطيب ربنا يهدينا جميعاَ للخير

عيد مبروك

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

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