 |
|
/* GPA Calculator Script.
>>>>>>>>>>>How it works ?<<<<<<<<<<<<<<
GPA CALCULATIN is based on table from : http://www.senecac.on.ca/registrar/records/gpa.html
LETTER GRADE CONVERTER are based on table from : http://people.senecac.on.ca/lucie.dutfield/GPA.htm
GPA Calculator:
1.You enter number of subjects you need to calculate gpa for
2.You enter marks for your subjects. (Mark can include "+")
3.GPA claculator uses formula:
Grade Point Average = Total Grade Points Earned / Total Units of Credits.
4. If GPA is bigger than 3.55 you receive Honour or if more than 3.95 High Honour
Letter grade converter:
You enter the percentage, script shows you letter grade
Created by Anatoly Spektor
Have fun! */
#include <stdio.h>
#include <ctype.h>
#define SIZE 3
/* Function prototypes */
void percent_to_letter(void);
void gpa_calc(void);
void clear_buffer(void);
int get_an_int(void);
float get_float(void);
int yesorno (void);
int validate_mark(char mark[]);
/* Main Menu */
char display_menu () {
char choice = 'o';
printf("\n\t-------------------------------------\n");
printf("\t SENECA COLLEGE GPA CALCULATOR");
printf("\n\t--------------------------------------\n");
printf("\n\t Please choose one of the following options:\n");
printf("\n\t\t[a] Convert percentage into letter grade");
printf("\n\t\t Calculate GPA");
printf("\n\t\t[q] Quit Program");
/* Choose between GPA calculator or percentage to grade converter */
do {
printf("\n\n\n\t * Enter letter [a], [b] or [q]: ");
choice=getchar();
clear_buffer();
choice=tolower(choice);
if ((choice != 'a') && (choice != 'b')&& (choice != 'q'))
printf("\n\t >>> Only three options available: [a] and [q].\n");
}while((choice != 'a') && (choice != 'b') && (choice != 'q'));
printf("\n\t >>> Your option is [%c] \n\n", choice);
if (choice == 'a')
return 2;
if (choice == 'b')
return 1;
else if (choice == 'q'){ /*[q] - Quit option */
printf("\t*** Thank you for using GPA Calculator! Good bye! ***\n\n");
return 0;
EOF;
}
}
/* MAIN function - Switch between GPA and Percent to letter */
int main (void) {
int yn=0, numsub=0, option =-3;;
while (option != 0){
option=display_menu(); /* Calling a menu */
switch (option) {
case 1:
do{
gpa_calc(); /*Calling GPA */
printf("\n\t* Do you want to try again [yes] [no]: ");
yn=yesorno();
clear_buffer();
}while(yn != 0);
break;
case 2:
do{
percent_to_letter(); /* Calling Percent to letter converter */
printf("\n\t* Do you want to check another letter [yes] [no]: ");
yn = yesorno();
clear_buffer();
}while (yn != 0);
/* Quit option is inside display_menu */
}
}
return 0;
}
/* Percent to letter function */
void percent_to_letter () {
float num;
do {
printf("\n\t* Please enter percentage: ");
num=get_float();
if (num < 0)
printf("\n\t>>> Input can not contain negative numbers\n");
else if (num > 100)
printf("\n\t>>> Max number is 100\n");
}while((num < 0)|| (num > 100));
if (num <= 54.9) /* Acting on input, based on table from URL listed above */
printf("\n\t>>> %.1f percent is letter : F\n\n", num);
if ((num >=55)&&(num<60))
printf("\n\t>>> %.1f percent is letter : D\n\n", num);
if ((num >=60)&&(num<65))
printf("\n\t>>> %.1f percent is letter : C\n\n",num);
if ((num >=65)&&(num<70))
printf("\n\t>>> %.1f percent is letter : C+\n\n",num);
if ((num >=70)&&(num<75))
printf("\n\t>>> %.1f percent is letter : B\n\n",num);
if ((num >=75)&&(num<80))
printf("\n\t>>> %.1f percent is letter : B+\n\n",num);
if ((num >=80)&&(num<90))
printf("\n\t>>> %.1f percent is letter : A\n\n",num);
if ((num >=90)&&(num<=100))
printf("\n\t>>> %.1f percent is letter : A+\n\n",num);
}
void gpa_calc (void) {
int i=0, grade_points =0, val=0;
int num =0;
char mark[SIZE]="OO";
float num_mark=0, u_of_credit=0, gpa_calc =0;
do{
printf("\n\t* Please enter number of subjects [max 10]: "); /* I assumed 10 subjects would be enough*/
num= get_an_int(); /* To avoid crashes calling function get_an_int instead of basic scanf */
if (num > 10)
printf("\n\t>>> Number of subjects should be 10 or less!\n");
else if (num <0)
printf("\n\t>>> Number of subjects can not be negative!\n");
}while ((num < 0) || (num >10));
for (i=0; i < num; i++){
do {
printf("\n\t* Please enter mark for subject number %d: ", i+1);
scanf("%s",mark);
clear_buffer();
mark[0]=toupper(mark[0]);
val=validate_mark(mark); /* Function needed to read "+" signs, and avoid errors */
}while(val != 1);
/* Assign number to each letter grade, for claculation that are based on table from URL listed above */
if(((mark[0] == 'A')&&(mark[1] == '+')) || (mark[0] == 'A')){
num_mark = 20.0;
}
else if ((mark[0] == 'B')&&(mark[1] == '+')){
num_mark = 17.5;
}
else if (mark[0] == 'B'){
num_mark = 15.0;
}
else if ((mark[0] == 'C')&&(mark[1] == '+')){
num_mark = 12.5;
}
else if (mark[0] == 'C'){
num_mark = 10.0;
}
else if (mark[0] == 'D'){
num_mark = 5.00;
}
else {
num_mark = 0;
}
u_of_credit+=num_mark;
grade_points+=5;
}
/* GPA output and calculations */
printf("\n\t>>>>>>>>>>>>>>>>>>>>>\n");
gpa_calc = u_of_credit/grade_points;
printf("\n\n\tYour GPA is: %.1f", gpa_calc);
if (gpa_calc > 3.95)
printf("\n\n\tCongratulations, you will recieve HIGH HONOUR!\n\n");
else if(gpa_calc > 3.55)
printf("\n\n\tCongratulations, you will recieve HONOUR!\n\n");
printf("\n\n\t This Program was created by Anatoly Spektor\n");
printf("\n\t>>>>>>>>>>>>>>>>>>>>>>>>>\n") ;
}
void clear_buffer(void){ /* function needed to read characters carefully */
while(getchar() != '\n')
;
}
int get_an_int(void){
int n,error = 0;
char junk;
do{
if( 0 == scanf("%d", &n) || getchar() != '\n'){
clear_buffer();
printf("\n\t>>> Error! Input should contain only numbers\n");
printf("\n\t * Please try again. Enter number of subjects [max 10]: " );
error = 1;
} else error = 0;
}while( error );
return n;
}
float get_float(void){ /* needed to read floats carefully */
float n;
int error = 0;
char junk;
do{
if( 0 == scanf("%f", &n) || getchar() != '\n'){
clear_buffer();
printf("\n\t>>> Error! Input should contain only numbers\n");
printf("\n\t * Please try again. Enter number: " );
error = 1;
} else error = 0;
}while( error );
return n;
}
int yesorno() {
int c, rc = -1;
do {
c = getchar();
if ((c == 'y') || (c == 'Y')) {
c = getchar();
if ((c == 'e') || (c == 'E')) {
c = getchar();
if ((c == 's') || (c == 'S')) {
rc = 1;
}
}
}
else if ((c == 'n') || (c == 'N')) {
c = getchar();
if ((c == 'o') || (c == 'O')) {
rc = 0;
}
}
if (rc == -1 && c != -1) {
printf("\n\t>>> There are only two options [yes] or [no]: ");
while (c != -1 && c != '\n')
c = getchar();
}
} while (rc == -1 && c != -1);
return rc;
}
int validate_mark ( char mark[]){
int i, val =0;
for (i=0; i<3;i++){
if ((mark[0] == 'A') || (mark[0] == 'B') || (mark[0] == 'C') || (mark[0] == 'D') || (mark[0] == 'E') || (mark[0] == 'F'))
val=1;
if ((mark [1] == '+') || (mark [1] == 0))
val+=val;
if (val == 2)
return 1;
else {
printf("\n\t >>> Invalid Letter!\n");
printf("\n\t >>> Available letters: A-F [including letters with '+' sign])\n");
return 0;
}
i++;
}
}

Tag : - - - -
|
ประวัติการแก้ไข 2014-01-06 12:24:35 2014-01-06 12:26:46 2014-01-06 12:27:53
|
 |
 |
 |
 |
Date :
2013-12-30 20:23:36 |
By :
noksaak |
View :
3620 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |