C++ का Mini Project — Calculator
In this page:
Operations Structure करना
हर math operation -- addition, subtraction, multiplication, division -- को अपने छोटे function में split करना calculator की logic को organized और individually testable रखता है, इसे एक बड़े conditional branches के block के अंदर उलझाने की बजाय।
उदाहरण: Structuring Operations
// Include std::cout and std::cin
#include <iostream>
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << add(3, 2) << " " << subtract(3, 2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Menu Design करना
एक text-based menu available operations print करता है और user की choice पढ़ता है, उस input को translate करता है कि आगे कौन-सा function call होगा -- यही control-flow backbone है जो individual operation functions को एक usable program में साथ जोड़ता है।
उदाहरण: Designing the Menu
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare choice and set it to 1
int choice = 1;
// Print to the console with cout
std::cout << "1. Add\n2. Subtract" << std::endl;
if (choice == 1) std::cout << "Add selected" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Division by Zero के खिलाफ Guard करना
Zero से divide करना integers के लिए undefined behavior है (और floating-point के लिए infinity या NaN produce करता है), और एक program crash कर सकता है या silently इसका output corrupt कर सकता है।
Division perform करने से पहले divisor zero नहीं है यह check करना, और इसकी बजाय एक clear error report करना, किसी भी calculator में एक mandatory safeguard है।
उदाहरण: Guard Against Division by Zero
// Include std::cout and std::cin
#include <iostream>
double safeDivide(double a, double b) {
// Check whether b == 0
if (b == 0) {
// Print to the console with cout
std::cout << "Cannot divide by zero" << std::endl;
// Send 0 back to the caller
return 0;
}
// Send a / b back to the caller
return a / b;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << safeDivide(10, 0) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Calculation History Store करना
हर calculation के inputs और result को होते ही एक std::vector में store करना आपको एक history feature build करने देता है -- request पर हर पिछली calculation user को वापस print करना, disk पर कुछ भी persist किए बिना।
उदाहरण: Storing Calculation History
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named history (a resizable array)
std::vector<double> history;
history.push_back(3 + 2);
history.push_back(10 - 4);
for (double result : history) std::cout << result << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Continuous Calculation Flow
एक calculation के result को अगले operation के input की तरह वापस feed करना (हर बार fresh शुरू करने की बजाय) users को operations की एक sequence साथ chain करने देता है, बिल्कुल वैसे ही जैसे एक physical calculator का running total काम करता है।
उदाहरण: Continuous Calculation Flow
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare result and set it to 5
double result = 5;
result = result + 3;
result = result * 2;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- पहले divisor check किए बिना zero से divide करना, जो ints के लिए crash करता है या doubles के लिए infinity देता है।
- menu input validate न करना, इसलिए एक number की जगह type किया एक letter input loop तोड़ देता है।
- numbers के लिए
intइस्तेमाल करना ताकि5 / 2जैसे results decimal part खो दें।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced