Computer Project
Computer Project
Computer Fundamentals
Course code: CS-101
PROJECT
Submitted to:
Submitted by:
This report will delve into the project's architecture, discussing the choice
of data structures, algorithms, and design patterns that optimize the calculator's
performance. Additionally, it will explore the user interface aspects, illustrating
how the graphical components enhance the overall usability.
3) Features:
a) User Interface:
The calculator will have a user-friendly interface that accepts input from
the user and displays the results of calculations.
b) Basic Operations:
c) Extended Operations:
d) Error Handling:
3
Mechanical Engineering Department, University of Wah, Wah Cantt
4) Development Environment:
5) Methodology:
a) Object-Oriented Design:
b) Modular Code:
The code will be modular, allowing for easy maintenance and scalability.
Each mathematical operation will be implemented as a separate function or
method.
6) Expected Outcome:
4
Mechanical Engineering Department, University of Wah, Wah Cantt
CODE/ PROGRAM:
#include <iostream>
#include <cmath> // Include the cmath library for pow
function
using namespace std;
int main() {
char operation;
double num1, num2, result;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter operation (+, -, *, /, %, ^): ";
cin >> operation;
cout << "Enter second number: ";
cin >> num2;
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
cout << "Error: Division by zero is
undefined." << endl;
return 1; // Exit program with an error code
}
5
Mechanical Engineering Department, University of Wah, Wah Cantt
break;
case '%':
if (num2 != 0) {
result = fmod(num1, num2);
} else {
cout << "Error: Modulus by zero is
undefined." << endl;
return 1; // Exit program with an error code
}
break;
case '^':
result = pow(num1, num2);
break;
default:
cout << "Error: Invalid operation." << endl;
return 1; // Exit program with an error code
}
cout << "Result: " << result << endl;
return 0; // Exit program successfully
}
6
Mechanical Engineering Department, University of Wah, Wah Cantt
PROCEDURE IN DETAIL:
1. Header Includes:
These lines include necessary header files. iostream is used for input and
output operations, and cmath is included for the pow function used later for
exponentiation.
#include <iostream>
#include <cmath> // Include the cmath library for
pow function
2. Namespace Declaration:
This line allows the use of standard C++ library functions without
explicitly specifying the namespace.
int main() {
// ...
}
4. Variable Declaration:
These lines declare variables to store the operation and two numbers
entered by the user, as well as the result of the calculation.
char operation;
double num1, num2, result;
5. User Input:
These lines prompt the user to enter the first number, operation, and the
second number.
7
Mechanical Engineering Department, University of Wah, Wah Cantt
cin >> operation;
cout << "Enter second number: ";
cin >> num2;
6. Switch Statement:
The switch statement checks the value of the operation variable and
executes the corresponding case. If none of the cases match (default case), it
prints an error message and exits the program with an error code.
switch (operation) {
// Cases for each operation
// ...
default:
cout << "Error: Invalid operation." << endl;
return 1; // Exit program with an error code
}
7. Case '+': Addition
If the operation is addition, it adds num1 and num2 and assigns the result
to the result variable.
case '+':
result = num1 + num2;
break;
8. Case '-': Subtraction
case '-':
result = num1 - num2;
break;
8
Mechanical Engineering Department, University of Wah, Wah Cantt
9. Case '*': Multiplication
case '*':
result = num1 * num2;
break;
10. Case '/': Division
case '/':
// Check for division by zero
if (num2 != 0) {
result = num1 / num2;
} else {
cout << "Error: Division by zero is
undefined." << endl;
return 1; // Exit program with an error code
}
break;
11. Case '%': Modulus
case '%':
// Check for modulus by zero
if (num2 != 0) {
result = fmod(num1, num2);
9
Mechanical Engineering Department, University of Wah, Wah Cantt
} else {
cout << "Error: Modulus by zero is
undefined." << endl;
return 1; // Exit program with an error code
}
break;
12. Case '^': Exponentiation
case '^':
result = pow(num1, num2);
break;
13. Result Output:
10
Mechanical Engineering Department, University of Wah, Wah Cantt
CONCLUSION STATEMENT:
In conclusion, the development of the C++ calculator project has
provided a comprehensive exploration into the fundamentals of programming
and software engineering. Through the implementation of 5 to 6 mathematical
operations, including addition, subtraction, multiplication, division, and
additional operations as per project requirements, this project has demonstrated
a solid understanding of core C++ concepts.
11
Mechanical Engineering Department, University of Wah, Wah Cantt
EXECUTION:
1. Addition:
2. Subtraction:
12
Mechanical Engineering Department, University of Wah, Wah Cantt
3. Multiplication:
4. Division:
13
Mechanical Engineering Department, University of Wah, Wah Cantt
5. Exponential:
THE END
14
Mechanical Engineering Department, University of Wah, Wah Cantt