[ create a new paste ] login | about

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

C, pasted on Oct 5:
#include<stdio.h>

#define N 1000
#define INPUT "in.txt"
#define OUTPUT "out.txt"

struct student{
  char name[20];
  int kokugo,sansu,eigo;
};

int main()
{
  FILE *fp;
  struct student starray[N];
  int i, h;
  int max_kokugo, max_sansu, max_eigo;

  fp = fopen(INPUT, "r");
  if (fp == NULL) { 
    printf("cannot open the file 'in.txt'.\n"); 
    return -1;
  }
  i = 0;
  while(fscanf(fp,"%s %d %d %d",starray[i].name, &starray[i].kokugo, &starray[i].sansu,&starray[i].eigo)==4 && i < N)
    i++;
  h = i;
  fclose(fp);

  max_kokugo = max_sansu = max_eigo = 0;
  for (i = 0; i < h; i++) {
    if (max_kokugo < starray[i].kokugo)
      max_kokugo = starray[i].kokugo;
    if (max_sansu < starray[i].sansu)
      max_sansu = starray[i].sansu;
    if (max_eigo < starray[i].eigo)
      max_eigo = starray[i].eigo;
  }
  
  fp = fopen(OUTPUT, "w");
  if (fp == NULL) {
    printf("cannot open the file 'out.txt'.\n");
    return -1;
  }
  for(i = 0; i < h; i++) {
    fprintf(fp, "%s\n", starray[i].name);
    fprintf(fp, "%d\n", (int)((double)starray[i].kokugo / max_kokugo * 100.0));
    fprintf(fp, "%d\n", (int)((double)starray[i].sansu / max_sansu * 100.0));
    fprintf(fp, "%d\n", (int)((double)starray[i].eigo / max_eigo * 100.0));
  }
  fclose(fp);
  return 0;
}
/* end */


Output:
1
2
3
cannot open the file 'in.txt'.

Exited: ExitFailure 255


Create a new paste based on this one


Comments: