Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct para {
- int nr_indeksu;
- char nazwisko[30];
- float ocena;
- struct para *nast;
- struct para *poprz;
- };
- int main(){
- struct para *head=NULL, *ptr=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL,*tmp=NULL;
- FILE *f;
- int i=0, is_in=0;
- char inputfile[35];
- char outputfile[35];
- int tmp1[30];
- printf("Podaj nazwe pliku tekstowego:\n");
- scanf("%s", inputfile);
- f = fopen(inputfile, "r");
- if (f == NULL){
- printf("Nie znaleziono pliku!\n");
- return 0;
- }
- if(fscanf(f, "%s", &tmp1)!=1){
- printf("Plik jest pusty\n");
- return 0;
- }
- rewind(f);
- while(!feof(f)){
- if (head==NULL){
- head=ptr=(struct para*)malloc(sizeof(struct para));
- if (ptr==NULL){
- printf("Blad przydzialu pamieci!\nKoniec programu!\n");
- return 0;
- }
- fscanf(f, "%d %s %f", &ptr->nr_indeksu, &ptr->nazwisko, &ptr->ocena);
- printf("%d\t%s\t%.1f\n", ptr->nr_indeksu, ptr->nazwisko, ptr->ocena);
- ptr->nast=NULL;
- ptr->poprz = NULL;
- i++;
- }
- else{
- //ptr->nast=NULL;
- tmp=(struct para*)malloc(sizeof(struct para));
- /*ptr2=ptr;
- ptr=ptr->nast;
- ptr->poprz=ptr2;*/
- if (tmp==NULL){
- printf("Blad przydzialu pamieci!\nKoniec programu!\n");
- return 0;
- }
- fscanf(f, "%d %s %f", &tmp->nr_indeksu, &tmp->nazwisko, &tmp->ocena);
- printf("%d\t%s\t%.1f\n", tmp->nr_indeksu, tmp->nazwisko, tmp->ocena);
- ptr3=head;
- is_in=0;
- while(ptr3!=NULL){
- if(ptr3->nr_indeksu < tmp->nr_indeksu){
- if(ptr3->nast==NULL){
- ;
- }
- else{
- ptr3=ptr3->nast;
- }
- }
- else{
- is_in=1;
- }
- if(is_in==1){
- if(ptr3==head){
- ptr4=ptr3;
- ptr->poprz=tmp;
- tmp->nast=ptr4;
- head=tmp;
- head->poprz=NULL;
- break;
- }
- else if(ptr3->nast!=NULL){
- ptr4=ptr3->poprz;
- ptr4->nast=tmp;
- tmp->poprz=ptr4;
- tmp->nast=ptr3;
- ptr3->poprz=tmp;
- break;
- }
- else{
- ptr3->nast=tmp;
- tmp->nast=NULL;
- }
- }
- }
- /*if (tmp->nr_indeksu < ptr2->nr_indeksu){
- if (ptr2=head){
- head->nast=tmp;
- }
- else{
- ptr2->nast=tmp;
- }
- tmp->poprz=ptr2;
- }*/
- //ptr=tmp;
- //free(tmp);
- //ptr->nast=NULL;
- i++;
- }
- }
- fclose(f);
- printf("\n\nPosortowana lista wedlug indeksu:\n");
- ptr=head;
- while(ptr->nast!=NULL){
- //if (ptr->ocena<4.0)
- printf("%d %s %.1f\n", ptr->nr_indeksu, ptr->nazwisko, ptr->ocena);
- ptr=ptr->nast;
- }
- printf("Podaj nazwe pliku wynikowego:\n");
- scanf("%s", outputfile);
- f = fopen(outputfile, "w");
- ptr=head;
- while(ptr->nast!=NULL){
- fprintf(f, "%i %s %.1lf\n", ptr->nr_indeksu, ptr->nazwisko, ptr->ocena);
- ptr=ptr->nast;
- }
- fclose(f);
- ptr=head;
- while (head != NULL) {
- tmp = head;
- head = head->nast;
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment