Thread: calculator program

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    calculator program

    I wrote my code for calculator

    Code:
    #include<stdio.h>
    
    int main ()
    {
        int op1;
    	int op2;
    	int result;
        enum operator
    	{ plus, minus, multiply, divide}op;
    	
    	printf("Enter first Number \n");
    	scanf("%d", &op1);
    	
    	printf("Enter operator \n");
    	scanf("%d", &op);
    	
    	printf("Enter second Number \n");
    	scanf("%d", &op2);
    	
    	switch (op)
    	{
    		case plus : 
    		        result = (op1 + op2);
    		        printf ("result : %d \n", result); 
    				break;
    		case minus : 
    		        result = (op1 - op2);
    		        printf ("result : %d \n", result);
    				break;
    				
    		case multiply : 
    		        result = (op1 * op2);
    		        printf ("result : %d \n", result); 
    				break;
    		case divide : 
    		        result = (op1 / op2);
    		        printf ("result : %d \n", result);
    				break;
    	}
        
    	return 0;
    }
    Enter first Number
    3
    Enter operator
    0
    Enter second Number
    2
    result : 5

    I do not see any problem in my code but can I make it better. mainly in operator I have to enter 0,1, 2, 3

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    297
    > I do not see any problem in my code but can I make it better.

    I believe not. It depends on what you want to "do" with your calculator program, what your target audience is and why do you think it's important to code a calculator. Also, if I were to be using such a calculator, I would like to type "1+1" or "1 + 1" instead of "1 0 1" to get the result. Think about how modern calculator apps tackle parsing problems. Maybe, coding a simple parser should be your next homework, if you'd like to do it.

    Also, signed integers range from "2^-31" to "2^31 - 1" on most systems where size of integer is 4 bytes. What if I wanted to do some bigger computation, which in many cases of my chemistry equilibrium problems I need to? How would you tackle overflow/underflow issues without using Python or Java to make your calculator program?

    That said, I'd like to add that this sort of program can be coded by any kid in their third grade too so long as they understand programming fundamentals and bare-minimum coding, so you should amp it up a bit and make coding a calculator a difficult task for yourself and dive into the probable issues that your program may face when end users would like to perform complex/tedious calculations, unless of-course you're a third grader ;)
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    928
    Code:
    #include <stdio.h>
    #include <stdbool.h>
     
    bool calc()
    {
        int a, b, c = 0;
        char o;
        printf("> ");
        if (scanf("%d %c %d", &a, &o, &b) != 3) return false;
        switch (o)
        {
        case '+': c = a + b; break;
        case '-': c = a - b; break;
        case '*': c = a * b; break;
        case '/': c = a / b; break;
        case '%': c = a % b; break;
        default:
            printf("Unknown operator.\n");
            return true;
        }
        printf("= %d\n", c);
        return true;
    }
     
    int main()
    {
        printf("Numbers must be integers.\n"
               "Operators: +, -, *, /, %%\n"
               "Enter NUMBER OPERATOR NUMBER\n"
               "Enter just q to quit.\n");
     
        while (calc()) ;
     
        return 0;
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator-like program
    By leroyjenkens in forum C Programming
    Replies: 4
    Last Post: 06-09-2012, 10:11 AM
  2. Calculator program
    By A34Chris in forum C Programming
    Replies: 18
    Last Post: 05-22-2012, 12:54 PM
  3. Help with Tax calculator program
    By Mshock in forum C++ Programming
    Replies: 1
    Last Post: 06-28-2006, 03:28 PM
  4. C++ Program Help (MPG calculator)
    By mrwooter in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2005, 01:46 PM
  5. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM

Tags for this Thread