-
C] A operate B 형 사칙연산 계산기Study/C 2009. 10. 14. 17:59This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <stdio.h> #include <ctype.h> #include <stdlib.h> #define MAXLINE 100 void read_in(char s[]); int calc(char *s); int main() { char line[MAXLINE]; read_in(line); printf("Result = %d\n", calc(line)); return 0; } void read_in(char s[]) { int c, i=0; printf("Input an equation: "); while ((c=getchar()) != '\n') s[i++]=c; s[i]='\0'; } int calc(char *s) { int num1=0, num2=0, ret; char c, op; c=*s++; while (isdigit(c)) { num1=10*num1+(c-'0'); c=*s++; } op=c; c=*s++; while (isdigit(c)) { num2=10*num2+(c-'0'); c=*s++; } switch (op) { case '+' : ret = num1+num2; break; case '-' : ret = num1-num2; break; case '*' : ret = num1*num2; break; case '/' : ret = num1/num2; break; default: printf("error: unknown operator %c\n", op); exit(1); } return ret; } num1 op num2 만 계산되므로,
stack 를 이용한 infix to postfix 를 적용한 후,
깔삼한 계산기를 두들겨 볼까,,
반응형'Study > C' 카테고리의 다른 글
C] AVL Tree demo (0) 2009.12.14 C] 문자열 버블정렬 (0) 2008.06.02 C] 15행 파스칼 삼각형 (0) 2008.06.02 C] 배열원소 비교 함수 (0) 2008.05.26 C] 2차원 배열의 최대원소와 최소원소를 찾기 (0) 2008.05.25