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

تصليح اخطاء في كود وتحسينه

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

ما تصليح وطرق تحسين الكود التالي ؟ علما بان وظيفته مدرجة كملاحظة في بدايته...

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
/*
.
.
.
there is adding recordes while the user want this ,whan we finish,we have to
print all info in records and again recordes the user enter the namne for it after searching


.

.
.*/

/*The general goal of this progect is to make radix sort depending on name
 for a group of studentes recordes */
// here, I declare a struct
typedef struct node {
                int Id;
                char name[50];
                float AVG;
                struct node *next;
                    }student;

int main()
{   student *head,
             *newn,
             *k;
    char a,
         Title[50],
         names[50],
         b;
 printf("do you want to enter first value? yes press y ,no press any other key\n");
 scanf("%s",&a);
   if(a='y')
   { printf("if you want to read from file press f, from kayboard press k \n");
   scanf("%s",&b);
 printf("do you want to enter another new value? yes press y ,no press any other key\n");
 scanf("%s",&a);
      switch(b)
       {
      case 'f': FILE *inp;
                inp = fopen(Title, "r");
                do  {
                    printf("please,Enter the complete path\nfor file  you want to read data from it: ");
                    gets(Title);
                    }while(!(inp = fopen(Title, "r")));


                head=(student*)malloc(sizeof(student));
                fscanf(inp, "%d", &head->Id);
               fgets(inp, "%s", &head->name);
               fscanf(inp, "%f", &head->AVG);
               break;

      case 'k':printf("Enter the No,Name,AVG");
               head=(student*)malloc(sizeof(student ));
               scanf(" %d%s%f",&head->Id,&head->name,&head->AVG);
                break;

      default : printf("error");
      break;
       }


   }

   printf("do you want to enter another new value? yes press y ,no press any other key\n");
 scanf("%s",&a);
   while(a='y')
   { printf("if you want to read from file press f, from kayboard press k \n");
   scanf("%s",&b);
     switch(b)
       {
      case 'f': FILE *inp;
                inp = fopen(Title, "r");
                do  {
                    printf("please,Enter the complete path\nfor file  you want to read data from it: ");
                    gets(Title);
                    }while(!(inp = fopen(Title, "r")));


                newn=(student*)malloc(sizeof(student));
                fscanf(inp, "%d", &newn->Id);
               fgets(inp, "%s", &newn->name);
               fscanf(inp, "%f", &newn->AVG);
               newn->next=head;
               head=newn;
               break;

      case 'k': printf("Enter the No,Name,AVG");
              newn=(student*)malloc(sizeof(student ));
               scanf(" %d%s%f",&newn->Id,&newn->name,&newn->AVG);
                newn->next=head;
                head=newn;
                break;

      default : printf("error");
      break;
       }
        printf("do you want to enter another new value? yes press y ,no press any other key\n");
        scanf("%s",&a);


   }


         k=head;
         while(k!=NULL)
           {
           printf(" %d%s%f",&k->Id,&k->name,&k->AVG);
           k=k->next;
           }
           free(k);
           // here we will print all data about an name ,the user enter it

           printf("Enter the Name to search and give you all information about it");
          gets(names);

         k=head;
         while(k!=NULL&&(strcmp(&k->name,names)==0))
           {
           printf(" %d%s%f",&k->Id,&k->name,&k->AVG);
           k=k->next;
           }

    return 0;
}

وسامحونا يا اعضاء كرام غلبناكم..

#2

ارجوكم الرد يا جماعة صحيح الكود طويل لكن نصفه مكرر حيث احتجت لنسخ الكود نفسه لاول قيمة يتم ادخالها ولباقي القيم كل على حدة .. ارجو ا من الخبراء المساعدة؟

#3

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

#4

السلام عليكم

1- لا تنسخ ابدا ( واعني ذلك بشدة ) كودا من منطقة في برنامجك و تلصقه في منطقة أخرى ( حتى ولو هددوك بالقتل laugh.gif ) لان النتائج وخيمة :

* اذا كان هناك خطأ في الكود الاصلي يسصبح عليك معالجته في كل مكان نسخته اليه wacko.gif

* سيصبح حجم الكود اكبر وسيصعب عليك متابعة كل شيء.

* اذا اردت تغييره فسيجب عليك التغيير في كل مكان نسخته اليه.

اذا مالحل ؟؟؟؟ استخدم الــfunctions

2- عند التصريح بمؤشر لا تنس ان تعطيه قيمة دائما و ابدا ( لانك لا تعلم متى يسبب لك مشاكل من نوع محاولة تحريره وهو دون قيمة ) و ابسط قيمة هي NULL

اليك بعض الاخطاء في كودك :

- استعمال الــ'=' بدل الـــ'==' في عمليات المقارنة.

- فتح ملف بــfopen دون غلقه بـــfclose

- عدم تحرير الذاكرة بعد الاستخدام,

بالاضافة الى بعض الاخطاء الابسط.

اليك الكود الجديد (قارن حجم الدالة main هنا مع حجمها قبل ) :

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

typedef struct node {
	int Id;
	char name[50];
	float AVG;
	struct node *next;
} student;
// depending on user's input, reads a student from file or console, adds it to list and returns the head of list
student* read_student(student* head);
// reads one student from a file
student* read_student_from_file();
// reads one student from console
student* read_student_from_console();
// adds a student to head of a linked list of students and returns the new head
student* add_student_to_list(student* head,student* s);
// prints the list od students
void print_students_list(student* head);
// search the list of student for a student with a given name
student* search_by_name(student* list_of_students,const char* name);
// frees the memory used
void free_memory(student* head);

int main() {
	student *head = NULL;
	char a[2] // i used an array to ignore the carriage return read when a user enters data
	, names[50];
	printf("do you want to enter first value? yes press y ,no press any other key\n");
	scanf("%s", a);
	if (a[0] == 'y') {
    	head = read_student(head);
	}

	printf("do you want to enter another new value? yes press y ,no press any other key\n");
	scanf("%s", a);
	while (a[0] == 'y') {
    	head = read_student(head);
    	printf("do you want to enter another new value? yes press y ,no press any other key\n");
    	scanf("%s", a);
	}


	// print the list of students
	print_students_list(head);
	// here we will print all data about an name ,the user enter it

	printf("Enter the Name to search and give you all information about it : ");
	scanf("%s",names);

	search_by_name(head,names);
	// finally, we shall free the memory
	free_memory(head);
	return 0;
}

student* read_student(student* head){
	char b[2];
	student* newn = NULL;
	printf("if you want to read from file press f, from kayboard press k \n");
	scanf("%s", b);
	switch (b[0]) {
    	case 'f': 
        	newn = read_student_from_file();
        	head = add_student_to_list(head,newn);
        	break;

    	case 'k': 
        	newn = read_student_from_console();
        	head = add_student_to_list(head,newn);
        	break;

    	default: printf("error");
        	break;
	}
	return head;
}


student* read_student_from_file(){
	char Title[50];
	student* new_student = NULL;
	FILE* inp = NULL;
	do {
    	printf("please,Enter the complete path\nfor file  you want to read data from it: ");
    	gets(Title);
    	inp = fopen(Title, "r");
	} while (!inp);


	new_student = (student*) malloc(sizeof (student));
	fscanf(inp, "%d", &new_student->Id);
	fscanf(inp, "%s", &new_student->name);
	fscanf(inp, "%f", &new_student->AVG);
	fclose(inp);
	return new_student;
}
student* read_student_from_console(){
	student* new_student = NULL;
	printf("Enter the No,Name,AVG : ");
	new_student = (student*) malloc(sizeof (student));
	scanf("%d%s%f", &new_student->Id, new_student->name, &new_student->AVG);
	return new_student;
}
student* add_student_to_list(student* head,student* s){
	if(s != NULL){
    	s->next = head;
    	head = s;
	}
	return head;
}

void print_students_list(student* head){
	student* iterator = NULL;
	if(head == NULL){
    	printf("There are no students in the list.\n");
	}else{
    	iterator = head;
    	while(iterator != NULL){
        	printf("Student : \n\t ID : %d\n\tName : %s\n\tAverage : %.2f\n", iterator->Id, iterator->name, iterator->AVG);
        	iterator = iterator->next;
    	}
	}
}
student* search_by_name(student* list_of_students,const char* name){
	student* iterator = NULL;
	bool is_found = false;
	if(list_of_students == NULL){
    	printf("The list of students is empty !\n");
	}else{
    	iterator = list_of_students;
    	while(iterator != NULL){
        	if(stricmp(iterator->name,name) == 0){
            	is_found = true;
            	break; // we found the student
        	}
        	iterator = iterator->next;
    	}
    	// check if we exited
    	if(is_found){
        	printf("Student found !\n\tID : %d\n\tName : %s\n\tAverage : %.2f\n",iterator->Id,iterator->name,iterator->AVG);
    	}else{
        	printf("Sorry ! no student with that name !\n");
    	}
	}
}


void free_memory(student* head){
	student* iterator = NULL;
	while(head != NULL){
    	iterator = head;
    	head = head->next;
    	free(iterator);
	}
	head = NULL;
}

بالتوفيق للجميع

تم تعديل هذه المشاركة بواسطة OmniVore في 2 مارس 2012 في 10:20

1

وما كلُّ ذِي لُبٍّ بمِؤْتيكَ نُصحَه ****** وما كلُّ مؤْتٍ نُصحَه بلبيب

************************************************

ما دعوة أنفع يا صـــــاحبي .... من دعوة الغــــائب للغــــائب

ناشدتك الــــرحمن يا قــــارئاً .... أن تسأل الغفران للكاتب (ولوالديه)

#5

+1 وشكرا لك... بعد طباعة الكودين ومقارنتهما .. يبدو كودك احترافي ومميز لكن لعلمك هذا الكود هو لم ينتهي بعد وانا ما اردته هو تصليحه لكي يعمل وادرك ان التكرار بالنسخ واللصق سيء .. لكن انت وكانك قدمت كود جديد للتغييرات الجذرية التي عملتها واضافة اقترانات الخ .. فهل يمكنك تعديل الكود بالحد الأدني من التغييرات.... واتمنى من كل من ساعدني في استفسارات سابقة ان يساعدني هذه المرة ايضا..

نقطة اخرى اخي بعد التطبيق ظهر خطأ في هذه الجملة

bool is_found = false;

وهو bool غير معرف ...

#6

السلام عليكم

يوسف حسين كتب:

+1 وشكرا لك... بعد طباعة الكودين ومقارنتهما .. يبدو كودك احترافي ومميز لكن لعلمك هذا الكود هو لم ينتهي بعد وانا ما اردته هو تصليحه لكي يعمل وادرك ان التكرار بالنسخ واللصق سيء .. لكن انت وكانك قدمت كود جديد للتغييرات الجذرية التي عملتها واضافة اقترانات الخ .. فهل يمكنك تعديل الكود بالحد الأدني من التغييرات.... واتمنى من كل من ساعدني في استفسارات سابقة ان يساعدني هذه المرة ايضا..

نقطة اخرى اخي بعد التطبيق ظهر خطأ في هذه الجملة

bool is_found = false;

وهو bool غير معرف ...

انا لم اغير كودك بالشكل الكبير و الجذري, فقط نسخت المقاطع المكررة و وضعتها في دوال على حدة مع اعطاء اسماء توحي بعملها و ربما غيرت اسماء بعض المتغيرات happy.gif

يمكنك مراقبة ذلك بمقارنة الاكواد التي تقرأ الطلبة من المستخدم و من الملفات, و اكواد الطبع واضافة طالب الى القائمة

اما بالنسبة الى :

اقتباس

bool is_found = false;

وهو bool غير معرف ...

فقط عوض bool بــint فيصبح :

int is_found = 0;

ثم غير التعليمة :

is_found = true;

الى :

is_found = 1;

بالتوفيق للجميع

1

وما كلُّ ذِي لُبٍّ بمِؤْتيكَ نُصحَه ****** وما كلُّ مؤْتٍ نُصحَه بلبيب

************************************************

ما دعوة أنفع يا صـــــاحبي .... من دعوة الغــــائب للغــــائب

ناشدتك الــــرحمن يا قــــارئاً .... أن تسأل الغفران للكاتب (ولوالديه)

#7

السلام عليكم .. اخي شكرا لك وصدقت فيما قلته ولقد اجريت تعديلاتي الشخصية بحيث جعلته افضل من وجهة نظري

وهي كالتالي ::::

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

typedef struct node {
        int Id;
        char name[50];
        float AVG;
        struct node *next;
} student;
// depending on user's input, reads a student from file or console, adds it to list and returns the head of list
student* read_student(student* head);
// reads one student from a file
student* read_student_from_file();
// reads one student from console
student* read_student_from_keyboard();
// adds a student to head of a linked list of students and returns the new head
student* add_student_to_list(student* head,student* s);
// prints the list od students
void print_students_list(student* head);
// search the list of student for a student with a given name
student* search_by_name(student* list_of_students,const char* name);
// frees the memory used
void free_memory(student* head);

int main() {
        student *head = NULL;
        char a[2] // i used an array to ignore the carriage return read when a user enters data
        , names[50];
        printf("do you want to enter first value? yes press y ,no press any other key\n");
        scanf("%s", a);
        if (a[0] == 'y') {
        head = read_student(head);
        }

        printf("do you want to enter another new value? yes press y ,no press any other key\n");
        scanf("%s", a);
        while (a[0] == 'y') {
        head = read_student(head);
        printf("do you want to enter another new value? yes press y ,no press any other key\n");
        scanf("%s", a);
        }


        // print the list of students
        print_students_list(head);
        // here we will print all data about an name ,the user enter it

        printf("Enter the Name to search and give you all information about it : ");
        scanf("%s",names);

        search_by_name(head,names);
        // finally, we shall free the memory
        free_memory(head);
        return 0;
}

