Reading and rearrangement of student information

Source: Internet
Author: User

There is a document that records student information, each line records a student's information, the format into the school number name gender score 1 score 2 score 3\n.

Requirements: (1) Read the contents of the file, strung into a linked list.

(2) in descending order of total score, save the result to the original file.

Right-click Compile the project name, debugging---command-line parameters to add the student information text to read and modify.

Order and transfer with structure array as carrier

Func.h

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define SWAP (A, b) {Pstud temp;temp=a;a=b;b=temp;}

typedef struct stud{

int num;

int sex;

Char name[20];

float Score1;

float Score2;

float Score3;

struct stud* pnext;

}stud,* Pstud;

void Scan_fp_list (Pstud *,pstud *,file *);

Float Sum_score (Pstud *);

void Strc_new (Pstud *,pstud *);

int compare (const void*, const void *);

void str_cpy (Pstud *,pstud *);

void A_quicksort (Pstud *,int, int);

int partition (pstud*, int, int);

List_txet.c

#include "func.h"

void Scan_fp_list (Pstud *pphead,pstud *pptail,file *FP)

{

Pstud pnew= (Pstud) malloc (sizeof (Stud));

memset (pnew,0,sizeof (Stud));

FSCANF (FP, "%d%s%d%f%f%f",&pnew->num,pnew->name,&pnew->sex,&pnew->score1,&pnew-> SCORE2,&PNEW->SCORE3);

if (*pphead==null)

{

*pphead=pnew;

*pptail=pnew;

}else{

pnew->pnext=*pphead;

*pphead=pnew;

}

}

Float Sum_score (Pstud *pphead)

{

Return ((*pphead)->score1+ (*pphead)->score2+ (*pphead)->score3);

}

void Strc_new (Pstud *pphead1,pstud *pptail1)

{

Pstud pnew= (Pstud) malloc (sizeof (Stud));

memset (pnew,0,sizeof (Stud));

if (*pphead1==null)

{

*pphead1=pnew;

*pptail1=pnew;

}else{

pnew->pnext=*pphead1;

*pphead1=pnew;

}

}

void str_cpy (Pstud *a,pstud *phead1)

{

int i;

Pstud Pcur=*phead1;

For (i=4;i>=0;i--, Pcur=pcur->pnext)

{

strcpy (Pcur->name,a[i]->name);

pcur->num=a[i]->num;

pcur->score1=a[i]->score1;

pcur->score2=a[i]->score2;

pcur->score3=a[i]->score3;

pcur->sex=a[i]->sex;

}

}

int partition (pstud* a,int left,int right)

{

int i,k;

K=left;

for (i=left;i<right;i++)

{

if (Sum_score (&a[i]) <sum_score (&a[right]))

{

SWAP (A[i],a[k]);

k++;

}

}

SWAP (A[k],a[right]);

return k;

}

void A_quicksort (Pstud *a,int left,int right)

{

int pivot;

if (left<right)

{

Pivot=partition (A,left,right);

A_quicksort (a,left,pivot-1);

A_quicksort (A,pivot+1,right);

}

}

Main.c

#include "func.h"

int main (int argc,char *argv[])

{

FILE *FP,*FP1;

int i;

int num=5;

Pstud Phead=null,ptail=null;

Pstud Phead1=null,ptail1=null;

Pstud Pcur=null,pcur1=null;

Pstud A[5];

Fp=fopen (Argv[1], "r+");

for (i=0;i<num;i++)

{

Scan_fp_list (&PHEAD,&PTAIL,FP);

}

Fclose (FP);

for (I=0;i<5;i++,phead=phead->pnext)

{

A[i]=phead;

}

A_quicksort (a,0,4);

for (i=0;i<5;i++)

{

Strc_new (&PHEAD1,&PTAIL1);

}

str_cpy (A,&PHEAD1);

Fp1=fopen (Argv[1], "w");

for (i=0;i<num;i++)

{

fprintf (FP1, "%d%s%d%5.2f%5.2f%5.2f\n", phead1->num,phead1->name,phead1->sex,phead1->score1,phead1- >SCORE2,PHEAD1->SCORE3);

phead1=phead1->pnext;

}

Fclose (FP1);

System ("pause");

return 0;

}

Reading and rearrangement of student information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.