Data_Structure/作業/unit3/新增資料夾/DS3.cpp

273 lines
7.8 KiB
C++
Raw Normal View History

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 postfix_1[MAX]; //<2F><><EFBFBD>ܥ<EFBFBD>
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);
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;
}
}
//<2F><><EFBFBD>ܥ<EFBFBD>
void convert_to_postfix_forprint() {
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++) {
postfix_1[postfix_index++] = number_str[j];
}
postfix_1[postfix_index++] = ' ';
if(is_negative) {
postfix_1[postfix_index++] = '-';
postfix_1[postfix_index++] = ' ';
}
}
else if(input[i] == '(') {
stack[++top] = input[i];
}
else if(input[i] == ')') {
while(top >= 0 && stack[top] != '(') {
printf("%c ", stack[top]);
postfix_1[postfix_index++] = stack[top];
postfix_1[postfix_index++] = ' ';
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]);
postfix_1[postfix_index++] = stack[top];
postfix_1[postfix_index++] = ' ';
top--;
}
stack[++top] = input[i];
}
}
i++;
}
while(top >= 0) {
printf("%c ", stack[top]);
postfix_1[postfix_index++] = stack[top];
postfix_1[postfix_index++] = ' ';
top--;
}
postfix_1[postfix_index] = '\0';
printf("\n");
}
// === <20><EFBFBD><E0B4AB><EFBFBD><EFBFBD><EFBFBD>Ǫ<EFBFBD><C7AA>F<EFBFBD><46> ===
//<2F>p<EFBFBD><70><EFBFBD><EFBFBD>
void convert_to_postfix() {
int i = 0 , j;
int postfix_index = 0;
top = -1;
while(input[i] != '\0') {
if(isdigit(input[i]) || input[i] == '.' || (input[i] == '-' && (i == 0 || input[i-1] == '(' || is_operator(input[i-1])))) { // <20>ˬd<CBAC>O<EFBFBD>_<EFBFBD><5F><EFBFBD>Ʀr<C6A6>B<EFBFBD>p<EFBFBD><70><EFBFBD>I<EFBFBD>άO<CEAC>t<EFBFBD><74><EFBFBD>]<5D>B<EFBFBD>O<EFBFBD><4F><EFBFBD>F<EFBFBD><46><EFBFBD><EFBFBD><EFBFBD>}<7D>l<EFBFBD>A<EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD>b<EFBFBD><62><EFBFBD>A<EFBFBD><41><EFBFBD>ιB<CEB9><42><EFBFBD>ū᭱<C5AB>^
char *end;
double num = strtod(&input[i], &end); //strtod <20><><EFBFBD>r<EFBFBD><EFBFBD><EAB4AB><EFBFBD>B<EFBFBD>I<EFBFBD><49>
int len = end - &input[i]; //<2F>p<EFBFBD><70><EFBFBD>Ʀr<C6A6>r<EFBFBD><EFBFBD><EAAABA><EFBFBD><EFBFBD> <20>Ʀr<C6A6><72><EFBFBD>ݪ<EFBFBD><DDAA>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}<7D><EFBFBD><EEB1BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m
// printf("%.1f ", num);
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] != '(') {
// printf("%c ", stack[top]);
postfix[postfix_index++] = stack[top];
postfix[postfix_index++] = ' ';
top--;
}
if(top >= 0) top--;
}
else if(is_operator(input[i])) {
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]);
postfix[postfix_index++] = stack[top];
postfix[postfix_index++] = ' ';
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';
// printf("\n");
}
// === <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) {
// printf("<22>B<EFBFBD><42><EFBFBD>l '%c' <20>ʤ֨<CAA4><D6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>⤸\n", token[0]);
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];
}
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_forprint(); //<2F><><EFBFBD>ܥ<EFBFBD>
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(")");
return 0;
}