#include #include #include "qlib.h" int getqueue(char *p, char **q) { int digit, acc; while(isspace(*p)) p++; acc = 0; digit = 0; while(isdigit(*p)){ acc = acc * 10 + *p - '0'; digit++; p++; } *q = p; if (*p && !isspace(*p)){ fprintf(stderr, "Non-digit '%c' (0%o) in queue number\n", *p, *p); return(-1); } if (!digit){ fprintf(stderr, "Where's the queue number?\n"); return(-1); } if (acc < 0 || acc > 9){ fprintf(stderr, "Bad queue number %d; must be 0 .. 9\n", acc); return(-1); } return(acc); } int getnum(char *p) { int digit, acc; while(isspace(*p)) p++; acc = 0; digit = 0; while(isdigit(*p)){ acc = acc * 10 + *p - '0'; digit++; p++; } if (*p && !isspace(*p)){ fprintf(stderr, "Non-digit in number\n"); return(-1); } if (!digit){ fprintf(stderr, "Where's the number?\n"); return(-1); } return(acc); } int main(int argc, char *argv[]) { struct { QTICKET q; int isq; } queues[10]; int i, j, n; char inbuf[BUFSIZ], *q, *b; for(i = 0; i < 10; i++) queues[i].isq = 0; while(printf("c q, d q, + q n, - q > "), fgets(inbuf, BUFSIZ, stdin) != NULL){ for(b = inbuf; isspace(*b); b++); switch(*b){ case 'c': case 'C': /* create queue */ if ((i = getqueue(++b, &q)) == -1) continue; if (queues[i].isq){ fprintf(stderr, "Delete queue %d first\n", i); continue; } j = create_queue(); if (QE_ISERROR(j)){ fprintf(stderr, "%s\n", qe_errbuf); continue; } queues[i].q = j; queues[i].isq = 1; break; case 'd': case 'D': /* delete queue */ if ((i = getqueue(++b, &q)) == -1) continue; if (!queues[i].isq){ fprintf(stderr, "Queue %d does not exist\n", i); continue; } j = delete_queue(queues[i].q); if (QE_ISERROR(j)){ fprintf(stderr, "%s\n", qe_errbuf); continue; } queues[i].isq = 0; break; case '+': /* push number */ if ((i = getqueue(++b, &q)) == -1) continue; if ((n = getnum(q)) == -1) continue; j = put_on_queue(queues[i].q, n); if (QE_ISERROR(i)){ fprintf(stderr, "%s\n", qe_errbuf); continue; } break; case '-': /* pop number */ if ((i = getqueue(++b, &q)) == -1) continue; qe_errbuf[0] = '\0'; j = take_off_queue(queues[i].q); if (qe_errbuf[0]){ fprintf(stderr, "%s\n", qe_errbuf); continue; } printf("Pop queue %d gives %d\n", i, j); break; } } putchar('\n'); return(0); }