Skip to content

Commit a0a269e

Browse files
author
Tyler Boyd
committed
I really hope this is windows compatible.......
1 parent 3ed3e88 commit a0a269e

File tree

8 files changed

+231
-45
lines changed

8 files changed

+231
-45
lines changed

compile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
g++ source/* -o mark
1+
g++ source/* -o mark -w

data/output.csv

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
First name;Last name;O.A1;Summative #1;Exam #1;;;Term mark (1 terms);Final mark
2+
last;first name;90;95;-1000000;;;90;73
3+
last;first name;75;100;-1000000;;;75;63
4+
last;first name;65;95;-1000000;;;65;55
5+
last;first name;90;95;-1000000;;;90;73
6+
last;first name;95;95;-1000000;;;95;76
7+
last;first name;90;65;-1000000;;;90;70
8+
last;first name;90;100;-1000000;;;90;73
9+
last;first name;90;95;-1000000;;;90;73
10+
last;first name;90;100;-1000000;;;90;73
11+
last;first name;85;100;-1000000;;;85;70
12+
last;first name;85;95;-1000000;;;85;69
13+
last;first name;90;72;-1000000;;;90;70
14+
last;first name;90;95;-1000000;;;90;73
15+
last;first name;35;90;-1000000;;;35;34
16+
last;first name;100;100;-1000000;;;100;80
17+
last;first name;78;95;-1000000;;;78;64
18+
last;first name;65;95;-1000000;;;65;55
19+
last;first name;95;95;-1000000;;;95;76
20+
last;first name;100;95;-1000000;;;100;80
21+
last;first name;100;95;-1000000;;;100;80
22+
last;first name;95;45;-1000000;;;95;71
23+
last;first name;95;95;-1000000;;;95;76
24+
last;first name;95;100;-1000000;;;95;77

include/resource.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
extern int opterr, optind, optopt, optreset;
4+
extern char *optarg;
5+
int getopt(int nargc, char * const nargv[], const char *ostr);

mark

8.59 KB
Binary file not shown.

output.csv

-25
This file was deleted.

source/getopt.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "../include/resource.h"
2+
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
int opterr = 1, /* if error message should be printed */
7+
optind = 1, /* index into parent argv vector */
8+
optopt, /* character checked for validity */
9+
optreset; /* reset getopt */
10+
char *optarg; /* argument associated with option */
11+
12+
#define BADCH (int)'?'
13+
#define BADARG (int)':'
14+
#define EMSG ""
15+
16+
/*
17+
* getopt --
18+
* Parse argc/argv argument vector.
19+
*/
20+
int getopt(int nargc, char * const nargv[], const char *ostr)
21+
{
22+
static char *place = EMSG; /* option letter processing */
23+
const char *oli; /* option letter list index */
24+
25+
if (optreset || !*place) { /* update scanning pointer */
26+
optreset = 0;
27+
if (optind >= nargc || *(place = nargv[optind]) != '-') {
28+
place = EMSG;
29+
return (-1);
30+
}
31+
if (place[1] && *++place == '-') { /* found "--" */
32+
++optind;
33+
place = EMSG;
34+
return (-1);
35+
}
36+
} /* option letter okay? */
37+
if ((optopt = (int)*place++) == (int)':' ||
38+
!(oli = strchr(ostr, optopt))) {
39+
/*
40+
* if the user didn't specify '-' as an option,
41+
* assume it means -1.
42+
*/
43+
if (optopt == (int)'-')
44+
return (-1);
45+
if (!*place)
46+
++optind;
47+
if (opterr && *ostr != ':')
48+
(void)printf("illegal option -- %c\n", optopt);
49+
return (BADCH);
50+
}
51+
if (*++oli != ':') { /* don't need argument */
52+
optarg = NULL;
53+
if (!*place)
54+
++optind;
55+
}
56+
else { /* need an argument */
57+
if (*place) /* no white space */
58+
optarg = place;
59+
else if (nargc <= ++optind) { /* no arg */
60+
place = EMSG;
61+
if (*ostr == ':')
62+
return (BADARG);
63+
if (opterr)
64+
(void)printf("option requires an argument -- %c\n", optopt);
65+
return (BADCH);
66+
}
67+
else /* white space */
68+
optarg = nargv[optind];
69+
place = EMSG;
70+
++optind;
71+
}
72+
return (optopt); /* dump back option letter */
73+
}

source/main.cpp

+123-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
11
#include "../include/harrizcsv.h"
22
#include "../include/mark.h"
33
#include "../include/student.h"
4+
#include "../include/resource.h"
45

56
using namespace std;
67

7-
int main() {
8+
int main(int argc, char **argv)
9+
{
10+
char *lpFn = new char[1024];
11+
char *only = new char[256];
12+
char *exclude = new char[256];
13+
char *outputFn = new char[1024];
14+
15+
strcpy(lpFn, "data/SCHS21-1.csv");
16+
strcpy(outputFn, "data/output.csv");
17+
18+
int n_terms = 0;
19+
char c;
20+
while((c = getopt(argc, argv, ":f:n:e:i")) != -1)
21+
{
22+
switch(c)
23+
{
24+
case 'f':
25+
strncpy(lpFn, optarg, 256);
26+
break;
27+
case 'n':
28+
sscanf(optarg, "%d", &n_terms);
29+
break;
30+
case 'e':
31+
strncpy(exclude, optarg, 256);
32+
break;
33+
case 'i':
34+
strncpy(only, optarg, 256);
35+
break;
36+
case '?':
37+
if(optopt == 'f' || optopt == 'n' || optopt == 'e' || optopt == 'i')
38+
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
39+
else
40+
fprintf(stderr, "Unknown option %c.\n", optopt);
41+
return 1;
42+
default:
43+
break;
44+
}
45+
}
46+
47+
printf("file path: \"%s\"\n", lpFn);
48+
printf("only: \"%s\"\n", only);
49+
printf("exclude: \"%s\"\n", exclude);
50+
printf("n_terms: %d\n", n_terms);
51+
852
HarrizCSV* csv = new HarrizCSV();
9-
csv->read("data/SCHS21-1.csv");
53+
csv->read(lpFn);
1054
vector<Student*> students;
1155

1256
vector<vector<char*>*>* prows = csv->rows();
@@ -39,17 +83,30 @@ int main() {
3983
new_student->Summatives.push_back(Mark(columns->at(col)));
4084
}
4185
}
42-
students.push_back(new_student);
86+
char* full_name = new char[1024];
87+
strcpy(full_name, new_student->Type);
88+
strcat(full_name, new_student->a);
89+
90+
bool add = true;
91+
if(strlen(only))
92+
{
93+
add = strstr(full_name, only) == 0;
94+
}
95+
if(strlen(exclude))
96+
{
97+
if(strstr(full_name, exclude) == 0)
98+
add = false;
99+
}
100+
if(add)
101+
students.push_back(new_student);
43102
}
44103

45104
for(int i=0; i<students.size(); i++)
46105
{
47106
Student* student = students.at(i);
48-
// float sum = 0.0f;
49-
cout << "Student # " << i << endl;
107+
cout << "Student name: " << student->Type << " " << student->a << endl;
50108
for(int ioa = 0; ioa < student->OAs.size(); ioa++)
51109
{
52-
// sum += student->OAs.at(ioa).to_int();
53110
cout << "O.A: " << student->OAs.at(ioa).to_int() << endl;
54111
}
55112
for(int ie = 0; ie < student->Exams.size(); ie++)
@@ -64,26 +121,73 @@ int main() {
64121
{
65122
cout << "Summative: " << student->Summatives.at(is).to_int() << endl;
66123
}
67-
// float average = sum / float(student->OAs.size());
68-
cout << "First 3 terms mark: " << student->term_mark(3).to_int() << endl << endl << endl;
124+
if(n_terms > 0) {
125+
cout << "Term mark: " << student->term_mark(n_terms).to_int() << endl;
126+
cout << "Final mark: " << student->final_mark(n_terms).to_int() << endl << endl << endl;
127+
}
128+
else {
129+
cout << "Term mark: " << student->term_mark().to_int() << endl;
130+
cout << "Final mark: " << student->final_mark().to_int() << endl << endl << endl;
131+
}
69132
}
70133

71-
ofstream outfile("output.csv");
134+
ofstream outfile(outputFn);
72135

73-
outfile << endl << endl;
136+
int n_summatives = students.at(0)->Summatives.size();
137+
int n_exams = students.at(0)->Exams.size();
138+
if(n_terms > 0) {
139+
outfile << "First name" << ";" << "Last name" << ";";
140+
for(int i=0; i<n_terms; i++)
141+
outfile << "O.A" << i+1 << ";";
142+
for(int i=0; i<n_summatives; i++)
143+
outfile << "Summative #" << i+1 << ";";
144+
for(int i=0; i<n_exams; i++)
145+
outfile << "Exam #" << i+1 << ";";
74146

75-
for(int i=0; i<students.size(); i++)
147+
outfile << ";;Term mark (" << n_terms << " terms);Final mark" << endl;
148+
149+
for(int i=0; i<students.size(); i++)
150+
{
151+
Student* student = students.at(i);
152+
outfile << student->Type << ";" << student->a << ";";
153+
for(int iterm = 0; iterm < n_terms && iterm < student->OAs.size(); iterm++)
154+
outfile << student->OAs.at(iterm).to_int() << ";";
155+
for(int isum = 0; isum < n_summatives; isum++)
156+
outfile << student->Summatives.at(isum).to_int() << ";";
157+
for(int iex = 0; iex < n_exams; iex++)
158+
outfile << student->Exams.at(iex).to_int() << ";";
159+
outfile << ";;" << student->term_mark(n_terms).to_int() << ";";
160+
outfile << student->final_mark(n_terms).to_int();
161+
outfile << endl;
162+
}
163+
}
164+
else
76165
{
77-
Student* student = students.at(i);
78-
float sum = 0.0f;
79-
outfile << student->Type << ";" << student->a;
80-
for(int ioa = 0; ioa < student->OAs.size(); ioa++)
166+
n_terms = students.at(0)->OAs.size();
167+
outfile << "First name" << ";" << "Last name" << ";";
168+
for(int i=0; i<n_terms; i++)
169+
outfile << "O.A" << i+1 << ";";
170+
for(int i=0; i<n_summatives; i++)
171+
outfile << "Summative #" << i+1 << ";";
172+
for(int i=0; i<n_exams; i++)
173+
outfile << "Exam #" << i+1 << ";";
174+
175+
outfile << ";;Term mark (all terms);Final mark" << endl;
176+
177+
for(int i=0; i<students.size(); i++)
81178
{
82-
sum += student->OAs.at(ioa).to_int();
83-
outfile << ";" << student->OAs.at(ioa).to_string();
179+
Student* student = students.at(i);
180+
outfile << student->Type << ";" << student->a << ";";
181+
for(int iterm = 0; iterm < student->OAs.size(); iterm++)
182+
outfile << student->OAs.at(iterm).to_int() << ";";
183+
for(int isum = 0; isum < n_summatives; isum++)
184+
outfile << student->Summatives.at(isum).to_int() << ";";
185+
for(int iex = 0; iex < n_exams; iex++)
186+
outfile << student->Exams.at(iex).to_int() << ";";
187+
outfile << ";;" << student->term_mark(n_terms).to_int() << ";";
188+
outfile << student->final_mark(n_terms).to_int();
189+
outfile << endl;
84190
}
85-
float average = sum / float(student->OAs.size());
86-
outfile << ";" << average << endl;
87191
}
88192

89193
outfile.close();

source/student.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ Mark Student::final_mark()
1212

1313
Mark Student::final_mark(int units)
1414
{
15+
float term_mark = this->term_mark(units).to_int();
16+
float exam_mark = this->exam_mark().to_int();
17+
float summative_mark = this->summative_mark().to_int();
1518

19+
float final = term_mark * 0.7f + exam_mark * 0.2f + summative_mark * 0.1f;
20+
return Mark(int(final + 0.5f));
1621
}
1722

1823
Mark Student::term_mark()

0 commit comments

Comments
 (0)