借助一个栈,以逆波兰表达式对算式进行解析。
算式的形式为特定协议,非字符串。
算式各节点需标明当前节点属于运算符还是运算对象。
可根据需要添加不同种类的运算对象及相应的运算函数,以适应不同业务需求。
节点的实现方式暂时以一般动态内存方式申请,可根据需要调整为内存池资源。
代码可直接执行,预设三个测试case:
stack_test_case_1,
stack_test_case_2,
stack_test_case_3,
stack_test_case_4,
分别对应算式:
col1 >3 && (col2 + col3) < 8,
(b - 2) * c < 0 & 8 - b * 2 + c = 0 | a * 3 + 6 / (2 + 1) > 10,
(((6/c)-b*a)) < 0,
(a-2=3)&(2+3<1|a=2)。
最终输出的结果为仅包含"> < >= <= == & |"的最简算式,如: 6>3&3<8
-2<0&8=0|20>10
-3<0
(4=3)&(5<1|6=2)
需要进一步解析以判断整个算式为真或假。
头文件:
#pragma once
#include <stdint.h>
/*
Plus sign
Minus sign
Multiplication sign
Division sign
Greater than sign
Less than sign
Greater than or equal to sign
Less than or equal to sign
Equal to sign
or
Related to
Left bracket
Right bracket
*/
typedef enum operatType_e {
OT_Invalid,
OT_Plus, // +
OT_Minus, // -
OT_Multiplication, // *
OT_Division, // /
OT_Greater_than, // >
OT_Less_than, // <
OT_Greater_than_or_equal, // >=
OT_Less_than_or_equal, // <=
OT_Equal, // ==
OT_Or, // ||
OT_And, // &&
OT_Left_bracket, // (
OT_Right_bracket, // )
OT_MAX
}operatType;
typedef enum valueType_e {
VT_operator,
VT_num,
VT_Bool
}valueType;
typedef struct stackNode_t{
valueType type;
uint32_t size;
char* value;
struct stackNode_t* next;
}stackNode;
int stackPush(stackNode** stack, valueType type, char* value, uint32_t size);
stackNode* stackPop(stackNode** stack);
void stackShow(stackNode* stack);
uint32_t getOpLevel(operatType op);
int calculatePush(stackNode** stack, valueType type, char* value, uint32_t size);
void stack_test_case_1();
void stack_test_case_2();
void stack_test_case_3();
void stack_test_case_4();
stackNode* makeCalculateStackByStr(char* str, uint32_t size);
源文件:
#include "myStack.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>
stackNode* gl_stack;
const uint32_t opLevel[OT_MAX] = {0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 5, 6, 6};
void stackFreeStack(stackNode** head) {
if (head == NULL)
return;
if (*head == NULL)
return;
stackNode* tmp = NULL;
while (*head != NULL) {
tmp = (*head)->next;
free((*head)->value);
free(*head);
*head = tmp;
}
return;
}
void stackFreeNode(stackNode** node){
if (node == NULL)
return;
if (*node == NULL)
return;
if ((*node)->next != NULL)
return;
free((*node)->value);
free(*node);
*node = NULL;
return;
}
int stackPush(stackNode** stack, valueType type, char* value, uint32_t size) {
if (stack == NULL)
return -1;
char* valueTmp = malloc(size);
if (valueTmp == NULL)
return -1;
memset(valueTmp, 0, size);
if (*stack == NULL) {
*stack = malloc(sizeof(stackNode));
if (*stack == NULL) {
free(valueTmp);
return -1;
}
(*stack)->type = type;
memcpy(valueTmp, value, size);
(*stack)->value = valueTmp;
(*stack)->size = size;
(*stack)->next = NULL;
return 0;
}
stackNode* tmp = *stack;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = malloc(sizeof(stackNode));
if (tmp->next == NULL) {
free(valueTmp);
return -2;
}
memset(tmp->next, 0, sizeof(stackNode));
tmp->next->type = type;
tmp->next->value = valueTmp;
memcpy(valueTmp, value, size);
tmp->next->size = size;
tmp->next->next = NULL;
return 0;
}
stackNode* stackPop(stackNode** stack) {
if (stack == NULL)
return NULL;
if (*stack == NULL)
return NULL;
stackNode* ret = NULL;
if ((*stack)->next == NULL) {
ret = (*stack);
(*stack) = NULL;
return ret;
}
stackNode* tmp = (*stack);
while (tmp->next->next != NULL) {
tmp = tmp->next;
}
ret = tmp->next;
tmp->next = NULL;
return ret;
}
void stackShow(stackNode* stack) {
stackNode* tmp = stack;
uint32_t cnt = 0;
while (tmp != NULL) {
switch (tmp->type)
{
case VT_operator: {
operatType* opType = (operatType*)(tmp->value);
//printf("[");
switch (*opType)
{
case OT_Plus:
printf("+");
break;
case OT_Minus:
printf("-");
break;
case OT_Multiplication:
printf("*");
break;
case OT_Division:
printf("/");
break;
case OT_Greater_than:
printf(">");
break;
case OT_Less_than:
printf("<");
break;
case OT_Greater_than_or_equal:
printf(">=");
break;
case OT_Less_than_or_equal:
printf("<=");
break;
case OT_Equal:
printf("=");
break;
case OT_Or:
printf("|");
break;
case OT_And:
printf("&");
break;
case OT_Left_bracket:
printf("(");
break;
case OT_Right_bracket:
printf(")");
break;
default:
break;
}
//printf("]");
}
break;
case VT_num:
printf("%d", *((int*)(tmp->value)));
break;
case VT_Bool:
if (*((unsigned int*)(tmp->value))) {
printf("true");
}
else {
printf("false");
}
break;
default:
printf("[invalid]");
break;
}
cnt++;
tmp = tmp->next;
}
if (cnt == 0) {
printf("空栈");
}
printf("\n");
}
uint32_t getOpLevel(operatType op) {
if (op >= OT_MAX - 1)
return 0;
return opLevel[op];
}
stackNode* makeCalculateStackByStr(char* str, uint32_t size) {
if (str == NULL || size < 3)
return NULL;
int err = 0;
int num = 0;
uint32_t isMinuse = 0;
valueType lastType = VT_operator;
operatType op = 0;
stackNode* ret = NULL;
char* ptr = str;
for (uint32_t i = 0; ptr - str < size;i++, ptr++) {
if (*ptr == '-') {
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
if (lastType == VT_operator) {
isMinuse = 1;
lastType = VT_Bool;
}
else {
op = OT_Minus;
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
}
else if (*ptr == '+') {
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
op = OT_Plus;
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr == '*') {
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
op = OT_Multiplication;
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr == '/') {
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
op = OT_Division;
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr == '>') {
if (ptr + 1 - str > size) {
goto error;
}
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
if (*(ptr + 1) == '=') {
op = OT_Greater_than_or_equal;
ptr++;
}
else {
op = OT_Greater_than;
}
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr == '<') {
if (ptr + 1 - str > size) {
goto error;
}
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
if (*(ptr + 1) == '=') {
op = OT_Less_than_or_equal;
ptr++;
}
else {
op = OT_Less_than;
}
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr == '=') {
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr >= '0' && *ptr <= '9') {
num = num * 10 + ((*ptr) - '0');
lastType = VT_num;
if (ptr + 1 - str >= size) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
}
}
else if (*ptr == '(' || *ptr == ')') {
if (lastType == VT_num) {
if (isMinuse)
num *= -1;
err = calculatePush(&ret, VT_num, &num, sizeof(num));
if (err < 0) {
goto error;
}
num = 0;
isMinuse = 0;
}
if (*ptr == '(') {
op = OT_Left_bracket;
}
else {
op = OT_Right_bracket;
}
err = calculatePush(&ret, VT_operator, &op, sizeof(op));
if (err < 0) {
goto error;
}
lastType = VT_operator;
}
else if (*ptr == '[') {
//col name or id
//naver hear
goto error;
}
else {
goto error;
}
stackShow(ret);
}
stackShow(ret);
err = calculateFinish(&ret);
if (err < 0)
goto error;
stackShow(ret);
return ret;
error:
printf("fail\n");
stackFreeStack(&ret);
return NULL;
}
int calculateInt(int num1, int num2, operatType op) {
int ret = 0;
switch (op)
{
case OT_Plus:
ret = num1 + num2;
break;
case OT_Minus:
ret = num1 - num2;
break;
case OT_Multiplication:
ret = num1* num2;
break;
case OT_Division:
ret = num1 / num2;
break;
default:
printf("invalid calculate\n");
break;
}
return ret;
}
unsigned int calculateEquation(int num1, int num2, operatType op) {
if (op < OT_Greater_than || op > OT_Equal) {
//(~,>) | (=, ~)
return -1;
}
switch (op)
{
case OT_Greater_than:
return num1 > num2;
case OT_Less_than:
return num1 < num2;
case OT_Greater_than_or_equal:
return num1 >= num2;
case OT_Less_than_or_equal:
return num1 <= num2;
case OT_Equal:
return num1 == num2;
default:
break;
}
return -1;
}
unsigned int calculateBoolEquation(unsigned int bool1, unsigned int bool2, operatType op) {
if (op != OT_Or && op != OT_And) {
return -1;
}
switch (op)
{
case OT_Or:
return bool1 || bool2;
case OT_And:
return bool1 && bool2;
default:
break;
}
return -1;
}
int calculateSimplestExpression(stackNode** stack, operatType newOp) {
if (stack == NULL)
return -1;
if (*stack == NULL) {
return -1;
}
if (newOp != OT_Or && newOp != OT_And) {
return -1;
}
stackNode* tmp = NULL;
int err = 0;
/*
6>3&3<8
-2<0&8=0|20>10
-3<0
(4=3)&(5<1|6=2)
*/
//(4<2) ->&
//false|true&
do {
tmp = stackPop(stack);
if (tmp == NULL) {
//null stack
return -1;
}
if (tmp->type == VT_num) {
stackNode* num2 = tmp;
stackNode* num1 = NULL;
stackNode* op = stackPop(stack);
if (*((operatType*)(op->value)) < OT_Greater_than || *((operatType*)(op->value)) > OT_Equal) {
//(~,>) | (=, ~)
return -1;
}
num1 = stackPop(stack);
if (num1->type != VT_num) {
return -1;
}
unsigned int isTrue = calculateEquation((*((int*)(num1->value))), (*((int*)(num2->value))), *((operatType*)(op->value)));
if (isTrue == -1) {
return -1;
}
err = stackPush(stack, VT_Bool, &isTrue, sizeof(isTrue));
if (err) {
return -2;
}
stackFreeNode(&num1);
stackFreeNode(&num2);
stackFreeNode(&op);
}
else if (tmp->type == VT_Bool) {
stackNode* bool2 = tmp;
stackNode* bool1 = NULL;
stackNode* op = stackPop(stack);
if (op == NULL) {
//true ->&
err = stackPush(stack, tmp->type, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
if (*((operatType*)(op->value)) != OT_Or && *((operatType*)(op->value)) != OT_And) {
return -1;
}
if (*((operatType*)(op->value)) == OT_Or) {
// wait finish
err = stackPush(stack, op->type, op->value, op->size);
if (err) {
return -2;
}
err = stackPush(stack, tmp->type, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
bool1 = stackPop(stack);
if (bool1->type != VT_Bool) {
return -1;
}
unsigned int isTrue = calculateBoolEquation((*((int*)(bool1->value))), (*((int*)(bool2->value))), *((operatType*)(op->value)));
if (isTrue == -1) {
return -1;
}
err = stackPush(stack, VT_Bool, &isTrue, sizeof(isTrue));
if (err) {
return -2;
}
stackFreeNode(&bool1);
stackFreeNode(&bool2);
stackFreeNode(&op);
}
else if(tmp->type == VT_operator){
//(2=3) -> &
operatType* op = (operatType*)(tmp->value);
if (*op == OT_Right_bracket) {
stackFreeNode(&tmp);
continue;
}
else if (*op == OT_Left_bracket) {
stackFreeNode(&tmp);
break;
}
}
else {
//+ - * / > < =
return -1;
}
} while (1);
return 0;
}
int calculateFinish(stackNode** stack) {
//true|false&2>0
if (stack == NULL)
return -1;
if (*stack == NULL) {
return -1;
}
stackNode* tmp = NULL;
int err = 0;
while (1) {
tmp = stackPop(stack);
if (tmp == NULL) {
//null stack
return -1;
}
if (tmp->type == VT_num) {
stackNode* num2 = tmp;
stackNode* num1 = NULL;
stackNode* op = stackPop(stack);
if (*((operatType*)(op->value)) < OT_Greater_than || *((operatType*)(op->value)) > OT_Equal) {
//(~,>) | (=, ~)
return -1;
}
num1 = stackPop(stack);
if (num1->type != VT_num) {
return -1;
}
unsigned int isTrue = calculateEquation((*((int*)(num1->value))), (*((int*)(num2->value))), *((operatType*)(op->value)));
if (isTrue == -1) {
return -1;
}
err = stackPush(stack, VT_Bool, &isTrue, sizeof(isTrue));
if (err) {
return -2;
}
stackFreeNode(&num1);
stackFreeNode(&num2);
stackFreeNode(&op);
}
else if (tmp->type == VT_Bool) {
stackNode* bool2 = tmp;
stackNode* bool1 = NULL;
stackNode* op = stackPop(stack);
if (op == NULL) {
//true ->&
err = stackPush(stack, tmp->type, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
if (*((operatType*)(op->value)) != OT_Or && *((operatType*)(op->value)) != OT_And) {
return -1;
}
bool1 = stackPop(stack);
if (bool1->type != VT_Bool) {
return -1;
}
unsigned int isTrue = calculateBoolEquation((*((int*)(bool1->value))), (*((int*)(bool2->value))), *((operatType*)(op->value)));
if (isTrue == -1) {
return -1;
}
err = stackPush(stack, VT_Bool, &isTrue, sizeof(isTrue));
if (err) {
return -2;
}
stackFreeNode(&bool1);
stackFreeNode(&bool2);
stackFreeNode(&op);
}
else if (tmp->type == VT_operator) {
operatType* op = (operatType*)(tmp->value);
if (*op == OT_Right_bracket) {
stackFreeNode(&tmp);
continue;
}
else if (*op == OT_Left_bracket) {
stackFreeNode(&tmp);
break;
}
}
else {
//+ - * / > < =
return -1;
}
}
}
int calculatePush(stackNode** stack, valueType type, char* value, uint32_t size) {
int err = 0;
stackNode* rNum = NULL;
stackNode* lNum = NULL;
stackNode* tOp = NULL;
stackNode* tmp = NULL;
if (type == VT_num) {
err = stackPush(stack, type, value, size);
if (err)
return -2;
}
else if(type == VT_operator) {
operatType* ot = (operatType*)value;
if (*ot == OT_Left_bracket) {
//err = stackPush(stack, type, value, size);
//if (err)
// return -2;
}
else if (*ot >= OT_Or && *ot <= OT_And) {
//calculate true or false
//(4=3) -> &
err = calculateSimplestExpression(stack, *ot);
if (err < 0) {
return -1;
}
//0: return false; 1: return true; 2: continue;
}
else {
// + - * / )
// > < =
while (1) {
tmp = stackPop(stack);
if(tmp == NULL){
return -1;
}
if (tmp->type == VT_num) {
rNum = tmp;
tOp = stackPop(stack);
if (tOp == NULL) {
err = stackPush(stack, VT_num, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
if (tOp->type != VT_operator) {
//invalid input
return -1;
}
operatType* op = (operatType*)(tOp->value);
if (*op == OT_Left_bracket) {
//..(.. -> )
//(2 -> +
if (*ot == OT_Right_bracket) {
err = stackPush(stack, VT_num, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
else if (*ot >= OT_Plus && *ot <= OT_Equal) {
err = stackPush(stack, VT_operator, tOp->value, tOp->size);
if (err) {
return -2;
}
err = stackPush(stack, VT_num, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
else {
//..(2 ->(
//..(2 ->&
return -1;
}
}
else if (*op == OT_Or || *op == OT_And) {
//..&3 -> +
if (*ot == OT_Right_bracket || *ot == OT_Left_bracket)
return -1;
err = stackPush(stack, VT_operator, tOp->value, tOp->size);
if (err) {
return -2;
}
err = stackPush(stack, VT_num, tmp->value, tmp->size);
if (err) {
return -2;
}
break;
}
else if (*op >= OT_Greater_than && *op <= OT_Equal) {
//1=3 -> )
if (*ot != OT_Right_bracket && *ot != OT_Or && *ot != OT_And) {
return -1;
}
err = stackPush(stack, VT_operator, tOp->value, tOp->size);
if (err) {
return -2;
}
err = stackPush(stack, VT_num, tmp->value, tmp->size);
if (err) {
return -2;
}
if (*ot == OT_Right_bracket) {
err = stackPush(stack, type, value, size);
if (err) {
return -2;
}
}
break;
}
else {
//..+3 -> *
// 3 + 2 -> =
if (getOpLevel(*op) >= getOpLevel(*ot) || (*ot >= OT_Greater_than && *ot <= OT_Equal)) {
lNum = stackPop(stack);
if (lNum->type != VT_num) {
//invalid input
return -3;
}
int* rNumi = (int*)(rNum->value);
int* lNumi = (int*)(lNum->value);
int result = calculateInt(*lNumi, *rNumi, *op);
err = stackPush(stack, VT_num, &result, sizeof(result));
if (err) {
return -1;
}
}
else {
err = stackPush(stack, VT_operator, tOp->value, tOp->size);
if (err) {
return -1;
}
err = stackPush(stack, VT_num, tmp->value, tmp->size);
if (err) {
return -1;
}
break;
}
}
}
else {
//& -> (
//( -> (
//+ -> (
operatType* op = (operatType*)(tmp->value);
if (*ot != OT_Left_bracket) {
return -1;
}
if (*op >= OT_Greater_than && *op <= OT_Equal) {
return -1;
}
//*op != )
err = stackPush(stack, VT_operator, &tmp->value, tmp->size);
if (err) {
return -1;
}
err = stackPush(stack, type, value, size);
if (err) {
return -1;
}
break;
}
}
}
if (*ot != OT_Right_bracket) {
err = stackPush(stack, type, value, size);
if (err) {
return -2;
}
}
}
else {
printf("invalid input\n");
return -1;
}
stackShow(*stack);
return 0;
}
//col1 >3 && (col2 + col3) < 8
void stack_test_case_1() {
//col1 >3 && (col2 + col3) < 8
int err = 0;
int col1 = 6;
int col2 = 1;
int col3 = 2;
stackNode* head = NULL;
operatType ot = 0;
int num = 3;
err = calculatePush(&head, VT_num, &col1, sizeof(col1));
if (err) {
printf("error\n");
}
ot = OT_Greater_than;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
if (err) {
printf("error\n");
}
num = 3;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_And;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
err = calculatePush(&head, VT_num, &col2, sizeof(col2));
if (err) {
printf("error\n");
}
ot = OT_Plus;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
err = calculatePush(&head, VT_num, &col3, sizeof(col3));
if (err) {
printf("error\n");
}
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
ot = OT_Less_than;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 8;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
err = calculateFinish(&head);
if (err) {
printf("error\n");
}
stackShow(head);
stackFreeStack(&head);
return;
}
//(b - 2) * c < 0 & 8 - b * 2 + c = 0 | a * 3 + 6 / (2 + 1) > 10
void stack_test_case_2() {
int err = 0;
int col1 = 6;
int col2 = 1;
int col3 = 2;
stackNode* head = NULL;
operatType ot = 0;
int num = 3;
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
if (err) {
printf("error\n");
}
err = calculatePush(&head, VT_num, &col2, sizeof(col2));
if (err) {
printf("error\n");
}
ot = OT_Minus;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
if (err) {
printf("error\n");
}
num = 2;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
if (err) {
printf("error\n");
}
ot = OT_Multiplication;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
err = calculatePush(&head, VT_num, &col3, sizeof(col3));
if (err) {
printf("error\n");
}
ot = OT_Less_than;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 0;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_And;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
//(b - 2) * c < 0 &
num = 8;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Minus;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
err = calculatePush(&head, VT_num, &col2, sizeof(col2));
if (err) {
printf("error\n");
}
ot = OT_Multiplication;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 2;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Plus;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 2;
err = calculatePush(&head, VT_num, &col3, sizeof(col3));
if (err) {
printf("error\n");
}
ot = OT_Equal;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 0;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Or;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
//8 - b * 2 + c = 0 |
//a * 3 + 6 / (2 + 1) > 10
num = 2;
err = calculatePush(&head, VT_num, &col1, sizeof(col1));
if (err) {
printf("error\n");
}
ot = OT_Multiplication;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 3;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Plus;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 6;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Division;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 2;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Plus;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 1;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
ot = OT_Greater_than;
err = calculatePush(&head, VT_operator, &ot, sizeof(ot));
if (err) {
printf("error\n");
}
num = 10;
err = calculatePush(&head, VT_num, &num, sizeof(num));
if (err) {
printf("error\n");
}
err = calculateFinish(&head);
if (err) {
printf("error\n");
}
stackShow(head);
stackFreeStack(&head);
}
//(((6/c)-b*a)) < 0
void stack_test_case_3() {
int err = 0;
int col1 = 6;
int col2 = 1;
int col3 = 2;
stackNode* head = NULL;
operatType ot = 0;
int num = 3;
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 6;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Division;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
err = calculatePush(&head, VT_num, &col3, sizeof(col3));
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_Minus;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
err = calculatePush(&head, VT_num, &col2, sizeof(col2));
ot = OT_Multiplication;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
err = calculatePush(&head, VT_num, &col1, sizeof(col1));
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_Less_than;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 0;
err = calculatePush(&head, VT_num, &num, sizeof(num));
err = calculateFinish(&head);
if (err) {
printf("error\n");
}
stackShow(head);
stackFreeStack(&head);
}
//(a-2=3)&(2+3<1|a=2)
void stack_test_case_4() {
int err = 0;
int col1 = 6;
int col2 = 1;
int col3 = 2;
stackNode* head = NULL;
operatType ot = 0;
int num = 3;
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
err = calculatePush(&head, VT_num, &col1, sizeof(col1));
ot = OT_Minus;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 2;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Equal;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 3;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_And;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
ot = OT_Left_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 2;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Plus;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 3;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Less_than;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 1;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Or;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
err = calculatePush(&head, VT_num, &col1, sizeof(col1));
ot = OT_Equal;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
num = 2;
err = calculatePush(&head, VT_num, &num, sizeof(num));
ot = OT_Right_bracket;
err = calculatePush(&head, VT_operator, &(ot), sizeof(ot));
err = calculateFinish(&head);
if (err) {
printf("error\n");
}
stackShow(head);
stackFreeStack(&head);
}