← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 15 of 17

C++ का Mini Project — Calculator

यह mini project एक calculator बनाता है जो numbers और एक operation माँगता है और answer दिखाता है। यह आपके सीखी चीज़ों को साथ जोड़ता है, हर हिस्सा सीखने के बाद एक छोटा model बनाने की तरह।

Operations Structure करना

हर math operation -- addition, subtraction, multiplication, division -- को अपने छोटे function में split करना calculator की logic को organized और individually testable रखता है, इसे एक बड़े conditional branches के block के अंदर उलझाने की बजाय।

उदाहरण: Structuring Operations

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

Calculation History Store करना

हर calculation के inputs और result को होते ही एक std::vector में store करना आपको एक history feature build करने देता है -- request पर हर पिछली calculation user को वापस print करना, disk पर कुछ भी persist किए बिना।

उदाहरण: Storing Calculation History

cpp
// 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;
}

Continuous Calculation Flow

एक calculation के result को अगले operation के input की तरह वापस feed करना (हर बार fresh शुरू करने की बजाय) users को operations की एक sequence साथ chain करने देता है, बिल्कुल वैसे ही जैसे एक physical calculator का running total काम करता है।

उदाहरण: Continuous Calculation Flow

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पहले divisor check किए बिना zero से divide करना, जो ints के लिए crash करता है या doubles के लिए infinity देता है।
  2. menu input validate न करना, इसलिए एक number की जगह type किया एक letter input loop तोड़ देता है।
  3. numbers के लिए int इस्तेमाल करना ताकि 5 / 2 जैसे results decimal part खो दें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.