C 기초특강 종합과제
과제내용 구조체를이용하여교과목이름과코드를파일로부터입력받아관리 구조체를이용하여학생들의이름, 학번과이수한교과목의코드와점수를파일로부터입력 학생개인별총점, 평균계산 교과목별이수학생수, 총점및평균을계산 결과를파일에저장하는프로그램을작성 2
Makefile OBJS = score_main.o score_input.o score_calc.o score_print.o score_util.o CC = gcc score : score.h $(OBJS) $(CC) -o score $(OBJS) score_main.o : score.h score_main.c score_util.o : score.h score_util.c score_input.o : score.h score_input.c score_calc.o : score.h score_calc.c score_print.o : score.h score_print.c test1 : test1.o score_input.o score_util.o gcc -o test1 test1.o score_input.o score_util.o test2 : test2.o score_input.o score_util.o gcc -o test2 test2.o score_input.o score_util.o test3 : test3.o score_input.o score_util.o score_print.o score_calc.o gcc -o test3 test3.o score_input.o score_util.o score_print.o score_calc.o clean : rm $(OBJS) score rebuild : make clean make score 3
score.h (1) 1 #ifndef _SCORE_H 2 #define _SCORE_H 3 4 #include <stdio.h> 5 6 #define MAX_CLASS_SIZE 100 7 #define MAX_SUBJECTS 50 8 #define MAX_SUBJECTS_PER_STD 10 9 10 #define LINE_SIZE 255 11 #define SUB_NAME_SIZE 40 12 #define STD_NAME_SIZE 12 13 #define STD_ID_SIZE 12 14 15 typedef struct { 16 char name[sub_name_size]; 17 int sub_code; 18 int scored_std_no; 19 int total_std_score; 20 } subject_t; 21 22 typedef struct { 23 int sub_code; 24 int score; 25 } sub_score_t; 26 27 typedef struct { 28 char std_name[std_name_size]; 29 char std_id[std_id_size]; 30 sub_score_t sub_scores[max_subjects_per_std]; 31 int subjects_no; 32 int total_score; 33 double average; 34 } score_t; 4
score.h (2) 36 int is_dup_sub(subject_t subjects[], int target); 37 int sub_code_to_index(subject_t subjects[], int table_size, int sub_code); 38 int is_valid_sub_code(subject_t subjects[], int table_size, int sub_code); 39 40 int fget_subjects(file *inflie, subject_t subjects[]); 41 int fget_std_scores(file *infile, score_t scores[], 42 subject_t subjects[], int sub_table_size); 43 44 void calc_personal_total_avg(score_t scores[], int table_size); 45 void calc_class_total_avg(score_t scores[], int table_size, 46 subject_t subjects[], int subjects_no, 47 int *total_all, double *average_all); 48 49 void fput_std_scores(file *outfile, score_t scores[], int table_size, 50 subject_t subjects[], int sub_table_size); 51 void fput_sub_summary(file *outfile, subject_t subjects[], int table_size); 52 53 #endif 5
test1.c 1 #include "score.h" 2 3 main() 4 { 5 FILE *ifile; 6 subject_t subjects[max_subjects] = {0}; 7 int subjects_no; 8 int i; 9 10 ifile = fopen("subject_data", "r"); 11 subjects_no = fget_subjects(ifile, subjects); 12 fclose(ifile); 13 14 for (i = 0; i < subjects_no; i++) { 15 printf("%s %d\n", subjects[i].name, subjects[i].sub_code); 16 } 17 } 6
test2.c 1 #include "score.h" 2 3 main() 4 { 5 FILE *ifile; 6 subject_t subjects[max_subjects] = {0}; 7 score_t scores[max_class_size] = {0}; 8 int subjects_no; 9 int students_no; 10 int i; 11 int j; 12 13 ifile = fopen("subject_data", "r"); 14 subjects_no = fget_subjects(ifile, subjects); 15 fclose(ifile); 16 17 for (i = 0; i < subjects_no; i++) { 18 printf("%s %d\n", subjects[i].name, subjects[i].sub_code); 19 } 20 21 ifile = fopen("score_data", "r"); 22 students_no = fget_std_scores(ifile, scores, subjects, subjects_no); 23 fclose(ifile); 24 25 for (i = 0; i < students_no; i++) { 26 printf("%s %s %d ", scores[i].std_name, scores[i].std_id, scores[i].subjects_no); 27 for (j = 0; j < scores[i].subjects_no; j++) { 28 printf("%d %d ", scores[i].sub_scores[j].sub_code, 29 scores[i].sub_scores[j].score); 30 } 31 printf("\n"); 32 } 33 } 7
test3.c 1 #include "score.h" 2 3 main() 4 { 5 FILE *ifile; 6 subject_t subjects[max_subjects] = {0}; 7 score_t scores[max_class_size] = {0}; 8 int subjects_no; 9 int students_no; 10 int total_score; 11 double total_avg; 12 int i; 13 int j; 14 15 ifile = fopen("subject_data", "r"); 16 subjects_no = fget_subjects(ifile, subjects); 17 fclose(ifile); 18 19 for (i = 0; i < subjects_no; i++) { 20 printf("%-20s %d\n", 21 subjects[i].name, subjects[i].sub_code); 22 } 23 24 ifile = fopen("score_data", "r"); 25 students_no = fget_std_scores(ifile, 26 scores, subjects, subjects_no); 27 fclose(ifile); 28 calc_personal_total_avg(scores, students_no); 29 calc_class_total_avg(scores, students_no, subjects, 30 subjects_no, &total_score, &total_avg); 31 fput_std_scores(stdout, scores, students_no, 32 subjects, subjects_no); 33 fput_sub_summary(stdout, subjects, subjects_no); 34 } 8
score_main.c (1) 1 #include "score.h" 2 #include <stdlib.h> 3 4 main() 5 { 6 FILE *ifile; 7 subject_t subjects[max_subjects] = {0}; 8 score_t scores[max_class_size] = {0}; 9 int subjects_no; 10 int students_no; 11 int total_score; 12 double total_avg; 13 14 ifile = fopen("subject_data", "r"); 15 if (ifile) { 16 subjects_no = fget_subjects(ifile, subjects); 17 fclose(ifile); 18 19 ifile = fopen("score_data", "r"); 20 if (ifile) { 21 students_no = fget_std_scores(ifile, scores, subjects, subjects_no); 22 fclose(ifile); 23 24 calc_personal_total_avg(scores, students_no); 25 calc_class_total_avg(scores, students_no, 26 subjects, subjects_no, &total_score, &total_avg); 27 28 fput_std_scores(stdout, scores, students_no, subjects, subjects_no); 29 fput_sub_summary(stdout, subjects, subjects_no); 30 } 9
score_main.c (2) 31 else { 32 fprintf(stderr, "Error in fopen!\n"); 33 exit(2); 34 } 35 } 36 else { 37 fprintf(stderr, "Error in fopen!\n"); 38 exit(1); 39 } 40 } 10
score_util.c (1) 1 #include "score.h" 2 3 /******************************************************************************/ 4 int is_dup_sub(subject_t subjects[], int target) 5 { 6 int i; 7 8 for (i = 0; i < target; i++) { 9 if (!strncmp(subjects[i].name, 10 subjects[target].name, 11 SUB_NAME_SIZE) 12 (subjects[i].sub_code 13 == subjects[target].sub_code)) 14 return 1; 15 } 16 return 0; 17 } 18 11
score_util.c (2) 19 /******************************************************************************/ 20 int sub_code_to_index(subject_t subjects[], int table_size, int target) 21 { 22 int i; 23 24 for (i = 0; i < table_size; i++) { 25 if (subjects[i].sub_code == target) 26 return i; 27 } 28 return -1; 29 } 30 31 /******************************************************************************/ 32 int is_valid_sub_code(subject_t subjects[], int table_size, int target) 33 { 34 if (sub_code_to_index(subjects, table_size, target) >= 0) 35 return 1; 36 else 37 return 0; 38 } 12
score_input.c (1) 1 #include "score.h" 2 #include <string.h> 3 4 /******************************************************************************/ 5 int fget_subjects(file* infile, subject_t subjects[]) 6 { 7 int i; 8 char line[line_size]; 9 int line_no = 0; 10 11 for (i = 0; i < MAX_SUBJECTS; i++) { 12 if (fgets(line, LINE_SIZE, infile)) { 13 ++line_no; 14 15 if (sscanf(line, "%s %d", 16 &subjects[i].name, 17 &subjects[i].sub_code)!= 2) { 18 return i; 19 } 20 else { 21 if (is_dup_sub(subjects, i)) { 22 fprintf(stderr, 23 "\nduplicated sub_code or sub_name in line %d!\n", 24 line_no); 25 i--; 26 } 27 } 28 } 29 else { 30 return i; 31 } 32 } 33 return i; 34 } 13
score_input.c (2) 36 /******************************************************************************/ 37 int fget_std_scores(file* infile, score_t scores[], subject_t subjects[], int sub_table_size) 38 { 39 int i; 40 int j; 41 char line[line_size]; 42 char *tmp; 43 int read_char_cnt; 44 int final_pos; 45 46 for (i = 0; i < MAX_CLASS_SIZE; i++) { 47 memset(line, 0, LINE_SIZE); 48 if (fgets(line, LINE_SIZE, infile)) { 49 tmp = line; 50 final_pos = strlen(line); 51 52 if (sscanf(tmp, "%s %s%n", 53 &scores[i].std_name, 54 &scores[i].std_id, 55 &read_char_cnt)!= 2) { 56 return i; 57 } 14
score_input.c (3) 58 else { 59 tmp += read_char_cnt; 60 for (j = 0; (j < MAX_SUBJECTS_PER_STD) && (tmp < line + final_pos); j++) { 61 if (sscanf(tmp, "%d %d%n", 62 &scores[i].sub_scores[j].sub_code, 63 &scores[i].sub_scores[j].score, 64 &read_char_cnt) == 2) { 65 tmp += read_char_cnt; 66 if (!is_valid_sub_code(subjects, 67 sub_table_size, 68 scores[i].sub_scores[j].sub_code) 69 (scores[i].sub_scores[j].score < 0) 70 (scores[i].sub_scores[j].score > 100)) { 71 fprintf(stderr, "Invalid data in scores[%d]\n", i); 72 j--; 73 } 74 } 75 else { 76 break; 77 } 78 } 79 scores[i].subjects_no = j; 80 } 81 } 82 else { 83 return i; 84 } 85 } 86 return i; 87 } 15
score_calc.c (1) 1 #include "score.h" 2 3 /******************************************************************************/ 4 void calc_personal_total_avg(score_t scores[], int table_size) 5 { 6 int i; 7 int j; 8 9 for (i = 0; i < table_size; i++) { 10 scores[i].total_score = 0; 11 if (scores[i].subjects_no > 0) { 12 for (j = 0; j < scores[i].subjects_no; j++) 13 scores[i].total_score += scores[i].sub_scores[j].score; 14 15 scores[i].average = (double)scores[i].total_score / scores[i].subjects_no; 16 } 17 else { 18 scores[i].average = 0.; 19 } 20 } 21 } 16
score_calc.c (2) 23 /******************************************************************************/ 24 void calc_class_total_avg(score_t scores[], int table_size, 25 subject_t subjects[], int subjects_no, 26 int *total_all, double *average_all) 27 { 28 int i; 29 int j; 30 int sub_index; 31 32 *total_all = 0; 33 *average_all = 0.; 34 35 for (i = 0; i < table_size; i++) { 36 *total_all += scores[i].total_score; 37 38 for (j = 0; j < scores[i].subjects_no; j++) { 39 sub_index = sub_code_to_index(subjects, subjects_no, 40 scores[i].sub_scores[j].sub_code); 41 if (sub_index >= 0) { 42 subjects[sub_index].scored_std_no++; 43 subjects[sub_index].total_std_score 44 += scores[i].sub_scores[j].score; 45 } 46 else { 47 fprintf(stderr, "Invalid subject code in scores[%d]!\n", i); 48 } 49 } 50 } 51 } 17
score_print.c (1) 1 #include "score.h" 2 3 /******************************************************************************/ 4 void fput_std_scores(file *outfile, score_t scores[], int table_size, 5 subject_t subjects[], int sub_table_size) 6 { 7 int i; 8 int j; 9 int sub_index; 10 11 if (table_size > 0) { 12 fprintf(outfile, "Class Score Table\n\n"); 13 14 for (i = 0; i < table_size; i++) { 15 if (scores[i].subjects_no > 0) { 16 fprintf(outfile, "%-20s %-20s ", 17 scores[i].std_name, scores[i].std_id); 18 fprintf(outfile, "%4d / %d = %.2f\t", 19 scores[i].total_score, 20 scores[i].subjects_no, 21 scores[i].average); 22 23 for (j = 0; j < scores[i].subjects_no; j++) { 24 sub_index = sub_code_to_index(subjects, 25 sub_table_size, 26 scores[i].sub_scores[j].sub_code); 27 28 if (sub_index >= 0) { 29 fprintf(outfile, "%-20s %4d ", 30 subjects[sub_index].name, 31 scores[i].sub_scores[j].score); 32 } 33 } 34 fprintf(outfile, "\n"); 35 } 36 } 37 } 38 } 18
score_print.c (2) 40 /******************************************************************************/ 41 void fput_sub_summary(file *outfile, subject_t subjects[], int table_size) 42 { 43 int i; 44 45 if (table_size > 0) { 46 fprintf(outfile, "\nsubjects Summary\n\n"); 47 48 for (i = 0; i < table_size; i++) { 49 if (subjects[i].scored_std_no > 0) { 50 fprintf(outfile, "%-20s %6d %4d / %2d = %.2f\n", 51 subjects[i].name, 52 subjects[i].sub_code, 53 subjects[i].total_std_score, 54 subjects[i].scored_std_no, 55 (double) subjects[i].total_std_score 56 / subjects[i].scored_std_no); 57 } 58 } 59 } 60 } 19
subject_data 1 전산개론 1101 2 C프로그래밍 1201 3 DataBase 1201 4 기초프로그래밍 1102 관련데이터파일 score_data 1 홍길동 20133301 1101 90 1102 80 1201 70 2 이몽룡 20133302 1101 70 1102 80 3 김기영 20133303 1102 80 1201 200 4 김철수 20133304 1105 80 1201 80 1101 80 5 안현수 20133305 1102 90 1201 80 1101 80 20