-2
이 프로그램을 완료해야합니다.다른 순서로 인쇄하기 위해 포인터 이동
나는했습니다 내가 목록을 작성 목록에 파일을로드하고 다음과 같이 주문 인쇄 할 필요가
Name iD Num_of_elements elem(1) elem(2), ... , elem(n)
james 1 3 AAA BBB CCC
arthur 2 2 EEE FFF
james 1 1 KKK
irine 3 4 EEE FFF DDD AAA
james 1 1 XXX
같은 파일 :
james 1 3 AAA BBB CCC
james 1 1 XXX
james 1 1 KKK
arthur 2 2 EEE FFF
irine 3 4 EEE FFF DDD AAA
(는 것이 필요하다 이전에 같은 아이디를 가진 사람을 인쇄 한 다음 다른 사람을 인쇄하십시오).
ANSI C에서 프로그램의 일부를 만들었지 만 요청으로 "최종 기능"을 완료 할 수 없습니다.
무효 printListOrderedByiD (구조체 목록 * 상단) {}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define len 35
struct elements
{
char name[len];
};
struct list
{
char name[len];
int id;
int numOfElements;
struct elements *pElements; /* pointer to the struct elements */
struct list *next;
};
FILE *openFile(FILE *fp)
{
fp=fopen("file.txt", "r");
if (fp==NULL)
{
perror("");
exit(1);
}
return (fp);
}
struct list *newNode(FILE *fp)
{
int i=0;
struct list *temp=(struct list *)malloc(sizeof(struct list));
fscanf(fp, "%s\t%d\t%d\t", temp->name, &temp->id, &temp->numOfElements);
temp->pElements=(struct elements *)malloc(temp->numOfElements*sizeof(struct elements));
for (i=0; i<temp->numOfElements; i++)
{
fscanf(fp, "%s\t", temp->pElements[i].name);
}
temp->next=NULL;
return temp;
}
struct list *insertAsLast(struct list *top, FILE *fp) /* this function will insert every node at the end of the list */
{
if (top==NULL)
{
top=newNode(fp);
}
else
{
top->next=insertAsLast(top->next, fp);
}
return top;
}
void printList(struct list *top) /* this procedure will stamp the list as loades from the file */
{
int i=0;
if (top==NULL)
{
printf("//\n");
}
else
{
printf("%s %d %d ", top->name, top->id, top->numOfElements);
for (i=0; i<top->numOfElements; i++)
{
printf("%s ", top->pElements[i].name);
}
printf("\n");
printList(top->next);
}
}
int main()
{
struct list *top=NULL;
char firstLine[200];
FILE *fp=NULL;
fp=openFile(fp);
fgets(firstLine, 200, fp); /* in order to jump the 1st line */
while (!feof(fp))
{
top=insertAsLast(top, fp);
}
fclose (fp);
printList(top);
return 0;
}
누군가가 나를 도울 수 내 코드의
?
당신이
static int cmp(const void *a, const void *b){ int x = (*(struct list**)a)->id; int y = (*(struct list**)b)->id; return x < y ? -1 : x > y; }
무엇인지 설명 할 수주십시오 배열의 –@Mariadegregorio 요소 qsort''에 사용되는 비교 함수의''무효 * 같은 요소에 대한 포인터를 통과 해 주셔서 감사합니다. 그래서'(struct list **) a'는'const void * a' ->'struct list **'입니다,'*'는'struct list **'->'struct list *'입니다. – BLUEPIXY
@Mariadegregorio 비교 함수는 첫 번째 인수가 각각 또는 두 번째 인수보다 큰 것으로 간주되는 경우 0보다 큰 정수,보다 작거나 같거나 작은 정수를 반환합니다. – BLUEPIXY