程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * get_dat_file_name(char * name, int n) {
int i = 0, j = n;
while(i++, j /= 10)
;
j = n;
char * temp = (char *)malloc(i);
temp[i] = '\0';
do {
temp[--i] = (char)(j % 10 + 48);
} while(j /= 10);
strcat(name, temp);
strcat(name, ".dat");
free(temp);
return name;
}
char * get_avg_file_name(char * name, int n) {
int i = 0, j = n;
while(i++, j /= 10)
;
j = n;
char * temp = (char *)malloc(i);
temp[i] = '\0';
do {
temp[--i] = (char)(j % 10 + 48);
} while(j /= 10);
strcat(name, temp);
strcat(name, "-");
strcat(name, temp);
strcat(name, ".dat");
free(temp);
return name;
}
int main(void) {
char dat_name[15] = {0};
char avg_name[26] = {0};
char number[4];
char course[3];
FILE * dat[5], * avg_dat[5]; // n个文件把数组大小、下面的total改为n即可
int total = 5, i, j, sum, temp;
for(i = 0; i < total; i++) {
if((dat[i] = fopen(get_dat_file_name(dat_name, i + 1), "r")) == NULL) {
printf("Can't open %s\n", get_dat_file_name(dat_name, i + 1));
return 1;
}
if((avg_dat[i] = fopen(get_avg_file_name(avg_name, i + 1), "w")) == NULL) {
printf("Can't open %s\n", get_avg_file_name(avg_name, i + 1));
return 1;
}
for(j = 0; j < 15; j++)
dat_name[j] = 0;
for(j = 0; j < 26; j++)
avg_name[j] = 0;
}
for(i = 0; i < total; i++) {
fscanf(dat[i], "%s", number);
fprintf(avg_dat[i], "%s\n", number);
sum = 0;
fscanf(dat[i], "%s", course);
fprintf(avg_dat[i], "\t%s\t", course);
for(j = 0; j < 3; j++) {
fscanf(dat[i], "%d", &temp);
sum += temp;
}
fprintf(avg_dat[i], "%f\n", sum / 1.0 / j);
sum = 0;
fscanf(dat[i], "%s", course);
fprintf(avg_dat[i], "\t%s\t", course);
for(j = 0; j < 3; j++) {
fscanf(dat[i], "%d", &temp);
sum += temp;
}
fprintf(avg_dat[i], "%f\n", sum / 1.0 / j);
sum = 0;
fscanf(dat[i], "%s", course);
fprintf(avg_dat[i], "\t%s\t", course);
for(j = 0; j < 3; j++) {
fscanf(dat[i], "%d", &temp);
sum += temp;
}
fprintf(avg_dat[i], "%f\n", sum / 1.0 / j);
sum = 0;
fscanf(dat[i], "%s", course);
fprintf(avg_dat[i], "\t%s\t", course);
for(j = 0; j < 2; j++) {
fscanf(dat[i], "%d", &temp);
sum += temp;
}
fprintf(avg_dat[i], "%f\n", sum / 1.0 / j);
sum = 0;
fscanf(dat[i], "%s", course);
fprintf(avg_dat[i], "\t%s\t", course);
for(j = 0; j < 2; j++) {
fscanf(dat[i], "%d", &temp);
sum += temp;
}
fprintf(avg_dat[i], "%f\n", sum / 1.0 / j);
sum = 0;
fscanf(dat[i], "%s", course);
fprintf(avg_dat[i], "\t%s\t", course);
for(j = 0; j < 2; j++) {
fscanf(dat[i], "%d", &temp);
sum += temp;
}
fprintf(avg_dat[i], "%f\n", sum / 1.0 / j);
}
return 0;
}
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
格式请要对应上面的哦
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[
本帖最后由 lz1091914999 于 2011-5-30 19:43 编辑 ]