Beispiel #1
0
int main()
{
	char *s = malloc(MAXOP * sizeof(char));
	char *line = NULL;
	char *p = NULL;
	size_t cap;
	ssize_t line_len;
	double fval;
	int consumed = 0;

	while ((free(line), line = NULL, line_len = getline(&line, &cap, stdin)) != -1) {
		p = line;
		while (strcmp(p, "\n") != 0 && strcmp(p, "") != 0) {
			while (isspace(*p))
				p++;
			consumed = 0;
			if (sscanf(p, "%lf%n", &fval, &consumed) && consumed > 0)
				push(fval);
			else if (sscanf(p, "%s%n", s, &consumed) && consumed > 0)
				perform_op(s);
			p += consumed;
		}
		last = pop();
		printf("\t%.8g\n", last);
	}
	return 0;
}
Beispiel #2
0
int main()
{
	char str1[10],str2[10],str3[10];
	int op;
	clrscr();
	cout<<"Enter string1:";
	cin>>str1;
	cout<<"\nEnter string2:";
	cin>>str2;
	cout<<"\nEnter string3:";
	cin>>str3;
	op=1;

	perform_op(str1,str2,str3,op);
	getch();
	return 0;
}
int main(void) {
    int i, * ptr;
    op_t op;
    void * addr;

    init_mem();
    srand(time(NULL));

    for (i = 0; i < NPTRS; i++) {
        ptrs[i] = NULL;
    }

    printf("STAGE #1: RANDOM OPERATIONS:\n"
           "============================\n");
    for (i = 0; i < NLOOPS; i++) {
        op = random_op();
        perform_op(op);
    }
    printf("\n\n\n");

    printf("STAGE #2: BOUNDARY CASES:\n"
           "=========================\n\n");

    printf("BOUNDARY CASE #1: ALLOCATING MEMORY WHEN FULL:\n\n");
    printf("Trying to allocate POOL_SIZE = %d bytes of memory...\n", POOL_SIZE);
    ptr = mem_malloc(POOL_SIZE);
    if (! ptr) {
        fprintf(stderr, "\nBoundary case verified: memory full\n");
    }
    else {
        printf("%d bytes of memory allocated successfully.\n", POOL_SIZE);
        mem_stat();
        printf("\nTrying again to allocate POOL_SIZE = %d bytes of memory...\n", POOL_SIZE);
        ptr = mem_malloc(POOL_SIZE);
        if (! ptr) {
            fprintf(stderr, "\nBoundary case verified: memory full\n");
        }
    }
    printf("\n\n");

    printf("BOUNDARY CASE #2: FREEING ALREADY FREED MEMORY:\n\n");
    if (count > 0) {
        printf("First trying to free existing memory at ptrs[0]...\n");
        mem_free(ptrs[0]);
        printf("mem_free() successful.\n");
        mem_stat();
        printf("Now trying to free already freed memory at ptrs[0]...\n");
        mem_free(ptrs[0]);
    }
    else {
        printf("Trying to free already freed memory at ptrs[0]...\n");
        mem_free(ptrs[0]);
    }
    printf("\nBoundary case verified: freeing already freed memory\n\n\n");

    printf("BOUNDARY CASE #3: FREEING AN INVALID POINTER:\n\n");
    addr = (void *) 0x30; /* random memory address */
    printf("Trying to free invalid pointer %p...\n", addr);
    mem_free(addr);
    printf("\nBoundary case verified: freeing an invalid pointer\n");

    return 0;
}
Beispiel #4
0
/** Take a string (hopefully) containing an operator and either
		push it to the stack or eval it depending on precedence.
**/
int handle_op ( const char* op_str,
								Stack* op_stack, 
								Stack* num_stack)
{
#if DEBUG==1 
	cout << "Handling operator '" << op_str << "'." << endl;
#endif
	op_code new_op = -1;

	if (! (0 == op_str_len))
	{
		new_op = parse_operator (op_str, op_str_len, check_unm);
#if DEBUG==1
		cout << "Operator code is: " << new_op << endl;
#endif
	
		if (new_op == OP_CODE_ERR)
		{
			return PARSER_STATUS_NOP;
		}
		else
		{
#if DEBUG==1
			cout << "Comparing previous op, '" << op_stack -> top()
						<< "' with new op '" << new_op << "'." << endl;
			cout << "Precedence is: " << compare_prec (new_op,
							op_stack -> top() ) << endl;
#endif
	/* Only do if holding a low-precedence operator: */	
			while ((op_stack -> top() != OP_CODE_END) && !compare_prec (new_op, op_stack -> top()) )
			{
				double R_arg = num_stack -> top();
				num_stack -> pop();

				double L_arg = 0;
				if ( !is_unary ( op_stack -> top() ) )
				{
					if( num_stack -> empty() )
					{
						cerr << "Operations remain but not enough numbers to "
											"evaluate." << endl;
						return PARSER_STATUS_SYNTAX;
					}
#if DEBUG==1
					cout << "Operator is binary." <<endl;
#endif
					L_arg = num_stack -> top();
					num_stack -> pop();
				}

				double result = perform_op ( L_arg,
															R_arg,
															op_stack -> top(),
															status );
				last_result = result;
				op_stack -> pop();

#if DEBUG==1
				cout << "Result is: " << result << endl;
#endif
				num_stack -> push (result);
			}
	/* As long as we're not holding a right-paren */
			if (new_op != OP_CODE_RPN)
			{
#if DEBUG==1
				cout << "Pushing operator '" << new_op << "' to stack."<<endl;
#endif
				op_stack -> push( new_op );
			}
	/* only LPN has higher prec than RPN. Eliminate it. */
			else if ( OP_CODE_LPN == op_stack -> top() )
			{
				op_stack -> pop();
			}


#if DEBUG==1
			if( !op_stack -> empty() )
				cout << "Next up on stack: " << op_stack -> top() << endl;
#endif
		}
	}
	return PARSER_STATUS_GOOD;
}