#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct {
char name[ 64 ] ;
int math;
int english;
int physics;
double average;
} Student;
int main ( )
{
int i;
Student students[ 5 ] ;
// (1) scores.txtを読み込みモード(r)でファイルオープン
FILE * fp = fopen ( "scores.txt" ,"r" ) ;
if ( fp== NULL ) {
// scores.txtが開けないことを通知+プログラム終了
printf ( "Cannot open scores.txt\n " ) ;
exit ( 0 ) ;
}
else {
for ( i= 0 ; i< 5 ; i++ ) {
// {氏名(char),数学の点数(int),英語の点数(int),物理の点数(int)}を読み込んでstudentsに保存
fscanf ( fp,"%s%d%d%d" ,students[ i] .name ,& students[ i] .math ,& students[ i] .english ,& students[ i] .physics ) ;
}
// ファイルを閉じる
fclose ( fp) ;
}
// (2) 3教科の平均の計算とaverage.txtへの書込み
for ( i= 0 ; i< 5 ; i++ ) {
// students[i]の平均(average)を計算
students[ i] .average = ( double ) ( students[ i] .math + students[ i] .english + students[ i] .physics ) / 3 ;
}
// average.txtを書き込みモード(w)でファイルオープン
FILE * wfp = fopen ( "avarage.txt" ,"w" ) ;
if ( wfp== NULL ) {
// average.txtが開けないことを通知+プログラム終了
printf ( "Cannot open average.txt\n " ) ;
exit ( 0 ) ;
}
else {
printf ( "Writing average to average.txt\n " ) ;
// {氏名(char),平均(double)}をaverage.txtへ書込み
for ( i= 0 ; i< 5 ; i++ ) {
fprintf ( wfp,"%s %lf\n " ,students[ i] .name ,students[ i] .average ) ;
}
// ファイルを閉じる
fclose ( wfp) ;
}
// (3) 任意の名前でstudents を検索
char name[ 64 ] ;
printf ( "Put the student name " ) ;
scanf ( "%s" , name) ;
// ここから
Student s;
strcpy ( s.name , "nanashi" ) ;
for ( i= 0 ; i< 5 ; i++ ) {
if ( strcmp ( name, students[ i] .name ) == 0 ) {
strcpy ( s.name , students[ i] .name ) ;
s.math = students[ i] .math ;
s.english = students[ i] .english ;
s.physics = students[ i] .physics ;
break ;
}
}
if ( strcmp ( s.name , "nanashi" ) == 0 ) {
printf ( "%s is not found in scores.txt\n " , name) ;
}
else {
printf ( "%s's scores are; math=%d, english=%d, physics=%d\n " , s.name , s.math , s.english , s.physics ) ;
}
// ここまで
// find 関数の呼び出し
find( name, students) ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGU8c3RyaW5nLmg+Cgp0eXBlZGVmIHN0cnVjdHsKCWNoYXIgbmFtZVs2NF07CglpbnQgbWF0aDsKCWludCBlbmdsaXNoOwoJaW50IHBoeXNpY3M7Cglkb3VibGUgYXZlcmFnZTsKCn0gU3R1ZGVudDsKCiBpbnQgbWFpbiAoKQogewogaW50IGk7CgogU3R1ZGVudCBzdHVkZW50c1s1XTsKCiAvLyAoMSkgc2NvcmVzLnR4dOOCkuiqreOBv+i+vOOBv+ODouODvOODiShyKeOBp+ODleOCoeOCpOODq+OCquODvOODl+ODswpGSUxFICpmcCA9IGZvcGVuKCJzY29yZXMudHh0IiwiciIpOwoKIGlmKGZwPT1OVUxMKXsKIC8vIHNjb3Jlcy50eHTjgYzplovjgZHjgarjgYTjgZPjgajjgpLpgJrnn6XvvIvjg5fjg63jgrDjg6njg6DntYLkuoYKIHByaW50ZigiQ2Fubm90IG9wZW4gc2NvcmVzLnR4dFxuIik7CiBleGl0KDApOwogfQogZWxzZXsKIGZvcihpPTA7aTw1O2krKyl7CiAvLyB75rCP5ZCNKGNoYXIp77yM5pWw5a2m44Gu54K55pWwKGludCnvvIzoi7Hoqp7jga7ngrnmlbAoaW50Ke+8jOeJqeeQhuOBrueCueaVsChpbnQpfeOCkuiqreOBv+i+vOOCk+OBp3N0dWRlbnRz44Gr5L+d5a2YCiBmc2NhbmYoZnAsIiVzJWQlZCVkIixzdHVkZW50c1tpXS5uYW1lLCZzdHVkZW50c1tpXS5tYXRoLCZzdHVkZW50c1tpXS5lbmdsaXNoLCZzdHVkZW50c1tpXS5waHlzaWNzKTsKIH0KIC8vIOODleOCoeOCpOODq+OCkumWieOBmOOC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oZSBzdHVkZW50IG5hbWUgIik7CnNjYW5mKCIlcyIsIG5hbWUpOwoKIC8vIOOBk+OBk+OBi+OCiQpTdHVkZW50IHM7CnN0cmNweShzLm5hbWUsICJuYW5hc2hpIik7CgogZm9yKGk9MDtpPDU7aSsrKXsKIGlmKHN0cmNtcChuYW1lLCBzdHVkZW50c1tpXS5uYW1lKT09MCl7CiBzdHJjcHkocy5uYW1lLCBzdHVkZW50c1tpXS5uYW1lKTsKIHMubWF0aCA9IHN0dWRlbnRzW2ldLm1hdGg7CiBzLmVuZ2xpc2ggPSBzdHVkZW50c1tpXS5lbmdsaXNoOwogcy5waHlzaWNzID0gc3R1ZGVudHNbaV0ucGh5c2ljczsKIGJyZWFrOwogfQogfQoKIGlmKHN0cmNtcChzLm5hbWUsICJuYW5hc2hpIik9PTApewogcHJpbnRmKCIlcyBpcyBub3QgZm91bmQgaW4gc2NvcmVzLnR4dFxuIiwgbmFtZSk7CiAgfQogZWxzZXsKIHByaW50ZigiJXMncyBzY29yZXMgYXJlOyBtYXRoPSVkLCBlbmdsaXNoPSVkLCBwaHlzaWNzPSVkXG4iLCBzLm5hbWUsIHMubWF0aCwgcy5lbmdsaXNoLCBzLnBoeXNpY3MpOwogfQogLy8g44GT44GT44G+44GnCgogLy8gZmluZCDplqLmlbDjga7lkbzjgbPlh7rjgZcKIGZpbmQobmFtZSwgc3R1ZGVudHMpOwogfQog
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:92: error: ‘find’ was not declared in this scope
prog.cpp:31: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:67: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout