Implementing a Scientific Calculator in C Language
Introduction
In daily programming, a simple calculator can perform basic operations such as addition, subtraction, multiplication, and division, while a scientific calculator can execute more complex mathematical operations, such as trigonometric functions and logarithms. This article will guide you on how to implement a simple scientific calculator using the C language.
Design Approach
Our scientific calculator needs to support the following functions:
- Basic mathematical operations: addition, subtraction, multiplication, and division
- Trigonometric functions: sine (sin), cosine (cos), tangent (tan)
- Exponential and logarithmic operations: e^x and log(x)
- Simple error handling, such as division by zero or invalid input
Environment Setup
We will use the math.h
from the standard C library to perform some mathematical operations and use stdio.h
for input and output.
Ensure that your development environment has a C compiler installed, such as GCC. The code should run correctly in any standard C compliant compilation environment.
Code Implementation
Below is the complete code example:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void display_menu() {
printf("==== Scientific Calculator ====
");
printf("Select operation:
");
printf("1. Addition (a + b)
");
printf("2. Subtraction (a - b)
");
printf("3. Multiplication (a * b)
");
printf("4. Division (a / b)
");
printf("5. Sine (sin x)
");
printf("6. Cosine (cos x)
");
printf("7. Tangent (tan x)
");
printf("8. Exponential (e ^ x)
");
printf("9. Logarithm (log x, base e)
");
}
int main() {
int choice;
while(1) {
display_menu();
// Get user choice
printf("Please enter your choice (0 to exit): ");
scanf("%d", &choice);
if(choice == 0) { // Exit condition
break;
}
double a, b;
switch(choice) {
case 1: // Addition
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
printf("%.2lf + %.2lf = %.2lf\n", a, b, a + b);
break;
case 2: // Subtraction
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
printf("%.2lf - %.2lf = %.2lf\n", a, b, a - b);
break;
case 3: // Multiplication
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
printf("%.2lf * %.2lf = %.2lf\n", a, b, a * b);
break;
case 4: // Division
printf("Enter two numbers:");
scanf("%lf %lf", &a, &b);
if(b == 0) {
printf("ERROR: Division by zero!\n");
} else {
printf("%.2lf / %.2lf = %.2lf\n", a, b, a / b);
}
break;
case 5: // Sine
printf("Enter angle in radians: ");
scanf("%lf", &a);
printf("sin(%.2lf) = %.2lf\n", a, sin(a));
break;
case 6: // Cosine
printf("Enter angle in radians: ");
scanf("%lf", &a);
printf("cos(%.2lf) = %.2lf\n", a, cos(a));
break;
case 7: // Tangent
printf("Enter angle in radians: ");
scanf("%lf", &a);
printf("tan(%.2lf) = %.2lf\n", a, tan(a));
break;
case 8: // Exponential
printf("Enter exponent: ");
scanf("%lf", &a);
printf("e^%.2lf = %.2lf\n", a, exp(a));
break;
case 9: // Logarithm
printf("Enter number: ");
scanf("%lf", &a);
if(a <= 0) {
printf("ERROR: Logarithm of non-positive number!\n");
} else {
printf("log(%.2lf) = %.2lf\n", a, log(a));
}
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Program Explanation
1) The program includes the stdio.h
and math.h
header files to facilitate standard I/O and mathematical function calls.
2) The display_menu()
function is used to display the operation menu, allowing users to perform different types of calculations based on their selection.
void display_menu() {…}
3) The main function contains an infinite loop that continuously displays the menu, allowing users to perform calculations multiple times. At the start of each loop, the display_menu()
function is called to show the available operations.
while(1) { … }
4) Based on the user’s input choice, the program executes the corresponding control flow using a switch statement and retrieves data. Additionally, to prevent the program from crashing due to unhandled division by zero, a check is added to provide an error message if division by zero is selected.
5) For trigonometric functions and exponential and logarithmic operations, the program directly calls the corresponding algorithms from the library without manual implementation.
Error Handling and Improvement Suggestions
This program is relatively simple and only provides basic functionality. In practical applications, you may want to consider more effective validation checks:
- Add exception handling mechanisms to ensure that even with poor or invalid input, the program flow is not affected.
- Support complex expression parsing, which can introduce stack structures to manage different priority parameters, multiple parentheses, etc.
Conclusion
This article introduced how to quickly build a simple yet practical scientific calculator using the C language. From design to development, you should have grasped some important steps. This project not only helps you consolidate your foundational knowledge but also enhances your ability to solve practical problems. I hope all readers can engage in practice and continuously improve their skills through optimization and expansion.