Example #1
0
void NumberDouble<T>::raiseToPower(const Number& _rhs)
{
    const NumberDouble<T>& rhs = number_cast<const NumberDouble<T>&>(_rhs);

    if (isZero() && !rhs.isZero())
        return;
    if (rhs.isZero())
    {
        realPart = 1.0;
        imaginaryPart = 0;
        return;
    }
    naturalLog();
    multiply(rhs);
    raiseEToSelf();
}
Example #2
0
double calculate(int numInputTokens, char **inputString)
{
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//for keeping track of # of operands and operators
	int numOfOperands = 0;
	int numOfOperators = 0;
	int check = 0;

	//set up the stack
	stack = createDynArr(20);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];

		if(strcmp(s, "+") == 0) {
			numOfOperators++;		//increase for binary operators
			add(stack);
		}
		else if(strcmp(s,"-") == 0) {
			numOfOperators++;
			subtract(stack);
		}
		else if(strcmp(s, "/") == 0) {
			numOfOperators++;
			divide(stack);
		}
		else if(strcmp(s, "x") == 0) {
			numOfOperators++;
			multiply(stack);
		}
		else if(strcmp(s, "^") == 0) {
			numOfOperators = numOfOperators;	//for unary operators don't increase
			power(stack);
		}
		else if(strcmp(s, "^2") == 0) {
			numOfOperators = numOfOperators;
			square(stack);
		}
		else if(strcmp(s, "^3") == 0) {
			numOfOperators = numOfOperators;
			cube(stack);
		}
		else if(strcmp(s, "abs") == 0) {
			numOfOperators = numOfOperators;
			absoluteValue(stack);
		}
		else if(strcmp(s, "sqrt") == 0) {
			numOfOperators = numOfOperators;
			squareRoot(stack);
		}
		else if(strcmp(s, "exp") == 0) {
			numOfOperators = numOfOperators;
			exponential(stack);
		}
		else if(strcmp(s, "ln") == 0) {
			numOfOperators = numOfOperators;
			naturalLog(stack);
		}
		else if(strcmp(s, "log") == 0) {
			numOfOperators = numOfOperators;
			logTen(stack);
		}
		else 
		{
			//check if its a number (or pi or e)
			if(isNumber(s, &result)) {
				numOfOperands++;			//increase count of operands
				pushDynArr(stack, result); //if it is push onto stack
			}
			else {
			printf("You entered bad characters, exiting!\n"); //else print error message
			exit(0);
			}
			
		}
	}	//end for 

	//check for correct balance of operands and operators
	//must be one more operand than operators in sequence
	check = numOfOperands - numOfOperators; //this must be equal to 1 for valid input
	if (check > 1) {
		printf("Illegal Input, too many operands or too few operators, exiting\n");
		exit(0);
	}
	else if (check < 1) {
		printf("Illegal Input, too few operands or too many operators, exiting\n");
		exit(0);
	}

	//if passed check store final result and return
	result = topDynArr(stack);
	return result;
}
Example #3
0
double calculate(int numInputTokens, char **inputString){
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;
	double num;
	//set up the stack
	stack = createDynArr(20);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++)
	{
		s = inputString[i];

		// Hint: General algorithm:
		// (1) Check if the string s is in the list of operators.
		//   (1a) If it is, perform corresponding operations.
		//   (1b) Otherwise, check if s is a number.
		//     (1b - I) If s is not a number, produce an error.
		//     (1b - II) If s is a number, push it onto the stack

		if(strcmp(s, "+") == 0){
			add(stack);
			printf("Adding\n");
		}
		else if(strcmp(s,"-") == 0){
			subtract(stack);
			printf("Subtracting\n");
		}
		else if(strcmp(s, "/") == 0){
			divide(stack);
			printf("Dividing\n");
		}
		else if(strcmp(s, "x") == 0){
			multiply(stack);
			printf("Multiplying\n");
		}
        else if(strcmp(s,"^") == 0){
			power(stack);
			printf("Power\n");
        }
		else if(strcmp(s, "^2") == 0){
			squared(stack);
			printf("Squaring\n");
		}
		else if(strcmp(s, "^3") == 0){
			cubed(stack);
			printf("Cubing\n");
		}
		else if(strcmp(s, "abs") == 0){
			absoluteValue(stack);
			printf("Absolute value\n");
		}
		else if(strcmp(s, "sqrt") == 0){
			squareRoot(stack);
			printf("Square root\n");
		}
		else if(strcmp(s, "exp") == 0){
			exponential(stack);
			printf("Exponential\n");
		}
		else if(strcmp(s, "ln") == 0){
			naturalLog(stack);
			printf("Natural Log\n");
		}
		else if(strcmp(s, "log") == 0){
			logBase10(stack);
			printf("Log\n");
		}

        else{
    // FIXME: You need to develop the code here (when s is not an operator)
    // Remember to deal with special values ("pi" and "e")
    //check if not a number
            if (isNumber(s, &num) == 0){
                if (strcmp(s, "pi") == 0){
                    num = 3.14159265;
                }
                else if (strcmp(s, "e") == 0){
                    num = 2.7182818;
                }
                else{	//wrong
                    printf("%s is not valid (number or operator) \n", s);
                    break;
                }
            }
            pushDynArr(stack, num);
        }
    }	//end for
/* FIXME: You will write this part of the function (2 steps below)
* (1) Check if everything looks OK and produce an error if needed.
* (2) Store the final value in result and print it out.
*/
    if (sizeDynArr(stack) != 1) {
        printf("Incorrect count of numbers is detected! Calculations CANNOT be preformed. ");
        return 0;
    }
    else {
        result = topDynArr(stack);
    }
    return result;
}
Example #4
0
/*	param: integer size of input and char
pre: argc is not equal to 1
post: the final number is calculated without errors. 
*/
double calculate(int numInputTokens, char **inputString)
{
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//set up the stack
	stack = createDynArr(20);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];

		if (strcmp(s, "+") == 0)			// add
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to add together
			{
				add(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "-") == 0)		// subtract
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to subtract together
			{
				subtract(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "/") == 0)		// divide
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to divide together
			{
				divide(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "x") == 0)		// multiply
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to multiply together
			{
				multiply(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "^") == 0)		// calculate power
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to use for power
			{
				power(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "^2") == 0)		// calculate square
		{
			square(stack);
		}
		else if (strcmp(s, "^3") == 0)		// calculate cube
		{
			cube(stack);
		}
		else if (strcmp(s, "abs") == 0)		// calculate absolute
		{
			absolute(stack);
		}
		else if (strcmp(s, "sqrt") == 0)	// calculate square root
		{
			squareRoot(stack);
		}
		else if (strcmp(s, "exp") == 0)		// calculate exponetial
		{
			exponential(stack);
		}
		else if (strcmp(s, "ln") == 0)		// calculate natural log
		{
			naturalLog(stack);
		}
		else if (strcmp(s, "log") == 0)		// get log
		{
			baseTenLog(stack);
		}
		else 
		{
			if (strcmp(s, "pi") == 0)			// convert pi to a number
			{
				s = "3.14159265";
			}
			else if (strcmp(s, "e") == 0)		// convert e to a number
			{
				s = "2.7182818";
			}

			if (isNumber(s, &result) != 0)		// if a number see if you can push on stack
			{
				pushDynArr(stack, result);	
			}
			else                               // exit if not a number
			{
				printf("Error. Following input causing an error: %s \n", s);
				exit (0);
			}
			
		}
	}	

	assert(!isEmptyDynArr(stack));		// check to make sure stack isn't empty
	if (sizeDynArr(stack) > 1)
	{
		printf("Not enough operators. Too many numbers. ");
		for (int i = 1; i < numInputTokens; i++)
		{
			printf("%s ", inputString[i]);
		}
		printf(" \n");

		exit(0);
	}

	result = topDynArr(stack);			// get final number and make it it result

	return result;
}
Example #5
0
double calculate(int numInputTokens, char **inputString)
{
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//set up the stack
	stack = createDynArr(20);
	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];
		// Hint: General algorithm:
		// (1) Check if the string s is in the list of operators.
		//   (1a) If it is, perform corresponding operations.
		//   (1b) Otherwise, check if s is a number.
		//     (1b - I) If s is not a number, produce an error.
		//     (1b - II) If s is a number, push it onto the stack

        if(strcmp(s, "+") == 0)
            add(stack);
        else if(strcmp(s,"-") == 0)
			subtract(stack);
        else if(strcmp(s, "/") == 0)
			divide(stack);
        else if(strcmp(s, "x") == 0)
            time(stack);
        else if(strcmp(s, "^") == 0)
            power(stack);
        else if(strcmp(s, "^2") == 0)
			/* FIXME: replace printf with your own function */
            squaring(stack);
		else if(strcmp(s, "^3") == 0)
			/* FIXME: replace printf with your own function */
            cubing(stack);
		else if(strcmp(s, "abs") == 0)
			/* FIXME: replace printf with your own function */
            myAbs(stack);
		else if(strcmp(s, "sqrt") == 0)
			/* FIXME: replace printf with your own function */
            squareRoot(stack);
		else if(strcmp(s, "exp") == 0)
			/* FIXME: replace printf with your own function */
            exponential(stack);
		else if(strcmp(s, "ln") == 0)
			/* FIXME: replace printf with your own function */
            naturalLog(stack);
		else if(strcmp(s, "log") == 0)
			/* FIXME: replace printf with your own function */
            baseLog(stack);
		else 
		{
			// FIXME: You need to develop the code here (when s is not an operator)
			// Remember to deal with special values ("pi" and "e")
            double *tmp = (double *)malloc(sizeof(double));
            
            if (isNumber(s, tmp))
                pushDynArr(stack, *tmp);
            else if(strcmp(s, "pi") == 0)
                pushDynArr(stack, 3.14159265);
            else if(strcmp(s, "e") == 0)
                pushDynArr(stack, 2.7182818);
            else
                flag = 0;
		}
	}	//end for 

	/* FIXME: You will write this part of the function (2 steps below) 
	 * (1) Check if everything looks OK and produce an error if needed.
	 * (2) Store the final value in result and print it out.
	 */
    if (sizeDynArr(stack) != 1)
        printf("There is an error in your input !\n");
    else {
        if (flag) {
            result = topDynArr(stack);
            popDynArr(stack);
            printf("The result is : %f\n", result);
        }
        else
            printf("There is an error in your input !\n");
    }
    
	return result;
}