fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct student {
  5. char name[20];
  6. double height;
  7. double weight;
  8. double bmi;
  9. };
  10.  
  11. int func(void)
  12. {
  13. char s[100],buf[100];
  14. int c=0,i,j;
  15. FILE *fp;
  16. struct student *data,t;
  17. double h;
  18.  
  19. /* printf("file name = ");*/
  20. scanf("%s",s);
  21. fp=fopen(s,"r");
  22. if(!fp) {
  23. printf("file open error!\n");
  24. return 1;
  25. }
  26.  
  27. while(fgets(buf,100,fp))c++;
  28. rewind(fp);
  29.  
  30. data=malloc(c*sizeof(struct student));
  31. for(i=0; fgets(buf,100,fp); i++) {
  32. sscanf(buf,"%s %lf %lf",data[i].name,&data[i].height,&data[i].weight);
  33. h=data[i].height/100;
  34. data[i].bmi=data[i].weight/h/h;
  35. }
  36.  
  37. for(i=0; i<c; i++) {
  38. for(j=i+1; j<c; j++) {
  39. if(data[i].bmi<data[j].bmi) {
  40. t=data[i];
  41. data[i]=data[j];
  42. data[j]=t;
  43. }
  44. }
  45. }
  46.  
  47. for(i=0; i<c; i++) {
  48. printf("%s BMI=%f",data[i].name,data[i].bmi);
  49. if(data[i].bmi>25)printf(" himan");
  50. printf("\n");
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56. int main(void)
  57. {
  58. func();
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0.01s 1856KB
stdin
Standard input is empty
stdout
file open error!