Code:
/*This program reads in a list of names, verifies their structure,
* sorts them by last name, calculates their letter grade, and
* prints all relevant information in a sorted list*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Assigns the structure of a person
struct person{
char firstName[30];
char lastName[30];
char grade;
float points;
};
// Swap function
void swap(struct person *p1,struct person *p2)
{
char z;
float x;
char temp[30];
char temp1[30];
strcpy(temp, p1->lastName);
strcpy(temp1, p1->firstName);
z = p1 -> grade;
x = p1 -> points;
strcpy(p1 ->lastName,p2->lastName);
strcpy(p1 ->firstName,p2->firstName);
p1 ->grade = p2->grade;
p1 ->points = p2->points;
strcpy(p2 ->lastName,temp);
strcpy(p2 ->firstName,temp);
p2->grade=z;
p2->points=x;
}
// Sort function
void psort(int n, struct person student[])
{
int i;
int j;
for (i=0; i < n; i++)
for (j=i+1; j<n; j++)
if (strcmp(student[i].lastName,student[j].lastName) > 0)
swap(&student[i],&student[j]);
}
// Beginning of main
int main(int args, char *argv[])
{
struct person student[30];
int i,n,gradeCalculate;
int a=0,b=0,c=0,d=0,f=0;
char *ch,*ch1,*ch2;
char ch3[10];
char buf[256];
FILE *fp;
FILE *fp2;
// Makes sure files and loads it to buf
fp = fopen(argv[1],"r");
if(fp == NULL){
printf("Cannot open file\n");
exit(0);
}
i=0;
fgets(buf,256,fp);
// Validates that person has a last name and grade
while(!feof(fp)){
if(buf[0] != '|' && buf[strlen(buf)-2] != '|')
{
// Splits the last name, first name, and grade
ch = strchr(buf,'\n');
if(ch != NULL) *ch = '\0';
else break;
ch = strchr(buf,'|');
ch2 = strrchr(buf,'|');
if(ch != NULL)
{
*ch = '\0';ch1 = ++ch2;
}
else break;
ch++;
strcpy(ch3,ch);
ch2=strchr(ch3,'|');
*ch2='\0';
gradeCalculate = atoi(ch1);
strcpy(student[i].lastName,buf);
strcpy(student[i].firstName,ch3);
student[i].points = atof(ch1);
// Calculates the letter grade
switch(gradeCalculate / 10)
{
case 10:
student[i].grade = 'A';
break;
case 9:
student[i].grade = 'A';
break;
case 8:
student[i].grade = 'B';
break;
case 7:
student[i].grade = 'C';
break;
default:
student[i].grade = 'F';
}
// Increments letter counter
if(gradeCalculate >= 90) a++;
if(gradeCalculate >= 80 && gradeCalculate < 90) b++;
if(gradeCalculate >= 70 && gradeCalculate < 80) c++;
if(gradeCalculate < 70) f++;
fgets(buf,256,fp);
i++; // Increments the student array
}
else
{
fp2 = fopen("REJECTS.txt","a");
fprintf(fp2,"%s",buf);
fclose(fp2);
fgets(buf,256,fp);
}//
} // End of While statement
// Prints the format for the Grade list
n = i;
printf("\n Grade list for CS-XXXX \n");
printf(" ---------------------- \n\n");
printf("Last Name First Name");
printf(" Points Grade \n");
printf("--------- ----------");
printf(" ------ ----- \n");
// Runs a loop to print the array information
for (i = 0; i < n; i++){
// Prints the array of students
printf("%-30s %-30s %4.2f ",student[i].
lastName,student[i].firstName,student[i].points);
printf("%c\n",student[i].grade);
}
// Prints the class summary
printf("\n Grade Summary for CS-XXXX \n");
printf(" ------------------------- \n\n");
printf(" # of A's %d\n",a);
printf(" # of B's %d\n",b);
printf(" # of C's %d\n",c);
printf(" # of D's %d\n",d);
printf(" # of F's %d\n",f);
return 0;
}