Created
September 23, 2025 17:50
-
-
Save Marie-zaj/da5ebee41c3d37ebf846144044af3119 to your computer and use it in GitHub Desktop.
Shring_calc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| class Shring | |
| { | |
| private: | |
| int num; | |
| int den; | |
| public: | |
| Shring() | |
| { | |
| num = 0; | |
| den = 1; | |
| } | |
| Shring(int _num, int _den) | |
| { | |
| if (_den == 0) { | |
| cout << "Denominator can`t be zero. Setting to 1...\n"; | |
| den = 1; | |
| } | |
| else { | |
| den = _den; | |
| } | |
| num = _num; | |
| } | |
| void Input() | |
| { | |
| cout << "Enter the numerator: "; | |
| cin >> num; | |
| cout << "Enter the denominator: "; | |
| cin >> den; | |
| if (den == 0) { | |
| cout << "Denominator cannot be zero. Setting to 1...\n"; | |
| den = 1; | |
| } | |
| } | |
| void Print() | |
| { | |
| cout << num << "/" << den << endl; | |
| } | |
| int GetNum() | |
| { | |
| return num; | |
| } | |
| int GetDen() | |
| { | |
| return den; | |
| } | |
| void SetNum(int _num) | |
| { | |
| num = _num; | |
| } | |
| void SetDen(int _den) | |
| { | |
| if (_den != 0) | |
| { | |
| den = _den; | |
| } | |
| else | |
| { | |
| cout << "Denominator can`t be zero!\n"; | |
| } | |
| } | |
| Shring Sum(Shring b) | |
| { | |
| Shring rez; | |
| rez.num = num * b.den + b.num * den; | |
| rez.den = den * b.den; | |
| return rez; | |
| } | |
| Shring Sub(Shring b) | |
| { | |
| Shring rez; | |
| rez.num = num * b.den - b.num * den; | |
| rez.den = den * b.den; | |
| return rez; | |
| } | |
| Shring Mul(Shring b) | |
| { | |
| Shring rez; | |
| rez.num = num * b.num; | |
| rez.den = den * b.den; | |
| return rez; | |
| } | |
| Shring Div(Shring b) | |
| { | |
| Shring rez; | |
| rez.num = num * b.den; | |
| rez.den = den * b.num; | |
| return rez; | |
| } | |
| }; | |
| int main() | |
| { | |
| Shring a, b; | |
| cout << "Enter first shring:\n"; | |
| a.Input(); | |
| cout << "Enter second shring:\n"; | |
| b.Input(); | |
| cout << "\nFirst shring: "; | |
| a.Print(); | |
| cout << "Second shring: "; | |
| b.Print(); | |
| Shring sum = a.Sum(b); | |
| Shring sub = a.Sub(b); | |
| Shring mul = a.Mul(b); | |
| Shring div = a.Div(b); | |
| cout << "\nResults:\n"; | |
| cout << "Sum = "; sum.Print(); | |
| cout << "Sub = "; sub.Print(); | |
| cout << "Mul = "; mul.Print(); | |
| cout << "Div = "; div.Print(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment