Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .h
- struct fraction {
- public:
- fraction();
- fraction(signed int a, signed int b);
- fraction(signed int a);
- fraction(std::string s);
- double frac_eval();
- signed int numerator();
- signed int denominator();
- std::string str();
- std::string tex();
- fraction& operator +(fraction &a1);
- fraction& operator -(fraction &a1);
- fraction& operator *(fraction &a1);
- fraction& operator /(fraction &a1);
- fraction& operator +(int a1);
- fraction& operator -(int a1);
- fraction& operator *(int a1);
- fraction& operator /(int a1);
- private:
- signed int top;
- signed int bot;
- };
- ######################################################################################################################
- .cpp
- std::string fraction::str() {
- if (top == 0)
- return "0";
- else if (bot == 1)
- return std::to_string(top);
- else
- return boost::str(boost::format("%1%/%2%") % std::to_string(top) % std::to_string(bot));
- }
- std::string fraction::tex() {
- if (top == 0)
- return "0";
- else if (bot == 1)
- return std::to_string(top);
- else
- return boost::str(boost::format("\\frac{%1%}{%2%}") % std::to_string(top) % std::to_string(bot));
- }
- fraction::fraction(signed int a, signed int b) :top(a), bot(b) {}
- fraction::fraction(signed int a) : top(a), bot(1) {}
- fraction::fraction(std::string s) {
- size_t pos = s.find_first_of("/");
- top = std::stoi(s.substr(0, pos));
- if (pos == std::string::npos) bot = 1;
- else bot = std::stoi(s.substr(pos + 1, s.length()));
- }
- fraction::fraction() {}
- double fraction::frac_eval() { return (double)top / (double)bot; }
- signed int fraction::numerator() { return top; }
- signed int fraction::denominator() { return bot; }
- fraction& fraction::operator+(fraction &a1) {
- top = a1.numerator()*bot + a1.denominator()*top;
- bot = a1.denominator()*bot;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction& fraction::operator-(fraction &a1) {
- top = -a1.numerator()*bot + a1.denominator()*top;
- bot = a1.denominator()*bot;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction& fraction::operator*(fraction &a1) {
- top = a1.numerator()*top;
- bot = a1.denominator()*bot;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction& fraction::operator/(fraction &a1) {
- bot = a1.numerator()*bot;
- top = a1.denominator()*top;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction & fraction::operator+(int a1)
- {
- top = a1*bot +top;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction & fraction::operator-(int a1)
- {
- top = -a1*bot + top;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction & fraction::operator*(int a1)
- {
- top = a1*top;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
- fraction & fraction::operator/(int a1)
- {
- bot = a1*bot;
- for (int i = abs(top*bot); i > 1; i--) {
- if ((top % i == 0) && (bot % i == 0)) {
- bot /= i;
- top /= i;
- }
- }
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement