An improved calculator : Calculator « Small Application « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C / ANSI-C » Small Application » CalculatorScreenshots 
An improved calculator
An improved calculator

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/
/*  */
#include <stdio.h>       /* Standard input/output                    */
#include <string.h>      /* For string functions                     */
#include <ctype.h>       /* For classifying characters               */
#include <stdlib.h>      /* For converting strings to numeric values */
#include <math.h>        /* For power() function                     */

void main()
{
   char input[256];        /* Input expression                  */
   char number_string[30]/* Stores a number string from input */
   char op = 0;            /* Stores an operator                */

  unsigned int index = 0/* Index of the current a character in input */
  unsigned int to = 0;    /* To index for copying input to itself      */         
  size_t input_length = 0/* Length of the string in input      */  
  unsigned int number_length = 0/* Length of the string in number_string */
  double result = 0.0;            /* The result of an operation        */        
  double number = 0.0;            /* Stores the value of number_string  */

   printf("\nTo use this calculator, enter any expression with"
                                           " or without spaces");
   printf("\nAn expression may include the operators:");
   printf("\n           +, -, *, /, %%, or ^(raise to a power).");
   printf("\nUse = at the beginning of a line to operate on ");
   printf("\nthe result of the previous calculation.");
   printf("\nUse quit by itself to stop the calculator.\n\n");

   /* The main calculator loop */
   while(strcmp(gets(input)"quit"!= 0)
   {
     input_length = strlen(input);     /* Get the input string length */

     /* Remove all spaces from the input by copy the string to itself */
     /* including the string terminating character                    */
     for(to = 0, index = ; index<=input_length ; index++)
     if(*(input+index!= ' ')          /* If it is not a space  */
       *(input+to++= *(input+index);  /* Copy the character    */

     input_length = strlen(input);  /* Get the new string length */
     index = 0;                    /* Start at the first character */

    if(input[index]== '=')    /* Is there =?         */
      index++;                /* Yes so skip over it */
    else
    {                         /* No - look for the left operand */
      /* Look for a number that is the left operand for the 1st operator */

      /* Check for sign and copy it */
      number_length = 0;      /* Initialize length      */
      if(input[index]=='+' || input[index]=='-')             /* Is it + or -?  */
        *(number_string+number_length++= *(input+index++)/* Yes so copy it */ 

      /* Copy all following digits */
      for; isdigit(*(input+index)) ; index++)           /* Is it a digit? */           
       *(number_string+number_length++= *(input+index)/* Yes - Copy it  */   

      /* copy any fractional part */
      if(*(input+index)=='.')             /* Is it decimal point? */                    
      /* Yes so copy the decimal point and the following digits */
        *(number_string+number_length++= *(input+index++);  /* Copy point */

        for; isdigit(*(input+index)) ; index++)         /* For each digit */         
          *(number_string+number_length++= *(input+index);     /* copy it */ 
      }
      *(number_string+number_length'\0';    /* Append string terminator */

      /* If we have a left operand, the length of number_string */
      /* will be > 0. In this case convert to a double so we    */
      /* can use it in the calculation                          */
      if(number_length>0)
        result = atof(number_string);  /* Store first number as result */        
    }

     /* Now look for 'op number' combinations */
     for(;index < input_length;)
     
       op = *(input+index++);     /* Get the operator */

       /* Copy the next operand and store it in number */
       number_length = 0;    /* Initialize the length  */

       /* Check for sign and copy it */
       if(input[index]=='+' || input[index]=='-')          /* Is it + or -?  */           
       *(number_string+number_length++= *(input+index++)/* Yes - copy it. */ 

       /* Copy all following digits */
       for; isdigit(*(input+index)) ; index++)      /* For each digit */           
       *(number_string+number_length++= *(input+index);   /* copy it. */   

       /* copy any fractional part */
       if(*(input+index)=='.')               /* Is is a decimal point? */
       /* Copy the  decimal point and the following digits */
         /* Copy point     */
         *(number_string+number_length++= *(input+index++);  
         for; isdigit(*(input+index)) ; index++)      /* For each digit */          
           *(number_string+number_length++= *(input+index)/* copy it. */ 
       }
       *(number_string+number_length'\0';   /* terminate string */              
  
       /* Convert to a double so we can use it in the calculation */
       number = atof(number_string);

       /* Execute operation, as 'result op= number' */
       switch(op)
       {  
          case '+':      /* Addition        */
            result += number;
            break;
          case '-':      /* Subtraction     */
            result -= number;
            break;
          case '*':      /* Multiplication  */
            result *= number;
            break;
          case '/':      /* Division         */
            /* Check second operand for zero */
            if(number == 0)   
              printf("\n\n\aDivision by zero error!\n");
            else
              result /= number;
            break;
          case '%':      /* Modulus operator - remainder  */
            /* Check second operand for zero */
            if((long)number == 0)               
              printf("\n\n\aDivision by zero error!\n");
            else
              result = (double)((long)result % (long)number);
            break;
          case '^':      /* Raise to a power              */
            result = pow(result, number);
            break;
          default:       /* Invalid operation or bad input */
            printf("\n\n\aIllegal operation!\n");
       
     }
     printf("= %f\n", result);  /* Output the result */
   }
}



           
       
Related examples in the same category
1. A calculator that allows multiple calculationsA calculator that allows multiple calculations
w__w__w___.__j__a__v__a___2__s.___c_o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.