0% found this document useful (0 votes)
5K views

Computer Project

Uploaded by

Qasimbutt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

Computer Project

Uploaded by

Qasimbutt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WAH ENGINEERING COLLEGE

Department of Mechanical Engineering Technology

Computer Fundamentals
Course code: CS-101

PROJECT

C++ Code to Make a Calculator

Submitted to:

Engr. Naveed Anwar


WEC, University of Wah

Submitted by:

M. Qasim Butt Touqeer Malik Ziad Ali


UW-23-MET-BS-017 UW-23-MET-BS-003 UW-23-MET-BS-015
MET 1st Semester MET 1st Semester MET 1st Semester
 INTRODUCTION:
In the realm of computer programming, creating a calculator serves as a
fundamental yet pivotal project. This project involves the development of a
robust and versatile calculator using the C++ programming language. The
objective is to implement a user-friendly interface capable of performing five to
six essential mathematical operations. This report outlines the design,
implementation, and functionality of the calculator, shedding light on the core
concepts and techniques employed in the development process.

The calculator application is not merely a compilation of arithmetic


operations; it strives to provide a seamless and intuitive user experience. From
addition and subtraction to more complex operations like multiplication,
division, and exponentiation, the calculator showcases the versatility and
efficiency of C++ in handling mathematical computations.

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.

Through this project, we aim to not only demonstrate proficiency in C++


programming but also provide a practical and functional tool that can be utilized
for everyday mathematical computations. The subsequent sections will provide
a comprehensive breakdown of the project, addressing various aspects such as
code structure, user interface design, and the underlying mathematical
algorithms.
1) Objective:

The primary objective of this project is to design and implement a C++


calculator capable of performing essential mathematical operations. The
calculator is intended to provide a user-friendly interface while demonstrating
the principles of object-oriented programming and modular code design.

2) Scope of the Project:

The calculator project encompasses the implementation of five to six


fundamental mathematical operations, such as addition, subtraction,
multiplication, division, and additional operations like exponentiation or square
root. The user interface will be designed to ensure ease of use, allowing users to
input values and select the desired operation.

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:

Addition, subtraction, multiplication, and division form the core


operations of the calculator.

c) Extended Operations:

Additional features may include exponentiation, square root, or any other


advanced mathematical operations.

d) Error Handling:

The program will incorporate error-checking mechanisms to handle


invalid inputs or mathematical operations.

3
Mechanical Engineering Department, University of Wah, Wah Cantt
4) Development Environment:

The project will be developed using the C++ programming language,


taking advantage of its features such as classes, functions, and conditional
statements. A standard Integrated Development Environment (IDE) will be used
for coding and debugging.

5) Methodology:
a) Object-Oriented Design:

The calculator will be designed using object-oriented principles, with


classes representing different components such as the calculator itself and the
mathematical operations.

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:

The anticipated outcome of this project is a fully functional C++


calculator that performs basic and extended mathematical operations with
precision. The calculator will showcase effective code organization, error
handling, and a well-designed user interface.

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.

using namespace std;


3. Main Function:

The main function is the entry point of the program.

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.

cout << "Enter first number: ";


cin >> num1;

cout << "Enter operation (+, -, *, /, %, ^): ";

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

If the operation is subtraction, it subtracts num2 from num1 and assigns


the result to the result variable.

case '-':
result = num1 - num2;
break;

8
Mechanical Engineering Department, University of Wah, Wah Cantt
9. Case '*': Multiplication

If the operation is multiplication, it multiplies num1 and num2 and


assigns the result to the result variable.

case '*':
result = num1 * num2;
break;
10. Case '/': Division

If the operation is division, it checks if num2 is not zero before


performing the division to avoid division by zero. If num2 is zero, it prints an
error message and exits the program with an error code.

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

If the operation is modulus (%), it checks if num2 is not zero before


performing the modulus operation. If num2 is zero, it prints an error message
and exits the program with an error code.

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

If the operation is exponentiation (^), it calculates num1 raised to the


power of num2 using the pow function from the cmath library.

case '^':
result = pow(num1, num2);
break;
13. Result Output:

Finally, it prints the calculated result to the console.

cout << "Result: " << result << endl;


14. Program Exit:

return 0; // Exit program successfully

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.

The calculator's design and functionality showcase effective use of


object-oriented programming principles, ensuring modularity and
maintainability. The incorporation of user input validation enhances the
robustness of the program, preventing unintended errors and providing a more
user-friendly experience.

Throughout the project, careful attention was given to code optimization


and efficiency. The use of appropriate data structures and algorithms has
contributed to a responsive and resource-efficient calculator application. Error
handling mechanisms have been implemented to gracefully manage unexpected
user inputs, enhancing the overall reliability of the program.

Moreover, the project adheres to best coding practices and style


guidelines, promoting readability and collaboration. Well-commented code and
documentation facilitate understanding for future maintenance or expansion of
the calculator application.

In summary, this C++ calculator project has successfully achieved its


objectives by delivering a functional, efficient, and well-documented solution.
The experience gained from this project not only reinforces programming skills
but also lays a solid foundation for tackling more complex software
development challenges in the future.

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

You might also like