C Me
So, I recently made my first “major” C program. While I did have previous experience with the C syntax and whatnot (C++ being the very first language that I learned on my own), I found C to be just as or even more…how to put it…ancient? I mean, this language is as old as I am practically. Nonetheless, it is still being used and is still pretty high level compared to assembly, so I don’t mind…much.This program basically takes grades inputted from the user, forms a linked list out of them, and calculates the grades, forming a % and gpa. The full specs can be found in the attached PDF. Note, I did not include main.c, because all it does is call the run() function.
func.h
/**
* Created on: February 05, 2008
* @author Alexander Chernikov
* @license GNU Public License v3
*/
#ifndef FUNC_H
#define FUNC_H
#include
#include
#include
//Set any G_* to 0 to have user input on max points
#define G_ASSIGN 0
#define G_QUIZ 25
#define G_TEST 100
#define G_EXAM 200
#define P_ASSIGN .40
#define P_QUIZ .10
#define P_EXAM .25
#define CAT 4
//Linked List FTW - the APE did bad things to me
typedef struct node {
double data;
int max;
struct node* next;
} list;
list* grades = NULL; //global linkedlist for storing a dynamic list of grades
void addNode( double, int); //adds a score and a max score
void clearList(); // obligatory command for memory - no memory leak for YUO!
void printlist(); //for debugging...unfortunately, the problem was never here
void getCatMax(int*); //gets number of items per category
void getScores( char*,int, int);
double calculateGPA(double*, double*); //actually calculates percentage...too lazy to change name
double calculateScores();
void print_results(double*, char**, double);
#endif
Func.c
/**
* Created on: February 05, 2008
* Grade Calculator
* Utilizes ALL extra credit available.
* @author Alexander Chernikov
* @license GNU Public License v3
*/
#include "func.h"
/**
* Sort of another main..err...
*/
void run(){
int catMax[CAT] = {7,3,2,1}, catMaxPoints[CAT] = {G_ASSIGN, G_QUIZ, G_TEST, G_EXAM};
double avg[CAT], catWeight[CAT] = {P_ASSIGN, P_QUIZ, P_EXAM, P_EXAM}, gpa;
char* catName[CAT] = {"Assignment","Quiz","Test","Final Exam"};
//list* grades = NULL;
//send off catMax to a function which asks for catMax values
//but just the first 3! we only have 1 final.
getCatMax(catMax);
//two for loops - outer with 4 for each category, inner doing catMax[x] and passing strings
int i, j;
char* s;
printf("\n");//just because
for ( i=0; i < sizeof(catMax)/sizeof(int); i++){
for (j = 0; j < *(catMax + i); j++)
getScores( *(catName+i), *(catMaxPoints+i), j+1);
avg[i] = calculateScores();
//DEBUG
//printlist();
clearList();
printf("\n");//just because
}
gpa = calculateGPA(avg, catWeight);
//print out results here
print_results(avg, catName, gpa);
}
/**
* The end - printing and quitting
* @param avg - Array of average scores from each category
* @param catName - array of category names
* @param perc - calculated overall percentage
*/
void print_results(double* avg, char** catName, double perc){
printf("--------------------------\n");
printf("Percentage per category.\n");
int i;
double gpa=4;
for (i = 0; i < CAT; i++)
printf("\t%-10s: %2.2f%%\n", *(catName+i), *(avg+i)*100);
printf("\nYour weighted percentage is: %%%3.2f\n", perc);
if (perc < 95)
gpa -= (95-perc)/10;
//and then the "exceptions"
if ( (int)perc == 63 )
gpa = .9;
else if ( (int)perc == 62 || (int)perc == 61 )
gpa = .8;
else if ((int)perc == 60)
gpa = .7;
//fail
if (perc < 60)
gpa = 0;
printf("Your grade point is: %1.1f\n",gpa);
}
/**
* Retrieves scores from user input
* @param name - Name of category
* @param maxPoints - Set number of the highest grade (0 for custom)
* @param num - the number of the item (Test #2 etc)
*/
void getScores( char* name, int points, int num){
double val;
if (points == 0){
printf("Please enter the max points possible for %s #%d: ",name, num);
scanf("%d", &points); //assuming the user is in HSL, otherwise add a while loop.
}
printf("Please enter grade for %s #%d: ",name, num);
scanf("%lf", &val);
while (val < 0 || val > points){
printf("Error: value is below 0 or above max points (%d) please re-enter grade: ",points);
scanf("%lf", &val);
}
addNode(val,points);
}
/**
* Calculates all scores in list and gets a percentage.
* @return double - percent average of category
*/
double calculateScores(){
double score=0, max=0;
list* temp = grades;
while (temp != NULL){
score += temp->data;
max += temp->max;
temp = temp->next;
}
return score/max;
}
/**
* Used to calculate GPA, now calculates overall percentage
* @param avg - Array of average scores from each category
* @param catWeight - Array of category percentage weights
* @return double - overall percentage, not GPA...I was lazy to change title
*/
double calculateGPA(double* avg, double* catWeight){
int i;
double score=0;
for (i=0; i < CAT; i++){
score += *(avg+i)**(catWeight+i);
}
return score * 100;
}
/**
* Gets max items in category from user
* This could have been modularized a bit more, but again, lazy.
* Also built so it won't ask for Final exam...since there is only 1.
* @param catMax - array to initialize
*/
void getCatMax(int* catMax){
int val;
printf("# of assignments (0 or less for default): ");
scanf("%d", &val);
if (val > 0)
*catMax = val;
printf("# of quizzes (0 or less for default): ");
scanf("%d", &val);
if (val > 0)
*(catMax+1) = val;
printf("# of tests (0 or less for default): ");
scanf("%d", &val);
if (val > 0)
*(catMax+2) = val;
}
/**
* Adds nodes to list - last item added to the front of list
* @param item - user inputted score
* @param max - max number of points possible, can be user inputted
*/
void addNode( double item, int max){
list *temp = (list *)malloc(sizeof(list));
if (temp == NULL)
return;
temp->next = grades;
grades = temp;
temp->data = item;
temp->max = max;
//return temp;
}
/**
* Removes contents from list.
*/
void clearList(){
list* p;
while (grades != NULL){
p = grades;
grades = grades->next;
free(p);
}
}
/**
* A debugging method - prints list contents
*/
void printlist(){
list* temp = grades;
while (temp != NULL){
printf("%f %d\n", temp->data, temp->max);
temp = temp->next;
}
}
Assignment #1 – Instructions PDF
