zoukankan      html  css  js  c++  java
  • calculator

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef enum
    {
        FALSE = 0,
        TRUE
    }BOOL;
    
    void calculator(void);
    
    int main(void)
    {
        char y = ' ';
        y = 'y';
        while('y' == y)
        {
            printf("start calculator:
    ");
            calculator();
            printf("restart?,printf 'y' to continue
    ");
            scanf("  %c",&y);
            printf("%c
    ",y);
        }
        return 0;
    }
    
    void calculator(void)
    {
        char c;
        char ucOperator = ' ';
        int  k = 1;
        int value = 0;
        int lastValue = 0;
        int result = 0;
        int lastk;
    
        printf("start calculator input:
    ");
    /*    while((c = getchar()) == '
    '||c == ' ')
        {
            ;
        }
    //this does work,the below way is more simple
    */
        scanf(" %c",&c);    //filter space and return
        do{
            if(c >= '0' && c <= '9')
            {
                if(!value)
                {
                    value = c - '0';
                }
                else
                {
                    value = value*10 + c- '0';
                }
                continue;
            }
    
    
            if('+' == c)
            {
                lastk = k;
                k =  1;
            }
            else
            {
                if('-' == c)
                {
                    lastk = k;
                    k = -1;
                    c = '+';
                }
            }
    
             switch(c)
             {
             case '+':
                 switch(ucOperator)
                 {
                 case '*':
                     result +=  lastValue * value * lastk;
                     break;
                 case '/':
                     result +=  lastValue / value * lastk;
                     break;
                 case '+':
                     result += value * lastk;
                     break;
                 case ' ':
                     result += value;
                     break;
                 default:
                     break;
                 }
                  ucOperator = '+';
                 break;
    
             case '*':
                 lastValue = value;
    
                  ucOperator = '*';
                 break;
             case '/':
                 lastValue = value;
    
                  ucOperator = '/';
                 break;
             case '=':
                 if('+' == ucOperator)
                 {
                     result += value * k;
                 }
                 if('*' == ucOperator)
                 {
                     result += value * lastValue * k;
                 }
                 if('/' == ucOperator)
                 {
                     result += lastValue / value * k;
                 }
                 printf("%d
    ",result);
                 result = 0;
                 ucOperator = ' ';
                 break;
             default:
                 break;
             }
             value = 0;
        }while((c = getchar()) != '
    ');
    }
  • 相关阅读:
    android 之 ListView相关
    android 之 菜单
    android 之 Dialog
    android 之 View
    android 之 service
    android 之 Intent、broadcast
    Service Broadcast简单音乐播放功能
    剑指offer面试题43:n个筛子的点数
    C# LINQ语法
    C# Linq
  • 原文地址:https://www.cnblogs.com/aprilapril/p/5399498.html
Copyright © 2011-2022 走看看