Annotation of researchv10no/games/quiz.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <signal.h>
        !             3: 
        !             4: #define NF 10
        !             5: #define NL 2000
        !             6: #define NC 200
        !             7: #define SL 100
        !             8: #define NA 10
        !             9: 
        !            10: int tflag;
        !            11: int xx[NL];
        !            12: char score[NL];
        !            13: int rights;
        !            14: int wrongs;
        !            15: int guesses;
        !            16: FILE *input;
        !            17: int nl = 0;
        !            18: int na = NA;
        !            19: int inc;
        !            20: int ptr  = 0;
        !            21: int nc = 0;
        !            22: char line[150];
        !            23: char response[100];
        !            24: char *tmp[NF];
        !            25: int select[NF];
        !            26: char   *malloc();
        !            27: char buf[BUFSIZ];
        !            28: 
        !            29: readline()
        !            30: {
        !            31:        char *t;
        !            32:        register c;
        !            33: loop:
        !            34:        for (t=line; c=getc(input), *t=c, c!=EOF; t++) {
        !            35:                nc++;
        !            36:                if(*t==' '&&(t==line||t[-1]==' '))
        !            37:                        t--;
        !            38:                if(*t=='\n') {
        !            39:                        if(t[-1]=='\\')         /*inexact test*/
        !            40:                                continue;
        !            41:                        while(t>line&&t[-1]==' ')
        !            42:                                *--t = '\n';
        !            43:                        *++t = 0;
        !            44:                        return(1);
        !            45:                }
        !            46:                if(t-line>=NC) {
        !            47:                        aprintf("Too hard for me\n");
        !            48:                        do {
        !            49:                                *line = getc(input);
        !            50:                                if(*line==0377)
        !            51:                                        return(0);
        !            52:                        } while(*line!='\n');
        !            53:                        goto loop;
        !            54:                }
        !            55:        }
        !            56:        return(0);
        !            57: }
        !            58: 
        !            59: char *eu;
        !            60: char *ev;
        !            61: cmp(u,v)
        !            62: char *u, *v;
        !            63: {
        !            64:        int x;
        !            65:        eu = u;
        !            66:        ev = v;
        !            67:        x = disj(1);
        !            68:        if(x!=1)
        !            69:                return(x);
        !            70:        return(eat(1,0));
        !            71: }
        !            72: 
        !            73: disj(s)
        !            74: {
        !            75:        int t, x;
        !            76:        char *u;
        !            77:        u = eu;
        !            78:        t = 0;
        !            79:        for(;;) {
        !            80:                x = string(s);
        !            81:                if(x>1)
        !            82:                        return(x);
        !            83:                switch(*ev) {
        !            84:                case 0:
        !            85:                case ']':
        !            86:                case '}':
        !            87:                        return(t|x&s);
        !            88:                case '|':
        !            89:                        ev++;
        !            90:                        t |= s;
        !            91:                        s = 0;
        !            92:                        continue;
        !            93:                }
        !            94:                if(s) eu = u;
        !            95:                if(string(0)>1)
        !            96:                        return(2);
        !            97:                switch(*ev) {
        !            98:                case 0:
        !            99:                case ']':
        !           100:                        return(0);
        !           101:                case '}':
        !           102:                        return(1);
        !           103:                case '|':
        !           104:                        ev++;
        !           105:                        continue;
        !           106:                default:
        !           107:                        return(2);
        !           108:                }
        !           109:        }
        !           110: }
        !           111: 
        !           112: string(s)
        !           113: {
        !           114:        int x;
        !           115:        for(;;) {
        !           116:                switch(*ev) {
        !           117:                case 0:
        !           118:                case '|':
        !           119:                case ']':
        !           120:                case '}':
        !           121:                        return(1);
        !           122:                case '\\':
        !           123:                        ev++;
        !           124:                        if(*ev==0)
        !           125:                                return(2);
        !           126:                        if(*ev=='\n') {
        !           127:                                ev++;
        !           128:                                continue;
        !           129:                        }
        !           130:                        if(eat(s,*ev)==1)
        !           131:                                continue;
        !           132:                        ev++;
        !           133:                        return 0;
        !           134:                default:
        !           135:                        if(eat(s,*ev)==1)
        !           136:                                continue;
        !           137:                        return(0);
        !           138:                case '[':
        !           139:                        ev++;
        !           140:                        x = disj(s);
        !           141:                        if(*ev!=']' || x>1)
        !           142:                                return(2);
        !           143:                        ev++;
        !           144:                        if(s==0)
        !           145:                                continue;
        !           146:                        if(x==0)
        !           147:                                return(0);
        !           148:                        continue;
        !           149:                case '{':
        !           150:                        ev++;
        !           151:                        x = disj(s);
        !           152:                        if(*ev!='}'||x>1)
        !           153:                                return(2);
        !           154:                        ev++;
        !           155:                        continue;
        !           156:                }
        !           157:        }
        !           158: }
        !           159: 
        !           160: eat(s,c)
        !           161: char c;
        !           162: {
        !           163:        if(*ev!=c)
        !           164:                return(2);
        !           165:        if(s==0) {
        !           166:                ev++;
        !           167:                return(1);
        !           168:        }
        !           169:        if(fold(*eu)!=fold(c))
        !           170:                return(0);
        !           171:        eu++;
        !           172:        ev++;
        !           173:        return(1);
        !           174: }
        !           175: 
        !           176: fold(c)
        !           177: char c;
        !           178: {
        !           179:        if(c<'A'||c>'Z')
        !           180:                return(c);
        !           181:        return(c|040);
        !           182: }
        !           183: 
        !           184: publish(t)
        !           185: char *t;
        !           186: {
        !           187:        ev = t;
        !           188:        pub1(1);
        !           189: }
        !           190: 
        !           191: pub1(s)
        !           192: {
        !           193:        for(;;ev++){
        !           194:                switch(*ev) {
        !           195:                case '|':
        !           196:                        s = 0;
        !           197:                        continue;
        !           198:                case ']':
        !           199:                case '}':
        !           200:                case 0:
        !           201:                        return;
        !           202:                case '[':
        !           203:                case '{':
        !           204:                        ev++;
        !           205:                        pub1(s);
        !           206:                        continue;
        !           207:                case '\\':
        !           208:                        if(*++ev=='\n')
        !           209:                                continue;
        !           210:                default:
        !           211:                        if(s)
        !           212:                                putchar(*ev);
        !           213:                }
        !           214:        }
        !           215: }
        !           216: 
        !           217: segment(u,w)
        !           218: char *u, *w[];
        !           219: {
        !           220:        char *s;
        !           221:        int i;
        !           222:        char *t;
        !           223:        s = u;
        !           224:        for(i=0;i<NF;i++) {
        !           225:                u = s;
        !           226:                t = w[i];
        !           227:                while(*s!=':'&&*s!='\n'&&s-u<SL) {
        !           228:                        if(*s=='\\')  {
        !           229:                                if(s[1] == '\n') {
        !           230:                                        s += 2;
        !           231:                                        continue;
        !           232:                                }
        !           233:                                *t++ = *s++;
        !           234:                        }
        !           235:                        *t++ = *s++;
        !           236:                }
        !           237: 
        !           238:                while(*s!=':'&&*s!='\n')
        !           239:                        s++;
        !           240:                *t = 0;
        !           241:                if(*s++=='\n') {
        !           242:                        return(i+1);
        !           243:                }
        !           244:        }
        !           245:        aprintf("Too many facts about one thing\n");
        !           246:        return(0);
        !           247: }
        !           248: 
        !           249: perm(u,m,v,n,p)
        !           250: int p[];
        !           251: char *u[], *v[];
        !           252: {
        !           253:        int i, j;
        !           254:        int x;
        !           255:        for(i=0;i<m;i++) {
        !           256:                for(j=0;j<n;j++) {
        !           257:                        x = cmp(u[i],v[j]);
        !           258:                        if(x>1) badinfo();
        !           259:                        if(x==0)
        !           260:                                continue;
        !           261:                        p[i] = j;
        !           262:                        goto uloop;
        !           263:                }
        !           264:                return(0);
        !           265: uloop:         ;
        !           266:        }
        !           267:        return(1);
        !           268: }
        !           269: 
        !           270: find(u,m)
        !           271: char *u[];
        !           272: {
        !           273:        int n;
        !           274:        while(readline()){
        !           275:                n = segment(line,tmp);
        !           276:                if(perm(u,m,tmp+1,n-1,select))
        !           277:                        return(1);
        !           278:        }
        !           279:        return(0);
        !           280: }
        !           281: 
        !           282: readindex()
        !           283: {
        !           284:        xx[0] = nc = 0;
        !           285:        while(readline()) {
        !           286:                xx[++nl] = nc;
        !           287:                if(nl>=NL) {
        !           288:                        aprintf("I've forgotten some of it;\n");
        !           289:                        aprintf("I remember %d items.\n", nl);
        !           290:                        break;
        !           291:                }
        !           292:        }
        !           293: }
        !           294: 
        !           295: talloc()
        !           296: {
        !           297:        int i;
        !           298:        for(i=0;i<NF;i++)
        !           299:                tmp[i] = malloc(SL);
        !           300: }
        !           301: 
        !           302: main(argc,argv)
        !           303: char *argv[];
        !           304: {
        !           305:        register j;
        !           306:        int i;
        !           307:        int x;
        !           308:        int z;
        !           309:        char *info;
        !           310:        long tm;
        !           311:        extern done();
        !           312:        int count;
        !           313:        info = "/usr/games/lib/quiz.k/index";
        !           314:        time(&tm);
        !           315:        inc = (int)tm&077774|01;
        !           316: loop:
        !           317:        if(argc>1&&*argv[1]=='-') {
        !           318:                switch(argv[1][1]) {
        !           319:                case 'i':
        !           320:                        if(argc>2) 
        !           321:                                info = argv[2];
        !           322:                        argc -= 2;
        !           323:                        argv += 2;
        !           324:                        goto loop;
        !           325:                case 't':
        !           326:                        tflag = 1;
        !           327:                        argc--;
        !           328:                        argv++;
        !           329:                        goto loop;
        !           330:                }
        !           331:        }
        !           332:        input = fopen(info,"r");
        !           333:        if(input==NULL) {
        !           334:                aprintf("No info\n");
        !           335:                exit(0);
        !           336:        }
        !           337:        talloc();
        !           338:        if(argc<=2)
        !           339:                instruct(info);
        !           340:        signal(SIGINT, done);
        !           341:        argv[argc] = 0;
        !           342:        if(find(&argv[1],argc-1)==0)
        !           343:                dunno();
        !           344:        fclose(input);
        !           345:        input = fopen(tmp[0],"r");
        !           346:        if(input==NULL)
        !           347:                dunno();
        !           348:        readindex();
        !           349:        if(!tflag || na>nl)
        !           350:                na = nl;
        !           351:        for(;;) {
        !           352:                i = next();
        !           353:                fseek(input,xx[i]+0L,0);
        !           354:                z = xx[i+1]-xx[i];
        !           355:                for(j=0;j<z;j++)
        !           356:                        line[j] = getc(input);
        !           357:                segment(line,tmp);
        !           358:                if(*tmp[select[0]] == '\0' || *tmp[select[1]] == '\0') {
        !           359:                        score[i] = 1;
        !           360:                        continue;
        !           361:                }
        !           362:                publish(tmp[select[0]]);
        !           363:                aprintf("\n");
        !           364:                for(count=0;;count++) {
        !           365:                        if(query(response)==0) {
        !           366:                                publish(tmp[select[1]]);
        !           367:                                aprintf("\n");
        !           368:                                if(count==0) wrongs++;
        !           369:                                score[i] = tflag?-1:1;
        !           370:                                break;
        !           371:                        }
        !           372:                        x = cmp(response,tmp[select[1]]);
        !           373:                        if(x>1) badinfo();
        !           374:                        if(x==1) {
        !           375:                                aprintf("Right!\n");
        !           376:                                if(count==0) rights++;
        !           377:                                if(++score[i]>=1 && na<nl)
        !           378:                                        na++;
        !           379:                                break;
        !           380:                        }
        !           381:                        aprintf("What?\n");
        !           382:                        if(count==0) wrongs++;
        !           383:                        score[i] = tflag?-1:1;
        !           384:                }
        !           385:                guesses += count;
        !           386:        }
        !           387: }
        !           388: 
        !           389: query(r)
        !           390: char *r;
        !           391: {
        !           392:        char *t;
        !           393:        for(t=r;;t++) {
        !           394:                if(read(0,t,1)==0)
        !           395:                        done();
        !           396:                if(*t==' '&&(t==r||t[-1]==' '))
        !           397:                        t--;
        !           398:                if(*t=='\n') {
        !           399:                        while(t>r&&t[-1]==' ')
        !           400:                                *--t = '\n';
        !           401:                        break;
        !           402:                }
        !           403:        }
        !           404:        *t = 0;
        !           405:        return(t-r);
        !           406: }
        !           407: 
        !           408: next()
        !           409: {
        !           410:        int flag;
        !           411:        inc = inc*3125&077777;
        !           412:        ptr = (inc>>2)%na;
        !           413:        flag = 0;
        !           414:        while(score[ptr]>0)
        !           415:                if(++ptr>=na) {
        !           416:                        ptr = 0;
        !           417:                        if(flag) done();
        !           418:                        flag = 1;
        !           419:                }
        !           420:        return(ptr);
        !           421: }
        !           422: 
        !           423: done()
        !           424: {
        !           425:        aprintf("\nRights %d, wrongs %d, ", rights, wrongs);
        !           426:        if(guesses)
        !           427:                aprintf("extra guesses %d, ", guesses);
        !           428:        if(rights+wrongs > 0)
        !           429:                aprintf("score %d%%\n",100*rights/(rights+wrongs));
        !           430:        else aprintf("score zero\n");
        !           431:        exit(0);
        !           432: }
        !           433: instruct(info)
        !           434: char *info;
        !           435: {
        !           436:        int i, n;
        !           437:        aprintf("Subjects:\n\n");
        !           438:        while(readline()) {
        !           439:                aprintf("-");
        !           440:                n = segment(line,tmp);
        !           441:                for(i=1;i<n;i++) {
        !           442:                        aprintf(" ");
        !           443:                        publish(tmp[i]);
        !           444:                }
        !           445:                aprintf("\n");
        !           446:        }
        !           447:        aprintf("\n");
        !           448:        input = fopen(info,"r");
        !           449:        if(input==NULL)
        !           450:                abort();
        !           451:        readline();
        !           452:        segment(line,tmp);
        !           453:        aprintf("For example,\n");
        !           454:        aprintf("    quiz ");
        !           455:        publish(tmp[1]);
        !           456:        aprintf(" ");
        !           457:        publish(tmp[2]);
        !           458:        aprintf("\nasks you a ");
        !           459:        publish(tmp[1]);
        !           460:        aprintf(" and you answer the ");
        !           461:        publish(tmp[2]);
        !           462:        aprintf("\n    quiz ");
        !           463:        publish(tmp[2]);
        !           464:        aprintf(" ");
        !           465:        publish(tmp[1]);
        !           466:        aprintf("\nworks the other way around\n");
        !           467:        aprintf("\nType empty line to get correct answer.\n");
        !           468:        exit(0);
        !           469: }
        !           470:        /*PRINTFLIKE1*/
        !           471: aprintf(f,a,b,c)
        !           472: char *f;
        !           473: {
        !           474:        printf(f,a,b,c);
        !           475:        fflush(stdout);
        !           476: }
        !           477: 
        !           478: badinfo(){
        !           479:        aprintf("Bad info %s\n",line);
        !           480: }
        !           481: 
        !           482: dunno()
        !           483: {
        !           484:        aprintf("I don't know about that\n");
        !           485:        exit(0);
        !           486: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.