सरल कैलकुलेटर
दो संख्याओं पर एक गणितीय ऑपरेटर लगाएँ।
सरल कैलकुलेटर क्या है?
कैलकुलेटर चुने गए ऑपरेशन को ब्रांचिंग और गणित से जोड़ता है।
महत्वपूर्ण बातें
- Compiler warnings चालू करके सभी ठीक करें।
- हर value का type और lifetime समझें।
- Input और array bounds validate करें।
C और C++ code examples
main.c
#include <stdio.h>
int main(void) {
int left = 12, right = 4, result = 0; char operation = '*';
// Select the calculation from the operator.
switch (operation) { case '+': result = left + right; break; case '-': result = left - right; break; case '*': result = left * right; break; case '/': result = left / right; }
printf("%d %c %d = %d\n", left, operation, right, result);
}
12 * 4 = 48
C++
main.cpp
#include <iostream>
int main() {
int left = 12, right = 4, result = 0; char operation = '*';
// Select the calculation from the operator.
switch (operation) { case '+': result = left + right; break; case '-': result = left - right; break; case '*': result = left * right; break; case '/': result = left / right; }
std::cout << left << ' ' << operation << ' ' << right << " = " << result << '\n';
}
12 * 4 = 48
C और C++ की तुलना
switch एक कैरेक्टर को सही ऑपरेशन तक भेजता है और हर केस को साफ़ रखता है।
C
C छोटे procedural APIs और representation details स्पष्ट करता है।
C++
C++ low-level model रखकर safer library types जोड़ता है।
अभ्यास
दोनों versions चलाकर बदलें और language guarantees की तुलना करें।
- Invalid और boundary inputs जोड़ें।
- -Wall -Wextra -Wpedantic से compile करें।
- Declarations और implementation अलग करें।
चैलेंज से अभ्यास करें
इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।
सभी चैलेंज