-
-
Save HarukaKajita/c5909cd9f52a26f8938c3f8ebb860a34 to your computer and use it in GitHub Desktop.
C++の授業:「区間」クラスのオペレーター
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> | |
| #include <cstdlib> | |
| using namespace std; | |
| //アクセサを定義しないIntervalクラス | |
| class Interval | |
| { | |
| private: | |
| double upperLimit; | |
| double lowerLimit; | |
| public: | |
| Interval(double u, double l) | |
| { | |
| upperLimit = u; | |
| lowerLimit = l; | |
| } | |
| friend Interval operator+(Interval a, Interval b); | |
| friend Interval operator*(Interval a, Interval b); | |
| friend ostream &operator<<(ostream &stream, Interval interval); | |
| }; | |
| //区間同士の和 | |
| Interval operator+(Interval a, Interval b) | |
| { | |
| Interval sum = Interval(a.upperLimit + b.upperLimit, a.lowerLimit + b.lowerLimit); | |
| return sum; | |
| } | |
| //区間同士の積 | |
| Interval operator*(Interval a, Interval b) | |
| { | |
| Interval product = Interval(a.upperLimit * b.upperLimit, a.lowerLimit * b.lowerLimit); | |
| return product; | |
| } | |
| //整数*区間 | |
| Interval operator*(double d, Interval interval) | |
| { | |
| return Interval(d, d) * interval; | |
| } | |
| //区間*整数(今回は不必要) | |
| Interval operator*(Interval interval, double d) | |
| { | |
| return d * interval; | |
| } | |
| //区間の入力 | |
| istream &operator>>(istream &stream, Interval &interval) | |
| { | |
| double upL, lowL; | |
| stream >> upL >> lowL; | |
| interval = Interval(upL, lowL); | |
| return stream; | |
| } | |
| //区間の出力 | |
| ostream &operator<<(ostream &stream, Interval interval) | |
| { | |
| stream << "[" + interval.upperLimit + ", " + interval.lowerLimit + "]"; | |
| return stream; | |
| } | |
| int main(){ | |
| Interval a,b; | |
| cout << ""; | |
| cin >> a; | |
| cout << ""; | |
| cin >> b; | |
| cout << "a+b=" << a+b << endl; | |
| cout << "a*b=" << a*b << endl; | |
| cout << "b+2a=" << b+2*a << endl; | |
| return 0; | |
| } |
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 <istream> | |
| using namespace std; | |
| class Interval | |
| { | |
| private: | |
| int upperLimit; | |
| int lowerLimit; | |
| public: | |
| Interval(int u, int l) | |
| { | |
| upperLimit = u; | |
| lowerLimit = l; | |
| } | |
| int getUpperLimit() { return upperLimit; } | |
| int getLowerLimit() { return lowerLimit; } | |
| }; | |
| //区間同士の和 | |
| Interval operator+(Interval a, Interval b) | |
| { | |
| Interval sum = Interval(a.getUpperLimit() + b.getUpperLimit(), a.getLowerLimit() + b.getLowerLimit()); | |
| return sum; | |
| } | |
| //区間同士の積 | |
| Interval operator*(Interval a, Interval b) | |
| { | |
| Interval product = Interval(a.getUpperLimit() * b.getUpperLimit(), a.getLowerLimit() * b.getLowerLimit()); | |
| return product; | |
| } | |
| //整数*区間 | |
| Interval operator*(int integer, Interval interval) | |
| { | |
| return Interval(integer, integer) * interval; | |
| } | |
| //区間*整数(今回は不必要) | |
| Interval operator*(Interval interval, int integer) | |
| { | |
| return integer * interval; | |
| } | |
| //区間の入力 | |
| istream &operator>>(istream &stream, Interval &interval) | |
| { | |
| int upL, lowL; | |
| stream >> upL >> lowL; | |
| interval = Interval(upL, lowL); | |
| return stream; | |
| } | |
| //区間の出力 | |
| ostream &operator<<(ostream &stream, Interval interval) | |
| { | |
| stream << "[" + interval.getUpperLimit() + ", " + interval.getLowerLimit() + "]"; | |
| return stream; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment