The gradebook file contains a sequence of printable characters and it is structured as a table, with the help of newlines and tabs. The table contains exactly 32 columns, and the column names are shown below
- First and Last for first name and last name.
- L1 through L10 for 10 labs.
- B1 through B10 for 10 exercises.
- P1 through P6 for 6 projects.
- E1 through E4 for 4 exams.
The first two columns are always the first name and the last name. The order of the remaining 30 columns is unknown. The first line of the file is the header line and it is a list of column names separated by tabs. Each following line represents a student. The number of students in the file is unknown either. Each student has exactly 32 fields separated by tabs. The first two fields are the first and last names of the student. The first and last names can contain up to 30 letters, and no two students have the same full name. Each student has 30 score fields and their values are either a double ranging from 0 through 100, or na (for not available). An unavailable score is viewed as 0 in one function and is excluded in the other computation functions.
- double getMin(char filename[], char column[]); Given a gradebook file, return the minimum of the specified column,
- double getMax(char filename[], char column[]); Given a gradebook file, return the minimum of the specified column,
- double getAvg(char filename[], char column[]); Given a gradebook file, return the minimum of the specified column,
- int getCount(char filename[], char column[], double threshold); Given a gradebook file, return the number of students with their column value >= threshold, excluding unavailable scores.
- double getGrade(char filename[], char first[], char last[]); Given a gradebook file, return the weighted average of the specified student or -1.0 if there is no such student. An unavailable score is viewed as 0. The weight percentage for each column is specified below.
Colume | Weight percentage | Score range |
L1 through L10 | 1% for each lab | 0-100 |
B1 through B10 | 1% for each exercise | 0-100 |
P1 through P6 | 2% for P1 5% each for P2, P3 6% each for P4, P5, P6 | 0-100 |
E1 through E4 | 10% each for E1, E2, E3 20% for E4 | 0-100 |
What to do
- To compile this program, use gcc -Wall -std=c99 main.c functions.c -lm -o a.out
- Please do not change main.c. You just need to complete functions.c.
- Please test with ./a.out case11.txt. A sample execution of the program is shown below.
Function.c
Main.c
Sample Execution $ ./a.out case11.txt Enter a command: min B1 min (B1)=10.7 Enter a command: min P3 min (P3)=7 Enter a command: max B5 max (B5)=100 Enter a command: max P4 max (P4)-100 Enter a command: avg E4 avg (E4)=87.913 Enter a command: avg L7 avg (L7)=97.7955 Enter a command: count E4 100 count (E4>=100)=14 Enter a command: count E4 60 count (E4>=60) = 44 Enter a command: count L4 100 count (L4>=100)=26 Enter a command: count L4 90 count (L4>=90) =31 Enter a command: grade Louanne Fisch grade (Louanne Fisch)=68.437 Enter a command: grade Stormy Beaufort grade (Stormy Beaufort)=79.463 Enter a command: quit P3 100 93.5 20 70 80 90 100 96 25 100 L4 100 24 62 100 30 100 100 100 100 25 100 72 85 95 93 80 100 First Last P1 L1 Lloyd Gibbon 100 100 Eleonora Schlosser Judith Tignor 3 100 Maida Mood 100 100 Kayla Bruner 100 100 Tajuana Galloway 93 Shantae Carcamo 100 100 Gayle Tash 100 100 Keneth Cuesta 100 100 Evangelina Bozeman 100 Owen Moniz 100 100 Aurelio Alton 100 100 Marcene Rothenberg 100 Sharla Mattocks 100 Rob Pauls 100 100 Calandra Ungar 100 Deloris Schoenborn 100 Brigette Finney 94 Milissa Mobley 100 100 Huong Letson 97 100 Ella Wike 100 100 Buster Kisner 96 100 Stormy Beaufort 100 Graig Reel 100 100 Rosy Hammontree 100 Armand Bladen 97 100 Francisco Masters 100 Aleida Francoeur 100 Nicola Alto 100 100 Dimple Whitson 100 90 Major Witherite 93 Louanne Fisch 97 80 Lan Mazon 97 100 Elias Kasel 100 Kaley Pawlak 90 95 Amalia Neiss 97 100 Shanda Nicley 100 52 Harry Sessions 100 Jeannine Kleinman Stacee Andres 80 100 Sherell Calabrese 100 Karan Bengtson 96 Merlin Shuford 100 100 Bill Fleckenstein 96 Jonathan Kisling 97 Valarie Tewksbury 100 B1 100 97 99.2 100 100 100 100 100 99.7 100 10.7 100 100 90 100 100 100 86 100 100 99.2 100 100 100 100 100 100 90 100 100 92 98.3 100 98.3 100 100 98.6 100 100 100 100 76 100 96 56 100 P2 100 32 100 100 88 100 100 100 100 100 100 100 100 98.6 96 100 99.2 87.9 96 100 100 100 96.1 100 98.9 100 100 100 100 100 99.4 94 100 100 94 94 97 100 na 93 100 95.6 100 100 100 100 L2 95 99.2 95 100 95 100 100 100 95 100 96 80 100 100 100 97 93 91 95 95 100 100 90 100 100 80 100 100 96 100 100 41 95 90 100 95 82 12 100 98 100 100 B2 100 97 100 100 100 90 100 100 100 95 97.9 100 100 100 100 80 100 91 100 99.5 99.5 100 100 100 100 100 95 100 100 98.5 95 97.4 100 L3 86 85 86 90 86 100 100 100 100 100 100 86 100 100 100 100 99 98.5 100 86 100 72 96.4 100 100 93 100 100 100 100 100 100 79 77 97 86 100 100 100 17 100 97.9 58 100 100 100 B3 100 95.9 100 100 100 86 100 100 100 93 100 100 86 100 100 100 86 54 100 100 100 100 na 100 86 100 100 86 100 100 93 100 100 100 100 98.1 100 97 99.5 100 86 100 100 76 100 86 E1 100 54 95 100 100 100 100 92 88 100 92 90 100 100 100 100 96.3 100 100 85 96 92 96.3 100 100 91 100 100 92 100 100 50 91 84 100 95 79 100 100 70 100 100 100 94.4 100 100 100 100 100 100 100 70 95 81 40 100 100 85 70 100 85 na B4 100 82 100 100 100 100 100 100 100 100 100 100 58 100 100 100 100 96 100 100 100 100 na 100 100 100 37 56 100 100 98 100 100 100 100 99.3 100 100 10 91.5 86 100 100 100 100 40 L5 100 90 100 100 100 100 100 100 100 100 100 70 100 100 100 100 85.6 100 100 15 100 100 0 100 100 100 100 100 100 100 100 85 100 100 90 60 100 100 17 90 100 100 100 100 100 99.3 L6 95 93.5 86 79 100 100 86 100 96 100 100 86 100 70 100 100 100 65 86 79 100 72 100 86 80 72 60 100 100 100 100 50 86 86 86 93 86 100 100 93 70 70 100 100 100 80 75 100 21 58 10 10 100 100 85 70 100 100 72 100 70 85 100 25 65 7 96 85 100 92 100 22 95 102 100 99 100 100 100 97.4 100 95 96 98.5 100 75 100 100 100 90 100 100 95 100 100 100 96 100 100 na 100 72 100 85 89 77 65 25 100 100 10 96 E3 94 54 90 100 94 B10 100 100 100 100 100 100 100 94 83 L7 100 94.9 100 100 100 81 100 90 100 100 100 100 44 94 100 88 91 71 E2 100 87.3 78 91 79 100 100 60 76 100 100 100 100 100 100 100 0 100 100 82 100 100 0 85 100 100 100 P4 100 90 100 100 75 100 100 100 85 65 90 100 100 100 90 100 72 79 100 0 95 95 86 100 79 70 79 86 95 100 72 100 100 0 95 100 95 79 100 na 55 69 B5 100 100 100 100 100 96 100 100 100 100 100 100 0 96 100 100 na 100 100 98.8 100 100 100 100 90 100 100 22 100 100 25 100 100 97.6 100 95.2 100 100 100 93.9 100 100 100 83 69 100 95 94 97 88 89 88 91 100 89 58 77 100 100 100 100 92 87 100 B6 100 97 100 100 100 100 100 100 100 100 93.2 100 100 100 100 100 95.2 97.6 100 100 100 100 91.5 100 100 100 100 100 100 98.3 100 100 100 100 100 93.2 100 100 100 78 100 100 100 100 98.2 100 64 L9 70 100 100 90 100 100 100 100 100 100 100 100 100 100 100 100 90 100 100 90 100 100 100 100 B7 100 92 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 73 100 100 na 100 100 100 100 100 100 100 64 100 100 100 100 70 100 100 P5 100 100 96 100 3 100 100 100 96 100 97 52 100 100 100 100 100 100 100 60 76 91 100 100 100 58 100 100 98 100 100 95 97 94 60 40 100 100 100 35 100 100 100 100 100 100 L8 100 97.6 100 100 100 65 100 100 100 100 100 100 10 100 100 100 100 100 100 100 100 100 100 100 97 100 93 7 100 100 100 100 100 100 100 100 100 96 100 100 100 100 100 76 100 90 P6 89 100 95 92 74 90 100 95 34 100 96 72 100 100 100 100 95 40 93 53 100 100 100 92 100 55 100 100 89 92 100 58 49 28 100 69 73 100 100 0 100 100 100 100 100 100 100 100 100 100 82 100 100 100 70 63 100 70 70 100 100 na B8 100 5 100 100 100 73 100 100 100 100 100 100 70 100 100 100 81 82 100 100 100 100 94 100 100 100 63 94 100 100 95 100 100 100 100 100 100 94 46 100 100 58 100 80 94 79 E4 100 100 97 100 81 100 96 96 100 100 100 100 100 100 100 100 0 100 92 97 100 81 100 100 100 85 100 100 100 100 100 51 L10 100 86 100 100 100 100 100 100 na 100 100 100 100 100 100 100 100 100 100 100 100 50 100 100 100 100 100 100 100 10 100 100 80 100 100 100 95 100 99 100 100 100 100 100 100 100 B9 100 97.6 100 100 100 50 100 100 100 100 100 100 100 60 100 100 100 na 100 100 100 100 100 100 100 100 100 80 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 50 96 100 90 100 100 46 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 98 100 92 100 100 70 100 94 na 100 100 100 100 100 90 87 71 100 96 95 83 100 100 100 38 100 39 99 76 95 100 95.2 76 100 100 100 100 100 100 100 0 74 0 100 100 91 82 94 50 99 36 94 100 100 32 96 59 100 92 61 78 na 100 100 100 74 100 100 82 76 100 82 64 64 92 100 95 90 100 100 100 79 95 100 100 100 100 100 100 61 96 62 67 54 100 83 100 100 96 100 84 100 100 67 86 93 96 86 65 100 100 100 100 100 100 100 na 100 85 93 83 88 #include
#include #include #include #include // Given a gradebook file, return the minimum of the specified column, excluding unavailable scores double getMin(char filename[], char column[]) { return 1.1; } // Given a gradebook file, return the maximum of the specified column, excluding unavailable scores double getMax(char filename[], char column[]) { return 2.2; } // Given a gradebook file, return the average of the specified column, excluding unavailable scores double getAvg(char filename[], char column[]) { return 3.3; } // Given a gradebook file, return the number of students with their column value >= threshold, excluding unavailable scores int getCount(char filename[], char column[], double threshold) { return 4; } // Given a gradebook file, return the weighted average of the specified student // or -1.0 if there is no such student. // An unavailable score is viewed as O double getGrade(char filename[], char first[], char last[]) { return -1.0; // not exist } #include #include #include #include #include // Given a gradebook file, return the minimum of the specified column, excluding unavailable scores double getMin(char filename[], char column[]); // Given a gradebook file, return the maximum of the specified column, excluding unavailable scores double getMax(char filename[], char column[]); // Given a gradebook file, return the average of the specified column, excluding unavailable scores double getAvg(char filename[], char column[]); // Given a gradebook file, return the number of students with their column value >= threshold, excluding unavailable scores int getCount(char filename[], char column[], double threshold); // Given a gradebook file, return the weighted average of the specified student // or -1.0 if there is no such student. // An unavailable score is viewed as 0 double getGrade(char filename[], char first[], char last[]); int isValid Column(char column()) { char type; int num; sscanf(column, "%c%d", &type, &num); switch (type) { case 'L': if (num10) return 0; break; case 'E': if (num4) return 0; break; case 'B': if (num10) return 0; break; case 'P': if (num6) return 0; break; default: return 0; } char column2[strlen(column)+1]; sprintf(column2, "%c%d", type, num); if (strcmp(column, column2)!=0) return 0; return 1; } void printHelp { printf(" The valid commands: "); printf("\tmin column "); printf("\t*** find the minimum of the specified column, excluding unavailable scores "); printf("\t*** for example: min P4 "); printf("\tmax column "); printf("\t*** find the maximum of the specified column, excluding unavailable scores "); printf("\t*** for example: max E1 "); printf("\tavg column "); printf("\t*** find the average of the specified column, excluding unavailable scores "); printf("\t*** for example: avg B10 "); printf("\tcount column threshold "); printf("\t*** find the number of rows with its column value >= threshold, excluding unavailable scores "); printf("\t*** for example: count L2 60 "); printf("\tgrade firstname lastname "); printf("\t*** find the weighted average of the specified student. An unavailable score is viewed as 0 "); printf("\t*** for example: grade John Smith "); printf("\tquit "); printf("\t*** quit this program "); printf("\thelpin"); printf("\t*** print this list "); } int main(int argc, char *argv[]) { if (argc!=2) { printf("Usage: %s filename ", argv[0]); return 1; } FILE *fp=fopen(argv[1], "r"); if (fp==NULL) { printf("Unable to open %s for reading ", argv[1]); return 2; } fclose(fp); while (1) { char cmd[30]; char column[30]; char line[300]; printf(" Enter a command: "); scanf("%s", cmd); if (strcmp(cmd, "quit")==0) break; if (strcmp(cmd, "grade")==0) { char first[30], last[30]; scanf("%s%s", first, last); double grade=getGrade(argv[1], first, last); if (grade>=0) printf("grade(%s %s)=%g ", first, last, grade); else printf("No student named %s %s ", first, last); } else if (strcmp(cmd, "min")==0) { scanf("%s", column); if (isValid Column(column)) { double min=getMin(argv[1], column); printf("min(%s)=%g ",column, min); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd, "max")==0) { scanf("%s", column); if (isValidColumn(column)) { double max=getMax(argv[1], column); printf("max(%s)=%g ", column, max); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd, "avg")==0) { scanf("%s", column); if (isValidColumn(column)) { double avg=getAvg(argv[1], column); printf("avg(%s)=%g ", column, avg); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd,"count")==0) { scanf("%s", column); if (isValidColumn(column)) { double threshold; scanf("%lf", &threshold); int count=getCount(argv[1], column, threshold); printf("count%s>=%g)=%d ", column, threshold, count); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd, "help")==0) { printHelp(); } else { printf("%s: invalid commmand. Type help for help. ", cmd); fgets(line, 300, stdin); // skip the rest of line } } return 0; } Sample Execution $ ./a.out case11.txt Enter a command: min B1 min (B1)=10.7 Enter a command: min P3 min (P3)=7 Enter a command: max B5 max (B5)=100 Enter a command: max P4 max (P4)-100 Enter a command: avg E4 avg (E4)=87.913 Enter a command: avg L7 avg (L7)=97.7955 Enter a command: count E4 100 count (E4>=100)=14 Enter a command: count E4 60 count (E4>=60) = 44 Enter a command: count L4 100 count (L4>=100)=26 Enter a command: count L4 90 count (L4>=90) =31 Enter a command: grade Louanne Fisch grade (Louanne Fisch)=68.437 Enter a command: grade Stormy Beaufort grade (Stormy Beaufort)=79.463 Enter a command: quit P3 100 93.5 20 70 80 90 100 96 25 100 L4 100 24 62 100 30 100 100 100 100 25 100 72 85 95 93 80 100 First Last P1 L1 Lloyd Gibbon 100 100 Eleonora Schlosser Judith Tignor 3 100 Maida Mood 100 100 Kayla Bruner 100 100 Tajuana Galloway 93 Shantae Carcamo 100 100 Gayle Tash 100 100 Keneth Cuesta 100 100 Evangelina Bozeman 100 Owen Moniz 100 100 Aurelio Alton 100 100 Marcene Rothenberg 100 Sharla Mattocks 100 Rob Pauls 100 100 Calandra Ungar 100 Deloris Schoenborn 100 Brigette Finney 94 Milissa Mobley 100 100 Huong Letson 97 100 Ella Wike 100 100 Buster Kisner 96 100 Stormy Beaufort 100 Graig Reel 100 100 Rosy Hammontree 100 Armand Bladen 97 100 Francisco Masters 100 Aleida Francoeur 100 Nicola Alto 100 100 Dimple Whitson 100 90 Major Witherite 93 Louanne Fisch 97 80 Lan Mazon 97 100 Elias Kasel 100 Kaley Pawlak 90 95 Amalia Neiss 97 100 Shanda Nicley 100 52 Harry Sessions 100 Jeannine Kleinman Stacee Andres 80 100 Sherell Calabrese 100 Karan Bengtson 96 Merlin Shuford 100 100 Bill Fleckenstein 96 Jonathan Kisling 97 Valarie Tewksbury 100 B1 100 97 99.2 100 100 100 100 100 99.7 100 10.7 100 100 90 100 100 100 86 100 100 99.2 100 100 100 100 100 100 90 100 100 92 98.3 100 98.3 100 100 98.6 100 100 100 100 76 100 96 56 100 P2 100 32 100 100 88 100 100 100 100 100 100 100 100 98.6 96 100 99.2 87.9 96 100 100 100 96.1 100 98.9 100 100 100 100 100 99.4 94 100 100 94 94 97 100 na 93 100 95.6 100 100 100 100 L2 95 99.2 95 100 95 100 100 100 95 100 96 80 100 100 100 97 93 91 95 95 100 100 90 100 100 80 100 100 96 100 100 41 95 90 100 95 82 12 100 98 100 100 B2 100 97 100 100 100 90 100 100 100 95 97.9 100 100 100 100 80 100 91 100 99.5 99.5 100 100 100 100 100 95 100 100 98.5 95 97.4 100 L3 86 85 86 90 86 100 100 100 100 100 100 86 100 100 100 100 99 98.5 100 86 100 72 96.4 100 100 93 100 100 100 100 100 100 79 77 97 86 100 100 100 17 100 97.9 58 100 100 100 B3 100 95.9 100 100 100 86 100 100 100 93 100 100 86 100 100 100 86 54 100 100 100 100 na 100 86 100 100 86 100 100 93 100 100 100 100 98.1 100 97 99.5 100 86 100 100 76 100 86 E1 100 54 95 100 100 100 100 92 88 100 92 90 100 100 100 100 96.3 100 100 85 96 92 96.3 100 100 91 100 100 92 100 100 50 91 84 100 95 79 100 100 70 100 100 100 94.4 100 100 100 100 100 100 100 70 95 81 40 100 100 85 70 100 85 na B4 100 82 100 100 100 100 100 100 100 100 100 100 58 100 100 100 100 96 100 100 100 100 na 100 100 100 37 56 100 100 98 100 100 100 100 99.3 100 100 10 91.5 86 100 100 100 100 40 L5 100 90 100 100 100 100 100 100 100 100 100 70 100 100 100 100 85.6 100 100 15 100 100 0 100 100 100 100 100 100 100 100 85 100 100 90 60 100 100 17 90 100 100 100 100 100 99.3 L6 95 93.5 86 79 100 100 86 100 96 100 100 86 100 70 100 100 100 65 86 79 100 72 100 86 80 72 60 100 100 100 100 50 86 86 86 93 86 100 100 93 70 70 100 100 100 80 75 100 21 58 10 10 100 100 85 70 100 100 72 100 70 85 100 25 65 7 96 85 100 92 100 22 95 102 100 99 100 100 100 97.4 100 95 96 98.5 100 75 100 100 100 90 100 100 95 100 100 100 96 100 100 na 100 72 100 85 89 77 65 25 100 100 10 96 E3 94 54 90 100 94 B10 100 100 100 100 100 100 100 94 83 L7 100 94.9 100 100 100 81 100 90 100 100 100 100 44 94 100 88 91 71 E2 100 87.3 78 91 79 100 100 60 76 100 100 100 100 100 100 100 0 100 100 82 100 100 0 85 100 100 100 P4 100 90 100 100 75 100 100 100 85 65 90 100 100 100 90 100 72 79 100 0 95 95 86 100 79 70 79 86 95 100 72 100 100 0 95 100 95 79 100 na 55 69 B5 100 100 100 100 100 96 100 100 100 100 100 100 0 96 100 100 na 100 100 98.8 100 100 100 100 90 100 100 22 100 100 25 100 100 97.6 100 95.2 100 100 100 93.9 100 100 100 83 69 100 95 94 97 88 89 88 91 100 89 58 77 100 100 100 100 92 87 100 B6 100 97 100 100 100 100 100 100 100 100 93.2 100 100 100 100 100 95.2 97.6 100 100 100 100 91.5 100 100 100 100 100 100 98.3 100 100 100 100 100 93.2 100 100 100 78 100 100 100 100 98.2 100 64 L9 70 100 100 90 100 100 100 100 100 100 100 100 100 100 100 100 90 100 100 90 100 100 100 100 B7 100 92 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 73 100 100 na 100 100 100 100 100 100 100 64 100 100 100 100 70 100 100 P5 100 100 96 100 3 100 100 100 96 100 97 52 100 100 100 100 100 100 100 60 76 91 100 100 100 58 100 100 98 100 100 95 97 94 60 40 100 100 100 35 100 100 100 100 100 100 L8 100 97.6 100 100 100 65 100 100 100 100 100 100 10 100 100 100 100 100 100 100 100 100 100 100 97 100 93 7 100 100 100 100 100 100 100 100 100 96 100 100 100 100 100 76 100 90 P6 89 100 95 92 74 90 100 95 34 100 96 72 100 100 100 100 95 40 93 53 100 100 100 92 100 55 100 100 89 92 100 58 49 28 100 69 73 100 100 0 100 100 100 100 100 100 100 100 100 100 82 100 100 100 70 63 100 70 70 100 100 na B8 100 5 100 100 100 73 100 100 100 100 100 100 70 100 100 100 81 82 100 100 100 100 94 100 100 100 63 94 100 100 95 100 100 100 100 100 100 94 46 100 100 58 100 80 94 79 E4 100 100 97 100 81 100 96 96 100 100 100 100 100 100 100 100 0 100 92 97 100 81 100 100 100 85 100 100 100 100 100 51 L10 100 86 100 100 100 100 100 100 na 100 100 100 100 100 100 100 100 100 100 100 100 50 100 100 100 100 100 100 100 10 100 100 80 100 100 100 95 100 99 100 100 100 100 100 100 100 B9 100 97.6 100 100 100 50 100 100 100 100 100 100 100 60 100 100 100 na 100 100 100 100 100 100 100 100 100 80 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 50 96 100 90 100 100 46 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 98 100 92 100 100 70 100 94 na 100 100 100 100 100 90 87 71 100 96 95 83 100 100 100 38 100 39 99 76 95 100 95.2 76 100 100 100 100 100 100 100 0 74 0 100 100 91 82 94 50 99 36 94 100 100 32 96 59 100 92 61 78 na 100 100 100 74 100 100 82 76 100 82 64 64 92 100 95 90 100 100 100 79 95 100 100 100 100 100 100 61 96 62 67 54 100 83 100 100 96 100 84 100 100 67 86 93 96 86 65 100 100 100 100 100 100 100 na 100 85 93 83 88 #include #include #include #include #include // Given a gradebook file, return the minimum of the specified column, excluding unavailable scores double getMin(char filename[], char column[]) { return 1.1; } // Given a gradebook file, return the maximum of the specified column, excluding unavailable scores double getMax(char filename[], char column[]) { return 2.2; } // Given a gradebook file, return the average of the specified column, excluding unavailable scores double getAvg(char filename[], char column[]) { return 3.3; } // Given a gradebook file, return the number of students with their column value >= threshold, excluding unavailable scores int getCount(char filename[], char column[], double threshold) { return 4; } // Given a gradebook file, return the weighted average of the specified student // or -1.0 if there is no such student. // An unavailable score is viewed as O double getGrade(char filename[], char first[], char last[]) { return -1.0; // not exist } #include #include #include #include #include // Given a gradebook file, return the minimum of the specified column, excluding unavailable scores double getMin(char filename[], char column[]); // Given a gradebook file, return the maximum of the specified column, excluding unavailable scores double getMax(char filename[], char column[]); // Given a gradebook file, return the average of the specified column, excluding unavailable scores double getAvg(char filename[], char column[]); // Given a gradebook file, return the number of students with their column value >= threshold, excluding unavailable scores int getCount(char filename[], char column[], double threshold); // Given a gradebook file, return the weighted average of the specified student // or -1.0 if there is no such student. // An unavailable score is viewed as 0 double getGrade(char filename[], char first[], char last[]); int isValid Column(char column()) { char type; int num; sscanf(column, "%c%d", &type, &num); switch (type) { case 'L': if (num10) return 0; break; case 'E': if (num4) return 0; break; case 'B': if (num10) return 0; break; case 'P': if (num6) return 0; break; default: return 0; } char column2[strlen(column)+1]; sprintf(column2, "%c%d", type, num); if (strcmp(column, column2)!=0) return 0; return 1; } void printHelp { printf(" The valid commands: "); printf("\tmin column "); printf("\t*** find the minimum of the specified column, excluding unavailable scores "); printf("\t*** for example: min P4 "); printf("\tmax column "); printf("\t*** find the maximum of the specified column, excluding unavailable scores "); printf("\t*** for example: max E1 "); printf("\tavg column "); printf("\t*** find the average of the specified column, excluding unavailable scores "); printf("\t*** for example: avg B10 "); printf("\tcount column threshold "); printf("\t*** find the number of rows with its column value >= threshold, excluding unavailable scores "); printf("\t*** for example: count L2 60 "); printf("\tgrade firstname lastname "); printf("\t*** find the weighted average of the specified student. An unavailable score is viewed as 0 "); printf("\t*** for example: grade John Smith "); printf("\tquit "); printf("\t*** quit this program "); printf("\thelpin"); printf("\t*** print this list "); } int main(int argc, char *argv[]) { if (argc!=2) { printf("Usage: %s filename ", argv[0]); return 1; } FILE *fp=fopen(argv[1], "r"); if (fp==NULL) { printf("Unable to open %s for reading ", argv[1]); return 2; } fclose(fp); while (1) { char cmd[30]; char column[30]; char line[300]; printf(" Enter a command: "); scanf("%s", cmd); if (strcmp(cmd, "quit")==0) break; if (strcmp(cmd, "grade")==0) { char first[30], last[30]; scanf("%s%s", first, last); double grade=getGrade(argv[1], first, last); if (grade>=0) printf("grade(%s %s)=%g ", first, last, grade); else printf("No student named %s %s ", first, last); } else if (strcmp(cmd, "min")==0) { scanf("%s", column); if (isValid Column(column)) { double min=getMin(argv[1], column); printf("min(%s)=%g ",column, min); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd, "max")==0) { scanf("%s", column); if (isValidColumn(column)) { double max=getMax(argv[1], column); printf("max(%s)=%g ", column, max); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd, "avg")==0) { scanf("%s", column); if (isValidColumn(column)) { double avg=getAvg(argv[1], column); printf("avg(%s)=%g ", column, avg); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd,"count")==0) { scanf("%s", column); if (isValidColumn(column)) { double threshold; scanf("%lf", &threshold); int count=getCount(argv[1], column, threshold); printf("count%s>=%g)=%d ", column, threshold, count); } else { printf("%s: invalid column name. ", column); fgets(line, 300, stdin); // skip the rest of line } } else if (strcmp(cmd, "help")==0) { printHelp(); } else { printf("%s: invalid commmand. Type help for help. ", cmd); fgets(line, 300, stdin); // skip the rest of line } } return 0; }