Thread: calculator

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question calculator

    Is it very complex to make a calculator program using C?

  2. #2
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Talking

    Can you please explain how to make one then if it isn't so complex to do so?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you please explain how to make one then if it isn't so complex to do so?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      char op;
      int a, b, result = 0;
    
      printf ( "Enter an expression (ex. +5 4): " );
    
      if ( scanf ( "%c%d%d", &op, &a, &b ) != 3 ) {
        fprintf ( stderr, "Invalid input\n" );
        exit ( EXIT_FAILURE );
      }
    
      switch ( op ) {
      case '+': result = a + b; break;
      case '-': result = a - b; break;
      case '*': result = a * b; break;
      case '/':
        if ( b == 0 ) {
          fprintf ( stderr, "Integer divide by zero\n" );
          exit ( EXIT_FAILURE );
        }
    
        result = a / b;
      }
    
      printf ( "%d %c %d = %d\n", a, op, b, result );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM