برنامه دفترچه تلفن در c
من این کدو نوشتم ولی اجرا نمی شه اگه میشه کمکم کنید
ممنون
#include
void Add_new(struct Phone*list);
int loading ();
struct Phone
{
char name[10];
char family[10];
char type[10];
int number;
struct Phone *next;
};
struct Phone* Start_ptr = NULL;
struct Phone* current = NULL;
FILE *fp;
//Selecting the action
void Select( int a )
{
char str[20];
switch ( a )
{
case 1 :
load();
Sort_name(struct Phone*current) ;
break;
case 2 :
Add_new(struct Phone*current);
Sort_name(struct Phone*current);
break;
case 3 :
gets(str);
delete (str,struct Phone*current);
Sort_name(struct Phone*current);
break;
case 4 :
Write_to_file(struct Phone*current);
break;
defult:
fclose(fp);
return ;
}
}
//loading structure
int loading ()
{
fp = fopen("c:test.txt", "a");
if(fp == NULL)
{
printf("Cannot open file
");
return -1;
}
else
{
printf("loaded successfully");
return 0;
}
while (current->next != NULL)
{
fscanf (fp,"%c,%c,%c,%d",current->name,current->family,current->current,list->number);
current=current->next;
}
}
// updating structure
void Add_new(struct Phone*list)
{
struct Phone* new;
printf ("Enter the name : ");
scanf ("%s",new->name);
printf ("Family : ");
scanf ("%s",new->family);
printf ( " number type : ");
scanf ("%s",new->type);
printf ( " number : ");
scanf ("%d",new->number);
new->next = NULL;
while (list->next==NULL)
{
list==list->next;
}
list->next=new;
}
//deleting structure
void Delete(char s[],struct Phone *list)
{
struct Phone* temp = NULL;
struct Phone* prev = NULL;
while (list->next!=NULL)
{
if (strcmp(list->family,s)==0)
{
/* If it`s the fisrt node */
if ( current == Start_ptr )
{
temp = Start_ptr;
Start_ptr = Start_ptr->next; //If we have only 1 node, start_ptr will point to NULL
temp = NULL;
}
/* If it`s in the middle of list or the last node */
else
{
prev = Start_ptr;
while( prev->next != current )
{
prev = prev->next;
}
prev->next = current->next;
// If it`s the last node prev will point to null
current = Start_ptr;
}
}
}
}
//Writing to file structure
void Write_to_file(struct Phone *list)
{
while( list != NULL )
{
fprintf(fp, "%c%c%c%d",list->name,list->family,list->type,list->number);
list = list->next;
}
printf("Data saved successfully.");
}
//Sorting
void Sort_name(struct Phone* temp)
{
struct Phone* sort[1000];
//size of list
int counter = 1;
while( temp->next != NULL )
{
temp = temp->next;
counter++;
}
sort[0] = Start_ptr;
int cnt;
for ( cnt = 1; cnt < counter; cnt++ )
{
sort[cnt] = sort[cnt-1]->next;
}
sort[counter] = NULL;
// bubble sort
/* Sort by family */
int i ,j;
for ( i = 0; i < counter; i++ )
{
for ( j = 0; j < counter - i - 1; j++)
{
if ( strcmp(sort[j]->family, sort[j+1]->family) > 0 )
{
struct Phone* temp2 = sort[j];
sort[j] = sort[j+1];
sort[j+1] = temp2;
}
}
}
return;
}
int main ()
{
int choice;
scanf ("%d",&choice);
while (choice!=5)
{
select (choice);
scanf ("%d",&choice);
}
return 0;
}
مشکلات زیادی داره این کد .
از جمله :
نحوه فراخوانی اشتباه تابع. در قسمتی که میخواید از جنس struct آرگمان بفرستید .
توجه نکردن به اسم دقیق تابع و رعایت نکردن حروف بزرگ و کوچک اسم تابع.
اینا مشکلاتی بود که در نگاه اول به چشم میاد و بدون توجه به منطق برنامه دیده میشه .
برای مثال وقتی شما میخواید تابعی رو فراخوانی کنید ، نباید نوع داده هم قبل متغیر بنویسید .
این مشکلات کلی برنامتون بود .
هزینه انجام این برنامه 20000 تومان است . در صورت تمایل میتونید مبلغ را واریز کنید و تا آخر شب برنامتون آمادست .
با تشکر ،
پاسخیاب