1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <stdio.h> #include <malloc.h> void main() { remove("D:\\newtest.txt"); FILE *fp; FILE *nw = NULL ; nw = fopen("D:\\newtest.txt","w") ; char filename[40] ; int i,j ; int m, n; n=3; printf("请输入行数:"); scanf("%d", &m); float** da = (float**)malloc(sizeof(float*) * m); for (i = 0; i < m; i++) { *(da+i) = (float*)malloc(sizeof(float) * n); } fp=fopen("D:\\test.xls","r"); if (fp == NULL) { perror("打开文件失败啦"); } fseek(fp, 0, SEEK_SET); for(i = 0 ;i < m ; i++) for(j = 0 ;j < 3 ; j++) { fscanf(fp,"%f",&da[i][j]); fseek(fp, 1L, SEEK_CUR); } for(i = 0 ;i < m ; i++) printf("%d\t%.3f\t%.3f\t%.3f\t\n",i,da[i][0], da[i][1],da[i][2]); float num1=0; float num2=0; float num3=0; int day=0; for(i = 1 ;i <= m ; i++) if (i%24==0) { day+=1; num1+=da[i-1][0]; num2+=da[i-1][1]; num3+=da[i-1][2]; printf("第%d天的值:%.3f\t%.3f\t%.3f\t\n",day,num1,num2,num3); char temp[30]=""; sprintf(temp, "第%d天:", day); fprintf(nw ,"%s\t%.3f\t%.3f\t%.3f\n",temp,num1,num2,num3 ) ; num1=0; num2=0; num3=0; }else{ num1+=da[i-1][0]; num2+=da[i-1][1]; num3+=da[i-1][2]; }; getchar() ; }
|