Skip to content

Instantly share code, notes, and snippets.

@Sharlottes
Last active June 19, 2023 12:21
Show Gist options
  • Save Sharlottes/19ffdfb9d13648725e9d74bf30a1ce10 to your computer and use it in GitHub Desktop.
Save Sharlottes/19ffdfb9d13648725e9d74bf30a1ce10 to your computer and use it in GitHub Desktop.
프로그래밍 기초 레포트2 과제
#include <stdio.h>
#include <stdbool.h>
typedef enum {
A, B, C, F
} Rank;
char rankAsChar[4] = "ABCF";
typedef struct {
unsigned int id;
float totalPoint;
Rank rank;
} Student;
typedef struct {
unsigned int id;
float totalPoint;
} StudentWithoutRank;
typedef struct {
char* errorMessage;
bool (*condition)(void*);
} Validator;
void cp_question(char* message, char* valuep, Validator validator)
{
while (true)
{
printf(message);
getchar();
scanf("%c", valuep);
if (validator.condition(valuep)) return;
printf("%s\n", validator.errorMessage);
}
}
void fp_question(char* message, float* valuep, Validator validator)
{
while (true)
{
printf(message);
scanf("%f", valuep);
if (validator.condition(valuep)) return;
printf("%s\n", validator.errorMessage);
}
}
void uip_question(char* message, unsigned int* valuep, Validator validator)
{
while (true)
{
printf(message);
scanf("%u", valuep);
if (validator.condition(valuep)) return;
printf("%s\n", validator.errorMessage);
}
}
bool ui_isRank(void* value)
{
char valueChar = *(char*)value;
if (valueChar == 'E') return true;
for (int i = 0; i < 4; i++)
{
char rankChar = rankAsChar[i];
if (valueChar == rankChar) return true;
}
return false;
}
bool ui_isHundredThousands(void* value)
{
if (*(int*)value < 0) return false; //may already overflowed
return *(unsigned int*)value >= 1000000000 && *(unsigned int*)value < 4294967295;
}
bool ui_isNotOverTwenty(void* value)
{
return *(unsigned int*)value <= 20;
}
bool f_isNotOverHoundred(void* value)
{
return *(float *)value <= 100;
}
Validator rankValidator = {
"경고! 이 값은 학점이 아닙니다. 유효한 입력: A, B, C, F, E", ui_isRank
};
Validator notOverTwentyValidator = {
"경고! 이 수는 20을 초과할 수 없습니다.", ui_isNotOverTwenty
};
Validator hundredThousandsValidator = {
"경고! 이 수는 10자리 수여야 합니다.", ui_isHundredThousands
};
Validator notOverHoundredValidator = {
"경고! 이 수는 100을 초과할 수 없습니다.", f_isNotOverHoundred
};
float getTotalPoint(float attendance, float mid_exam, float end_exam, float report, float manner)
{
float totalPoint = attendance * 0.1f + mid_exam * 0.3f + end_exam * 0.3f + report * 0.2f + manner * 0.1f;
return totalPoint;
}
Student createStudent(int id, float totalPoint, float studentsTotalPoint)
{
Rank rank;
float ratio = (studentsTotalPoint - totalPoint) / studentsTotalPoint;
if (totalPoint == 0) rank = F;
else if (ratio <= 0.3f) rank = A;
else if (ratio <= 0.7f) rank = B;
else rank = C;
Student student = { id, totalPoint, rank };
return student;
}
int main()
{
Student students[20];
StudentWithoutRank studentsWithoutRank[20];
unsigned int studentAmount;
unsigned int id;
float attendance, mid_exam, end_exam, report, manner;
float studentsTotalPoints = 0;
uip_question("학생 수(최대 20): ", &studentAmount, notOverTwentyValidator);
printf("----------------------------\n");
for (int i = 0; i < studentAmount; i++)
{
uip_question("학번: ", &id, hundredThousandsValidator);
fp_question("출석 점수: ", &attendance, notOverHoundredValidator);
fp_question("중간고사 점수: ", &mid_exam, notOverHoundredValidator);
fp_question("기말고사 점수: ", &end_exam, notOverHoundredValidator);
fp_question("레포트 점수: ", &report, notOverHoundredValidator);
fp_question("학습태도 점수: ", &manner, notOverHoundredValidator);
float totalPoint = getTotalPoint(attendance, mid_exam, end_exam, report, manner);
studentsTotalPoints += totalPoint;
StudentWithoutRank studentWithoutRank = { id, totalPoint };
studentsWithoutRank[i] = studentWithoutRank;
}
printf("----------------------------\n");
for (int i = 0; i < studentAmount; i++)
{
StudentWithoutRank studentWithoutRank = studentsWithoutRank[i];
Student student = createStudent(studentWithoutRank.id, studentWithoutRank.totalPoint, studentsTotalPoints);
students[i] = student;
printf("학번: %u, 학점: %c, 총점: %.3f\n", student.id, rankAsChar[student.rank], student.totalPoint);
}
printf("----------------------------\n");
while (true)
{
char searchRankChar;
cp_question("학점 검색(나가기: E): ", &searchRankChar, rankValidator);
if (searchRankChar == 'E') break;
for (int i = 0; i < studentAmount; i++)
{
Student student = students[i];
if (rankAsChar[student.rank] == searchRankChar)
{
printf("학번: %u, 학점: %c, 총점: %.3f\n", student.id, rankAsChar[student.rank], student.totalPoint);
}
}
printf("----------------------------\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment