void makeRandomTree (treegen_t* tg, edgefn ef) { resetStack(tg->sp); resetTree(tg->tp); genTree (tg->N, tg->T, tg->sp, tg->tp); writeTree (tg->tp, ef); }
//Infix to Prefix conversion void PREFIX_Convert(char infix[], char prestr[]) { int j,p; size_t n, i; char next ; char symbol; char temp; OPERANDSTACK opstk; resetStack(&opstk); n = strlen(infix); p = 0; for (i = n-1; n >= i; i--) { symbol = infix[i]; switch(symbol) { case ')': push(&opstk, symbol); break; case '(': while( (next=pop(&opstk)) != ')') { prestr[p++] = next; } break; case '+': case '-': case '*': case '/': case '%': case '^': while(!isEmpty(&opstk) && (getPrecedence(peek(&opstk)) > getPrecedence(symbol)) ) { prestr[p++] = pop(&opstk); } push(&opstk, symbol); break; //for an operand default: prestr[p++] = symbol; } } //end for while(!isEmpty(&opstk)) { prestr[p++] = pop(&opstk); } prestr[p] = '\0'; for(i = 0, j = p-2; i<j; i++, j--) { temp = prestr[i]; prestr[i] = prestr[j]; prestr[j] = temp; } prestr[p-1] = '\0'; }
main( int argc, char *argv[] ) { Stack myStack; initStack( &myStack); int flag = 0; int numRead, sumRead; char stdCh, temp, *line, *lntemp, *errStr; char openSet[5] = "<{[("; line = (char*)malloc( sizeof(char)*MAXLINELEN); errStr = (char*)malloc( sizeof(char)*MAXERRLEN); if( argc >= 2 && argv[1][1] == 'd') flag = 1; // set the debug mode while( fgets( line, 300, stdin) != NULL) { lntemp = line; // used to advance scanf sumRead = 0; // used to print to err message, if needed while( 0 < sscanf( lntemp, "%c%n", &stdCh, &numRead) ) { lntemp += numRead; sumRead += numRead; if( stdCh == 'q') // quit return 0; else if( strchr( openSet, stdCh) != NULL) // open brack push( &myStack, stdCh, flag); else if( strchr( openSet, invBrack(stdCh) ) != NULL) { // close brack if( isEmpty( myStack) ) { sprintf( errStr, "%*c:Missing Here", sumRead, invBrack( stdCh) ); break; //exit (parse line) } else { // could match the stack elem, could not temp = pop( &myStack, flag); if( invBrack(stdCh) != temp) { sprintf( errStr, "%*c:Expected Here", sumRead, invBrack( temp)); break; // exit (parse line) } //else the chars match! no other work needed } } } //end while (parse line) printf("%s", line); if( *errStr != '\0') printf("%s\n", errStr); else printf("Expression is balanced\n"); resetStack( &myStack); resetLine( &errStr); printf("\n"); }//end while (line from stdin) free(errStr); free(line); return 0; }
void remove_client ( int client_fd ) { /* !!!!!!!!!!!!!!!! needs to be revised */ int i; CLIENT *client; MALL(client,1); pthread_mutex_lock(&p_mutex); client = clients_desc.first; for(i=0; i < clients_desc.count; i++) { if(client->fd == client_fd) { /* FD found. Rearranging the list. */ if (client == clients_desc.first) { /* Was the first one found? */ if (clients_desc.count == 1) { /* Is there only one node in the list? */ clients_desc.first = NULL; clients_desc.last = NULL; } else { client->next->previous = NULL; /* Second is the first now */ clients_desc.first = client->next; /* Second is the first now */ } } else if (client == clients_desc.last) { /* Was the last one found? */ client->previous->next = NULL; /* Penultimate is the last now */ clients_desc.last = client->previous; /* Penultimate is the last now */ } else { /* Was founded in the middle of list? */ client->previous->next = client->next; client->next->previous = client->previous; } clients_desc.count--; resetStack(client->stack_desc); /*Not necessary to create a new function to remove all the stack. resetStack already frees all nodes in the list, so we only need to free the descriptor of the stack.*/ free(client->stack_desc); /*Remove the descriptor of the stack*/ free(client); /* Remove the node */ break; /* FD not found */ } else { client=client->next; } } pthread_mutex_unlock(&p_mutex); if( VRB & 1 ) { fprintf(stdout,":: -1 client.\n"); } pthread_kill(master_pthread_t, SIGCONT); /* signals master to accept more clients */ }
void handle_client ( CLIENT client ) { int num[1]; PACKAGE outPackage, inPackage; signal(SIGPIPE,SIG_IGN); /* instead of handling the signal, we handle write() error */ errno = 0; read(client.fd,(void *)&inPackage,COM_SIZE); if (errno != EWOULDBLOCK){ if( (errno == EAGAIN) || (errno == ENOTCONN) || (errno == ECONNRESET) || (errno == ETIMEDOUT) ){ close(client.fd); remove_client(client.fd); } /* DEMO */ /*sscanf(inPackage.num,"%X", (unsigned int *) num); fprintf(stdout, "%c\t%d\n", inPackage.msg, *num);*/ /* DEMO */ switch(inPackage.msg){ case 'D': sscanf(inPackage.num,"%X", (unsigned int *) num); outPackage = cmd_D(num,client.stack_desc,outPackage); break; case '+': outPackage = cmd_add(client.stack_desc,outPackage); break; case '-': outPackage = cmd_sub(client.stack_desc,outPackage); break; case '*': outPackage = cmd_mult(client.stack_desc,outPackage); break; case '/': outPackage = cmd_div(client.stack_desc,outPackage); break; case '%': outPackage = cmd_reminder(client.stack_desc,outPackage); break; case 'R': outPackage = cmd_R(client.stack_desc,outPackage); break; case 'T': outPackage = cmd_T(client.stack_desc,outPackage); break; case 'P': outPackage = mountResponsePackage('V', client.stack_desc->count, outPackage); break; case 'I': resetStack(client.stack_desc); outPackage = mountResponsePackage('V',OK,outPackage); break; case 'K': close(client.fd); remove_client(client.fd); return; /* nothing else to do */ default: outPackage = mountResponsePackage('E',BAD_CMD,outPackage); /* bad command */ } errno = 0; write(client.fd,(void *)&outPackage,COM_SIZE); if( (errno == EPIPE) || (errno == EAGAIN) || (errno == ECONNRESET) ){ close(client.fd); remove_client(client.fd); } } }
Stack(void) { resetStack(); }