일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- crud
- 콘솔
- 버블
- 구조체
- 3차원 배열
- 난수
- Time
- 함수
- 셀
- C언어
- 커서
- 알고리즘
- 정렬
- scanf
- Windows API
- 문자열
- 공백
- 선택
- 삽입
- C
- 구현
- 테트리스
- string
- 배열
- 포인터
- 이진탐색#binary_search
- 파일입출력
- 연결리스트
- 오목#함수#gotoxy#금수#알고리즘#2차원#배열#실무#프로젝트
Archives
- Today
- Total
C언어 알고리즘 정리 및 실무 프로젝트
C언어 성적 처리 프로그램 (파일 입출력, 함수, 구조체, 포인터 이용) 본문
반응형
함수 단위로 기능을 세분화하였습니다. 프로그램 뿐만 아니라 파일에도 성적을 입출력할 수 있게 구성하였습니다.
포인터로 Call by Reference 기능을 구현하였습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // exit(1)
typedef struct Score { // 성적 처리를 위한 구조체 학생 정보 데이터
char hakbun[15];
char name[10];
int kor, eng, tot;
double avg;
}score;
void print_start(FILE *in); // 시작문 출력 (파일에도)
int count_data(); // 성적 처리할 학생 수 입력
score input_data(score people[]); // 입력한 학생의 점수를 반환.
score total_data(score total, score people[]); // 총합 점수를 반환
void swap1(score *n, score *m); // 총점에 따른 입력 데이터 내림차순 정렬
void input_output(FILE *in, score people); // 입력한 학생의 점수 출력 (파일에도)
void print_sentence(FILE *in, score total, int count); // 성적의 총합 출력 (파일에도)
int main(int argc, char * argv[]) { // 명령행 인자
if (argc != 2) {
puts("프로그램 사용법:(실행파일) (내용이 저장될 파일)"), exit(1);
// 명령 프롬프트에서 프로그램을 실행할 때, 매개변수가 2개가 아니면 프로그램을 비정상적으로 종료한다.
}
FILE *in;
in = fopen(argv[1], "wt");
if (in == NULL)puts("쓸 파일이 없습니다.");
score people[20]; // 학생 데이터 처리를 위한 구조체 배열 20개
score total = { "","",0,0,0,0. }; // 학생 데이터의 총합
int count; // 입력하려는 학생 수
int i, j; // for문 반복 변수
count = count_data(); // 성적 처리할 학생 수 입력
for (i = 0; i < count; i++) {
printf("%d 번째 학생의 정보: ", i + 1);
people[i] = input_data(people + i); // 입력 데이터 반환 (구조체를 리턴해서 한 번에 대입한다.)
total = total_data(total, people + i);// 출력 데이터 반환 (구조체를 리턴해서 한 번에 대입한다.)
}
for (i = 0; i < count - 1; i++) {
for (j = i + 1; j < count; j++) {
if (people[i].tot < people[j].tot)
swap1(&people[i], &people[j]); // 총점에 따른 입력 데이터 내림차순 정렬
} // Call by Reference
}
print_start(in); // 시작문 출력
for (i = 0; i < count; i++) {
input_output(in, people[i]); // 각 학생의 입력 정보 출력
}
print_sentence(in, total, count); // 총합 정보 출력
fclose(in);
return 0; // 성공!
}
void print_start(FILE *in) {
printf("\n\n");
printf("------------------------------------------------------------\n");
printf(" 학번\t 이름 국어 영어 총점 평균\n");
printf("------------------------------------------------------------\n");
fprintf(in, "------------------------------------------------------------\n");
fprintf(in, " 학번\t 이름 국어 영어 총점 평균\n");
fprintf(in, "------------------------------------------------------------\n");
}
int count_data() {
int count;
printf("성적 처리할 학생이 몇명입니까? ");
while (1) {
scanf(" %d", &count);
if (count > 10 || count < 1)
{
printf("1 ~ 10명 사이의 수를 입력해줬으면 해. 나 힘들단 말이야! ");
continue;
}
else
break;
}
system("cls");
return count;
}
score input_data(score people[]) { // 배열 요소 1개의 주소를 입력받는다.
scanf(" %s %s %d %d", &(people->hakbun), &(people->name), &(people->kor), &(people->eng)); // 주소로 멤버 접근(->)
people->tot = people->kor + people->eng;
people->avg = people->tot / 2.;
return *people; // score형 배열 요소의 주소로 가서 score형 값을 가져와서 리턴
}
score total_data(score total, score people[]) {
// 매개변수로 배열을 설정하면, 배열 요소의 주소를 입력받는다.
total.kor += people->kor; // 주소로 멤버에 접근한다.
total.eng += people->eng;
total.tot += people->tot;
total.avg += people->avg;
return total;
}
void swap1(score *n, score *m) { // n과 m의 주소로 가서 값을 바꾼다.
score temp;
temp = *n;
*n = *m;
*m = temp;
}
void input_output(FILE *in, score people) {
// score형 배열의 요소 1개를 매개변수로 받아서 파일 포인터가 가리키는 곳에 출력한다.
printf("%12s %12s %7d %7d %7d %7.2f\n", people.hakbun, people.name, people.kor, people.eng, people.tot, people.avg);
fprintf(in, "%12s %12s %7d %7d %7d %7.2f\n", people.hakbun, people.name, people.kor, people.eng, people.tot, people.avg);
}
void print_sentence(FILE *in, score total, int count) { // 파일 포인터가 가리키는 곳에 총합 점수를 출력한다.
printf("------------------------------------------------------------\n");
fprintf(in, "------------------------------------------------------------\n");
printf("%12s %12s %7d %7d %7d %7.2f\n", total.hakbun, total.name, total.kor, total.eng, total.tot, total.avg);
fprintf(in, "%12s %12s %7d %7d %7d %7.2f\n", total.hakbun, total.name, total.kor, total.eng, total.tot, total.avg);
if (count >= 2) { // 2명 이상일 경우에 총점과 총점 평균, 과목 총점을 출력한다.
printf("%12s %12s %7.2f %7.2f %7.2f %7.2f\n", total.hakbun, total.name, total.kor / (double)count, total.eng / (double)count, total.tot / (double)count, total.avg / (double)count);
fprintf(in, "%12s %12s %7.2f %7.2f %7.2f %7.2f\n", total.hakbun, total.name, total.kor / (double)count, total.eng / (double)count, total.tot / (double)count, total.avg / (double)count);
}
}
반응형
'C언어 실무 프로젝트' 카테고리의 다른 글
C언어 콘솔 테트리스 프로그램(사용자 정의 함수 , 3차원 배열, 제어문 로직 활용) Windows API , 표준 라이브러리 활용 ( x86 버전) (4) | 2023.06.12 |
---|---|
C언어 컴소과 학생 관리[CRUD] 프로그램(구조체, 연결리스트(포인터), 함수 활용) x86 버전 (4) | 2022.05.28 |
C언어 오목 프로그램(2인용, 2차원 배열과 심화된 제어문 로직 활용) Windows API 및, 표준 라이브러리 사용 (x86 버전) (4) | 2022.05.28 |
C언어 Up Down 콘솔 게임 (2) | 2022.05.18 |
C언어 숫자야구 프로그램 ( 배열, 제어문, %연산자 활용) 1 ~ 9자리까지 입력이 가능 (2) | 2022.04.22 |
Comments