שאלה

hila893

New member
שאלה

יש לי תרגיל שבו אני אמורה לעשות רשימה מקושרת בסי, של קורסים.
ואני צריכה שתהיה לי את האופציה להוסיף קורסים. עכשיו יש לי את הפונקציה של הוספת איברים ברשימה.

אך אין לי מושג איך לרשום את זה במיין.

אשמח אם תוכלו לעזור.

הילה
 

hila893

New member
תראה את הקוד בחלק הבעייתי.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
struct Student
{
char* firstName;
char* lastName;
int id;
char courseStatus;
int grade;
}typedef student_t;

typedef struct Course
{
char* courseName;
char* lecturerName;
int courseNum;
int numOfStudents;
student_t students[SIZE];
struct Course* next;
}Course;

typedef struct Courselist
{
struct Course *course_list;

}Courselist;

Course* addItemToStart(Course* list, char* data1,char* data2,int* ind)
{
Course* newItem = (Course*)malloc(sizeof(Course));
newItem->courseName= (char*)malloc(1+(strlen(data1)*sizeof(char)));
newItem->lecturerName=(char*)malloc(1+(strlen(data2)*sizeof(char)));
strcpy(newItem->courseName,data1);
strcpy(newItem->lecturerName,data2);
newItem->courseNum = *ind;
newItem->numOfStudents = NULL;
(*ind)++;
newItem->next=NULL;
return newItem;
}

int menu()
{
int choise;
printf("Please enter your choice:\n");
printf("1-Add course to the list\n");
printf("2-Add student to course\n");
printf("3-Cancelled student participation at specific course\n");
printf("4-Cancel course\n");
printf("5-Update status student and his grade \n");
printf("6-Print course name & course number\n");
printf("7-Print students list with all the details, sorted by last name\n");
printf("8-Print students list with all the details, sorted smallest to largest grade\n");
printf("9-Print the number of the course, which was the highest average\n");
printf("10-Print the number of the course,which most students pass 60\n");
printf("11-Print all the students,who did moed b\n");
printf("12- quit \n");
scanf ("%d", &choise);
flushall();
return choise;
}

int quit()
{
int i=0, answar=1;
printf("Do You really want to quit? (y/n)\n");
scanf("%c",&i);
if (i=='y'||i=='Y')
return answar=0;
return answar;
}
void main()
{
int option,loop=1;
int ind[]={1};
Course* Item;
char courseName[SIZE];
char lecturerName[SIZE];
Courselist* head=(Courselist*)malloc(sizeof(Courselist));
Courselist *p;
p=head;
head->course_list = NULL;
p->course_list=head->course_list;
while (loop!=0)
{
option = menu();
switch (option)
{
case 1:
{
if (head==NULL)
printf("The List Is Empty\n");
else
{
printf("Please enter course name and lecturer name:\n");
flushall();
gets(courseName);
gets(lecturerName);
if(head->course_list != NULL)
{

p->course_list->next= (Course*)malloc(sizeof(Course));
p->course_list=p->course_list->next;
p->course_list= addItemToStart(p->course_list,courseName,lecturerName,ind);
}
else
{
head->course_list = (Course*)malloc(sizeof(Course));
head->course_list = addItemToStart(head->course_list,courseName,lecturerName,ind);
p->course_list=p->course_list->next;
}



}
}
}
}
}

אנחנו צריכים ליצור רשימת קורסים ולקלוט מהמשתמש קורסים. משום מה., זה כל פעם דורס את האיבר הראשון.
אם מישהו יכול לעזור, אשמח!
 

nocgod

New member
תשתמשי בתגיות

תחילת קוד וסיום קוד כדי שזה יהיה מוצמד לשמאל
או תעלי את הקוד בקובץ TXT נפרד.
 

BravoMan

Active member
קוד ההוספה שלך חסר משמעות!

מה בדיוק ניסית לעשות בשורות האלה:

if(head->course_list != NULL)
{
p->course_list->next= (Course*)malloc(sizeof(Course));
p->course_list=p->course_list->next;
p->course_list= addItemToStart(p->course_list,courseName,lecturerName,ind);
}


נראה, שאת יוצרת אובייקט ריק חדש, משרשרת אותו לסוף הרשימה, ואז מתעלמת ממנו לחלוטין ושמה במשתנה שמחזיק כתובת לרשימה, את הכתובת של אובייקט חדש שפונקציה addItemToStart יצרה ומילאה בו נתונים.
אבל, לאובייקט החדש הזה אין שום קשר לרשימה, כי שום איבר ברשימה אינו מחזיק בכתובת שלו, וגם הוא בעצמו לא מחזיק בכתובת של שום איבר אחר ברשימה.

אם את באמת רוצה להוסיף איבר לתחילת הרשימה, הפונקציה addItemToStart צריכה לעשות את הדברים הבאים:
א) להקצות איבר חדש.
ב) למלא בו נתונים.
ג) להכניס כתובת של ראש הרשימה לתוך איבר next של האובייקט החדש

כרגע, הפונקציה לא מבצעת סעיף ג.
וכמובן, שההקצאה ב-main מיותרת לחלוטין.
 
למעלה