Annotation of capa/capa51/Historic/bubblersurveyexam.c, revision 1.1

1.1     ! albertel    1: #include <stdio.h>
        !             2: #include "Capa/capaCommon.h"
        !             3: #include "bubbler.h"
        !             4: 
        !             5: 
        !             6: #include <ctype.h>
        !             7: 
        !             8: #ifdef __sun
        !             9: #include <unistd.h>  /* lockf() */
        !            10: #endif
        !            11: 
        !            12: #include "capaParser.h"
        !            13: #include "capaToken.h"
        !            14: #include "ranlib.h"
        !            15: /*********************************************/
        !            16: /*  flock() in SUN is in BSD compatibility lib */
        !            17: /*  #include <sys/file.h> */
        !            18: 
        !            19: #ifdef   F_DBUG
        !            20: extern FILE *dfp; 
        !            21: #endif
        !            22: 
        !            23: 
        !            24: /********************************************************** file locking */
        !            25: int
        !            26: flockstream_sh(sp) FILE *sp;
        !            27: {
        !            28:   int fd;
        !            29:   
        !            30:   fd = fileno(sp);
        !            31:   
        !            32: #ifdef __sun
        !            33:   return ( lockf(fd,F_LOCK, 0L) );
        !            34: #else
        !            35:   return (flock(fd,LOCK_SH));
        !            36: #endif
        !            37: }
        !            38: 
        !            39: int
        !            40: flockstream(sp) FILE *sp;
        !            41: {
        !            42:   int fd;
        !            43:   
        !            44:   fd = fileno(sp);
        !            45:   
        !            46: #ifdef __sun
        !            47:   return ( lockf(fd,F_LOCK, 0L) );
        !            48: #else
        !            49:   return (flock(fd,LOCK_EX));
        !            50: #endif
        !            51: }
        !            52: int
        !            53: funlockstream(sp) FILE *sp;
        !            54: {
        !            55:   int fd;
        !            56:   
        !            57:   fd = fileno(sp);
        !            58:   
        !            59: #ifdef __sun
        !            60:   return ( lockf(fd,F_ULOCK, 0L) );
        !            61: #else
        !            62:   return (flock(fd,LOCK_UN));
        !            63: #endif
        !            64: }
        !            65: 
        !            66: char *
        !            67: capa_malloc(num,sz) unsigned num,sz;
        !            68: {
        !            69:   char *p;
        !            70:   p = calloc(num, sz);
        !            71:   return (p);
        !            72: }
        !            73: 
        !            74: 
        !            75: /****************************************************** Database Entry */
        !            76: int /* RETURNS: error code */
        !            77: capa_set_entry(entry, student_number, set, offset) 
        !            78: T_entry   *entry;          /* pointer to entry structure to fill in */
        !            79: char      *student_number;
        !            80: int        set;
        !            81: long       offset;
        !            82: {
        !            83:    FILE    *fp;
        !            84:    int      errcode=0;
        !            85:    int      len;
        !            86:    char     filename[FILE_NAME_LENGTH];
        !            87:    char     a_line[512];
        !            88: 
        !            89:    sprintf(filename,"records/set%d.db",set);
        !            90:    if ((fp=fopen(filename,"r+"))==NULL) {
        !            91:       printf("Error: can't open %s\n",filename);  return (-1);
        !            92:    }
        !            93:    sprintf(a_line,"%s %s,%s\n",entry->student_number,entry->answers,entry->tries);
        !            94:    len = strlen(a_line);
        !            95:    flockstream(fp);
        !            96:    fseek(fp,offset,0);
        !            97:      if (!fwrite(a_line,len,1,fp) ) {
        !            98:        printf("Error writing data to file\n");
        !            99:        errcode= (-1);
        !           100:      }
        !           101:    funlockstream(fp);
        !           102:    fclose(fp);
        !           103:    return (errcode);
        !           104: }
        !           105: 
        !           106: /**************************************************** Get db entry*/
        !           107: 
        !           108: long /* RETURNS: byte offset to start of record, 0 if error,
        !           109:                     -offset if not found & newly created  */
        !           110: capa_get_entry(entry, student_number, set) 
        !           111: T_entry   *entry;           
        !           112: char      *student_number;  
        !           113: int        set;            
        !           114: {
        !           115:    char      filename[FILE_NAME_LENGTH];
        !           116:    FILE     *fp;
        !           117:    int       len, nq;          
        !           118:    char     *ans_p, *tries_p, oneline[512],fmtbuf[128];          
        !           119:    long      offset, next_r;             
        !           120:    int       ii, done=0, found=0;
        !           121:    char      a_sn[MAX_STUDENT_NUMBER+1];
        !           122:    
        !           123:    sprintf(filename,"records/set%d.db",set); 
        !           124:    if ((fp=fopen(filename,"r"))==NULL) {
        !           125:       printf("Error: can't open %s\n",filename);
        !           126:       return (-1); 
        !           127:    }
        !           128:    sprintf(entry->student_number,"%s",student_number);
        !           129:    sprintf(fmtbuf, "%%%dc",MAX_STUDENT_NUMBER);
        !           130:    flockstream(fp);
        !           131:    fgets(oneline,511,fp); len = strlen(oneline); sscanf(oneline,"%d",&nq);
        !           132:    ans_p = capa_malloc(nq+1,1); tries_p = capa_malloc(3*nq,1);
        !           133:    fgets(oneline,511,fp); /* skip weight line */
        !           134:    fgets(oneline,511,fp); /* hand grading */
        !           135:    done = 0;
        !           136:    while(!done) {
        !           137:      done = !fgets(oneline,511,fp); len = strlen(oneline);
        !           138:      if( !done ) {
        !           139:        sscanf(oneline,fmtbuf,a_sn);
        !           140:        if( !strncasecmp(a_sn,student_number,MAX_STUDENT_NUMBER) ) { /* Found */
        !           141:          next_r = ftell(fp); offset = next_r - len; done = 1; found = 1;
        !           142:        }
        !           143:      } else {
        !           144:        fseek(fp,0L,SEEK_END);
        !           145:        offset = ftell(fp);  /* last byte, if last bye is cr, back up one */
        !           146:        fseek(fp,-1L,SEEK_END);
        !           147:        while(fgetc(fp) == '\n' ) { offset--; fseek(fp,offset,SEEK_SET); }
        !           148:        offset = offset +2; /* last char and cr */
        !           149:        found = 0; done=1;
        !           150:      }
        !           151:    }
        !           152:    funlockstream(fp); fclose(fp);
        !           153:    if(!found) {
        !           154:      for(ii=0;ii<nq;ii++) { /* Initialize answer string and tries string */
        !           155:        ans_p[ii] = '-'; tries_p[3*ii] = ' '; tries_p[3*ii + 1] = '0';
        !           156:        if(ii < nq-1) tries_p[3*ii + 2] = ',';
        !           157:      }
        !           158:      entry->answers = ans_p;
        !           159:      entry->tries   = tries_p;
        !           160:      capa_set_entry(entry,student_number,set,offset);
        !           161:      offset = -offset;
        !           162:    } else {
        !           163:      sprintf(fmtbuf, "%%%dc",nq);
        !           164:      sscanf(oneline + MAX_STUDENT_NUMBER+1,fmtbuf,ans_p);
        !           165:      sprintf(fmtbuf, "%%%dc",(3*nq-1));
        !           166:      sscanf(oneline + MAX_STUDENT_NUMBER+1+nq+1,fmtbuf,tries_p);
        !           167:      entry->answers = ans_p;
        !           168:      entry->tries   = tries_p;
        !           169:    }
        !           170:    return (offset);
        !           171: }
        !           172: 
        !           173: int main()
        !           174: {
        !           175:   T_entry grade,examgrade;
        !           176:   FILE * inputFile, * outputFile;
        !           177:   int i=0,setnumber,score,section,setId,done=0,numQuestions,examSetId;
        !           178:   char class[10],set[3],name[MAX_NAME_CHAR+1],buf,buffmt[128],
        !           179:       studentnumber[MAX_STUDENT_NUMBER+1],filename[128];
        !           180:   int q=0,r=0,answerTop[MAXQUEST][11],answerMid[MAXQUEST][11],
        !           181:       answerBot[MAXQUEST][11],examtotal=0,firstDiv,secondDiv;
        !           182:   Question questions[MAXQUEST];
        !           183:   
        !           184:   printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        !           185:   printf("Covert form Bubbler output to survey results ");
        !           186:   printf("Version 0.1.00\n");
        !           187:   printf("Please enter the Set Id number. of survey");
        !           188:   scanf("%d",&setId);
        !           189:   printf("Please enter the SetId of the Final exam.");
        !           190:   scanf("%d",&examSetId);
        !           191:   printf("Please enter score of first divison.");
        !           192:   scanf("%d",&firstDiv);
        !           193:   printf("Please enter score of second divison.");
        !           194:   scanf("%d",&secondDiv);
        !           195:   sprintf(filename,"bubbler.output.%d",setId);
        !           196:   inputFile=fopen(filename,"r");
        !           197: 
        !           198:   if (inputFile==NULL)
        !           199:     {
        !           200:       fprintf(stderr,"%s not found\n",filename);
        !           201:       exit(-1);
        !           202:     }
        !           203: 
        !           204:   fscanf(inputFile,"%s %s",class,set);
        !           205:   printf("%s %s\n",class,set);
        !           206:   setnumber=atoi(set);
        !           207: 
        !           208:   i=0;
        !           209:   fscanf(inputFile,"%c",&buf);
        !           210:   while(!done)
        !           211:     {
        !           212:       buf=fgetc(inputFile);
        !           213:       if (buf!='\n')
        !           214: 	{
        !           215: 	  questions[i].type=buf;
        !           216: 	  buf=fgetc(inputFile);
        !           217: 	  questions[i].points=questions[i].leafs=(int)(buf-'0');
        !           218: 	  i++;
        !           219: 	}
        !           220:       else
        !           221: 	{
        !           222: 	  done=1;
        !           223: 	}
        !           224:     }
        !           225:   
        !           226:   numQuestions=i;
        !           227: 
        !           228:   for(q=0;q<MAXQUEST;q++)
        !           229:     for(r=0;r<11;r++)
        !           230:       {
        !           231: 	answerTop[q][r]=0;
        !           232: 	answerMid[q][r]=0;
        !           233: 	answerBot[q][r]=0;
        !           234:       }
        !           235: 
        !           236:   printf("Processing");
        !           237:   while(fscanf(inputFile,"%s",studentnumber)!=EOF)
        !           238:     {
        !           239:       examtotal=0;
        !           240:       printf(".");
        !           241:       fflush(stdout);
        !           242:       fscanf(inputFile,"%32c",name);
        !           243:       sprintf(buffmt,"%%%dc",numQuestions);
        !           244:       fscanf(inputFile,buffmt,grade.answers);
        !           245:       fscanf(inputFile,"%d",&score);
        !           246:       fscanf(inputFile,"%d",&section);
        !           247:       buf='\0';
        !           248:       while(buf!='\n')
        !           249: 	{
        !           250: 	  buf=fgetc(inputFile);
        !           251: 	}
        !           252:       capa_get_entry(&examgrade,studentnumber,examSetId);
        !           253:       
        !           254: #ifdef DEBUG
        !           255:       printf("%d %d\n",numQuestions,strlen(grade.answers));
        !           256: #endif /*DEBUG*/
        !           257: 
        !           258:       for(i=0;i<numQuestions;i++)
        !           259: 	{
        !           260: 	  switch(examgrade.answers[i])
        !           261: 	    {
        !           262: 	    case 'Y':
        !           263: 	    case 'y':
        !           264: 	      i=0;
        !           265: 	      fprintf(stderr,"Skipping %s b/c of Ys\n",studentnumber);
        !           266: 	      goto skip_student;
        !           267: 	      break;
        !           268: 	    case 'N':
        !           269: 	    case 'n':
        !           270: 	      i=0;
        !           271: 	      fprintf(stderr,"Skipping %s b/c of Ns\n",studentnumber);
        !           272: 	      goto skip_student;	      
        !           273: 	      break;
        !           274: 	    case ' ':
        !           275: 	    case '-':
        !           276: 	      i=0;
        !           277: 	      fprintf(stderr,"Skipping %s b/c not complete\n",studentnumber);
        !           278: 	      goto skip_student;
        !           279: 	      break;
        !           280: 	    default:
        !           281: 	      if (isdigit(examgrade.answers[i]))
        !           282: 		{
        !           283: 		  examtotal+=(int)(examgrade.answers[i]-'0');
        !           284: 		}
        !           285: 	      else
        !           286: 		{
        !           287: 		  i=0;
        !           288: 		  fprintf(stderr,"Skipping %s b/c weird\n",studentnumber);
        !           289: 		  goto skip_student;
        !           290: 		}
        !           291: 	      break;
        !           292: 	    }
        !           293: 	}
        !           294:       for(i=0;i<numQuestions;i++)
        !           295: 	{
        !           296: 	  switch(questions[i].type)
        !           297: 	    {
        !           298: 	    case 'a':
        !           299: 	    case 'f':
        !           300: 	    case 'g':
        !           301: 	    case 'b':
        !           302: 	    case 'c':
        !           303: 	    case 'e':
        !           304: 	    case 'd':
        !           305: 	      if (isdigit(grade.answers[i]))
        !           306: 		{
        !           307: 		  if (examtotal < firstDiv)
        !           308: 		    answerBot[i][grade.answers[i]-'0']++;
        !           309: 		  else if (examtotal < secondDiv)
        !           310: 		    answerMid[i][grade.answers[i]-'0']++;
        !           311: 		  else
        !           312: 		    answerTop[i][grade.answers[i]-'0']++;
        !           313: 		}
        !           314: 	      else if(isspace(grade.answers[i]))
        !           315: 		{
        !           316: 		  if (examtotal < firstDiv)
        !           317: 		    answerBot[i][10]++;
        !           318: 		  else if (examtotal < secondDiv)
        !           319: 		    answerMid[i][10]++;
        !           320: 		  else
        !           321: 		    answerTop[i][10]++;
        !           322: 		}
        !           323: 	      break;
        !           324: 	    default:
        !           325: 	      printf("Unknown question type\n");
        !           326: 	      break;
        !           327: 	    }
        !           328: 	}
        !           329:       skip_student:
        !           330:       grade.answers[i]='\0';
        !           331: #ifdef DEBUG
        !           332:       printf("%s\n",studentnumber);
        !           333: #endif /*DEBUG*/
        !           334:     }
        !           335: 
        !           336:   sprintf(filename,"surveyexam.%d",setId);
        !           337:   outputFile=fopen(filename,"w");
        !           338:   if (outputFile==NULL)
        !           339:     {
        !           340:       fprintf(stderr,"%s not found\n",filename);
        !           341:       exit(-1);
        !           342:     }
        !           343: 
        !           344:   fprintf(outputFile,"Bottom Third:\n");
        !           345:   for(q=0;q<numQuestions;q++)
        !           346:     {
        !           347:       fprintf(outputFile,"Question: %d\n",q+1);
        !           348:       fprintf(outputFile,"  0   1   2   3   4   5   6   7   8   9   S\n");
        !           349:       for(r=0;r<11;r++)
        !           350: 	{
        !           351: 	  fprintf(outputFile,"%3d ",answerBot[q][r]);
        !           352: 	}
        !           353:       fprintf(outputFile,"\n");
        !           354:     }
        !           355:   fprintf(outputFile,"\014Middle Third:\n");
        !           356:   for(q=0;q<numQuestions;q++)
        !           357:     {
        !           358:       fprintf(outputFile,"Question: %d\n",q+1);
        !           359:       fprintf(outputFile,"  0   1   2   3   4   5   6   7   8   9   S\n");
        !           360:       for(r=0;r<11;r++)
        !           361: 	{
        !           362: 	  fprintf(outputFile,"%3d ",answerMid[q][r]);
        !           363: 	}
        !           364:       fprintf(outputFile,"\n");
        !           365:     }
        !           366:   fprintf(outputFile,"\014Top Third:\n");
        !           367:   for(q=0;q<numQuestions;q++)
        !           368:     {
        !           369:       fprintf(outputFile,"Question: %d\n",q+1);
        !           370:       fprintf(outputFile,"  0   1   2   3   4   5   6   7   8   9   S\n");
        !           371:       for(r=0;r<11;r++)
        !           372: 	{
        !           373: 	  fprintf(outputFile,"%3d ",answerTop[q][r]);
        !           374: 	}
        !           375:       fprintf(outputFile,"\n");
        !           376:     }
        !           377:   printf("\nProcessing completed. Look in survey.%d for results.\n",
        !           378: 	 setId);
        !           379:   return 0;
        !           380: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>