#include #include #include #define MAX 100 char change_postfix(char arr[MAX], char Queue[MAX]); int calculate(unsigned stack2[MAX], char Queue[MAX]); int count = 0; int count2 = 0; int count3 = 0; int main(void){ unsigned stack2[MAX] = { 0 }; //°è»êÇÒ¶§ char·Î °è»êÀÌ ¾ÈµÇ±â ¶§¹®¿¡ unsigned »ç¿ë char insert; char arr[MAX] = { 0 }; char Queue[MAX] = { 0 }; // ÀÔ·Â while (1){ printf("½Ä ÀÔ·Â"); scanf("%s", &insert); if (insert == '='){ break; } arr[count] = insert; count++; } //ÈÄÀ§ Ç¥±â¹ýÀ¸·Î º¯°æ change_postfix(arr, Queue); //ÈÄÀ§ Ç¥±â¹ý Ãâ·Â for(int i =0; i < count2; i++){ printf("%c ", Queue[i]); } printf("\n"); //°è»ê int result = calculate(stack2, Queue); //°è»ê °á°ú°ª Ãâ·Â printf("%d", result); } char change_postfix(char arr[MAX], char Queue[MAX]){ int check = 0; int beforestack = 0; int gwalhocheck = 0; char stack[MAX] = { 0 }; while(1){ if( check == count){ break; } if((arr[check] <= '9') && (arr[check]> '0')){ Queue[count2] = arr[check]; check++; count2++; } else if(arr[check] == '('){ gwalhocheck++; check++; while(1){ if(arr[check] == ')'){ gwalhocheck--; check++; while(1){ if(count3 == beforestack){ break; } Queue[count2] = stack[count3 -1]; count2++; count3--; } break; } if((arr[check] < 58) && (arr[check] > 48)){ Queue[count2] = arr[check]; check++; count2++; if( (stack[count3 -1] == '*') || (stack[count3 -1] == '/')){ Queue[count2] = stack[count3 -1]; stack[count3 -1] = NULL; count3 --; count2++; } } else if((arr[check] == '+') || (arr[check] == '*') || (arr[check] == '-') || (arr[check] == '/')){ stack[count3] = arr[check]; beforestack = count3; count3++; check++; } } } else if((arr[check] == '+') || (arr[check] == '*') || (arr[check] == '-') || (arr[check] == '/')){ stack[count3] = arr[check]; count3++; check++; } if((Queue[count2-1] > 48) && (gwalhocheck == 0) && (count2 > 1)){ if((arr[check] == '*') || (arr[check] == '/')){ stack[count3] = arr[check]; count3++; check++; } else{ while(1){ if(count3 == 0){ break; } Queue[count2] = stack[count3 -1]; count2++; count3--; } } } } return Queue[MAX]; } int calculate(unsigned stack2[MAX], char Queue[MAX]){ int cal1 = 0; int cal2 = 0; int count3 = 0; int check = 0; for(int i = 0; i < count2; i++){ if((Queue[i] <= '9') && (Queue[i]> '0')){ stack2[count3] = Queue[i]; count3++; }else if(((Queue[i] == '+') || (Queue[i] == '*') || (Queue[i] == '-') || (Queue[i] == '/'))){ count3--; cal1 = stack2[count3] -48; stack2[count3] = NULL; count3--; cal2 = stack2[count3] -48; stack2[count3] = NULL; if(Queue[i] == '+'){ stack2[count3] = (cal1 + cal2) + 48; count3++; }else if(Queue[i] == '*'){ stack2[count3] = (cal1 * cal2) + 48; count3++; }else if(Queue[i] == '/'){ stack2[count3] = (cal1 / cal2) + 48; count3++; }else if(Queue[i] == '-'){ stack2[count3] = (cal1 - cal2) + 48; count3++; } } } int result = stack2[0] - 48; return result; }