csv형식의 파일을 읽어 총점 평균 석차 분산 표준편차를 계산한 후 csv파일로 생성하는 프로그램
csv는 엑셀에서 불러올수있는 쉼표단위로 구분된 텍스트파일이다
csv파일의 내용은 다음과같다
학번,성명,국어,영어,수학,과학,윤리,회계,총점,평균,석차
1,홍길동,90,90,60,30,60,60,
2,민경미,50,50,80,60,80,60,
3,김기성,30,30,90,60,30,90,
4,박지훈,60,60,50,90,60,50,
5,손흥민,60,60,30,50,60,30,
6,류현진,80,80,60,30,80,60,
7,강동원,60,90,60,60,90,60,
8,동물원,60,50,80,60,60,60,
9,유재하,90,70,50,90,60,80,
10,김광석,50,50,30,50,60,90,
//file*파일포인터가 있어야한다
//파일이 크기가 정해져있지않기때문 fclose(fp);닫기
//strtor(str,",")콤마단위로 분리
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include<math.h>
#define MAN 10
struct student
{
int number;
char name[20];
int kor;
int eng;
int mat;
int sci;
int eth;
int acc;
int sum;
float avg;
int rank;
float variance;
float deviation;
};
struct student studentinfo[MAN];//학생수
int main(void)
{
FILE* fp;
char str[128];
if ((fp = fopen("sungjuk.csv", "r")) == NULL)
{
printf("File open error...\n");//파일이 열리지않을때
fclose(fp);
return 0;
}
//자료를 섞지말고 통일하는것이 편하다 문자열을 많이쓴다
fgets(str, 128, fp);//파일에서 문자열을 가져오는함수
//fgets(문자열을 가리키는 캐릭터타입의 포인터,한번에 가져올 문자열의 길이,파일의 파일포인터)
//\n이 나올때까지 가져온다(파일끝까지 가서 읽을게없으면 null문자 반환)
char* ptr = strtok(str, ",");//가져온 문자열을 쉼표기준으로 자름(쉼표가 사라지고 널문자가 들어간다) 후에 대입한다
while (ptr != NULL)//널값이 나올때까지 반복한다는뜻(줄이 끝날때까지 반복)
{
printf("%s ", ptr);//ptr출력
ptr = strtok(NULL, ",");//출력한 다음부터 쉽표단위로 자른후 저장한다
}//첫줄 출력
for (int i = 0; i < MAN; i++)//사람수만큼 반복한다 모든데이터를 구조체에 넣는다
{
fgets(str, 128, fp);
ptr = strtok(str, ",");
studentinfo[i].number = atoi(ptr);//번호를 구조체에 저장
ptr = strtok(NULL, ",");
strcpy(studentinfo[i].name, ptr);//문자열 저장
ptr = strtok(NULL, ",");
studentinfo[i].kor = atoi(ptr);
ptr = strtok(NULL, ",");
studentinfo[i].eng = atoi(ptr);
ptr = strtok(NULL, ",");
studentinfo[i].mat = atoi(ptr);
ptr = strtok(NULL, ",");
studentinfo[i].sci = atoi(ptr);
ptr = strtok(NULL, ",");
studentinfo[i].eth = atoi(ptr);
ptr = strtok(NULL, ",");
studentinfo[i].acc = atoi(ptr);
}
for (int i = 0; i < MAN; i++) //평균과 합계 분산과 표준편차를 구조체에 넣는다
{
studentinfo[i].sum = studentinfo[i].eng + studentinfo[i].kor + studentinfo[i].mat + studentinfo[i].sci + studentinfo[i].eth + studentinfo[i].acc;
studentinfo[i].avg = (float)studentinfo[i].sum / 6;
studentinfo[i].variance = (pow((studentinfo[i].kor - studentinfo[i].avg),2) + pow((studentinfo[i].eng - studentinfo[i].avg),2) + pow((studentinfo[i].mat - studentinfo[i].avg),2) +
pow((studentinfo[i].sci - studentinfo[i].avg),2) + pow((studentinfo[i].eth - studentinfo[i].avg),2) + pow((studentinfo[i].acc - studentinfo[i].avg),2) ) / 6;//분산
studentinfo[i].deviation = sqrt(studentinfo[i].variance);//표준편차
}
for (int i = 0; i < MAN; i++)//순위를 구한다
{
studentinfo[i].rank = 1;//순위는 1부터 시작이다 초기값
for (int j = 0; j < MAN; j++)
{
{
if (studentinfo[i].sum < studentinfo[j].sum)
{
studentinfo[i].rank = studentinfo[i].rank + 1;
}
}
}
}
for (int i = 0; i < MAN; i++)//출력부
{
printf("번호:%d 이름:%s 국어:%d 영어:%d 수학:%d 과학:%d 윤리:%d 회계:%d 총점:%d 평균:%.1f 등수%d 분산:%.1f 표준편차:%.1f\n",
studentinfo[i].number, studentinfo[i].name, studentinfo[i].kor, studentinfo[i].eng, studentinfo[i].mat, studentinfo[i].sci, studentinfo[i].eth, studentinfo[i].acc, studentinfo[i].sum, studentinfo[i].avg, studentinfo[i].rank, studentinfo[i].variance, studentinfo[i].deviation);
}
FILE* fp1 = fopen("Trans.csv", "w");
if (fp1 == NULL)
{
printf("File creation error...\n");
return 0;
}
fprintf(fp1, "번호,이름,국어,영어,수학,과학,윤리,회계,총점,평균,등수,분산,표준편차\n");
for (int i = 0; i < MAN; i++)
{
fprintf(fp1, "%d,%s,%d,%d,%d,%d,%d,%d,%d,%.1f,%d,%.1f,%.1f\n",
studentinfo[i].number, studentinfo[i].name, studentinfo[i].kor, studentinfo[i].eng, studentinfo[i].mat, studentinfo[i].sci, studentinfo[i].eth, studentinfo[i].acc, studentinfo[i].sum, studentinfo[i].avg, studentinfo[i].rank,studentinfo[i].variance, studentinfo[i].deviation);
}
fclose(fp);
fclose(fp1);
}
콘솔에서의 출력결과
Trans.csv로 생성된 파일
번호,이름,국어,영어,수학,과학,윤리,회계,총점,평균,등수,분산,표준편차
1,홍길동,90,90,60,30,60,60,390,65.0,3,425.0,20.6
2,민경미,50,50,80,60,80,60,380,63.3,5,155.6,12.5
3,김기성,30,30,90,60,30,90,330,55.0,8,725.0,26.9
4,박지훈,60,60,50,90,60,50,370,61.7,6,180.6,13.4
5,손흥민,60,60,30,50,60,30,290,48.3,10,180.6,13.4
6,류현진,80,80,60,30,80,60,390,65.0,3,325.0,18.0
7,강동원,60,90,60,60,90,60,420,70.0,2,200.0,14.1
8,동물원,60,50,80,60,60,60,370,61.7,6,80.6,9.0
9,유재하,90,70,50,90,60,80,440,73.3,1,222.2,14.9
10,김광석,50,50,30,50,60,90,330,55.0,8,325.0,18.0
엑셀로 csv파일을 불러온 결과
'개발 > C++기본문법' 카테고리의 다른 글
오버로딩과 다형성 (0) | 2023.05.16 |
---|---|
클래스의 상속과 가상함수의 예제(오버라이딩) (0) | 2023.05.16 |
C언어에서 .txt파일을 읽고 배열에 저장한후 출력하는 예제 (0) | 2023.04.20 |
scanf와 gets의 차이와 문자열 입력시 공백에 대한 해결방법 (0) | 2023.04.18 |
1~45까지의 난수를 [6]배열에 저장 후 크기대로 정렬하고 중복은 제거 (0) | 2023.04.18 |