Menu



Manage

Study_C > 성적받아오기).c Lines 56 | 1.7 KB
다운로드

                        // 학번 국어 영어 수학 성적을 받아 받은 내용과 총점 평균을 콜솔에 출력하는 프로그램
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <stdlib.h> 

//학생 점수 정보를 받는 구조체 score생성
typedef struct
{
    int ID;        //학번
    int Math;      //수학성적 
    int Kor;       //국어성적 
    int Eng;       //영어성적 
    double Aver;   //평균성적 
    int total;     //총점 
}score;

void main()
{
    int i, cnt;     //for문을 돌리기 위한 i변수와, 재적?값 추가할 cnt 변수 추가
    score *s, t = { 0 };

    //파일 열리면 열고 안되면 꺼버리는 코드 추가
    FILE *in;
    in = fopen("in.txt", "r");
    if (in == NULL)
    {
        printf("파일 열기 실패!\n 프로그램 강제종료;");
        return 1;
    }

    //문서 읽어보기 while 쓰니 뻑나서 for문으로 돌린 코드
    for (cnt = 0, s = NULL; !feof(in); cnt++)
    {
        if (fscanf(in, "%d%d%d%d", &t.ID, &t.Math, &t.Kor, &t.Eng) != 4) break;
        s = (score*)realloc(s, sizeof(score) * (cnt + 1));
        s[cnt] = t;
    }
    fclose(in); //다 읽었으면 종료

    //형식 만들기
    printf("===============================================\n");
    printf("   학번     국어    영어   수학    총점    평균\n");
    printf("===============================================\n");

    //저장된 순서대로 불러오기
    for (i = 0; i < cnt; i++)
    {
        (s + i)->total = (s + i)->Math + (s + i)->Kor + (s + i)->Eng;
        (s + i)->Aver = ((s + i)->Math + (s + i)->Kor + (s + i)->Eng) / 3;
        printf("  %d    %d      %d     %d     %d    %.2f\n", (s + i)->ID, (s + i)->Math, (s + i)->Kor, (s + i)->Eng, (s + i)->total, (s + i)->Aver);
    }
    getchar(); //버퍼 비우기
    if (s) free(s); //사용한 메모리 반납

    return 0; //프로그램 종료
}