Prefix to Postfix 코드인데요 ㅠㅠ
글쓴이: onam3125 / 작성시간: 수, 2014/03/26 - 2:27오후
#include <stdio.h> #include <string.h> #define OPERATORS "+-*/" void preToPostFix(char* preFixIn, char* exprOut); int findExprLen(char* exprIn); int main(void) { char preFixExpr[256] = "-+*ABC/EF"; char postFixExpr[256] = ""; preToPostFix(preFixExpr, postFixExpr); printf("Prefix expr : %-s\n", preFixExpr); printf("Post expr : %-s\n", postFixExpr); return 0; }//main void preToPostFix(char* preFixIn, char* postFix) { char operator [2]; char postFix1[256]; char postFix2[256]; char temp[256]; int lenPreFix; if (strlen(preFixIn) == 1) { *postFix = *preFixIn; *(postFix + 1) = '\0'; return; }//if only operand *operator = *preFixIn; *(operator + 1) = '\0'; //find 1st expression lenPreFix = findExprLen(preFixIn + 1); strncpy(temp, preFixIn + 1, lenPreFix); *(temp + lenPreFix) = '\0'; preToPostFix(temp, postFix1); //Find Second expression strcpy(temp, preFixIn + 1 + lenPreFix); preToPostFix(temp, postFix2); //Concatenate to postFix strcpy(postFix, postFix1); strcpy(postFix, postFix2); strcpy(postFix, operator); return; }//preToPostFix int findExprLen(char* exprIn) { //Local Definitions int len1; int len2; if (strcspn(exprIn, OPERATORS) == 0) //General Case : First character is operator //find length of 1st expression { len1 = findExprLen(exprIn + 1); //find length of 2nd expression len2 = findExprLen(exprin + 1 + len1); } else //base case 1st char is operand len1 = len2 = 0; return len1 + len2 + 1; }//findExprLen
Prefix to Postfix 코드입니다...
지금 알고 싶은부분이
if (strlen(preFixIn) == 1) { *postFix = *preFixIn; *(postFix + 1) = '\0'; return; }//if only operand *operator = *preFixIn; *(operator + 1) = '\0'; //find 1st expression
이 부분인데요 operator 배열 크기가 2뿐이 안되는데 postFixIn이 대입이 되나요 ;;
그리고 저게 어떻게 해서 1st expression을 찾는 식이 되는건가요 ㅠㅠ...
Forums:
숙제는 스스로 모르는것은 교수에게
숙제는 스스로
모르는것은 교수에게
*preFixIn은 한글자입니다.
*preFixIn은 한글자입니다.
댓글 달기