+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Very Simple C Calculator

  1. #1
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Very Simple C Calculator

    This tutorial is for newbie C programmers. It uses basic skills and makes a useful program.
    Things you should know:
    how to use scanf and printf
    how to use if and while
    how to declare variables
    know about variable types

    First start your C program like normal
    Code:
    #include <stdio.h>
    
    int main()
    {
    Okay, so what what do you do now? Well, your program needs to accept input for a calculation. For that, we will use the scanf() function. Think of a simple math problem. 1+1. In C programming that is an int, a char, and another int. But wait, what about .2+.5? Those are floating point numbers in C. So the scanf code should look like this:
    Code:
    float num1, num2;
    char operation;
    scanf("%f%c%f", &num1, &operation, &num2);
    Remember: Use the & sign because scanf needs to point to the variable!
    This will define two floating point numbers and an operator. Now your program has to compute that. Add this line of code:
    Code:
    if (operation == '+')
    printf("%f\n", num1+num2);
    Remember: Use == not = when comparing things!
    This code says: if char operation equals plus sign then print floating point number num1+num2. Now that you know that, the rest is easy:
    Code:
    if (operation == '-')
    printf("%f\n", num1-num2);
    if (operation == '*')
    printf("%f\n", num1*num2);
    if (operation == '/')
    printf("%f\n", num1/num2);
    We did the same thing, but added subtraction, multiplication, and division.
    Now the program does all of that only 1 time. We want to keep entering more problems for the computer to solve. The solution: put the all of the scanf code and the if code into a while loop.
    Code:
    while (1) {
    scanf("%f%c%f", &num1, &operation, &num2);
    // this is where the rest of the code is
    printf("%f\n", num1/num2);
    }
    We use while (1) because we want to loop forever. As long as the user has math problems, we will keep accepting them until they click X. Now lets finish the program:
    Code:
    return 0;
    }
    Exit with success. Now we are done! Let's see what all the code looks like at the same time:
    Code:
    #include <stdio.h>
    
    int main()
    {
        float num1, num2;
        char operation;
        while (1) {
            scanf("%f%c%f", &num1, &operation, &num2);
            if (operation == '+')
                printf("%f\n", num1+num2);
            if (operation == '-')
                printf("%f\n", num1-num2);
            if (operation == '*')
                printf("%f\n", num1*num2);
            if (operation == '/')
                printf("%f\n", num1/num2);
        }
        return 0;
    }
    Your code should look like that, if it doesn't, fix your mistakes.
    Compile your calculator and try it out! It should work fine, just remember it can only do one calculation at a time.
    This calculator is very simple, I will post a more advanced calculator tutorial later if this was too easy.
    Last edited by Guest; 08-11-2010 at 11:33 AM.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Very Simple C Calculator

    Simple and does the job. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Very Simple C Calculator

    Nice. What if you wanted to expand it to include more operations? How about the value from a previous calculation is used in the next calculation?

  5. #4
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Very Simple C Calculator

    I will have a more advanced tutorial later that explains all of those things and more.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  6. #5
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Very Simple C Calculator

    Good Tutorial +rep

  7. #6
    Jordan Guest

    Re: Very Simple C Calculator

    Good tutorial, simple and works. +rep

  8. #7
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Re: Very Simple C Calculator

    interesting thanks!

  9. #8
    heatblazer is offline Newbie
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Re: Very Simple C Calculator

    Actually if you want to create a real calc ex. 1+1+2-2+3+3-1-1-1-1-.... etc. You should use a string like if you look at the calculator screen, no more than 12 digits for example, these digits will be chars with atoi() function to get the numerical value of the strings, also you can exclude all chars to be entered by the user except '+' '-' '*' and '\'. I am not 100% sure right now how to do it but coding a real calc is not an easy job, you may want to use a XOR function for exclusive OR which can refer to TRUE when a=1 && b=0 or a=0 or b=1... it`s simple
    int xor_demo(int a, int b)
    {
    return (a || b) && !(a && b); // returns a or b if either is 1(true) or returns FALSE if both are 0=false or both are 1=true
    }

  10. #9
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Very Simple C Calculator

    Yeah, I was going to have a more advanced tutorial, but I never got around to it. You have the right idea with getting string input, but I would avoid atoi() personally. Research the sscanf() function. It's like scanf(), but it takes in a string as input, and it let's you do error checking! strtol() may also be worth checking out.

    I may write the bigger and better tutorial after all. If you want, I can send the link to you through PM when it's done.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  11. #10
    heatblazer is offline Newbie
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Re: Very Simple C Calculator

    Sure thing, always interested in C codes since I am learning more and more! I`ll try to figure out some code too. I know about sscanf. I stuck to atoi() for ints and atof() for floats however there might be many solutions for doing a calc.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. Simple calculator programming help.
    By ivan711 in forum C and C++
    Replies: 11
    Last Post: 06-14-2011, 03:51 AM
  2. a simple calculator in C please helppp
    By nexaas in forum C and C++
    Replies: 9
    Last Post: 05-30-2010, 10:42 AM
  3. Simple GTK# calculator in mono - help please
    By DrSpaceman in forum C# Programming
    Replies: 3
    Last Post: 05-28-2010, 02:48 PM
  4. Simple Calculator Not Outputting Result
    By Maxxamo in forum PHP Development
    Replies: 5
    Last Post: 01-10-2010, 08:46 AM
  5. How to implement a simple calculator?
    By Uninverted in forum General Programming
    Replies: 5
    Last Post: 04-01-2008, 06:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts