Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int id;
- char metal[40];
- int density;
- float price;
- struct node *next;
- };
- int CheckOpen(FILE *fp);
- void ReadData(struct node *ptr, FILE *fp);
- void PrintData(struct node *ptr, int limit);
- void FreeMem(struct node *ptr);
- int Sorting1(const void * a, const void * b);
- int Sorting2(const void * a, const void * b);
- int main(void)
- {
- FILE *fpread = fopen("5.csv", "r");
- CheckOpen(fpread);
- struct node *root;
- root = malloc(sizeof(struct node));
- ReadData(root, fpread);
- PrintData(root, 100);
- FreeMem(root);
- qsort(root, 100, sizeof(struct node *), Sorting1);
- printf("asdf");
- PrintData(root, 100);
- qsort(root, 100, sizeof(struct node *), Sorting2);
- PrintData(root, 3);
- fclose(fpread);
- return 0;
- }
- int CheckOpen(FILE *fp)
- {
- if(fp == NULL)
- {
- printf("File not opened!\nExiting program!\n");
- return 0;
- }
- else
- {
- printf("File opened successfully!\n");
- return 1;
- }
- }
- void ReadData(struct node *ptr, FILE *fp)
- {
- int i;
- char FirstLine[100];
- fgets(FirstLine, 100, fp);
- struct node *current;
- current = ptr;
- for (i = 0; i < 100; i++)
- {
- fscanf(fp, "%d,%[^,],%d,%f", ¤t->id,
- current->metal, ¤t->density, ¤t->price);
- if (i < 100)
- {
- current->next = malloc(sizeof(struct node));
- current = current->next;
- }
- }
- current->next = NULL;
- }
- void PrintData(struct node *ptr, int limit)
- {
- int i = 0;
- struct node *current;
- current = ptr;
- printf("id\tmetal\n");
- while (current->next != NULL && i < limit)
- {
- printf("%d, %s, %d, %f\n", current->id, current->metal,
- current->density, current->price);
- current = current -> next;
- i++;
- }
- }
- void FreeMem(struct node *ptr)
- {
- struct node *current;
- struct node *space;
- current = ptr;
- while(current != NULL)
- {
- space = current->next;
- free(current);
- current = space;
- }
- }
- int Sorting1(const void * a, const void * b)
- {
- struct node *aa, *bb;
- aa = *(struct node **) a;
- bb = *(struct node **) b;
- return (aa->density - bb->density);
- }
- int Sorting2(const void * a, const void * b)
- {
- struct node *aa, *bb;
- aa = *(struct node **) a;
- bb = *(struct node **) b;
- return (aa->price - bb->price);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement