topical media & game development
lib-ch-example-sample-grade.cgi / cgi
#!/bin/ch
/* Calculate the Grade
Report in the form of html format.
Created by Harry H. Cheng, 11/7/1995
Last time modified, 10/25/1996
*/
include <cgi.h>
void errorHandler(char *reason) {
printf("Content-type: text/html\n\n");
printf("<TITLE>Grade Report Failed</TITLE>\n");
printf("<body bgcolor=\"#FFFFFF\" text=\"#000000\" vlink=\"#FF0000\">\n");
printf("<H3>Grade Report Failed</H3>\n");
printf("Your SSN has not been submitted to Grade Report,\n");
printf("because \%s. ",reason);
printf("<A HREF=\"/chdoc/util/grade.html\">Try again.</A>\n");
printf("<p>
\n");
printf("<A HREF=\"http://www.softintegration.com\" target=\"_top\">\n");
printf("<img src=\"/chhtml/images/poweredbych.gif\" alt=\"Powered by Ch\"></A>\n");
exit(1);
}
void sendGrade(int ssn) {
/* grading */
/* number of students */
define NUMST 11
/* number of homework */
define NUMHW 9
/* number of project */
define NUMPJ 1
/* number of midterm */
define NUMMID 1
/* number of final */
define FINAL 1
struct student_t {
int ssn;
float hw[NUMHW];
float pj[NUMPJ];
float mid[NUMMID];
float final;
float score;
} s[NUMST+1] = {
// }s[12] = {
{123456789, 10,10,32, 45,65,55,85,50,10,200,100,100}, /* full mark */
{1,10,10,23, 0, 0,44,76, 0,10,140,52,49},
{2,8, 10,23, 37,50,55,84,35,10,200,86,69},
{3,10,10,30.5,44,45,50,83,47,10,170,68,51},
{4,10,10,29, 32,45,37,74,35,10,140,69,64.5},
{5,10, 9,29.5,42,65,55,81,35,10,190,80,86},
{6,10,10,29, 40,35,55,81,35,10,200,91,56.5},
{7,10,10,15, 19,45,55,81,32,10,170,80,78},
{8,10,10,32, 12,50,55,84,40,10,165,70,67.5},
{9,10,10,30, 42,60,42,72,37,10,200,65,64},
{10,10,10,30, 37,35,55,73,35,10,160,74,70},
{11,10,10,27, 24,25,55,81,38,10,165,90,61}
};
float g_hwpj, g_mid, g_final;
float hwpj, mid;
float p_hwpj=40, p_mid=20, p_final=40; /* grade percentage */
int i, j, st;
string_t reason, score;
class CResponse Response;
for(i=0; i<NUMST+1; i++) {
if(ssn==s[i].ssn) {
st = i;
break;
}
}
if(i==NUMST+1) {
sprintf(reason,"invalid S.S.N. --- \%d is submitted\n", ssn);
errorHandler(reason);
}
for(g_hwpj=0,i=0; i<NUMHW; i++)
g_hwpj += s[0].hw[i];
for(i=0; i<NUMPJ; i++)
g_hwpj += s[0].pj[i];
for(g_mid=0,i=0; i<NUMMID; i++)
g_mid += s[0].mid[i];
g_final = 100;
//printf("homework/project total points = %.1f\n", g_hwpj);
//printf("mid total points = %.1f\n", g_mid);
Response.setContentType("text/html");
Response.begin();
printf("<TITLE>Grade Report</TITLE>\n");
printf("<body bgcolor=\"#FFFFFF\" text=\"#000000\" vlink=\"#FF0000\">\n");
printf("<H3>Grade Report</H3>\n");
printf("<PRE>\n");
printf(" \t Grade \n");
printf("----------------------\n");
//for(j=1; j<NUMST+1; j++) {
for(hwpj=0,i=0; i<NUMHW; i++) {
hwpj += s[st].hw[i];
printf("Homework \%d:\t %.1f\n", i+1, s[st].hw[i]);
}
for(i=0; i<NUMPJ; i++) {
hwpj += s[st].pj[i];
printf("Project \%d:\t %.1f\n", i+1, s[st].pj[i]);
}
for(mid=0,i=0; i<NUMMID; i++) {
mid += s[st].mid[i];
printf("Midterm \%d:\t %.1f\n", i+1, s[st].mid[i]);
}
printf("Final Exam:\t %.1f\n", s[st].final);
s[st].score = hwpj*p_hwpj/g_hwpj + mid*p_mid/g_mid +
s[st].final*p_final/g_final;
//printf("hw = %.1f final score = %.1f\n", hwpj*p_hwpj/g_hwpj, s[j].score);
printf("----------------------\n");
if(s[st].score > 95)
score = "Congratulation! Your final grade for my class is 'A+'.\nKeep up good work.\n";
else if (s[st].score > 90)
score = "Congratulation! Your final grade for my class is 'A'. \nKeep up good work.\n";
else if (s[st].score > 80)
score = "Good work! Your final grade for this class is 'A-'.\n";
else if (s[st].score > 70)
score = "Your final grade for this class is 'B+'.\n";
else
score = "Your final grade for this class is 'C'.\n";
printf(score);
printf("</PRE>\n");
printf("<p>
\n");
printf("<A HREF=\"http://www.softintegration.com\" target=\"_top\">\n");
printf("<img src=\"/chhtml/images/poweredbych.gif\" alt=\"Powered by Ch\"></A>\n");
Response.end();
}
int main() {
int i, num, ssn;
chstrarray name; /* name[i] is a string of char with a passed name */
chstrarray value; /* value[i] is a string of char with a passed value */
class CRequest Request;
setvbuf(stdout, NULL, _IONBF, 0);
num = Request.getFormNameValue(name, value);
if(num == 0) {
errorHandler("No name/value has been submitted\n");
exit(0);
}
else if(num == -2) {
errorHandler("No enough memory\n");
}
if(!strcmp("SSN", name[i]) && isnum(value[i]))
ssn = atoi(value[i]);
else
errorHandler(stradd(name[i], " is not a valid integral number"));
sendGrade(ssn);
exit(0);
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.