In today's project, we'll be building a pretty basic calculator. Later improving upon it, adding more operations, performing operations on more than 2 numbers, adding multiple modes, and perhaps a graphing calculator later!
Keeping faithful to my modular "increment improvements" philosophy, we will be working to release the most basic of functionalities required to call our creation a "calculator".
Click on the Github links below to gain access to the codes used:
https://github.com/CyberTHX1138/CalculatorBelow is most likely the most basic mathematical code of all in C, the addition of an integer to another. As of now, the addition function is as bare as it gets. It only accepts 2 integers and integers only, not floats. In the next iteration, I'll either add the ability to perform operations on more than 2 numbers, or add more mathematical operations and organize the formatting more. Below is the code snippet concerned with the addition function:
int addition(int num1, int num2) { int result = num1 + num2; printf("%d\n", result); }In the snippet below concerned with the division functionality, we implement a conditional statement that handles dividing by 0. If num2 is assigned the integer value of 0, then we'll print out "Cannot divide by 0!". If the user is not dividing by 0, then we proceed as normal with our operation.
Below is the snippet of code for subtraction, and it follows the same logic as the snippet above, except the operation performed is (-):
int subtraction(int num1, int num2) { int result = num1 - num2; printf("%d\n", result); }
int multiplication(int num1, int num2) { int result = num1 * num2; printf("%d\n", result); }
In the snippet below concerned with the division functionality, we implement a conditional statement that handles dividing by 0. If num2 is assigned the integer value of 0, then we'll print out "Cannot divide by 0!". If the user is not dividing by 0, then we proceed as normal with our operation.
int division(int num1, int num2) { if (num2 == 0) { printf("Cannot divide by 0!\n"); } else { int result = num1 / num2; printf("%d\n", result); } }
int modulo(int num1, int num2) { int result = num1 % num2; printf("%d\n", result); }
In the code below, we'll put it together neatly. This will be done by enclosing all of the function calls in a while(1) loop, making them run "forever" (unless the program is interrupted by a ^c). Then to check for which operation is chosen, we view the assigned operation (character) symbol assigned to "character" and use switch/case statements to perform the appropriate function to the function chosen.
What are switch/case statements? They're just like if/else statements but, instead of checking the given value against conditions-to-be-met until it reaches a case that it agrees with, switches don't need to run against every statement in order to check if it's true or false or whatever, it directly goes in an instant (compared to
the time if/else takes) to the statement that returns true and executes the corresponding code. Why am I showing this to you guys? I don't want you to make the same mistake Yandere Dev. made with his use of if/else statements (go search it on Youtube). Later in your career when you're working
on larger projects with larger sets of conditionals, I would like for you to know what's the most optimal technique to use.
#include <stdio.h> int addition(int num1, int num2) { int result = num1 + num2; printf("%d\n", result); } int subtraction(int num1, int num2) { int result = num1 - num2; printf("%d\n", result); } int multiplication(int num1, int num2) { int result = num1 * num2; printf("%d\n", result); } int division(int num1, int num2) { if (num2 == 0) { printf("Cannot divide by 0!\n"); } else { int result = num1 / num2; printf("%d\n", result); } } int modulo(int num1, int num2) { int result = num1 % num2; printf("%d\n", result); } // Main function for user input and operation selection int main() { int num1, num2; char operation; while (1) { printf("Input your first number:"); scanf("%d", &num1); printf("Pick from the following operations: + - * / %% :\n"); scanf(" %c", &operation); printf("Input your second number:"); scanf("%d", &num2); switch (operation) { case '+': addition(num1, num2); break; case '-': subtraction(num1, num2); break; case '*': multiplication(num1, num2); break; case '/': division(num1, num2); break; case '%': modulo(num1, num2); break; default: printf("Invalid Choice!"); break; } } return 0; }
Lastly, how does the scanf() function work? The scanf function allows you to accept input from standard in, which for us is generally the keyboard. It is most similar to python's input() function. It works by first declaring the data type to be taken from the user, followed by a comma (,) then the name of the variable.
As in the future updates to this program, I'll add the functionalities to perform operations on more than 2 numbers, the ability to add an integer and a float (2 + 2.5), and add more operations, most probably using the maths.h header file in C.
Please don't shy from sending suggestions or anything to the contacts listed in Whoami.