/** * @retval 1 arbitrary extents * @retval 2 power-of-two extents * @retval 3 extents are combination of powers of (3,5,7) {3^r * 5^s * 7^t} */ size_t computeDimkind() { bool p2=true; for( size_t k=0; k<dim_; ++k ) { p2 &= powerOf(extents_[k], 2.0); } if(p2) return 2; for( size_t k=0; k<dim_; ++k ) { if( isNotOnlyDivBy_2_3_5_7(extents_[k]) ){ return 1; } } return 3; }
/* parseHex takes an array of characters representing a hexadecimal * number and returns an integer. * * Parameters: * char hex[]: The array of characters representing a hexadecimal * number, from the argv array. * Return: * int total: An integer value of the represented hexadecimal number. */ int parseHex(char hex[]) { if (hex[0] != '0' || hex[1] != 'x') { return -1; } int pos = 2; char curr; curr = hex[pos]; int count = 0; /* Checks all the values after 0x to make sure they are valid, and * counts the number of values to make conversions easier. Count + 1 * will be the last index of array (other than \0) */ while ( curr != '\0' ) { pos++; int ascii = curr; /* Acceptable ASCII values are 48-57, and 97 to 102, inclusive. */ if (ascii < 48 || (ascii > 57 && ascii < 97) || ascii > 102) { return -1; } curr = hex[pos]; count++; } /* Converts to integer, based on the value/offset of each place. */ int offset = 0; int j; int total = 0; for ( j = count + 1; j > 1; j-- ) { int i; if ( hex[j] >= 48 && hex[j] <= 57 ) { i = hex[j] - '0'; } else { i = hex[j] - 'a' + 10; } total = total + ( i * powerOf(16, offset)); offset++; } return total; }
uint32 KeyPad_getSeriesOfPressedNumbers(uint8 len) { uint8 SeriesOfpressedKeys[len];/*Only C99,VLA feature*/ uint8 index; /*Used For Looping*/ uint32 value=0; /*The return Number,Only unsigned !*/ for(index=0;index<len;index++) { SeriesOfpressedKeys[index]=KeyPad_getPressedKey(); _delay_ms(300); } for(index=0;index<len;index++) { value+=SeriesOfpressedKeys[len-index-1]*powerOf(10,index); } if(!(value >= 0)) return -1; return value; }
// This function determines the value of the power of sin(x), determines // where it should place the *, then prints out a single slice of the // graph double printSlice(double degree, int power) { // Find value of (sin(x))^n, and scale by 10 double value = powerOf(sin(degree),power)*10; // Define our interval double i = -10.0; double j = -9.0; // Iterate through ~20 spaces to determine if spot should be // a space, a line, or a star. while (j <= 10) { // If "j" (right side of interval) is 1, we want to draw the axis line if (j == 1.0) { printf("|"); i++; j++; } else { // If our value falls within our interval, print a star if (i <= value && value <= j) { printf("*"); i++; j++; // Otherwise, print a space } else { printf(" "); i++; j++; } } } printf("\n"); /* USING ROUND (disabled) // Determine location of * by rounding value, taking the // absolute value, then casting it as an int. Note that // starLoc is positive. It's location if value is negative // is taken care of in the next section. int starLoc = abs((int)round(value*10)); int i; // If value is less than 0, print the appropriate half of the slice // of the graph. if (value < 0.0) { for (i = 0; i < (10 - starLoc); i++) { printf(" "); } printf("*"); for (i = 0; i < (starLoc); i++) { printf(" "); } printf("| \n"); } else { // Otherwise, print a blank half then print out the appropriate // second half. printf(" |"); for(i = 0; i < (starLoc - 1); i++) { printf(" "); } printf("*"); for(i = 0; i < (10 - starLoc); i++) { printf(" "); } printf("\n"); }*/ }
main() { int selection = 0; int c=0; do //begin do while { float x=0; float y=0; float fResult=0; int ix=0; int iy=0; int iResult=0; int factor=0; int temp=0; int*pResult=0; printf("\nCalculator Menu\n\n"); printf("[1]\tAddition\n"); printf("[2]\tSubtraction\n"); printf("[3]\tMultiplication\n"); printf("[4]\tDivision\n"); printf("[5]\tModulus (Integers Only)\n"); printf("[6]\tPrime Test (Integers Only)\n"); printf("[7]\tFactorial (Integers Only)\n"); printf("[8]\tPower\n"); printf("[9]\tFibonacci Sequence\n"); printf("[0]\tExit\n"); printf("\nPlease Select an option: "); scanf("%d",&selection); switch(selection) { case 1://Addition { printf("\nYou have selected \"Addition\""); printf("\nx + y = ?"); printf("\nPlease input a value for \"x\": "); scanf("%f",&x); printf("\nPlease input a value for \"y\": "); scanf("%f",&y); addTwoNumbers(x,y); printf("\n-------------------------------------------"); break; } case 2://Subtraction { printf("\nYou have selected \"Subtraction\""); printf("\nx - y = ?"); printf("\nPlease input a value for \"x\": "); scanf("%f",&x); printf("\nPlease input a value for \"y\": "); scanf("%f",&y); subTwoNumbers(x,y); printf("\n-------------------------------------------"); break; } case 3://Multiplication { printf("\nYou have selected \"Multiplication\""); printf("\nx * y = ?"); printf("\nPlease input a value for \"x\": "); scanf("%f",&x); printf("\nPlease input a value for \"y\": "); scanf("%f",&y); multTwoNumbers(x,y); printf("\n-------------------------------------------"); break; } case 4://Division { printf("\nYou have selected \"Division\""); printf("\nx // y = ?"); printf("\nPlease input a value for \"x\": "); scanf("%f",&x); printf("\nPlease input a value for \"y\": "); scanf("%f",&y); divTwoNumbers(x,y); printf("\n-------------------------------------------"); break; } case 5://Modulus { printf("\nYou have selected \"Modulus\""); printf("\nx %% y = ?"); printf("\nPlease input an integer value for \"x\": "); scanf("%d",&ix); printf("\nPlease input an integer value for \"y\": "); scanf("%d",&iy); modTwoNumbers(ix,iy); printf("\n-------------------------------------------"); break; } case 6://Prime Test { printf("\nYou have selected \"Prime Test\""); printf("\nTest if x is prime"); printf("\nPlease input an integer value for \"x\": "); scanf("%d",&ix); primeFactor(ix); printf("\n-------------------------------------------"); break; } case 7://Factorial { printf("\nYou have selected \"Factorial\""); printf("\nx!"); printf("\nPlease input an integer value for \"x\": "); scanf("%d",&ix); factorial(ix); printf("\n-------------------------------------------"); break; } case 8://Power { printf("\nYou have selected \"Power\""); printf("\nx to the power of y"); printf("\nPlease input a value for \"x\": "); scanf("%f",&x); printf("%f",x); printf("\nPlease input an integer value for \"y\": "); scanf("%d",&ix); powerOf(x,ix); printf("\n-------------------------------------------"); break; } case 9://Fibonacci { printf("\nYou have selected \"Fibonacci Sequence\""); printf("\nDisplay the Fibonacci Sequence up to and including n=x"); printf("\nPlease input an integer value for \"x\": "); scanf("%d",&ix); fibonacci(ix); printf("\n-------------------------------------------"); break; } case 0://Exit { printf("\nHave a nice day!"); printf("\n-------------------------------------------"); break; } default://Invalid { printf("\nInvalid Option!\nPlease sekect a valid option!"); printf("\n-------------------------------------------"); break; } } }while(selection!=0); return 0; }
/* Functions added for Standard Basis Library are all indirected through here. */ Handle Real_dispatchc(TaskData *mdTaskData, Handle args, Handle code) { unsigned c = get_C_unsigned(mdTaskData, DEREFWORDHANDLE(code)); switch (c) { case 0: /* tan */ return real_result(mdTaskData, tan(real_arg(args))); case 1: /* asin */ { double x = real_arg(args); if (x < -1.0 || x > 1.0) return real_result(mdTaskData, notANumber); else return real_result(mdTaskData, asin(x)); } case 2: /* acos */ { double x = real_arg(args); if (x < -1.0 || x > 1.0) return real_result(mdTaskData, notANumber); else return real_result(mdTaskData, acos(x)); } case 3: /* atan2 */ return real_result(mdTaskData, atan2(real_arg1(args), real_arg2(args))); case 4: /* pow */ return powerOf(mdTaskData, args); case 5: /* log10 */ { double x = real_arg(args); /* Make sure the result conforms to the definition. */ if (x < 0.0) return real_result(mdTaskData, notANumber); /* Nan. */ else if (x == 0.0) /* x may be +0.0 or -0.0 */ return real_result(mdTaskData, negInf); /* -infinity. */ else return real_result(mdTaskData, log10(x)); } case 6: /* sinh */ return real_result(mdTaskData, sinh(real_arg(args))); case 7: /* cosh */ return real_result(mdTaskData, cosh(real_arg(args))); case 8: /* tanh */ return real_result(mdTaskData, tanh(real_arg(args))); case 9: /* setroundingmode */ setrounding(mdTaskData, args); return mdTaskData->saveVec.push(TAGGED(0)); /* Unit */ case 10: /* getroundingmode */ return mdTaskData->saveVec.push(TAGGED(getrounding(mdTaskData))); /* Floating point representation queries. */ #ifdef _DBL_RADIX case 11: /* Value of radix */ return mdTaskData->saveVec.push(TAGGED(_DBL_RADIX)); #else case 11: /* Value of radix */ return mdTaskData->saveVec.push(TAGGED(FLT_RADIX)); #endif case 12: /* Value of precision */ return mdTaskData->saveVec.push(TAGGED(DBL_MANT_DIG)); case 13: /* Maximum number */ return real_result(mdTaskData, DBL_MAX); /* float.h describes DBL_MIN as the minimum positive number. In fact this is the minimum NORMALISED number. The smallest number which can be represented is DBL_MIN*2**(-DBL_MANT_DIG) */ case 14: /* Minimum normalised number. */ return real_result(mdTaskData, DBL_MIN); case 15: /* Is finite */ /* No longer used - implemented in ML. */ return mdTaskData->saveVec.push(finite(real_arg(args)) ? TAGGED(1) : TAGGED(0)); case 16: /* Is Nan */ /* No longer used - implemented in ML. */ return mdTaskData->saveVec.push(isnan(real_arg(args)) ? TAGGED(1) : TAGGED(0)); case 17: /* Get sign bit. There may be better ways to find this. */ return mdTaskData->saveVec.push(copysign(1.0, real_arg(args)) < 0.0 ? TAGGED(1) : TAGGED(0)); case 18: /* Copy sign. */ return real_result(mdTaskData, copysign(real_arg1(args), real_arg2(args))); case 19: /* Return largest integral value (as a real) <= x. */ return real_result(mdTaskData, floor(real_arg(args))); case 20: /* Return smallest integral value (as a real) >= x */ return real_result(mdTaskData, ceil(real_arg(args))); case 21: { /* Truncate towards zero */ double dx = real_arg(args); if (dx >= 0.0) return real_result(mdTaskData, floor(dx)); else return real_result(mdTaskData, ceil(dx)); } case 22: /* Round to nearest integral value. */ { double dx = real_arg(args); double drem = fmod(dx, 2.0); if (drem == 0.5 || drem == -1.5) /* If the value was exactly positive even + 0.5 or negative odd -0.5 round it down, otherwise round it up. */ return real_result(mdTaskData, ceil(dx-0.5)); else return real_result(mdTaskData, floor(dx+0.5)); } case 23: /* Compute ldexp */ { int exp = get_C_int(mdTaskData, DEREFHANDLE(args)->Get(1)); return real_result(mdTaskData, ldexp(real_arg1(args), exp)); } case 24: /* Get mantissa. */ { int exp; return real_result(mdTaskData, frexp(real_arg(args), &exp)); } case 25: /* Get exponent. */ { int exp; (void)frexp(real_arg(args), &exp); return mdTaskData->saveVec.push(TAGGED(exp)); } case 26: /* Return the mantissa from a Nan as a real number. */ // I think this is no longer used. { union db r_arg_x, r_arg_y; /* We want to simply replace the exponent by the exponent value for 0.5<=x<1. I think there may be a more portable way of doing this. */ r_arg_x.dble = posInf; /* Positive infinity. */ r_arg_y.dble = 0.5; /* Use the infinity value as a mask, removing any bits set and replace by the exponent from 0.5. */ byte *barg = DEREFBYTEHANDLE(args); for(unsigned i = 0; i < DBLE; i++) { r_arg_x.bytes[i] = (barg[i] & ~r_arg_x.bytes[i]) | r_arg_y.bytes[i]; } return real_result(mdTaskData, r_arg_x.dble); } case 27: /* Construct a Nan from a given mantissa. */ // I think this is no longer used. { union db r_arg; r_arg.dble = posInf; /* Positive infinity. */ /* OR in the exponent. */ byte *barg = DEREFBYTEHANDLE(args); for(unsigned i = 0; i < DBLE; i++) { r_arg.bytes[i] = r_arg.bytes[i] | barg[i]; } return real_result(mdTaskData, r_arg.dble); } case 28: /* Return the number of bytes for a real. */ return mdTaskData->saveVec.push(TAGGED(sizeof(double))); default: { char msg[100]; sprintf(msg, "Unknown real arithmetic function: %d", c); raise_exception_string(mdTaskData, EXC_Fail, msg); return 0; } } }
int main(int argc, char* argv[]){ DynArr *dyn; dyn = createDynArr(2); double a,b,c,d,e,f,g,h; a = 3; b = 4; c = 10; d = 6; e = 5; f = 20; printf("\n\nTesting addDynArr...\n"); addDynArr(dyn, a); addDynArr(dyn, b); addDynArr(dyn, c); addDynArr(dyn, d); addDynArr(dyn, e); printf("The array's content: [3,4,10,6,5]\n"); assertTrue(EQ(getDynArr(dyn, 0), a), "Test 1st element == 3"); assertTrue(EQ(getDynArr(dyn, 1), b), "Test 2nd element == 4"); assertTrue(EQ(getDynArr(dyn, 2), c), "Test 3rd element == 10"); assertTrue(EQ(getDynArr(dyn, 3), d), "Test 4th element == 5"); assertTrue(EQ(getDynArr(dyn, 4), e), "Test 5th element == 6"); assertTrue(sizeDynArr(dyn) == 5, "Test size = 5"); printf("\n\nTesting add...\nCalling addDynArr(dyn)\n"); add(dyn); printf("The array's content: [3,4,7,6,5]\n"); assertTrue(EQ(getDynArr(dyn, 3), (double)11), "Test 3rd element == 11"); assertTrue(sizeDynArr(dyn) == 4, "Test size = 4"); //removing result of add test and restoring array removeDynArr(dyn, 3); addDynArr(dyn, d); addDynArr(dyn, e); printf("\n\nTesting sub..."); subtract(dyn); printf("The array's content: [3,4,7,6,5]\n"); assertTrue(EQ(getDynArr(dyn, 3), (double)1), "Test 3rd element == 1"); assertTrue(sizeDynArr(dyn) == 4, "Test size = 4"); //printf("%d \n", sizeDynArr(dyn)); printf("Top: %f \n", topDynArr(dyn)); //removing result of add test and restoring array popDynArr(dyn); printf("Top: %f \n", topDynArr(dyn)); pushDynArr(dyn, f); pushDynArr(dyn, e); printf("Top: %f \n", topDynArr(dyn)); printf("\n\nTesting divide..."); divide(dyn); //printf("The array's content: [3,4,10,20,5]\n"); //assertTrue(EQ(getDynArr(dyn, 3),(double)4), "Test 3rd element == 4"); //assertTrue(sizeDynArr(dyn) == 4, "Test size = 4"); /* printf("At 4: %f \n", topDynArr(dyn)); removeDynArr(dyn,4); printf("At 3: %f \n", topDynArr(dyn)); removeDynArr(dyn,3); printf("At 2: %f \n", topDynArr(dyn)); removeDynArr(dyn,2); printf("At 1: %f \n", topDynArr(dyn)); printf("%d \n",sizeDynArr(dyn)); */ //removing result of add test and restoring array printf("\nTop: %f \n", topDynArr(dyn)); popDynArr(dyn); printf("Top: %f \n", topDynArr(dyn)); pushDynArr(dyn, f); pushDynArr(dyn, e); printf("Top: %f \n", topDynArr(dyn)); printf("\n\nTesting multiply...\nCalling addDynArr(dyn)\n"); multiply(dyn); //printf("The array's content: [3,4,10,6,5]\n"); //assertTrue(EQ(getDynArr(dyn, 3),(float)30), "Test 3rd element == 30"); //assertTrue(sizeDynArr(dyn) == (float)4, "Test size = 4"); printf("Before pop Top: %f \n", topDynArr(dyn)); //removing result of add test and restoring array popDynArr(dyn); printf("After pop Top: %f \n", topDynArr(dyn)); pushDynArr(dyn,(double) 2); pushDynArr(dyn,(double) 1); printf("After 2 push Top: %f \n", topDynArr(dyn)); printf("\n\nTesting power of...n"); powerOf(dyn); //printf("The array's content: [3,4,10,6,5]\n"); //assertTrue(EQ(getDynArr(dyn, 3),(float)2), "Test 3rd element == 2"); //assertTrue(sizeDynArr(dyn) == (float)4, "Test size = 4"); }