2025-01-20 21:25:33 +08:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#define MAX 100
|
|
|
|
|
|
|
|
|
|
// === <20><><EFBFBD><EFBFBD><EFBFBD>ܼƫŧi ===
|
|
|
|
|
char input[MAX];
|
|
|
|
|
char postfix[MAX];
|
|
|
|
|
char stack[MAX];
|
|
|
|
|
double numStack[MAX];
|
|
|
|
|
int top = -1;
|
|
|
|
|
int numTop = -1;
|
|
|
|
|
|
|
|
|
|
// === <20>禡<EFBFBD>ŧi ===
|
|
|
|
|
int get_priority(char op);
|
|
|
|
|
int is_operator(char c);
|
|
|
|
|
double calculate(double a, double b, char op);
|
|
|
|
|
void convert_to_postfix();
|
|
|
|
|
double evaluate_postfix();
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
// === <20>ˬd<CBAC>B<EFBFBD><42><EFBFBD>l<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ===
|
2025-01-20 21:25:33 +08:00
|
|
|
|
int get_priority(char c){
|
2025-01-20 21:30:53 +08:00
|
|
|
|
if(c=='+' || c=='-'){
|
|
|
|
|
return 1;
|
|
|
|
|
}else if(c=='*' || c=='/'){
|
|
|
|
|
return 2;
|
|
|
|
|
}else if(c=='^'){
|
|
|
|
|
return 3;
|
|
|
|
|
}else{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// === <20>ˬd<CBAC>r<EFBFBD><72><EFBFBD>O<EFBFBD>_<EFBFBD><5F><EFBFBD>B<EFBFBD><42><EFBFBD>l ===
|
|
|
|
|
int is_operator(char c) {
|
|
|
|
|
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// === <20><><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD><42> ===
|
|
|
|
|
double calculate(double a, double b, char op) {
|
|
|
|
|
switch(op) {
|
|
|
|
|
case '+': return a + b;
|
|
|
|
|
case '-': return a - b;
|
|
|
|
|
case '*': return a * b;
|
|
|
|
|
case '/':
|
|
|
|
|
if(b != 0) return a / b;
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD><47><EFBFBD>Ƥ<EFBFBD><C6A4>ର<EFBFBD>s<EFBFBD>I\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
case '^': return pow(a, b);
|
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
// === <20>ഫ<EFBFBD><E0B4AB><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
|
|
|
|
|
void convert_to_postfix() {
|
2025-01-20 21:25:33 +08:00
|
|
|
|
int i = 0, j;
|
|
|
|
|
int postfix_index = 0;
|
|
|
|
|
top = -1;
|
|
|
|
|
char number_str[MAX];
|
|
|
|
|
int is_negative;
|
|
|
|
|
|
|
|
|
|
while(input[i] != '\0') {
|
|
|
|
|
if(isdigit(input[i]) || input[i] == '.' ||
|
|
|
|
|
(input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) {
|
|
|
|
|
// <20>B<EFBFBD>z<EFBFBD>Ʀr<C6A6>]<5D>]<5D>A<EFBFBD>t<EFBFBD>ơ^
|
|
|
|
|
is_negative = 0;
|
|
|
|
|
int num_len = 0;
|
|
|
|
|
|
|
|
|
|
// <20>ˬd<CBAC>O<EFBFBD>_<EFBFBD><5F><EFBFBD>t<EFBFBD><74>
|
|
|
|
|
if(input[i] == '-') {
|
|
|
|
|
is_negative = 1;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʀr<C6A6>r<EFBFBD><72>
|
|
|
|
|
while(input[i] != '\0' && (isdigit(input[i]) || input[i] == '.')) {
|
|
|
|
|
number_str[num_len++] = input[i++];
|
|
|
|
|
}
|
|
|
|
|
number_str[num_len] = '\0';
|
|
|
|
|
i--; // <20>^<5E>h<EFBFBD>@<40>Ӧ<EFBFBD><D3A6>m<EFBFBD>A<EFBFBD>]<5D><><EFBFBD>~<7E>h while <20>|<7C>A<EFBFBD>[<5B>@
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>X<EFBFBD>Ʀr
|
|
|
|
|
printf("%s ", number_str);
|
|
|
|
|
if(is_negative) {
|
|
|
|
|
printf("- ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>N<EFBFBD>Ʀr<C6A6>[<5B>J<EFBFBD><4A><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46>
|
|
|
|
|
for(j = 0; j < num_len; j++) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
postfix[postfix_index++] = number_str[j];
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
2025-01-20 21:30:53 +08:00
|
|
|
|
postfix[postfix_index++] = ' ';
|
2025-01-20 21:25:33 +08:00
|
|
|
|
if(is_negative) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
postfix[postfix_index++] = '-';
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(input[i] == '(') {
|
|
|
|
|
stack[++top] = input[i];
|
|
|
|
|
}
|
|
|
|
|
else if(input[i] == ')') {
|
|
|
|
|
while(top >= 0 && stack[top] != '(') {
|
|
|
|
|
printf("%c ", stack[top]);
|
2025-01-20 21:30:53 +08:00
|
|
|
|
postfix[postfix_index++] = stack[top];
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
2025-01-20 21:25:33 +08:00
|
|
|
|
top--;
|
|
|
|
|
}
|
|
|
|
|
if(top >= 0) top--;
|
|
|
|
|
}
|
|
|
|
|
else if(is_operator(input[i])) {
|
|
|
|
|
// <20>p<EFBFBD>G<EFBFBD><47><EFBFBD>O<EFBFBD>B<EFBFBD>z<EFBFBD>t<EFBFBD>ƪ<EFBFBD><C6AA>
|
|
|
|
|
if(!(input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) {
|
|
|
|
|
while(top >= 0 && stack[top] != '(' &&
|
|
|
|
|
(get_priority(stack[top]) > get_priority(input[i]) ||
|
|
|
|
|
(get_priority(stack[top]) == get_priority(input[i]) && input[i] != '^'))) {
|
|
|
|
|
printf("%c ", stack[top]);
|
2025-01-20 21:30:53 +08:00
|
|
|
|
postfix[postfix_index++] = stack[top];
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
2025-01-20 21:25:33 +08:00
|
|
|
|
top--;
|
|
|
|
|
}
|
|
|
|
|
stack[++top] = input[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(top >= 0) {
|
|
|
|
|
printf("%c ", stack[top]);
|
|
|
|
|
postfix[postfix_index++] = stack[top];
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
|
|
|
|
top--;
|
|
|
|
|
}
|
|
|
|
|
postfix[postfix_index] = '\0';
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("\n");
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// === <20>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
|
|
|
|
|
double evaluate_postfix() {
|
|
|
|
|
int i;
|
|
|
|
|
numTop = -1;
|
|
|
|
|
char *token = strtok(postfix, " ");
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("\n<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD>L<EFBFBD>{<7B>G\n");
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
while(token != NULL) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD>B<EFBFBD>z<EFBFBD>аO<EFBFBD>G'%s'\n", token);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
if(isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
|
|
|
|
|
double num = atof(token);
|
|
|
|
|
numStack[++numTop] = num;
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ʀr<EFBFBD>G%.2f\n", num);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
else if(is_operator(token[0])) {
|
|
|
|
|
if(numTop < 1) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD>B<EFBFBD><42><EFBFBD>l '%c' <20>ʤ֨<CAA4><D6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ާ@<40><>\n", token[0]);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
double b = numStack[numTop--];
|
|
|
|
|
double a = numStack[numTop--];
|
|
|
|
|
double result = calculate(a, b, token[0]);
|
|
|
|
|
numStack[++numTop] = result;
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD>G%.2f %c %.2f = %.2f\n", a, token[0], b, result);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>аO '%s'\n", token);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>e<EFBFBD><EFBFBD><EFBFBD>|<7C>G");
|
2025-01-20 21:25:33 +08:00
|
|
|
|
for(i = 0; i <= numTop; i++) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("%.2f ", numStack[i]);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("\n\n");
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
token = strtok(NULL, " ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(numTop != 0) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD>p<EFBFBD><EFBFBD><E2B5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|<7C><><EFBFBD>Ѿl %d <20>Ӥ<EFBFBD><D3A4><EFBFBD>\n", numTop + 1);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return numStack[numTop];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
printf("<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><EFBFBD>ܦ<EFBFBD>(<28>䴩+-*/^()<29>B<EFBFBD><42>) => ");
|
|
|
|
|
|
|
|
|
|
fgets(input, MAX, stdin);
|
|
|
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
|
|
|
|
|
|
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><EFBFBD>ܪk<EFBFBD><EFBFBD><EFBFBD>G ");
|
|
|
|
|
convert_to_postfix();
|
|
|
|
|
double postfix_result = evaluate_postfix();
|
|
|
|
|
printf("\n<EFBFBD>B<EFBFBD><EFBFBD>G<EFBFBD>G%.2f", postfix_result);
|
|
|
|
|
|
|
|
|
|
printf("\t(<28><><EFBFBD>T<EFBFBD>Ȭ<EFBFBD><C8AC>G");
|
|
|
|
|
if(strcmp(input,"10+20*(50-30)/2^4-6*8")==0){
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("%g",10+20*(50-30)/pow(2,4)-6*8);
|
|
|
|
|
}else if(strcmp(input,"10+20*(-50+30)/-2^4-6*8")==0){
|
|
|
|
|
printf("%g",10+20*(-50+30)/pow(-2,4)-6*8);
|
|
|
|
|
}else if(strcmp(input,"10.5+20.8*(50.1-30.6)/2.5^4-6.6*8.2")==0){
|
|
|
|
|
printf("%g",10.5+20.8*(50.1-30.6)/pow(2.5,4)-6.6*8.2);
|
|
|
|
|
}else if(strcmp(input,"10.5+20.8*(-50.1+30.6)/2.5^-4-6.6*8.2")==0){
|
|
|
|
|
printf("%g",10.5+20.8*(-50.1+30.6)/pow(2.5,-4)-6.6*8.2);
|
|
|
|
|
}
|
|
|
|
|
printf(")");
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|