- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
/* Do the conversion */
while (!isEmpty_2(stackPtr) && infix[i] != '\0') {
c = infix[i++];
if (my_isdigit(c) || my_isalpha(c)) {
while (my_isdigit(c) || my_isalpha(c)) {
postfix[j++] = c;
c = infix[i++];
}
postfix[j++] = ' ';
if (c == ')' || isOperator_2(c))
i--;
} else if (c == '(')
push_2(&stackPtr, '(');
else if (isOperator_2(c) == 1) {
if (!isEmpty_2(stackPtr) && isOperator_2(stackTop_1(stackPtr))) {
while (isOperator_2(stackTop_1(stackPtr)) &&
(precedence_1(stackTop_1(stackPtr), c)) >= 0) {
postfix[j++] = pop_2(&stackPtr);
postfix[j++] = ' ';
}
push_2(&stackPtr, c);
} else
push_2(&stackPtr, c);
} else if (c == ')') {
if (!isEmpty_2(stackPtr)) {
c = pop_2(&stackPtr);
while (!isEmpty_2(stackPtr) && c != '(') {
postfix[j++] = c;
postfix[j++] = ' ';
c = pop_2(&stackPtr);
}
}
}
}
postfix[j] = '\0';
}
int precedence_1(char operator1, char operator2) {
int result = 1;
switch (operator1) {
case '^':
if (operator2 == '^')
result = 0;
break;
case '*':
case '/':
case '%':
switch (operator2) {
case '^':
result = -1;
break;
case '*':
case '/':
case '%':
result = 0;
break;
default:
break;
}
break;
case '+':
case '-':
switch (operator2) {
case '-':
case '+':
result = 0;
break;
default:
result = -1;
break;
}
break;
}
return result;
}
void push_2(StackNodePtr_2 *topPtr, int info) {
StackNodePtr_2 newPtr;
newPtr = malloc(sizeof(StackNode_2)); /* Now points to a place in memory*/
if (newPtr != NULL) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} else
printf("%c not inserted. No memory available.\n", info);
}
Комментарии (0) RSS
Добавить комментарий