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
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:#include <stdio.h> int main() {
Remember: Use the & sign because scanf needs to point to the variable!Code:float num1, num2; char operation; scanf("%f%c%f", &num1, &operation, &num2);
This will define two floating point numbers and an operator. Now your program has to compute that. Add this line of code:
Remember: Use == not = when comparing things!Code:if (operation == '+') printf("%f\n", num1+num2);
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:
We did the same thing, but added subtraction, multiplication, and division.Code:if (operation == '-') printf("%f\n", num1-num2); if (operation == '*') printf("%f\n", num1*num2); if (operation == '/') printf("%f\n", num1/num2);
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.
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:while (1) { scanf("%f%c%f", &num1, &operation, &num2); // this is where the rest of the code is printf("%f\n", num1/num2); }
Exit with success. Now we are done! Let's see what all the code looks like at the same time:Code:return 0; }
Your code should look like that, if it doesn't, fix your mistakes.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; }
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)
Simple and does the job. +rep![]()
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?
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)
Good tutorial, simple and works. +rep
interesting thanks!
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
}
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)
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.
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks