*종*
개인
판매자 정보
- 학교정보
-
입력된 정보가 없습니다.
- 직장정보
-
입력된 정보가 없습니다.
- 자격증
-
판매지수
-
- 판매중 자료수
- 3개
-
- 전체 판매량
- 14개
-
- 최근 3개월 판매량
- 0개
-
- 자료후기 점수
- 평균A
-
- 자료문의 응답률
- -
전체자료 3개
-
-
[자료구조] infix -> postfix
-
## Program Overview ##1. main 함수에서 expr에 식을 입력 받는다.2. err_check 함수에서 체크한 에러의 종류- 오퍼랜드 다음에 이어서 다시 오퍼랜드가 나올 수 없다.- 오퍼랜드는 오퍼레이터 보다 하나가 더 많아야 한다.- 왼쪽 괄호와 오른쪽 괄호의 개수는 같아야 한다.3. err_check 함수에서 리턴된 값(1: 정상 , 0: 잘못된 입력) 으로 에러 체크후 postfix 호출4. postfix 함수에서 오퍼랜드는 그냥 화면에 출력.연산자는 우선순위를 비교해 push, pop 함수를 이용, 스텍에 저장,삭제 한다.5. pop함수에서 스택에서 pop 함과 동시에 화면에 item을 출력한다.## Program Source ###include#include#include#define MAX_STACK_SIZE 50#define MAX_EXPR_SIZE 50int top = 0; // top of Stacktypedef enum { lparen, rparen, plus, minus, times, divide, operand, eos} precedence;precedence stack[MAX_STACK_SIZE]; // declare Stackchar expr[MAX_EXPR_SIZE]; // input charater arraystatic int isp[] = { 0, 19, 1, 1, 2, 2, 3, 0 }; // precedence number in stackstatic int icp[] = { 20, 19, 1, 1, 2, 2, 3, 0 }; // precedence number at store// declare functionvoid postfix(void);precedence get_token(char *symbol, int *n);void push(int *top, precedence item);precedence pop(int *top);void stack_full();precedence stack_empty();voi************************//* Main function *//* ============================================================ *//* Input: noting *//* Output: nothing *//* Purpose: call postfix, err_check function *//*********************************************************************/int main(){printf("Input : ");gets(expr);if( err_check() ) postfix(); // call postfix function after error checkreturn 0;}/*********************************************************************//* Error check function *//* ============================================================ *//* Input: noting *//* Output: int - 1 : right input *//* 0 : wrong input *//* Purpose: check wrong input *//*********************************************************************/int err_check(){int lparen_num=0 , rparen_num=0 , operand_num=0 , operator_num=0;char *p = expr; // get pointer of expr arraywhile(*p) // until end of sentence{int k = *p; // get ASCII code of characterif ( k == 40 ) lparen_num++ ; // 40 is ASCII of "("else if ( k == 4 = icp , pop stack and print tokenwhile( isp[stack[top]] >= icp[token] )print_token( pop(&top) );push(&top, token);}}while( (token = pop(&top)) != eos )print_token( token );printf("n");}/*********************************************************************//* Get_token function *//* ============================================================ *//* Input: char* -> symbol *//* int* -> n *//* Output: precedence -> symbol type *//* Purpose: get token and return type *//*********************************************************************/precedence get_token(char *symbol, int *n){*symbol = expr[(*n)++]; // get pointer of expr variableswitch(*symbol) // return type{case '(' : return lparen;case ')' : return rparen;case '+' : return plus;case '-' : return minus;case '/' : return divide;case '*' : return times;case '