📄 handle_opt.c
字号:
/**handle_opt.c* define the function declared in handle_opt.h;*AUTHOR:liyangth@gmail.com;*VERSION:1.0;*DATE:2006-8-9;*/#include "handle_opt.h"/*define the operator's PRI*OPERATOR: ( ^ * +- )*PRI: 8 6 4 2 1*@PARAM:a operator;*@RETURN:the operator's PRI;*DATE:2006-8-8;*/int operator_pri(char operator){ char opr = operator; switch (opr) { case '(' : return 8; case '^' : return 6; case '/' : case '*' : return 4; case '+' : case '-' : return 2; case ')' : return 1; default:printf("Undefined operator: %c.\n", opr); break; }}/*handle the operator '(' */void handle_left_b(char operator){ push(&operator_stack, operator);}/*handle the operator '^' */void handle_pow(char operator){ push(&operator_stack, operator);}/*handle the operator '*' and '/' */void handle_multi(char operator){ int val; char opt = operator; if (!is_empty(&operator_stack)) { push(&operator_stack, opt); return; } char opt_instack = sp_value(&operator_stack); if (operator_pri(opt_instack) >= operator_pri(opt)) { switch (opt_instack) { case '(' : push(&operator_stack, opt);break; case '*' : { val = pop(&oprand_stack); val *= pop(&oprand_stack); push(&oprand_stack, val); pop(&operator_stack); push(&operator_stack, opt); break; } case '/' : { val = pop(&oprand_stack); val = pop(&oprand_stack) / val; push(&oprand_stack, val); pop(&operator_stack); push(&operator_stack, opt); break; } case '^' : { int tmp = pop(&oprand_stack); val = pop(&oprand_stack); val = (int)pow(val, tmp); push(&oprand_stack, val); pop(&operator_stack); handle_multi(opt); break; } default : break; } } else { push(&operator_stack, opt); }}/*handle the '+'and '-'*/void handle_add_and_sub(char operator){ int val; char opt = operator; if (!is_empty(&operator_stack)) { push(&operator_stack, opt); return; } char opt_instack = sp_value(&operator_stack); if (operator_pri(opt_instack) >= operator_pri(opt)) { switch (opt_instack) { case '(' : push(&operator_stack, opt); break; case '*' : { val = pop(&oprand_stack); val *= pop(&oprand_stack); push(&oprand_stack, val); pop(&operator_stack); handle_add_and_sub(opt); break; } case '/' : { val = pop(&oprand_stack); val = pop(&oprand_stack) / val; push(&oprand_stack, val); pop(&operator_stack); handle_add_and_sub(opt); break; } case '^' : { int tmp = pop(&oprand_stack); val = pop(&oprand_stack); val = (int)pow(val, tmp); push(&oprand_stack, val); pop(&operator_stack); handle_add_and_sub(opt); break; } case '+' : { val = pop(&oprand_stack); val += pop(&oprand_stack); push(&oprand_stack, val); pop(&operator_stack); push(&operator_stack, opt); break; } case '-' : { val = pop(&oprand_stack); val = pop(&oprand_stack) - val; push(&oprand_stack, val); pop(&operator_stack); push(&operator_stack, opt); break; } default : break; } } else { push(&operator_stack, opt); }}/*handle the ')' ; * when we meet ')', we handle the stack until meet '(';*/void handle_right_b(char operator){ int val; char opt = operator; if (!is_empty(&operator_stack)) { printf("Wrong:NO operator in stack but you put a ')'!\n"); return; } char opt_instack = sp_value(&operator_stack); switch (opt_instack) { case '(' : pop(&operator_stack); break; case '*' : { val = pop(&oprand_stack); val *= pop(&oprand_stack); push(&oprand_stack, val); pop(&operator_stack); handle_right_b(opt); break; } case '/' : { val = pop(&oprand_stack); val = pop(&oprand_stack) / val; push(&oprand_stack, val); pop(&operator_stack); handle_right_b(opt); break; } case '^' : { int tmp = pop(&oprand_stack); val = pop(&oprand_stack); val = (int)pow(val, tmp); push(&oprand_stack, val); pop(&operator_stack); handle_right_b(opt); break; } case '+' : { val = pop(&oprand_stack); val += pop(&oprand_stack); push(&oprand_stack, val); pop(&operator_stack); handle_right_b(opt); break; } case '-' : { val = pop(&oprand_stack); val = pop(&oprand_stack) - val; push(&oprand_stack, val); pop(&operator_stack); handle_right_b(opt); break; } default : break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -