示例#1
0
/**
 * reverse polish calculator\
 * 1 2 - 4 5 + * == (1 - 2) * (4 + 5)
 */
int main (void) {
	int type;
	double op2;
	char s[MAXOP];

	while ((type = getop(s)) != EOF) {
		switch(type) {
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				/** first pop second operand*/
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if (op2 != 0.0)
					push(pop() / op2);
				else
					printf("error: division by zero\n");
				break;
			case '%':
				op2 = pop();
				if (op2 != 0.0)
					push(fmod(pop(), op2));
				else
					printf("error: division by zero\n");
				break;
			case '?':
				stack_show();
				break;
			case '#':
				stack_duplicate();
				break;				
			case '~':
				stack_swap();
				break;
			case '!':
				stack_clear();
				break;
			case '\n':
				printf("\t%.8f\n", pop());
				break;
			default:
				printf("error: unknown command %s\n", s);
				break;
		}
	}
	return 0;
}
示例#2
0
/**
 * reverse polish calculator\
 * 1 2 - 4 5 + * == (1 - 2) * (4 + 5)
 */
int main (void) {
	int type;
	double op2;
	char s[MAXOP];
	/** signal that we`ve printed last variable and don`t want to show pop() */
	int last_var_printed = 0;

	clear_variables();
	while (_getline(line, MAXLINE) != 0) {
		line_index = 0;
//		while ((type = getop(s)) != EOF) {
		while ((type = getop(s)) != '\0') {
			switch(type) {
				case NUMBER:
					push(atof(s));
					break;
				case FUNCTION_OR_VARIABLE:
					process_function_or_variable(s);
					break;
				case ENDSTRING:
					break;
				case '+':
					push(pop() + pop());
					break;
				case '*':
					push(pop() * pop());
					break;
				case '-':
					/** first pop second operand*/
					op2 = pop();
					push(pop() - op2);
					break;
				case '/':
					op2 = pop();
					if (op2 != 0.0)
						push(pop() / op2);
					else
						printf("error: division by zero\n");
					break;
				case '%':
					op2 = pop();
					if (op2 != 0.0)
						push(fmod(pop(), op2));
					else
						printf("error: division by zero\n");
					break;
				case '?':
					stack_show();
					break;
				case '#':
					stack_duplicate();
					break;				
				case '~':
					stack_swap();
					break;
				case '!':
					stack_clear();
					break;
				case '=':
					/** get '=' from stack - it`s not used */
					pop();
					/** next get real value */
					variables[var_pos].value = pop();
					last_var.value = variables[var_pos].value;
					push(last_var.value);
					break;
				case '$':
					printf("last added value: %s = %f\n", last_var.name, last_var.value);
					last_var_printed = 1;
					break;
				case '\n':
					if (last_var_printed == 0)
						printf("\t%.8f\n", pop());
					else
						last_var_printed = 0;
					break;
				default:
					printf("error1: unknown command \'%s\'\n", s);
					break;
			}
		}
	}
	return 0;
}
示例#3
0
文件: main.c 项目: Dioxylin/shs
void program_loop()
{
	char *line = NULL;
	size_t line_size = 0;
	int start_from = 0;
	char *token = NULL;

	while(true) {
		printf("%d > ", num_stack_items());
		line_size = getline(&line, &line_size, stdin);
		if (line_size == -1) break;

		while (true) {
			token = get_next_token(line, line_size, &start_from);
			if (token == NULL) {
				break;
			}
			else if (strcmp(token, ".s") == 0) {
				stack_show();
				continue;
			}
			else if (strcmp(token, ".") == 0) {
				stack_dot();
				continue;
			}
			else if (strcmp(token, ".dropall") == 0) {
				stack_dropall();
				continue;
			}
			else if (strcmp(token, "+") == 0) {
				stack_plus();
				continue;
			}
			else if (strcmp(token, "-") == 0) {
				stack_minus();
				continue;
			}
			else if (strcmp(token, ".swap") == 0) {
				stack_swap();
				continue;
			}
			else if (strcmp(token, ".dup") == 0) {
				stack_dup();
				continue;
			}
			else if (strcmp(token, ".over") == 0) {
				stack_over();
				continue;
			}
			else if (strcmp(token, ".drop") == 0) {
				stack_pop();
				continue;
			}
			else if (strcmp(token, ".rot") == 0) {
				stack_rot();
				continue;
			}
			else if (strcmp(token, ".-rot") == 0) {
				stack_rot();
				stack_rot();
				continue;
			}
			else if (strcmp(token, "!") == 0) {
				set_variable();
				continue;
			}
			else if (strcmp(token, "$") == 0) {
				get_variable();
				continue;
			}
			else if (token[0] == '$' && token[1] == '$') {
				eprintf("error: variable name can't begin with $.\n");
				continue;
			}
			else if (token[0] == '$' && token[1] == '.') {
				/* Put literals in for stack operations. */
				/* shift everything after $ over one */
				for (int i = 0; i < BUFSIZE-1; ++i) {
					token[i] = token[i+1];
				}
				stack_push(token);
				continue;
			}
			else if (token[0] == '$') {
				/* shift everything after $ over one */
				for (int i = 0; i < BUFSIZE-1; ++i) {
					token[i] = token[i+1];
				}
				stack_push(token);
				get_variable();
				continue;
			}
			else if (strcmp(token, ";") == 0) {
				stack_execute();
				continue;
			}
			else if (strcmp(token, ".cd") == 0) {
				stack_cd();
				continue;
			}
			else if (token[0] == '.' && strlen(token) > 1) {
				/* shift everything after . over one */
				for (int i = 0; i < BUFSIZE-1; ++i) {
					token[i] = token[i+1];
				}
				stack_push(token);
				stack_execute();
				continue;
			}
			stack_push(token);
		}
		start_from = 0;
	}
}