student* read_student(student* head){
        char b[2];
        student* newn = NULL;
        printf("if you want to read from file press f, from kayboard press k \n");
        scanf("%s", b);
        switch (b[0]) {
        case 'f':
                newn = read_student_from_file();
                head = add_student_to_list(head,newn);
                break;

        case 'k':
                newn = read_student_from_keyboard();
                head = add_student_to_list(head,newn);
                break;

        default: printf("error");
                break;
        }
        return head;
}


student* read_student_from_file(){
        char Title[50];
        student* new_student ;
        FILE* inp ;
        do {
        printf("please,Enter the complete path\nfor file  you want to read data from it: ");
        gets(Title);
        inp = fopen(Title, "r");
        } while ( !inp );


        new_student = (student*) malloc(sizeof (student));
        fscanf(inp, "%d", &new_student->Id);
       fscanf(inp, "%s", &new_student->name);
        //fgets(inp,"%s",& new_student->name);
        fscanf(inp, "%f", &new_student->AVG);
        fclose(inp);
        return new_student;
}


student* read_student_from_keyboard(){
        student* new_student;
        printf("Enter the No,Name,AVG : ");
        new_student = (student*) malloc(sizeof (student));
        scanf("%d", &new_student->Id);
        gets(new_student->name);
        scanf("%f",&new_student->AVG);

        return new_student;
}
student* add_student_to_list(student* head,student* s){
        if(s != NULL){
        s->next = head;
        head = s;
        }
        return head;
}

void print_students_list(student* head){
        student* k = NULL;
        if(head == NULL){
        printf("There are no students in the list.\n");
        }else{
       k = head;
        while(k != NULL){
                printf("Student : \n\t ID : %d\n\tName : %s\n\tAverage : %.2f\n", k->Id, k->name,k->AVG);
                k =k->next;
        }
        }
}
student* search_by_name(student* list_of_students,const char* name){
        student* s = NULL;
       int c=0;

        if(list_of_students == NULL){
        printf("The list of students is empty !\n");
        }else{
        s = list_of_students;
        while(s != NULL){
                if(stricmp(s->name,name) == 0){
                       printf("Student found !\n\tID : %d\n\tName : %s\n\tAverage : %.2f\n",s->Id,s->name,s->AVG);
                     c++;

                }
                s = s->next;
        }}
       if(c==0)
            printf("Sorry ! no student with that name !\n");

 }


void free_memory(student* head){
        student* f = NULL;
        while(head != NULL){
       f = head;
        head = head->next;
        free(f);
        }
        head = NULL;
}

لكن مع ذلك تبقى لي عدة اسئلة وهي ::

لماذ عرفت

 char a[2]

بدلا من

char a;

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

if(s!=NULL) ما المقصود به؟

#8
يوسف حسين كتب:

لماذ عرفت

 char a[2]

بدلا من

char a;

واستمررت في استعمالها هكذا في الدوال

عندما يدخل المستخدم حرفا ثم يتبعه بــenter فانه في الحقيقة ادخل حرفين الحرف المقصود و زر enter ' ) فعند قراءة حرف واحد يبقى الحرف الثاني (زر الــenter ) في الــbuffer الخاص بالادخال و قد يؤثر على المدخلات القادمة لذا انا اتخلص منه بقراءة حرفين مرة واحدة.

يوسف حسين كتب:

.. وما معنى كلمة iteratorالتي كنت تكرر استخدامها

انا احب ان اسمي المتغيرات و الدوال اسماء توحي بعملها داخل البرنامج وبما ان ذلك الكود المقصود يعمل عدة iterations (حسب عدد الـــnodes داخل القائمة ) فقد سميت المتغير iterator

يوسف حسين كتب:

واستفسار اخيرة .. في الاقتران المخصص لاضافة نود جديد لماذا ابتداته ب

if(s!=NULL) ما المقصود به؟

لو كان s يساوي NULL فان الكود :

s->next = head;

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

ثم ان الكود:

        head = s;

سيجعل النود head يساوي NULL كذلك وبالتالي ستضيع كل القائمة لاننا لم نعد نملك عنوان العنصر الاول,( لن نصل الى هنا لان الخطأ الاول كفيل بتوقيف البرنامج )

بالتوفيق للجميع

1

وما كلُّ ذِي لُبٍّ بمِؤْتيكَ نُصحَه ****** وما كلُّ مؤْتٍ نُصحَه بلبيب

************************************************

ما دعوة أنفع يا صـــــاحبي .... من دعوة الغــــائب للغــــائب

ناشدتك الــــرحمن يا قــــارئاً .... أن تسأل الغفران للكاتب (ولوالديه)

#9

شكرا لك على الرد.. هنالك موضوع اخر حبذا لو تساعدني او اي احد من الخبراء والاخوة...

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