示例#1
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	
	char c;
	DynArr *Parens;
	Parens = newDynArr(5); // Arbitrary first length

	//printf("DEBUG 40 s = %p\n", s);
	while ( (c = nextChar(s)) != '\0' ) 
	{
		if (EQ(c, '(')) pushDynArr(Parens, ')');
		else if (EQ(c, '{')) pushDynArr(Parens, '}');
		else if (EQ(c, '[')) pushDynArr(Parens, ']');
		else if (EQ(c, ')') || EQ(c,'}') || EQ(c, ']'))
		{
			if(isEmptyDynArr(Parens))
				return 0; // We already know we're unbalanced
			else
				if ( EQ(topDynArr(Parens), c)) popDynArr(Parens);
		}
	}

	if (isEmptyDynArr(Parens))
		return 1;  // True, is balanced
  else
		return 0; // False, is not balanced
}
示例#2
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	/* FIXME: You will write this function */
    DynArr* work;
    work = newDynArr(sizeof(s));
    char testChar = 0;
    
    do{
        testChar = nextChar(s);
        
        if (testChar == '(') {
            pushDynArr(work, ')');
        }
        else if (testChar == ')') {
            if (isEmptyDynArr(work)) {
                return 0;
            }
        else if (testChar != topDynArr(work)) {
                return 0;
            }
        else
            popDynArr(work);
        }
        
        else if (testChar == '[') {
            pushDynArr(work, ']');
        }
        else if (testChar == ']') {
            if (isEmptyDynArr(work)) {
                return 0;
            }
            else if (testChar != topDynArr(work)) {
                return 0;
            }
            else
                popDynArr(work);
        }
        
        else if (testChar == '{') {
            pushDynArr(work, '}');
        }
        else if (testChar == '}') {
            if (isEmptyDynArr(work)) {
                return 0;
            }
            else if (testChar != topDynArr(work)) {
                return 0;
            }
            else
                popDynArr(work);
        }
    } while (testChar != '\0');

    if (!isEmptyDynArr(work))
        return 0;
	return 1;
}
示例#3
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	/* FIXME: You will write this function */		
	struct DynArr *arr = newDynArr(7); 
 	
	char a;
	
	if (s != 0)
	{	
		while (a = nextChar(s) != '\0')
		{
			
			if (&a ==  "(")
				pushDynArr(arr, a);		
			else if (&a == "{")
				pushDynArr(arr, a);
		
			else if (&a == "[")
                                pushDynArr(arr, a);
		 	
			else if (&a == ")")
				{
					if(topDynArr(arr) == '(')
						popDynArr(arr);
					else
						return 0;
				}
			else if (&a == "}")
				{
                                        if(topDynArr(arr) == '{')
                                                popDynArr(arr);
                                        else
                                                return 0;
                                }
			else if (&a == "]")
                                {
                                        if(topDynArr(arr) == '[')
                                                popDynArr(arr);
                                        else
                                                return 0;
                                }

			
		}

				

		
		
	}
	
	return 0;
}
示例#4
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their sum is pushed back onto the stack.
*/
void add (struct DynArr *stack)
{
	//declare necessary doubles for operator use on stack
	double operand_1, operand_2, add_result;

	//make sure the stack is not empty
	//assert(!isEmptyDynArr(stack));

	//store and pop first number off
	operand_1 = topDynArr(stack);
	popDynArr(stack);

	//make sure stack is not empty
	//assert(!isEmptyDynArr(stack));

	//store and pop second number off
	operand_2 = topDynArr(stack);
	popDynArr(stack);

	//store in result
	add_result = operand_2 + operand_1;

	//push back onto stack
	pushDynArr(stack, add_result);

}
示例#5
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string
	pre: s is not null
	post:
