본문 바로가기
C 언어

C 언어를 이용한 계산기 (2)

by treeCoder 2021. 1. 20.

C 언어를 이용한 계산기의 version 2를 만들어 보았다.

달라진 점은, 에러 메세지 관련된 곳을 좀 더 수정하였고, unary minus 부분의 기능을 추가하였다.,

#include <stdio.h>
#include <stdlib.h>
  
char *x;
  
double calc();
double add();
double mult();
double factor();
double parseNumber();
  
double add() {
    double pro1 = mult();
    while( *x && *x == ' ' ) x++; //skip spaces
    
    while( *x  ==  '+' || *x  ==  '-') {
        char op  =  *x++;
        double pro2  =  mult();
  
        while( *x && *x == ' ' ) x++; //skip spaces
  
        if(op == '+') pro1  =  pro1 + pro2;
        if(op == '-') pro1  =  pro1 - pro2;
    }
    
    return pro1;
}
  
double mult() {
    double fac1 = factor();
    while( *x && *x == ' ' ) x++; //skip spaces
    
    while( *x == '*' || *x == '/') {
        char op  =  *x++;
        double fac2 = factor();
  
        while( *x && *x == ' ' ) x++; //skip spaces
  
        if(op ==  '*') fac1  =  fac1 * fac2;
        if(op ==  '/') {
           if( fac2 == 0.0 ) {
               puts("Error: division by zero.");
               exit(0);
           }
           fac1  =  fac1 / fac2;
        }
    }
    
    return fac1;
}
  
double factor() {
  
    while( *x && *x == ' ' ) x++; //skip spaces
  
    if( *x  ==  '-' ) {
  
       x++; // Consume -
       if( *x == ' ' ) {
           printf("space unexpected after unary operator");
           exit(0);
       }
  
       return -factor();
  
    } else if( *x >= '0' && *x <= '9' ) {
  
         return parseNumber();
  
    } else if( *x ==  '(' ) {
  
         x++; // Consume (
         double sum  =  add();
  
         if( *x == ')' ) 
             x++; // Consume )
         else {
             printf("expected \')\' but found \'%c\'\n", *x);
             exit(0);
         }
  
         return sum;
  
    } else {
  
         printf("unexpected \'%c\'", *x);
         exit(0);
  
    }
}
  
double parseNumber() {
  
      double number  =  0;
  
      while( *x >= '0' && *x <=  '9' ) {
  
           number = number * 10;
           number = number + *x - '0';
           x++;
  
      }
      
      if( *x == '.') {
          x++; // Consume .
          double weight  =  1;
  
          while( *x >= '0' && *x <=  '9' ) {
              weight = weight / 10;
              double scaled  =  ( *x - '0' ) * weight;
              number = number + scaled;
              x++;
          }
      }
      
      return number;
}
  
double calc() {
  
      double result = add();
  
      if( *x == '\0' ) {
         return result;
      }
  
      printf("expected end of input but found \'%c\'\n", *x);
      exit(0);
}
  
int main(int argc, char * argv[] ) {
    //char s[512];
  
    //s[0] = '\0';
  
    //fgets(s, sizeof(s)-1, stdin);
    
    if( argc != 2 ) {
        printf("\n Enter  numbers as follows. \n\n");
        printf("     %s \" 1  +  32 * 3 -7.5\"\n", argv[0]);
        printf("     %s \" ( 15.3  +  2.4 ) * 23 \"\n", argv[0]);
        printf("     %s \" ( 2.1  -  2.7) * 48.7 \"\n", argv[0]);

        exit(0);
    }
  
    x = argv[1];
    double ans = calc();
    printf("%f", ans);
  
    return 0;
}

 

실행 결과는 아래와 같다.

 

댓글