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();
|
|
|
|
|
double evaluate_infix(char *expr);
|
|
|
|
|
double parse_number(char **expr);
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
// === <20>ˬd<CBAC>B<EFBFBD><42><EFBFBD>l<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ===
|
|
|
|
|
//int get_priority(char op) {
|
|
|
|
|
// switch(op) {
|
|
|
|
|
// case '+': case '-': return 1;
|
|
|
|
|
// case '*': case '/': return 2;
|
|
|
|
|
// case '^': return 3;
|
|
|
|
|
// default: return 0;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2025-01-20 21:25:33 +08:00
|
|
|
|
int get_priority(char c){
|
|
|
|
|
if(c=='+' || c=='-'){
|
|
|
|
|
return 1;
|
|
|
|
|
}else if(c=='*' || c=='/'){
|
|
|
|
|
return 2;
|
|
|
|
|
}else if(c=='^'){
|
|
|
|
|
return 3;
|
|
|
|
|
}else{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// === <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>ѪR<D1AA>Ʀr<C6A6>]<5D>]<5D>A<EFBFBD>B<EFBFBD>I<EFBFBD>ơ^ ===
|
|
|
|
|
double parse_number(char **expr) {
|
|
|
|
|
char *end;
|
|
|
|
|
double result = strtod(*expr, &end);
|
|
|
|
|
*expr = end;
|
|
|
|
|
return result;
|
2025-01-20 21:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
// === <20>ഫ<EFBFBD><E0B4AB><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
|
2025-01-20 21:25:33 +08:00
|
|
|
|
void convert_to_postfix() {
|
|
|
|
|
int i = 0 , j;
|
|
|
|
|
int postfix_index = 0;
|
|
|
|
|
top = -1;
|
2025-01-20 21:30:53 +08:00
|
|
|
|
|
|
|
|
|
// printf("<22><><EFBFBD>Ǫ<EFBFBD><C7AA>ܪk<DCAA>G");
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
while(input[i] != '\0') {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
if(isdigit(input[i]) || input[i] == '.' || (input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) {
|
2025-01-20 21:25:33 +08:00
|
|
|
|
char *end;
|
2025-01-20 21:30:53 +08:00
|
|
|
|
double num = strtod(&input[i], &end);
|
|
|
|
|
int len = end - &input[i];
|
|
|
|
|
printf("%.1f ", num);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
for(j = 0; j < len; j++) {
|
|
|
|
|
postfix[postfix_index++] = input[i+j];
|
|
|
|
|
}
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
|
|
|
|
i += len - 1;
|
|
|
|
|
}
|
|
|
|
|
else if(input[i] == '(') {
|
|
|
|
|
stack[++top] = input[i];
|
|
|
|
|
}
|
|
|
|
|
else if(input[i] == ')') {
|
|
|
|
|
while(top >= 0 && stack[top] != '(') {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("%c ", stack[top]);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
postfix[postfix_index++] = stack[top];
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
|
|
|
|
top--;
|
|
|
|
|
}
|
|
|
|
|
if(top >= 0) top--;
|
|
|
|
|
}
|
|
|
|
|
else if(is_operator(input[i])) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
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:25:33 +08:00
|
|
|
|
postfix[postfix_index++] = stack[top];
|
|
|
|
|
postfix[postfix_index++] = ' ';
|
|
|
|
|
top--;
|
|
|
|
|
}
|
|
|
|
|
stack[++top] = input[i];
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(top >= 0) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
printf("%c ", stack[top]);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
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, " ");
|
|
|
|
|
|
|
|
|
|
// printf("\n<>p<EFBFBD><70><EFBFBD>L<EFBFBD>{<7B>G\n");
|
|
|
|
|
|
|
|
|
|
while(token != NULL) {
|
|
|
|
|
// printf("<22>B<EFBFBD>z<EFBFBD>аO<D0B0>G'%s'\n", token);
|
|
|
|
|
|
|
|
|
|
if(isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
|
|
|
|
|
double num = atof(token);
|
|
|
|
|
numStack[++numTop] = num;
|
|
|
|
|
// printf("<22><><EFBFBD>J<EFBFBD>Ʀr<C6A6>G%.2f\n", num);
|
|
|
|
|
}
|
|
|
|
|
else if(is_operator(token[0])) {
|
|
|
|
|
if(numTop < 1) {
|
2025-01-20 21:30:53 +08:00
|
|
|
|
// printf("<22><><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;
|
|
|
|
|
// printf("<22>p<EFBFBD><70><EFBFBD>G%.2f %c %.2f = %.2f\n", a, token[0], b, result);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// printf("<22><><EFBFBD>~<7E>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>аO '%s'\n", token);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// printf("<22><><EFBFBD>e<EFBFBD><65><EFBFBD>|<7C>G");
|
|
|
|
|
for(i = 0; i <= numTop; i++) {
|
|
|
|
|
// printf("%.2f ", numStack[i]);
|
|
|
|
|
}
|
|
|
|
|
// printf("\n\n");
|
|
|
|
|
|
|
|
|
|
token = strtok(NULL, " ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(numTop != 0) {
|
|
|
|
|
// printf("<22><><EFBFBD>~<7E>G<EFBFBD>p<EFBFBD><EFBFBD><E2B5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|<7C><><EFBFBD>Ѿl %d <20>Ӥ<EFBFBD><D3A4><EFBFBD>\n", numTop + 1);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return numStack[numTop];
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 21:30:53 +08:00
|
|
|
|
// === <20><><EFBFBD><EFBFBD><EFBFBD>p<EFBFBD>⤤<EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
|
|
|
|
|
double evaluate_infix(char *expr) {
|
|
|
|
|
char opStack[MAX];
|
|
|
|
|
double valStack[MAX];
|
|
|
|
|
int opTop = -1;
|
|
|
|
|
int valTop = -1;
|
|
|
|
|
|
|
|
|
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>ǭp<EFBFBD><EFBFBD><EFBFBD>L<EFBFBD>{<7B>G\n");
|
|
|
|
|
|
|
|
|
|
while(*expr) {
|
|
|
|
|
if(isspace(*expr)) {
|
|
|
|
|
expr++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(isdigit(*expr) || *expr == '.' || (*expr == '-' && (valTop == -1 || opStack[opTop] == '('))) {
|
|
|
|
|
double num = parse_number(&expr);
|
|
|
|
|
valStack[++valTop] = num;
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ʀr: %f\n", num);
|
|
|
|
|
}
|
|
|
|
|
else if(*expr == '(') {
|
|
|
|
|
opStack[++opTop] = *expr++;
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD>\n");
|
|
|
|
|
}
|
|
|
|
|
else if(*expr == ')') {
|
|
|
|
|
printf("<EFBFBD>B<EFBFBD>z<EFBFBD>k<EFBFBD>A<EFBFBD><EFBFBD>\n");
|
|
|
|
|
while(opTop >= 0 && opStack[opTop] != '(') {
|
|
|
|
|
char op = opStack[opTop--];
|
|
|
|
|
double b = valStack[valTop--];
|
|
|
|
|
double a = valStack[valTop--];
|
|
|
|
|
valStack[++valTop] = calculate(a, b, op);
|
|
|
|
|
printf("<EFBFBD>p<EFBFBD><EFBFBD>: %f %c %f = %f\n", a, op, b, valStack[valTop]);
|
|
|
|
|
}
|
|
|
|
|
if(opTop >= 0) opTop--; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><41>
|
|
|
|
|
expr++;
|
|
|
|
|
}
|
|
|
|
|
else if(is_operator(*expr)) {
|
|
|
|
|
while(opTop >= 0 && opStack[opTop] != '(' &&
|
|
|
|
|
(get_priority(opStack[opTop]) > get_priority(*expr) ||
|
|
|
|
|
(get_priority(opStack[opTop]) == get_priority(*expr) && *expr != '^'))) {
|
|
|
|
|
char op = opStack[opTop--];
|
|
|
|
|
double b = valStack[valTop--];
|
|
|
|
|
double a = valStack[valTop--];
|
|
|
|
|
valStack[++valTop] = calculate(a, b, op);
|
|
|
|
|
printf("<EFBFBD>p<EFBFBD><EFBFBD>: %f %c %f = %f\n", a, op, b, valStack[valTop]);
|
|
|
|
|
}
|
|
|
|
|
opStack[++opTop] = *expr++;
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>B<EFBFBD><EFBFBD><EFBFBD>l: %c\n", *(expr-1));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
expr++; // <20><><EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD>r<EFBFBD><72>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while(opTop >= 0) {
|
|
|
|
|
char op = opStack[opTop--];
|
|
|
|
|
double b = valStack[valTop--];
|
|
|
|
|
double a = valStack[valTop--];
|
|
|
|
|
valStack[++valTop] = calculate(a, b, op);
|
|
|
|
|
printf("<EFBFBD>p<EFBFBD><EFBFBD>: %f %c %f = %f\n", a, op, b, valStack[valTop]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return valStack[valTop];
|
|
|
|
|
}
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
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:30:53 +08:00
|
|
|
|
// printf("\n=== <20><><EFBFBD>Ǫ<EFBFBD><C7AA><EFBFBD><EFBFBD>p<EFBFBD><70> ===\n");
|
|
|
|
|
// double infix_result = evaluate_infix(input);
|
|
|
|
|
// printf("<22><><EFBFBD>ǭp<C7AD><EFBFBD>G<EFBFBD>G%f\n", infix_result);
|
2025-01-20 21:25:33 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|