*/
int isBalanced(char* s)
{
    char check;
    char cmp;
    DynArr *stck;
	stck = newDynArr(100);
    do
    {
        check = nextChar(s);

        if (check == '(' || check == '{' || check == '[')
        {
            pushDynArr(stck, check);
        }
        else if (check == ')' || check == '}' || check == ']')
        {
			if (isEmptyDynArr(stck))
			{
				return 0;
			}
            cmp = topDynArr(stck);
            if ((check == ')' && cmp == '(') || (check == '}' && cmp == '{') || (check == ']' && cmp == '['))
            {
                popDynArr(stck);
            }
        }
    } while (check != '\0');
    if (isEmptyDynArr(stck))
    {
        return 1;
    }
    else
        return 0;
}
示例#6
0
/* param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their quotient is pushed back onto the stack.
*/
void power(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 2);
    double num2 = topDynArr(stack);
    popDynArr(stack);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, pow(num1,num2));
}
示例#7
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void squareRoot(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = sqrt(firstNum);
	pushDynArr(stack, sum);
}
示例#8
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void exponential(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = exp(firstNum);
	pushDynArr(stack, sum);
}
示例#9
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void naturalLog(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = log(firstNum);
	pushDynArr(stack, sum);
}
示例#10
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void baseTenLog(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = log10(firstNum);
	pushDynArr(stack, sum);
}
示例#11
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void absolute(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = (abs(firstNum));
	pushDynArr(stack, sum);
}
示例#12
0
文件: calc.c 项目: sk911215/CS261
void baseLog(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = log10(topDynArr(stack));
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#13
0
文件: calc.c 项目: sk911215/CS261
void exponential(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = exp(topDynArr(stack));
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#14
0
文件: calc.c 项目: sk911215/CS261
void squareRoot(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = sqrt(topDynArr(stack));
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#15
0
文件: calc.c 项目: sk911215/CS261
void myAbs(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = topDynArr(stack) > 0 ? topDynArr(stack) : -topDynArr(stack);
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#16
0
文件: calc.c 项目: sk911215/CS261
void cubing(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = pow(topDynArr(stack), 3);
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#17
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void cube(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE secondNum = 3;

	TYPE sum = pow(firstNum, secondNum);
	pushDynArr(stack, sum);
}
示例#18
0
/*	param: stack is the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and their difference is pushed back onto the stack.
*/
void subtract(struct DynArr *stack) {
	/* FIXME: You will write this function */
	TYPE temp;
	TYPE temp2;
	TYPE temp3;
	temp = peekDynArr(stack);
	popDynArr(stack);
	temp2=peekDynArr(stack);
	popDynArr(stack);
	temp3=temp2-temp;
	pushDynArr(stack, temp3);
}
示例#19
0
/*	param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their product is pushed back onto the stack.
*/
void multiply(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = firstNum * secondNum;
	pushDynArr(stack, sum);
}
示例#20
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their quotient is pushed back onto the stack.
*/
void divide(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = firstNum / secondNum;
	pushDynArr(stack, sum);
}
示例#21
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their difference is pushed back onto the stack.
*/
void subtract(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = firstNum - secondNum;
	pushDynArr(stack, sum);
}
示例#22
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and
	their difference is pushed back onto the stack.
*/
void subtract(struct DynArr *stack){
     if (sizeDynArr(stack) < 2){
        printf( "\n ERROR \n");
        printf("You need to have at least 2 numbers for subtraction \n");
        printf("Please use:  number1 number2 - format \n");
        printf("number1 - number2 will not work \n");
    }
     assert(sizeDynArr(stack) >= 2);
    double number = topDynArr(stack);
    popDynArr(stack);
    double subtract = topDynArr(stack) - number;
    popDynArr(stack);
    pushDynArr(stack, subtract);
}
示例#23
0
/* param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their quotient is pushed back onto the stack.
*/
void multiply(struct DynArr *stack){
    if (sizeDynArr(stack) < 2){
    printf( "\n ERROR \n");
    printf("You need to have at least 2 numbers for multiplication \n");
    printf("Please use:  number1 number2 x format \n");
    printf("number1 x number2 will not work \n");
    }
    assert(sizeDynArr(stack) >= 2);
    double num2 = topDynArr(stack);
    popDynArr(stack);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, num1 * num2);
}
示例#24
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and
	their sum is pushed back onto the stack.
*/
void add (struct DynArr *stack){
    if (sizeDynArr(stack) < 2){
        printf( "\n ERROR \n");
        printf("You need to have at least 2 numbers for addition \n");
        printf("Please use:  number1 number2 + format \n");
        printf("number1 + number2 will not work \n");
    }
    assert(sizeDynArr(stack) >= 2);
    double first = topDynArr(stack);
    popDynArr(stack);
    double second = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, first + second);
}
示例#25
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and
	their quotient is pushed back onto the stack.
*/
void divide(struct DynArr *stack){
    if (sizeDynArr(stack) < 2){
    printf( "\n ERROR \n");
    printf("You need to have at least 2 numbers for division\n");
    printf("Please use:  number1 number2 / format \n");
    printf("number1 / number2 will not work \n");
    }
    assert(sizeDynArr(stack) >= 2);
    double number = topDynArr(stack);
    popDynArr(stack);
    double divide = topDynArr(stack)/number;
    popDynArr(stack);
    pushDynArr(stack, divide);
}
示例#26
0
文件: calc.c 项目: sk911215/CS261
void power(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 2) {
        TYPE firstNum = topDynArr(stack);
        popDynArr(stack);
        TYPE secondNum = topDynArr(stack);
        popDynArr(stack);
        
        TYPE result = pow(secondNum, firstNum);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#27
0
文件: calc.c 项目: sk911215/CS261
void time(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 2) {
        TYPE firstNum = topDynArr(stack);
        popDynArr(stack);
        TYPE secondNum = topDynArr(stack);
        popDynArr(stack);
        
        TYPE result = firstNum * secondNum;
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#28
0
文件: calc.c 项目: sk911215/CS261
/*	param: stack the stack being manipulated
	pre: the stack contains at least two elements
	post: the top two elements are popped and 
	their quotient is pushed back onto the stack.
*/
void divide (struct DynArr *stack)
{
	/* FIXME: You will write this function */
    if (sizeDynArr(stack) >= 2) {
        TYPE firstNum = topDynArr(stack);
        popDynArr(stack);
        TYPE secondNum = topDynArr(stack);
        popDynArr(stack);
        
        TYPE result = secondNum / firstNum;
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
示例#29
0
文件: stackapp.c 项目: Merfoo/CS-261
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	/* FIXME: You will write this function */

    /* Check if s is null */
    assert(s);    

    /* Create stack to hold letters */
    DynArr* stack = newDynArr(16);
    char c = nextChar(s);
		
    while(c)
    {
        if(c == '(' || c == '{' || c == '[')
            pushDynArr(stack, c);

        else if(c == ')' || c == '}' || c == ']')
        {
            int balanced = 0;

            if(!isEmptyDynArr(stack))
            {
                char top = topDynArr(stack);

                if(top == '(' && c == ')')
                    balanced = 1;

                else if(top == '{' && c == '}')
                    balanced = 1;

                else if(top == '[' && c == ']')
                    balanced = 1;
            }

            if(balanced)
                popDynArr(stack);

            else
                return 0;
        }

        c = nextChar(s);
    }

	return isEmptyDynArr(stack);
}
示例#30
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least 1 element
	post: the top element is popped, log10'd,
	and is pushed back onto the stack.
*/
void logTen(struct DynArr *stack)
{
	//declare necessary doubles for operator use on stack
	double operand_1, logTen_result;

	//make sure the stack is not empty
	assert(!isEmptyDynArr(stack));

	//store and pop first number off
	operand_1 = topDynArr(stack);
	popDynArr(stack);

	//store in result
	logTen_result = log10(operand_1);

	//push back onto stack
	pushDynArr(stack, logTen_result);
}