접미사 접미사 표현식 및 값 구하기
OutPut:
The init formula:3+4*5+(6*7+8)*9 The stack is empty. Convert result:345*+67*8+9*+ Calculate result:473
  
  
  
  
   
   
   
   - //Code by Pnig0s1992  
- //Date:2012,3,21  
- #include <stdio.h>  
- #include <Windows.h>  
- #include "Header.h"  
-  
- #define MAXLENGTH 50  
-  
- VOID ConvertAndCalc(LPSTR lpContainer,Stack S);  
- int Caculate(LPSTR lpContainer,Stack S);  
- int getOptPriv(CHAR cOpt);  
-  
- int main(int argc,char **argv)  
- {  
-     LPSTR lpContainer = "3+4*5+(6*7+8)*9";  
-     Stack myStack = CreateStack();  
-     printf("
 The init formula:%s",lpContainer);
-     ConvertAndCalc(lpContainer,myStack);  
-     system("pause");  
-     return 0;  
- }  
-  
- int getOptPriv(CHAR cOpt)  
- {  
-     switch(cOpt)  
-     {  
-     case '(':  
-         return 7;  
-     case ')':  
-         return 7;  
-     case '*':  
-         return 5;  
-     case '/':  
-         return 5;  
-     case '+':  
-         return 3;  
-     case '-':  
-         return 3;  
-     default:  
-         return 0;  
-     }  
- }  
-  
- VOID ConvertAndCalc(LPSTR lpContainer,Stack S)  
- {  
-     LPSTR lpResult = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH);  
-     LPSTR lpTemp = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH);  
-     while(*lpContainer != '\0')  
-     {  
-         if(isalnum(*lpContainer))  
-         {  
-             sprintf(lpTemp,"%c",*lpContainer);  
-             strcat(lpResult,lpTemp);  
-             ZeroMemory(lpTemp,MAXLENGTH);  
-         }else 
-         {  
-             if(isEmpty(S))  
-             {  
-                 Push(*lpContainer,S);  
-             }else if (*lpContainer == ')')  
-             {  
-                 while(!isEmpty(S) && getTop(S) != '(')  
-                 {  
-                     sprintf(lpTemp,"%c",Pop(S));  
-                     strcat(lpResult,lpTemp);  
-                     ZeroMemory(lpTemp,MAXLENGTH);  
-                 }  
-                 Pop(S);  
-             }else 
-             {  
-                 if(getOptPriv(getTop(S)) < getOptPriv(*lpContainer))  
-                 {  
-                     Push(*lpContainer,S);  
-                 }else 
-                 {  
-                     while((!isEmpty(S)) && (getOptPriv(getTop(S)) >= getOptPriv(*lpContainer)) && getTop(S) != '(')  
-                     {  
-                         sprintf(lpTemp,"%c",Pop(S));  
-                         strcat(lpResult,lpTemp);  
-                         ZeroMemory(lpTemp,MAXLENGTH);  
-                     }  
-                     Push(*lpContainer,S);  
-                 }  
-             }  
-         }  
-         lpContainer++;  
-     }  
-     while(!isEmpty(S))  
-     {  
-         sprintf(lpTemp,"%c",Pop(S));  
-         strcat(lpResult,lpTemp);  
-         ZeroMemory(lpTemp,MAXLENGTH);  
-     }  
-     MakeEmpty(S);  
-     printf("
 Convert result:%s",lpResult);
-     int iResult = Caculate(lpResult,S);  
-     printf("
 Calculate result:%d",iResult);
-     return;  
- }  
-  
- int Caculate(LPSTR lpContainer,Stack S)  
- {  
-     int iTemp;  
-     int iResult;  
-     while(*lpContainer != '\0')  
-     {  
-         if(isdigit(*lpContainer))  
-         {  
-             LPSTR lpTemo = (LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH);  
-             sprintf(lpTemo,"%c",*lpContainer);  
-             Push(atoi((const char *)lpTemo),S);  
-         }else 
-         {  
-             switch(*lpContainer)  
-             {  
-             case '+':  
-                 Push(Pop(S)+Pop(S),S);  
-                 break;  
-             case '-':  
-                 Push(Pop(S)-Pop(S),S);  
-                 break;  
-             case '*':  
-                 Push(Pop(S)*Pop(S),S);  
-                 break;  
-             case '/':  
-                 Push(Pop(S)/Pop(S),S);  
-                 break;  
-             default:  
-                 break;  
-             }  
-         }  
-         lpContainer++;  
-     }  
-     return Pop(S);  
- } 
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
양식 제출 후 제출 버튼 비활성화텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.