#include <stdio.h>
#include <math.h>
struct testdata{
char name[20];
int score;
float deviation_value;
int ranking;
};
/*STEP1*/
void read(struct testdata *p, int n)
{
int i;
FILE *fp;
fp = fopen("50data.txt","r");
if(fp == NULL){
printf("ファイルがありませんでしたorz¥n");
return;
}
for(i=0;i<n;i++){
fscanf(fp, "%s", (*(p+i)).name);
fscanf(fp, "%d", &(*(p+i)).score);
}
fclose(fp);
}
/*STEP2*/
void hensachi(struct testdata *p, int n)
{
float ave, variance, deviation;
int i;
ave = 0.0;
variance = 0.0;
for(i=0;i<n;i++){
ave += (float)(*(p+i)).score / (float)n;
}
for(i=0;i<n;i++){
variance += ((float)(*(p+i)).score-ave) * ((float)(*(p+i)).score-ave) / (float)n;
}
deviation = sqrt(variance);
for(i=0;i<n;i++){
(*(p+i)).deviation_value = 10*((float)(*(p+i)).score-ave)/deviation + 50;
}
}
/*STEP3*/
void jyuni(struct testdata *p, int n)
{
int i, j, count;
for(i=0;i<n;i++){
count = 1;
for(j=0;j<n;j++){
if((*(p+i)).score < (*(p+j)).score){
count++;
}
}
(*(p+i)).ranking = count;
}
}
/*テストデータ表示の関数*/
void disp_testdata(struct testdata *p, int n)
{
int i;
for(i=0;i<n;i++){
printf("%s¥t%d¥t%f¥t%d¥n",(*(p+i)).name,(*(p+i)).score,(*(p+i)).deviation_value,(*(p+i)).ranking);
}
}
/*STEP4*/
void write(struct testdata *p, int n)
{
int i, j, count;
FILE *fp;
fp = fopen("histogram.csv","w");
if(fp == NULL){
printf("ファイルを作れませんでしたorz¥n");
return;
}
fprintf(fp, "点数,個数¥n");
for(i=0;i<11;i++){
count = 0;
for(j=0;j<n;j++){
if((*(p+j)).score >= i*10 && (*(p+j)).score < (i+1)*10){
count++;
}
}
fprintf(fp, "%d0-,%d¥n", i, count);
}
fclose(fp);
}
/*STEP5*/
void disp_histogram(struct testdata *p, int n)
{
int i, j, count;
for(i=0;i<11;i++){
count = 0;
for(j=0;j<n;j++){
if((*(p+j)).score >= i*10 && (*(p+j)).score < (i+1)*10){
count++;
}
}
printf("%d0-¥t%d¥t", i, count);
for(j=0;j<count;j++){
printf("*");
}
printf("¥n");
}
}
/*メイン関数*/
void main()
{
struct testdata list[50];
read(list,50);
hensachi(list,50);
jyuni(list,50);
disp_testdata(list,50);
printf("¥n");
write(list,50);
disp_histogram(list,50);
}
まず50data.txtの中身の[EOF]を51行目に改行する.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct testdata{
char name[20];
int score;
float deviation_value;
int ranking;
};
/*STEP1*/
int read(struct testdata *p)
{
int n;
FILE *fp;
fp = fopen("50data.txt","r");
if(fp == NULL){
exit(1);
}
n = 0;
while(! feof(fp) && n < 512){
fscanf(fp, "%s", (*(p+n)).name);
fscanf(fp, "%d", &(*(p+n)).score);
n++;
}
fclose(fp);
n = n-1;
return n;
}
/*STEP2*/
void hensachi(struct testdata *p, int n)
{
float ave, variance, deviation;
int i;
ave = 0.0;
variance = 0.0;
for(i=0;i<n;i++){
ave += (float)(*(p+i)).score / (float)n;
}
for(i=0;i<n;i++){
variance += ((float)(*(p+i)).score-ave) * ((float)(*(p+i)).score-ave) / (float)n;
}
deviation = sqrt(variance);
for(i=0;i<n;i++){
(*(p+i)).deviation_value = 10*((float)(*(p+i)).score-ave)/deviation + 50;
}
}
/*STEP3*/
void jyuni(struct testdata *p, int n)
{
int i, j, count;
for(i=0;i<n;i++){
count = 1;
for(j=0;j<n;j++){
if((*(p+i)).score < (*(p+j)).score){
count++;
}
}
(*(p+i)).ranking = count;
}
}
/*テストデータ表示の関数*/
void disp_testdata(struct testdata *p, int n)
{
int i;
for(i=0;i<n;i++){
printf("%s¥t%d¥t%f¥t%d¥n",(*(p+i)).name,(*(p+i)).score,(*(p+i)).deviation_value,(*(p+i)).ranking);
}
}
/*STEP4*/
void write(struct testdata *p, int n)
{
int i, j, count;
FILE *fp;
fp = fopen("histogram.csv","w");
if(fp == NULL){
exit(1);
}
fprintf(fp, "点数,個数¥n");
for(i=0;i<11;i++){
count = 0;
for(j=0;j<n;j++){
if((*(p+j)).score >= i*10 && (*(p+j)).score < (i+1)*10){
count++;
}
}
fprintf(fp, "%d0-,%d¥n", i, count);
}
fclose(fp);
}
/*STEP5*/
void disp_histogram(struct testdata *p, int n)
{
int i, j, count;
for(i=0;i<11;i++){
count = 0;
for(j=0;j<n;j++){
if((*(p+j)).score >= i*10 && (*(p+j)).score < (i+1)*10){
count++;
}
}
printf("%d0-¥t%d¥t", i, count);
for(j=0;j<count;j++){
printf("*");
}
printf("¥n");
}
}
/*メイン関数*/
void main()
{
struct testdata list[512];
int n;
n = read(list);
hensachi(list,n);
jyuni(list,n);
disp_testdata(list,n);
printf("¥n");
write(list,n);
disp_histogram(list,n);
}