打印计算器

  1. S 运算符为设值功能,E 运算符为终止程序功能。

//print calculator

import

int main(int argc, char *argv[])
{
@autoreleasepool
{
double accumulator=0, number ;
char operator ;
while(1)
{

    //input
    NSLog(@"Type in your expression:") ;
    scanf("%lf %c", &number, &operator) ;
    
    //judge
        if ( operator == '+' )
            accumulator += number ;
    
        if ( operator == '-' )
            accumulator -= number ;

        if ( operator == '*' )
            accumulator *= number ;
    
        if ( operator == '/' )
        {
            if ( number != 0 )
                accumulator /= number ;
            else
                NSLog(@"除数为0!程序中断!") ;
        }
    
        if ( operator == 'S' )
            accumulator = number ;
            
        if ( operator == 'E' )
        {
            NSLog(@"终止运行程序!") ;
            break ;
        }
        
        //view
        NSLog(@"= %lf", accumulator) ;
    }
}
return 0 ;

}

你可能感兴趣的:(打印计算器)