180 lines
4.3 KiB
C
180 lines
4.3 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <stdbool.h>
|
|||
|
#include <math.h> // <20>[<5B>J<EFBFBD>ƾǹB<C7B9><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
#define N 50
|
|||
|
#define OP 6
|
|||
|
|
|||
|
char op[OP] = {'(','+','-','*','/','^'}; // <20>W<EFBFBD>[ ^ <20>B<EFBFBD><42><EFBFBD><EFBFBD>
|
|||
|
int op_priority[OP] = {0,1,1,2,2,3}; // <20>N ^ <20><><EFBFBD>u<EFBFBD><75><EFBFBD>ų]<5D><> 3
|
|||
|
|
|||
|
int priority(char c);
|
|||
|
void to_postfix(char infix[], char postfix[]);
|
|||
|
|
|||
|
char stack[N]; // This is a global variable.
|
|||
|
int top = -1;
|
|||
|
|
|||
|
// -------------------------
|
|||
|
// <20>N<EFBFBD><4E><EFBFBD><EFBFBD> item <20><><EFBFBD>J<EFBFBD><4A><EFBFBD>|
|
|||
|
// -------------------------
|
|||
|
void push(char item) {
|
|||
|
if (top >= N - 1) {
|
|||
|
printf("Stack full!\n");
|
|||
|
exit(-1);
|
|||
|
}
|
|||
|
stack[++top] = item;
|
|||
|
}
|
|||
|
|
|||
|
// ------------------------------------
|
|||
|
// <20>Ǧ^<5E><><EFBFBD>|<7C><><EFBFBD>ݪ<EFBFBD><DDAA><EFBFBD><EFBFBD>ơA<C6A1><41><EFBFBD>ëD<C3AB><44><EFBFBD>X
|
|||
|
// ------------------------------------
|
|||
|
int pop() {
|
|||
|
if (top == -1) {
|
|||
|
printf("Stack empty!\n");
|
|||
|
exit(-1);
|
|||
|
}
|
|||
|
return stack[top--];
|
|||
|
}
|
|||
|
|
|||
|
// ----------------------
|
|||
|
// <20>P<EFBFBD>_<EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD>Ű<EFBFBD><C5B0>|
|
|||
|
// ----------------------
|
|||
|
bool IsEmpty(void) {
|
|||
|
return (top < 0) ? true : false;
|
|||
|
}
|
|||
|
|
|||
|
// ----------------------
|
|||
|
// <20>P<EFBFBD>_<EFBFBD><5F><EFBFBD>|<7C>O<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD>
|
|||
|
// ----------------------
|
|||
|
bool IsFull() {
|
|||
|
return (top >= N - 1) ? true : false;
|
|||
|
}
|
|||
|
|
|||
|
// --------------------------
|
|||
|
// <20>Ǧ^<5E><><EFBFBD>|<7C><><EFBFBD>ݪ<EFBFBD><DDAA><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// --------------------------
|
|||
|
char top_data() {
|
|||
|
return stack[top];
|
|||
|
}
|
|||
|
|
|||
|
// ---------------------------
|
|||
|
// <20>Ǧ^<5E>B<EFBFBD><42><EFBFBD>l c <20><><EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD>
|
|||
|
// ---------------------------
|
|||
|
int priority(char c) {
|
|||
|
int i;
|
|||
|
for (i = 0; i < OP; i++) {
|
|||
|
if (op[i] == c)
|
|||
|
return op_priority[i];
|
|||
|
}
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
// ------------------------------------
|
|||
|
// <20>N<EFBFBD><4E><EFBFBD>m<EFBFBD><6D>infix<69>ন<EFBFBD><E0A6A8><EFBFBD>m<EFBFBD><6D>postfix
|
|||
|
// ------------------------------------
|
|||
|
void to_postfix(char infix[], char postfix[]) {
|
|||
|
int i = 0, j = -1;
|
|||
|
char x, y;
|
|||
|
|
|||
|
while ((x = infix[i++]) != '\0') {
|
|||
|
if (x >= '0' && x <= '9') {
|
|||
|
// <20>B<EFBFBD>z<EFBFBD>h<EFBFBD><68><EFBFBD>Ʀr
|
|||
|
while (infix[i] >= '0' && infix[i] <= '9') {
|
|||
|
postfix[++j] = x;
|
|||
|
x = infix[i++];
|
|||
|
}
|
|||
|
postfix[++j] = x; // <20>N<EFBFBD>Ʀr<C6A6>s<EFBFBD>J postfix
|
|||
|
postfix[++j] = ' '; // <20>ΪŮ<CEAA><C5AE><EFBFBD><EFBFBD>j<EFBFBD>Ʀr
|
|||
|
} else {
|
|||
|
switch (x) {
|
|||
|
case '(':
|
|||
|
push(x);
|
|||
|
break;
|
|||
|
case ')':
|
|||
|
while (!IsEmpty() && (y = pop()) != '(') {
|
|||
|
postfix[++j] = y;
|
|||
|
postfix[++j] = ' ';
|
|||
|
}
|
|||
|
break;
|
|||
|
case '+':
|
|||
|
case '-':
|
|||
|
case '*':
|
|||
|
case '/':
|
|||
|
case '^':
|
|||
|
while (!IsEmpty() && priority(top_data()) > priority(x)) {
|
|||
|
postfix[++j] = pop();
|
|||
|
postfix[++j] = ' ';
|
|||
|
}
|
|||
|
push(x);
|
|||
|
break;
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
while (!IsEmpty()) {
|
|||
|
postfix[++j] = pop();
|
|||
|
postfix[++j] = ' ';
|
|||
|
}
|
|||
|
postfix[j] = '\0';
|
|||
|
}
|
|||
|
|
|||
|
bool IsDigit(char c) {
|
|||
|
return c >= '0' && c <= '9';
|
|||
|
}
|
|||
|
|
|||
|
int calculate(char postfix[]) {
|
|||
|
int point = 0, num = 0;
|
|||
|
while (postfix[point] != '\0') {
|
|||
|
if (IsDigit(postfix[point])) {
|
|||
|
num = 0;
|
|||
|
// <20>B<EFBFBD>z<EFBFBD>h<EFBFBD><68><EFBFBD>Ʀr
|
|||
|
while (postfix[point] != ' ') {
|
|||
|
num = num * 10 + (postfix[point] - '0');
|
|||
|
point++;
|
|||
|
}
|
|||
|
push(num); // <20>N<EFBFBD>Ʀr<C6A6><72><EFBFBD>J<EFBFBD><4A><EFBFBD>|
|
|||
|
} else {
|
|||
|
if (postfix[point] != ' ') {
|
|||
|
int a = pop();
|
|||
|
int b = pop();
|
|||
|
int c = 0;
|
|||
|
switch (postfix[point]) {
|
|||
|
case '+':
|
|||
|
c = b + a;
|
|||
|
break;
|
|||
|
case '-':
|
|||
|
c = b - a;
|
|||
|
break;
|
|||
|
case '*':
|
|||
|
c = b * a;
|
|||
|
break;
|
|||
|
case '/':
|
|||
|
c = b / a;
|
|||
|
break;
|
|||
|
case '^':
|
|||
|
c = (int)pow(b, a); // <20><><EFBFBD>ƹB<C6B9><42>
|
|||
|
break;
|
|||
|
}
|
|||
|
push(c);
|
|||
|
}
|
|||
|
}
|
|||
|
point++;
|
|||
|
}
|
|||
|
return pop(); // <20>̫᪺<CCAB><E1AABA><EFBFBD>G
|
|||
|
}
|
|||
|
|
|||
|
int main(void) {
|
|||
|
char infix[50], postfix[50];
|
|||
|
|
|||
|
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>B<EFBFBD>⦡: ");
|
|||
|
scanf("%s", infix);
|
|||
|
to_postfix(infix, postfix);
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>Ǧ<EFBFBD> : %s \t<EFBFBD><EFBFBD><EFBFBD>Ǧ<EFBFBD> : %s\n", infix, postfix);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>סG %d\n", calculate(postfix));
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|