/* * getnum - get a number from input. * * getnum handles all of the numeric input. it accepts * decimal, octal, hexidecimal, and floating point numbers. */ getnum() { register int i; i = 0; if(lastch == '0') { getch(); if(lastch == 'x' || lastch == 'X') { getch(); getbase(16); } else getbase(8); } else { getbase(10); if(lastch == '.') { getch(); rval = ival; /* float the integer part */ getfrac(); /* add the fractional part */ lastst = rconst; } if(lastch == 'e' || lastch == 'E') { getch(); getexp(); /* get the exponent */ } } }
/* * getnum - get a number from input. * * getnum handles all of the numeric input. it accepts * decimal, octal, hexidecimal, and floating point numbers. */ void getnum() { register int i; i = 0; ival = 0; rval = 0.0; Float128::Assign(&rval128,Float128::Zero()); numstrptr = &numstr[0]; *numstrptr = lastch; numstrptr++; if(lastch == '0') { getch(); if (lastch=='.') goto j1; if(lastch == 'x' || lastch == 'X') { getch(); getbase(16); } else getbase(8); } else { getbase(10); j1: if(lastch == '.') { getch(); rval = (double)ival; /* float the integer part */ Float128::IntToFloat(&rval128, (__int64)ival); getfrac(); /* add the fractional part */ lastst = rconst; } if(lastch == 'e' || lastch == 'E') { getch(); getexp(); /* get the exponent */ // This must be reset because getting the exponent // calls getbase() which will set lastst=iconst lastst = rconst; } if (lastst==rconst && (lastch=='Q' || lastch=='q' || lastch=='D' || lastch=='d' || lastch=='s' || lastch=='S' || lastch=='T' || lastch=='t')) { float_precision = tolower(lastch); getch(); } else float_precision = 't'; // Ignore 'U' unsigned suffix if (lastch=='U' || lastch=='u') { getch(); } } numstrptr[-1]='\0'; numstrptr = NULL; // dd_real::read(numstr,rval); // printf("leave getnum=%s\r\n", numstr); }
int ZhuangBeiListLayer::getexp(int ap,float x, int n) { if(n == 0) { return ap; } return x * getexp(ap,x, n-1); }
main() { char str[100],c; int res,var[26],val[26],j,i=0; initialise(var); while(1) { printf("\n>"); i=0; while((c=getchar())!='\n') str[i++]=c; str[i]='\0'; //scanf("%s",str); res=removeunwanted(str); if(res==0) { printf("enter correct string"); continue; } else { //printf("%s",str); res=getexp(str,var,val); if(res==0) { printf("enter correct string"); continue; } else { for(j=0;j<26;j++) { if(var[j]==1) { printf("%c=%d,",(j+'a'),val[j]); } } } } } getch(); }
double CCALC::get_p() { double a = 0; s = this -> skip_space(); if(*s == '(') { s++; a = this -> getexp(); } else if(*s >= '0' && *s <= '9') { a = this -> get_num(); std::cout<<a<<std::endl; } else { if (strncmp(s, "sin", 3) == 0) { s += 3; a = sin(getexp()); return a; } if (strncmp(s, "cos", 3) == 0) { s += 3; a = cos(getexp()); return a; } if (strncmp(s, "sqrt", 4) == 0) { s += 3; a = sqrt(getexp()); return a; } } return a; }
static inline void insert_tag( struct boundary_tag *tag, int index ) { int realIndex; if ( index < 0 ) { realIndex = getexp( tag->real_size - sizeof(struct boundary_tag) ); if ( realIndex < MINEXP ) realIndex = MINEXP; } else realIndex = index; tag->index = realIndex; if ( l_freePages[ realIndex ] != NULL ) { l_freePages[ realIndex ]->prev = tag; tag->next = l_freePages[ realIndex ]; } l_freePages[ realIndex ] = tag; }
int float2int(float fl) { U u; int number = 0; u.f = fl; unsigned int s=getsig(u.i),e = getexp(u.i) - 127,frac = getfrac(u.i); if(fl == 0 || e & 0x80) return 0; // Quando o valor passado for zero, ou quando o expoente for negativo, retornar 0 (no caso do expoente negativo, aproximar para zero) if(e > 30 && !s) { // Se o expoente é maior 30 e o número não é negativo, é um caso de overflow printf("Overflow!!!"); return -1; // Retorna código -1 quando dá overflow } if((s != 0 && e == 31 && frac) || e > 31) { // Se o número é negativo, sua mantissa não é zero, e o expoente é maior que 30, é um caso de overflow printf("Overflow!!!"); return -1; // Retorna código -1 quando dá overflow } number |= 1 << (e); if(e < 23) { number |= frac >> (23 - e); }
/* * getnum - get a number from input. * * getnum handles all of the numeric input. it accepts * decimal, octal, hexidecimal, and floating point numbers. */ void getnum(void) { int isfloat = FALSE; char *ptr = intstring; while (isxdigit(lastch) || lastch == 'x' || lastch == 'X') { *ptr++ = lastch; getch(); } if (lastch == '.') { isfloat = TRUE; *ptr++ = lastch; getch(); while (isdigit(lastch)) { *ptr++ = lastch; getch(); } } if (lastch == 'e' || lastch == 'E') { isfloat = TRUE; *ptr++ = lastch; getch(); if (lastch == '+' || lastch == '-') { *ptr++ = lastch; getch(); } while (isdigit(lastch)) { *ptr++ = lastch; getch(); } } if (lastch == 'F') { isfloat = TRUE; } *ptr = 0; ptr = intstring; if (!isfloat) { if (*ptr == '0') { ptr++; if (*ptr == 'x' || *ptr == 'X') { ptr++; getbase(16, &ptr); } else getbase(8, &ptr); } else getbase(10, &ptr); if (lastch == 'U' || lastch == 'u') { lastst = iuconst; getch(); if (lastch == 'L' || lastch == 'l') { lastst = luconst; getch(); } } else if (lastch == 'L' || lastch == 'l') { lastst = lconst; getch(); if (lastch == 'U' || lastch == 'u') { lastst = luconst; getch(); } } } else { getbase(10, &ptr); if (*ptr == '.') { ptr++; rval = ival; /* float the integer part */ getfrac(&ptr); /* add the fractional part */ lastst = rconst; } if (*ptr == 'e' || *ptr == 'E') { ptr++; getexp(&ptr); /* get the exponent */ } if (lastch == 'F' || lastch == 'f') { if (lastst != rconst) { rval = ival; } lastst = fconst; getch(); } else if (lastch == 'L' || lastch == 'l') { if (lastst != rconst) { rval = ival; } lastst = lrconst; getch(); } } }
void free(void *ptr) { int index; struct boundary_tag *tag; if ( ptr == NULL ) return; liballoc_lock(); tag = (struct boundary_tag*)((unsigned int)ptr - sizeof( struct boundary_tag )); if ( tag->magic != LIBALLOC_MAGIC ) { liballoc_unlock(); // release the lock return; } // MELT LEFT... while ( (tag->split_left != NULL) && (tag->split_left->index >= 0) ) { tag = melt_left( tag ); remove_tag( tag ); } // MELT RIGHT... while ( (tag->split_right != NULL) && (tag->split_right->index >= 0) ) { tag = absorb_right( tag ); } // Where is it going back to? index = getexp( tag->real_size - sizeof(struct boundary_tag) ); if ( index < MINEXP ) index = MINEXP; // A whole, empty block? if ( (tag->split_left == NULL) && (tag->split_right == NULL) ) { if ( l_completePages[ index ] == MAXCOMPLETE ) { // Too many standing by to keep. Free this one. unsigned int pages = tag->real_size / l_pageSize; if ( (tag->real_size % l_pageSize) != 0 ) pages += 1; if ( pages < l_pageCount ) pages = l_pageCount; liballoc_free( tag, pages ); liballoc_unlock(); return; } l_completePages[ index ] += 1; // Increase the count of complete pages. } // .......... insert_tag( tag, index ); liballoc_unlock(); }
void *malloc(uint64_t size) { int index; void *ptr; struct boundary_tag *tag = NULL; liballoc_lock(); if ( l_initialized == 0 ) { for ( index = 0; index < MAXEXP; index++ ) { l_freePages[index] = NULL; l_completePages[index] = 0; } l_initialized = 1; } index = getexp( size ) + MODE; if ( index < MINEXP ) index = MINEXP; // Find one big enough. tag = l_freePages[ index ]; // Start at the front of the list. while ( tag != NULL ) { // If there's enough space in this tag. if ( (tag->real_size - sizeof(struct boundary_tag)) >= (size + sizeof(struct boundary_tag) ) ) { break; } tag = tag->next; } // No page found. Make one. if ( tag == NULL ) { if ( (tag = allocate_new_tag( size )) == NULL ) { liballoc_unlock(); return NULL; } index = getexp( tag->real_size - sizeof(struct boundary_tag) ); } else { remove_tag( tag ); if ( (tag->split_left == NULL) && (tag->split_right == NULL) ) l_completePages[ index ] -= 1; } // We have a free page. Remove it from the free pages list. tag->size = size; // Removed... see if we can re-use the excess space. unsigned int remainder = tag->real_size - size - sizeof( struct boundary_tag ) * 2; // Support a new tag + remainder if ( ((int)(remainder) > 0) /*&& ( (tag->real_size - remainder) >= (1<<MINEXP))*/ ) { int childIndex = getexp( remainder ); if ( childIndex >= 0 ) { struct boundary_tag *new_tag = split_tag( tag ); new_tag = new_tag; // Get around the compiler warning about unused variables. } } ptr = (void*)((unsigned int)tag + sizeof( struct boundary_tag ) ); liballoc_unlock(); return ptr; }
int main(int argc, char **argv) { int FState,LState,GramState,CalcuState; double result,TempData; char TempOprt; STK.data_curp=STK.data_stack; STK.oprt_curp=STK.oprt_stack; if (argc < 2) { Usage(argv[0]); return 1; } for (int i = 1; i < argc; i++) { strncat (liyx, argv[i], strlen(argv[i])); } strncat(liyx, "\n", strlen("\n")); clear(&STK); EXP_INIT=YES; BRACKET_COUNT=0; LState=100; GramState=1; CalcuState=1; do{ FState=LState; if((LState=getexp(&STK.data_in,&STK.oprt_in))<0){ error(LState); break; } if((GramState=checkgram(FState,LState))<0){ error(GramState); break; } if(LState==1||LState==2||LState==3||LState==4) PushData(&STK); if(LState==2||(LState==6&&(FState==3||FState==7))){ TempOprt=STK.oprt_in; STK.oprt_in='*'; } while(priority(*(STK.oprt_curp),STK.oprt_in)>0&& *STK.oprt_curp!='(' && STK.oprt_curp!=STK.oprt_stack) { PopData(&STK); TempData=STK.data_out; PopData(&STK); PopOprt(&STK); CalcuState=calculat(STK.data_out,STK.oprt_out,TempData,&result); if(CalcuState<0) break; STK.data_in=result; PushData(&STK); } if(*STK.oprt_curp=='(' && (LState==3||LState==7)) PopOprt(&STK); if(!(LState==3||LState==4||LState==7||LState==8)) PushOprt(&STK); if(LState==2||(LState==6&&(FState==3||FState==7))){ STK.oprt_in=TempOprt; PushOprt(&STK); } if(CalcuState<0){ error(CalcuState); break; } }while (LState!=4&&LState!=8); if((LState==4||LState==8)&&BRACKET_COUNT!=0) error(-8); if((LState==4||LState==8) && BRACKET_COUNT==0 && GramState>0 && CalcuState>0 && FState+LState!=108) printf("%f\n",STK.data_in); return 0; }
void free(void *ptr) { int index; struct boundary_tag *tag; if ( ptr == NULL ) return; liballoc_lock(); tag = (struct boundary_tag*)((unsigned int)ptr - sizeof( struct boundary_tag )); if ( tag->magic != LIBALLOC_MAGIC ) { liballoc_unlock(); // release the lock return; } #ifdef DEBUG l_inuse -= tag->size; printf("free: %x, %i, %i\n", ptr, (int)l_inuse / 1024, (int)l_allocated / 1024 ); #endif // MELT LEFT... while ( (tag->split_left != NULL) && (tag->split_left->index >= 0) ) { #ifdef DEBUG printf("Melting tag left into available memory. Left was %i, becomes %i (%i)\n", tag->split_left->real_size, tag->split_left->real_size + tag->real_size, tag->split_left->real_size ); #endif tag = melt_left( tag ); remove_tag( tag ); } // MELT RIGHT... while ( (tag->split_right != NULL) && (tag->split_right->index >= 0) ) { #ifdef DEBUG printf("Melting tag right into available memory. This was was %i, becomes %i (%i)\n", tag->real_size, tag->split_right->real_size + tag->real_size, tag->split_right->real_size ); #endif tag = absorb_right( tag ); } // Where is it going back to? index = getexp( tag->real_size - sizeof(struct boundary_tag) ); if ( index < MINEXP ) index = MINEXP; // A whole, empty block? if ( (tag->split_left == NULL) && (tag->split_right == NULL) ) { if ( l_completePages[ index ] == MAXCOMPLETE ) { // Too many standing by to keep. Free this one. unsigned int pages = tag->real_size / l_pageSize; if ( (tag->real_size % l_pageSize) != 0 ) pages += 1; if ( pages < l_pageCount ) pages = l_pageCount; liballoc_free( tag, pages ); #ifdef DEBUG l_allocated -= pages * l_pageSize; printf("Resource freeing %x of %i pages\n", tag, pages ); dump_array(); #endif liballoc_unlock(); return; } l_completePages[ index ] += 1; // Increase the count of complete pages. } // .......... insert_tag( tag, index ); #ifdef DEBUG printf("Returning tag with %i bytes (requested %i bytes), which has exponent: %i\n", tag->real_size, tag->size, index ); dump_array(); #endif liballoc_unlock(); }
void *malloc(size_t size) { int index; void *ptr; struct boundary_tag *tag = NULL; liballoc_lock(); if ( l_initialized == 0 ) { #ifdef DEBUG printf("%s\n","liballoc initializing."); #endif for ( index = 0; index < MAXEXP; index++ ) { l_freePages[index] = NULL; l_completePages[index] = 0; } l_initialized = 1; } index = getexp( size ) + MODE; if ( index < MINEXP ) index = MINEXP; // Find one big enough. tag = l_freePages[ index ]; // Start at the front of the list. while ( tag != NULL ) { // If there's enough space in this tag. if ( (tag->real_size - sizeof(struct boundary_tag)) >= (size + sizeof(struct boundary_tag) ) ) { #ifdef DEBUG printf("Tag search found %i >= %i\n",(tag->real_size - sizeof(struct boundary_tag)), (size + sizeof(struct boundary_tag) ) ); #endif break; } tag = tag->next; } // No page found. Make one. if ( tag == NULL ) { if ( (tag = allocate_new_tag( size )) == NULL ) { liballoc_unlock(); return NULL; } index = getexp( tag->real_size - sizeof(struct boundary_tag) ); } else { remove_tag( tag ); if ( (tag->split_left == NULL) && (tag->split_right == NULL) ) l_completePages[ index ] -= 1; } // We have a free page. Remove it from the free pages list. tag->size = size; // Removed... see if we can re-use the excess space. #ifdef DEBUG printf("Found tag with %i bytes available (requested %i bytes, leaving %i), which has exponent: %i (%i bytes)\n", tag->real_size - sizeof(struct boundary_tag), size, tag->real_size - size - sizeof(struct boundary_tag), index, 1<<index ); #endif unsigned int remainder = tag->real_size - size - sizeof( struct boundary_tag ) * 2; // Support a new tag + remainder if ( ((int)(remainder) > 0) /*&& ( (tag->real_size - remainder) >= (1<<MINEXP))*/ ) { int childIndex = getexp( remainder ); if ( childIndex >= 0 ) { #ifdef DEBUG printf("Seems to be splittable: %i >= 2^%i .. %i\n", remainder, childIndex, (1<<childIndex) ); #endif struct boundary_tag *new_tag = split_tag( tag ); new_tag = new_tag; // Get around the compiler warning about unused variables. #ifdef DEBUG printf("Old tag has become %i bytes, new tag is now %i bytes (%i exp)\n", tag->real_size, new_tag->real_size, new_tag->index ); #endif } } ptr = (void*)((unsigned int)tag + sizeof( struct boundary_tag ) ); #ifdef DEBUG l_inuse += size; printf("malloc: %x, %i, %i\n", ptr, (int)l_inuse / 1024, (int)l_allocated / 1024 ); dump_array(); #endif liballoc_unlock(); return ptr; }