项目作者: Chukak

项目描述 :
Class of rational numbers
高级语言: C++
项目地址: git://github.com/Chukak/rational-numbers.git
创建时间: 2018-03-12T18:28:57Z
项目社区:https://github.com/Chukak/rational-numbers

开源协议:

下载


rational-numbers

How to use

Class of rationals numbers. Stores numbers in the format 2/3.

Creation:

  1. rational a(2, 3); rational a = {2, 3}; rational a;

Arithmetic operations

You can add, subtract, multiply and divide two rational numbers.
For example:

  1. rational a(2, 3);
  2. rational b(2, 3);
  3. rational c = a + b;
  4. rational d = a - b;
  5. rational e = a * b;
  6. rational f = a / b;

Also you can use this operator:

  1. rational a(2, 3);
  2. rational b(2, 3);
  3. a += b

You can compare two rational numbers. For example:

  1. rational a(2, 3);
  2. rational b(2, 3);
  3. if (a == b) { ... }
  4. if (a != b) { ... }
  5. if (a >= b) { ... }
  6. if (a < b) { ... }

You can check true or false rational. For example:

  1. rational a(0, 1);
  2. bool c = a ? true : false; // c == false
  3. if (!a) { ... }
  4. rational a(2, 3)
  5. bool c = a ? true : false; // c = true
  6. if (a) { ... }

You can use input and output for rational numbers. For example:

  1. rational a;
  2. cin >> a;
  3. cout << a;

You can use rational in vector, map, tuple, set. For example:

  1. vector<rational> a = {{2, 1}, {3, 4}};
  2. set<rational> a = {{2, 1}, {3, 4}};
  3. tuple<rational, rational, rational> a = make_tuple(rationa(2, 1), rational(3, 4), rational(5, 4));
  4. map<rational, int> a;
  5. rational r= {2, 1};
  6. map[r] = 1;

Tests

Main.cpp by default run tests.
The file test.cpp contains tests.