程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
struct student
{
int num;
char name[20];
int sex;
float score;
};
int main()
{
struct student* stu;
int n, i, j = 0, first = 0,dot=0;
int m;
char str[256];
float score[2];
int num[2];
memset(score, 0, sizeof(float) * 2);
memset(num, 0, sizeof(int) * 2);
printf("请输入学生个数:");
scanf("%d", &n);
getchar();
stu = (struct student*)malloc(sizeof(struct student) * n);
for(j=0;j<n;j++)
{
memset(str,0,256);
fgets(str,256,stdin);
first = 0;
i = 0;
while (str[i] != ';')
{
if (first == 0)
{
(stu + j)->num = str[i] - '0';
first = 1;
}
else
(stu + j)->num = (stu + j)->num * 10 + str[i]-'0';
i++;
}
m = i+1;
while (str[++i] != ';');
memset((stu + j)->name, 0, 20);
memcpy((stu + j)->name, &str[m],i-m);
first = 0;
while (str[++i] != ';')
{
if (first == 0)
{
(stu + j)->sex = str[i] - '0';
first = 1;
}
else
(stu + j)->sex = (stu + j)->sex * 10 + str[i] - '0';
}
first = 0;
dot = 0;
while (str[++i] != ';')
{
if (first == 0)
{
(stu + j)->score = (float)(str[i] - '0');
first = 1;
}
else
{
if (str[i] == '.')
{
dot = 1;
}
else
{
if (dot)
{
(stu + j)->score = (stu + j)->score+ (str[i] - '0')/(float)pow(10,dot++);
}
else
{
(stu + j)->score = (stu + j)->score * 10 + str[i] - '0';
}
}
}
}
printf("%d,%s,%d,%f\n", (stu + j)->num, (stu + j)->name, (stu + j)->sex, (stu + j)->score);
}
for (j = 0; j < n; j++)
{
if ((stu + j)->sex == 0)
{
score[0] += (stu + j)->score;
num[0]++;
}
else
{
score[1] += (stu + j)->score;
num[1]++;
}
}
printf("女 %.2f\n", score[0] / num[0]);
printf("男 %.2f\n", score[1] / num[1]);
return 0;
}