In this article, we will learn how to implement a simple command-line calculator using the C programming language. This calculator will support basic arithmetic operations: addition, subtraction, multiplication, and division. We will start with the design of the user interface and then gradually implement the core algorithms.
1. Project Structure
Our project will include the following components:
- User Input
- Operator Parsing
- Calculation Logic
- Output Result
2. User Input
First, we need to obtain user input. In the command line, the user can input two numbers and an operator. For example, <span>3 + 5</span>.
Example Code:
#include <stdio.h>
int main() { double num1, num2; char operator;
printf("Please enter the first number: "); scanf("%lf", &num1);
printf("Please enter the operator (+, -, *, /): "); scanf(" %c", &operator);
printf("Please enter the second number: "); scanf("%lf", &num2);
// Further processing...
return 0;}
Explanation:
- We use the
<span>scanf</span>function to read user input. <span>%lf</span>is used to read a double-precision floating-point number, while<span>%c</span>is used to read a character.- Note that the space before
<span>scanf(" %c", &operator);</span>is to ignore any preceding newline characters.
3. Operator Parsing and Calculation Logic
Next, we need to perform the corresponding operation based on the operator provided by the user. We can use a <span>switch</span> statement to handle different types of operations.
Example Code:
#include <stdio.h>
int main() { double num1, num2, result; char operator;
// Get user input (same as above)
// Perform corresponding operations based on the operator switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) { result = num1 / num2; } else { printf("Error: Division by zero is not allowed.\n"); return -1; // Return error code } break; default: printf("Error: Unknown operator.\n"); return -1; // Return error code }
printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
return 0;}
Explanation:
- We use a
<span>switch-case</span>to determine the different operators and perform the corresponding operations. - In the division operation, we check if the divisor is zero to avoid runtime errors.
- Finally, we use
<span>printf</span>to output the result, formatted to two decimal places.
Complete Code Example
Below is the complete program code that combines all parts together:
#include <stdio.h>
int main() { double num1, num2, result; char operator;
printf("Please enter the first number: "); scanf("%lf", &num1);
printf("Please enter the operator (+, -, *, /): "); scanf(" %c", &operator);
printf("Please enter the second number: "); scanf("%lf", &num2);
switch (operator) { case '+': result = num1 + num2; break; case '-': result = number_01 - number_02; break; case '*': result = number_01 * number_02; break; case '/': if (number_02 != 0) { result = number_01 / number_02; } else { printf("Error: Division by zero.\n"); return -10; } break; default: printf("Error: Unknown operator.\n"); return -10; }
printf("%.4f %c %.4f = %.4f\n", number_01, operator, number_02, result);
return 0;}
Conclusion
Through the above steps, we have successfully created a simple yet fully functional command-line calculator. You can expand this program according to your needs, such as adding more complex features (like power, square root, etc.), or improving the user interface experience. This is a great way to learn programming, and I hope you benefit from it!