struct _STUDENT_ { int iNumber; char name[30]; int kor; int math; int eng; float avg; /* 평균 */ int iRank; /* 석차 */ }; #define MAX_STUDENT_COUNT 10 /* 학생수 */
struct _STUDENT_ infoStudent[MAX_STUDENT_COUNT]; int iRealStudentCount = 0; // 파일에서 읽은 실제 학생수
if(fpOutput){ fprintf(fpOutput,"\n학생수 : %d명",iRealStudentCount); fprintf(fpOutput,"%s","\n================================================"); fprintf(fpOutput,"\n%s"," 번호 이름 국어 수학 영어 평균 순위 학점\n"); fprintf(fpOutput,"%s","================================================");
printf("\n학생수 : %d명",iRealStudentCount); printf("%s","\n================================================"); printf("\n%s"," 번호 이름 국어 수학 영어 평균 순위 학점\n"); printf("%s","================================================");
http://whiteat.com/study/student_wizard.php?file_name=default.c&student_count=10&use_menu=on&INPUT_TYPE=READ_FILE#
////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _STUDENT_ {
int iNumber;
char name[30];
int kor;
int math;
int eng;
float avg; /* 평균 */
int iRank; /* 석차 */
};
#define MAX_STUDENT_COUNT 10 /* 학생수 */
struct _STUDENT_ infoStudent[MAX_STUDENT_COUNT];
int iRealStudentCount = 0; // 파일에서 읽은 실제 학생수
struct _STUDENT_ structMAXData; // 최고 점수만 기록할 변수
char *GetGrade(int iScore){
if(iScore >=90) return "A";
else if(iScore >=80) return "B";
else if(iScore >=70) return "C";
else if(iScore >=60) return "D";
else return "F";
}
void ViewAllList(){
FILE *fpOutput;
int i;
fpOutput=fopen("output_data.txt", "w");
if(fpOutput){
fprintf(fpOutput,"\n학생수 : %d명",iRealStudentCount);
fprintf(fpOutput,"%s","\n================================================");
fprintf(fpOutput,"\n%s"," 번호 이름 국어 수학 영어 평균 순위 학점\n");
fprintf(fpOutput,"%s","================================================");
printf("\n학생수 : %d명",iRealStudentCount);
printf("%s","\n================================================");
printf("\n%s"," 번호 이름 국어 수학 영어 평균 순위 학점\n");
printf("%s","================================================");
for( i=0;i<MAX_STUDENT_COUNT;i++){
if(0 ==strlen(infoStudent[i].name)) break;
fprintf(fpOutput,"%s","\n");
fprintf(fpOutput,"%4d %6s %4d %4d %4d %5.2lf %4d %4s",infoStudent[i].iNumber,infoStudent[i].name,infoStudent[i].kor,infoStudent[i].math,infoStudent[i].eng,infoStudent[i].avg,infoStudent[i].iRank+1,GetGrade((int)infoStudent[i].avg));
printf("%s","\n");
printf("%4d %6s %4d %4d %4d %5.2lf %4d %4s",infoStudent[i].iNumber,infoStudent[i].name,infoStudent[i].kor,infoStudent[i].math,infoStudent[i].eng,infoStudent[i].avg,infoStudent[i].iRank+1,GetGrade((int)infoStudent[i].avg));
}
fprintf(fpOutput,"%s","\n==============================================");
fprintf(fpOutput,"\n최고점 %4d %4d %4d %5.2lf\n",structMAXData.kor,structMAXData.math,structMAXData.eng,structMAXData.avg);
fprintf(fpOutput,"%s","==============================================");
printf("%s","\n==============================================");
printf("\n최고점 %4d %4d %4d %5.2lf\n",structMAXData.kor,structMAXData.math,structMAXData.eng,structMAXData.avg);
printf("%s","==============================================");
fclose(fpOutput);
}
}
void SortNumber(){
struct _STUDENT_ structTempData;
int i;
int j;
// 번호순으로 정렬
for( i=0;i<iRealStudentCount;i++){
for( j=0;j<iRealStudentCount;j++){
if(infoStudent[i].iNumber < infoStudent[j].iNumber ){
// 데이터 교환
memcpy(&structTempData,&infoStudent[i],sizeof(struct _STUDENT_));
memcpy(&infoStudent[i],&infoStudent[j],sizeof(struct _STUDENT_));
memcpy(&infoStudent[j],&structTempData,sizeof(struct _STUDENT_));
}
}
}
}
void SortName(){
struct _STUDENT_ structTempData;
int i;
int j;
// 번호순으로 정렬
for( i=0;i<iRealStudentCount;i++){
for( j=0;j<iRealStudentCount;j++){
if(strcmp(infoStudent[i].name ,infoStudent[j].name)< 0){
// 데이터 교환
memcpy(&structTempData,&infoStudent[i],sizeof(struct _STUDENT_));
memcpy(&infoStudent[i],&infoStudent[j],sizeof(struct _STUDENT_));
memcpy(&infoStudent[j],&structTempData,sizeof(struct _STUDENT_));
}
}
}
}
void SortRank(){
struct _STUDENT_ structTempData;
int i;
int j;
// 석차순으로 정렬
for( i=0;i<iRealStudentCount;i++){
for( j=i;j<iRealStudentCount;j++){
if(infoStudent[i].iRank > infoStudent[j].iRank ){
// 데이터 교환
memcpy(&structTempData,&infoStudent[i],sizeof(struct _STUDENT_));
memcpy(&infoStudent[i],&infoStudent[j],sizeof(struct _STUDENT_));
memcpy(&infoStudent[j],&structTempData,sizeof(struct _STUDENT_));
}
}
}
}
void InputData(){
int i;
int j;
FILE *fpInput;
char chKor[10]; // 임시로 문자열로 받을 국어 점수
char chmath[10];// 임시로 문자열로 받을 수학 점수
char chEng[10]; // 임시로 문자열로 받을 영어 점수
iRealStudentCount = 0;
fpInput=fopen("input_data.txt", "rt");
if(fpInput)// 꼭 파일이 열렸는지 확인해야 합니다.
{
i = 0;
while(!feof(fpInput) && i<MAX_STUDENT_COUNT) {
fscanf(fpInput, "%s %s %s %s",infoStudent[i].name,chKor,chmath,chEng);
if(0 == strlen(infoStudent[i].name)) break;
infoStudent[i].iNumber = i+1;
infoStudent[i].kor = atoi(chKor);
infoStudent[i].math = atol(chmath);
infoStudent[i].eng = atol(chEng);
infoStudent[i].avg = (float)((infoStudent[i].kor +infoStudent[i].math+infoStudent[i].eng )/3.0);
// 최고점수 갱신
if (structMAXData.kor < infoStudent[i].kor) structMAXData.kor = infoStudent[i].kor;
if (structMAXData.math < infoStudent[i].math) structMAXData.math = infoStudent[i].math;
if (structMAXData.eng < infoStudent[i].eng) structMAXData.eng = infoStudent[i].eng;
if (structMAXData.avg < infoStudent[i].avg) structMAXData.avg = infoStudent[i].avg;
i++;
iRealStudentCount++;
}
fclose(fpInput); // 파일이 열렸을 때만 close 가능
}
// 석차 결정
for( i=0;i<iRealStudentCount;i++){
infoStudent[i].iRank = 0;
for( j=0;j<iRealStudentCount;j++){
if(infoStudent[i].avg < infoStudent[j].avg ){
infoStudent[i].iRank++;
}
}
}
}
int main(){
int i=0;
int j=0;
int iMenu; // 선택된 메뉴
// 최고점수 초기화
structMAXData.kor = 0;
structMAXData.math = 0;
structMAXData.eng = 0;
iMenu = 0;
while((-1 != iMenu)|| (0 == iMenu)){
printf("\n1. 데이터 입력");
printf("\n2. 리스트 보기");
printf("\n3. 번호순으로 정렬");
printf("\n4. 석차순으로 정렬");
printf("\n5. 이름순으로 정렬");
printf("\n메뉴를 선택해 주세요.");
scanf("%d",&iMenu);
switch(iMenu){
case 1:
printf("\n1 번 메뉴.");
InputData();
break;
case 2:
printf("\n2 번 메뉴.");
ViewAllList();
break;
case 3:
printf("\n3 번 메뉴.");
SortNumber();
ViewAllList();
break;
case 4:
printf("\n4 번 메뉴.");
SortRank();
ViewAllList();
break;
case 5:
printf("\n5 번 메뉴.");
SortName();
ViewAllList();
break;
default:
printf("\ndefault메뉴.");
iMenu = (-1);
break;
}
}
ViewAllList();
printf("\n작업 완료!!!!!\n");
return 0;
}