[ create a new paste ] login | about

Link: http://codepad.org/xQ7jLJ15    [ raw code | output | fork ]

C, pasted on Jan 15:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 20  /*取り扱い人数*/
#define LEN 15  /*名前の長さ*/

typedef struct seiseki{
    char name[LEN];
    int eng;
    int math;
    int sci;
    int total;
    double mean;
}SEISEKI;

int main(int ,char **);
int input(FILE *,SEISEKI *);
void output(FILE *,SEISEKI *,int);
int compare(const SEISEKI *,const SEISEKI *);

int main(int argc,char **argv)
{
  FILE *infile,*outfile;
  SEISEKI student[MAX];
  int number;               /*入力ファイルの行数*/

  if(argc!=3){
   fprintf(stderr,"Example: qtotal motofile.dat insatsu.dat\n");
   exit(1);
  }
  if((infile=fopen(argv[1],"r"))==NULL){
   fprintf(stderr,"Can not open file:%s\n",argv[1]);
   exit(1);
  }
  if((outfile=fopen(argv[2],"w"))==NULL){
   fprintf(stderr,"Can not open file:%s\n",argv[2]);
   exit(1);
  }

  number=input(infile,student);
  
  qsort(student,number,sizeof(SEISEKI),(int (*)(const void *,const void *))compare);

  output(outfile,student,number);

  fclose(infile);  fclose(outfile);
  return 0;
}

int input(FILE *infile,SEISEKI *student)  
{
  char n[LEN];
  int e,m,s,i=0;          /*iは、人数のカウント*/

  while(fscanf(infile,"%s %d %d %d",n,&e,&m,&s)!=EOF){
      strcpy(student[i].name,n);
      student[i].eng=e;  student[i].math=m;  student[i].sci=s;
      student[i].total=e+m+s;
      student[i].mean=student[i].total/3.0;
      i++;
  }

  return i;
}

void output(FILE *outfile,SEISEKI *student,int number)
{
  int i;

  for(i-0;i<number;i++){
     SEISEKI *tmp=&student[i];
     fprintf(outfile,"%-14s %3d %3d %3d %3d %6.2f\n",tmp->name,tmp->eng,tmp->math,tmp->sci,tmp->total,tmp->mean);
  }

  return;
}

int compare(const SEISEKI *p,const SEISEKI *q)    /*クイックソートによる比較*/
{
 return -(p->total - q->total);
} 
 


Output:
1
Example: qtotal motofile.dat insatsu.dat


Create a new paste based on this one


Comments: