In this section, we will guide you through the process of building a scientific calculator using Fortran. This project will help you apply various concepts learned throughout the course, including control structures, functions, and file handling.
Objectives
- Understand the structure of a scientific calculator program.
- Implement basic arithmetic operations.
- Implement advanced mathematical functions.
- Handle user input and output.
- Organize the code using subroutines and functions.
Step-by-Step Guide
- Program Structure
First, let's outline the structure of our scientific calculator program. The main components will include:
- A main program to handle user interaction.
- Subroutines for basic arithmetic operations (addition, subtraction, multiplication, division).
- Functions for advanced mathematical operations (e.g., sine, cosine, logarithm).
- Setting Up the Main Program
Create a new Fortran file named calculator.f90
and start by setting up the main program structure.
program scientific_calculator implicit none integer :: choice real :: num1, num2, result do call display_menu() read(*,*) choice if (choice == 0) exit call get_input(num1, num2) select case (choice) case (1) result = add(num1, num2) case (2) result = subtract(num1, num2) case (3) result = multiply(num1, num2) case (4) result = divide(num1, num2) case (5) result = sine(num1) case (6) result = cosine(num1) case (7) result = logarithm(num1) case default print *, "Invalid choice. Please try again." cycle end select print *, "Result: ", result end do print *, "Exiting the calculator. Goodbye!" end program scientific_calculator
- Display Menu Subroutine
Create a subroutine to display the menu options to the user.
subroutine display_menu() print *, "Scientific Calculator" print *, "1. Addition" print *, "2. Subtraction" print *, "3. Multiplication" print *, "4. Division" print *, "5. Sine" print *, "6. Cosine" print *, "7. Logarithm" print *, "0. Exit" print *, "Enter your choice: " end subroutine display_menu
- Get Input Subroutine
Create a subroutine to get input from the user. For simplicity, we will assume that advanced functions like sine, cosine, and logarithm only require one input.
subroutine get_input(num1, num2) real, intent(out) :: num1, num2 print *, "Enter the first number: " read(*,*) num1 print *, "Enter the second number (if applicable, otherwise enter 0): " read(*,*) num2 end subroutine get_input
- Basic Arithmetic Operations
Implement subroutines for basic arithmetic operations.
real function add(a, b) real, intent(in) :: a, b add = a + b end function add real function subtract(a, b) real, intent(in) :: a, b subtract = a - b end function subtract real function multiply(a, b) real, intent(in) :: a, b multiply = a * b end function multiply real function divide(a, b) real, intent(in) :: a, b if (b == 0.0) then print *, "Error: Division by zero" divide = 0.0 else divide = a / b end if end function divide
- Advanced Mathematical Functions
Implement functions for advanced mathematical operations.
real function sine(x) real, intent(in) :: x sine = sin(x) end function sine real function cosine(x) real, intent(in) :: x cosine = cos(x) end function cosine real function logarithm(x) real, intent(in) :: x if (x <= 0.0) then print *, "Error: Logarithm of non-positive number" logarithm = 0.0 else logarithm = log(x) end if end function logarithm
- Compiling and Running the Program
To compile and run the program, use the following commands in your terminal:
- Practical Exercises
Exercise 1: Add More Functions
Add more mathematical functions to the calculator, such as:
- Tangent
- Exponential
- Square root
Exercise 2: Error Handling
Improve the error handling in the program. For example, handle cases where the user inputs non-numeric values.
Exercise 3: Modularize the Code
Refactor the code to use modules for better organization and reusability.
- Summary
In this section, we built a simple scientific calculator using Fortran. We covered:
- Setting up the main program structure.
- Implementing basic arithmetic operations.
- Implementing advanced mathematical functions.
- Handling user input and output.
This project helped reinforce various Fortran concepts and provided a practical application to solidify your understanding. In the next section, we will explore numerical methods in Fortran.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability