Pit_Anonim

Untitled

Nov 28th, 2021
689
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<rational/rational.h>
  2.  
  3. #include<iostream>
  4. #include<sstream>
  5.  
  6. bool testParse(const std::string&str) {
  7.     std::istringstream istrm(str);
  8.     Rational z(1,1);
  9.     istrm>>z;
  10.     if(istrm.good()) {
  11.         std::cout<<"Read success: "<<str<<"->"<<z<<std::endl;
  12.     } else{
  13.         std::cout<<"Read error:   "<<str<<"->"<<z<<std::endl;
  14.     }
  15.     return istrm.good();
  16. }
  17. bool testComplexEquality(Rational lhs, Rational answer) {
  18.     if (lhs == answer) {
  19.         std::cout<<"Operator == success:"<<lhs<<"=="<<answer<<std::endl;
  20.         return true;
  21.     } else {
  22.         std::cout<<"Operator == error:"<<lhs<<"=="<<answer<<std::endl;
  23.         return false;
  24.     }
  25. }
  26. bool testComplexUnequality(Rational lhs, Rational answer) {
  27.     if (lhs != answer) {
  28.         std::cout<<"Operator != success:"<<lhs<<"!="<<answer<<std::endl;
  29.         return true;
  30.     } else {
  31.         std::cout<<"Operator != error:"<<lhs<<"!="<<answer<<std::endl;
  32.         return false;
  33.     }
  34. }
  35. bool testComplexSum(Rational lhs, Rational rhs, Rational answer) {
  36.     bool t(false);
  37.     if (lhs+rhs == answer) {
  38.         std::cout<<"Operator +  success:"<<lhs<<"+"<<rhs<<"="<<answer<<std::endl;
  39.         t = true;
  40.     } else {
  41.         std::cout<<"Operator +  error:"<<lhs<<"+"<<rhs<<"="<<answer<<std::endl;
  42.         t = false;
  43.     }
  44.  
  45.     lhs+=rhs;
  46.  
  47.     if (lhs == answer) {
  48.         std::cout<<"Operator += success:"<<lhs<<"="<<answer<<std::endl;
  49.         t = true;
  50.     } else {
  51.         std::cout<<"Operator += error:"<<lhs<<"="<<answer<<std::endl;
  52.         t = false;
  53.     }
  54.  
  55.     return t;
  56. }
  57. bool testComplexSubtracting(Rational lhs, Rational rhs, Rational answer) {
  58.     bool t(false);
  59.     if (lhs-rhs == answer) {
  60.         std::cout<<"Operator -  success:"<<lhs<<"-"<<rhs<<"="<<answer<<std::endl;
  61.         t = true;
  62.     } else {
  63.         std::cout<<"Operator -  error:"<<lhs<<"-"<<rhs<<"="<<answer<<std::endl;
  64.         t = false;
  65.     }
  66.  
  67.     lhs-=rhs;
  68.  
  69.     if (lhs == answer) {
  70.         std::cout<<"Operator -= success:"<<lhs<<"="<<answer<<std::endl;
  71.         t = true;
  72.     } else {
  73.         std::cout<<"Operator -= success:"<<lhs<<"="<<answer<<std::endl;
  74.         t = false;
  75.     }
  76.     return t;
  77. }
  78. bool testComplexMultiplication(Rational lhs, Rational rhs, Rational answer) {
  79.     bool t(false);
  80.     if (lhs*rhs == answer) {
  81.         std::cout<<"Operator *  success:"<<lhs<<"*"<<rhs<<"="<<answer<<std::endl;
  82.         t = true;
  83.     } else {
  84.         std::cout<<"Operator *  error:"<<lhs<<"*"<<rhs<<"="<<answer<<std::endl;
  85.         t = false;
  86.     }
  87.  
  88.     lhs *= rhs;
  89.  
  90.     if (lhs == answer) {
  91.         std::cout<<"Operator *= success:"<<lhs<<"="<<answer<<std::endl;
  92.         t = true;
  93.     } else {
  94.         std::cout<<"Operator *= error:"<<lhs<<"="<<answer<<std::endl;
  95.         t = false;
  96.     }
  97.     return t;
  98. }
  99. bool testComplexDividing(Rational lhs, Rational rhs, Rational answer) {
  100.     bool t(false);
  101.     if (lhs/rhs == answer) {
  102.         std::cout<<"Operator /  success:"<<lhs<<"/"<<rhs<<"="<<answer<<std::endl;
  103.         t = true;
  104.     } else {
  105.         std::cout<<"Operator /  error:"<<lhs<<"/"<<rhs<<"="<<answer<<std::endl;
  106.         t = false;
  107.     }
  108.  
  109.     lhs /= rhs;
  110.  
  111.     if (lhs == answer) {
  112.         std::cout<<"Operator /= success:"<<lhs<<"="<<answer<<std::endl;
  113.         t = true;
  114.     } else {
  115.         std::cout<<"Operator /= error:"<<lhs<<"="<<answer<<std::endl;
  116.         t = false;
  117.     }
  118.     return t;
  119. }
  120.  
  121. bool complexEqualityTest() {
  122.     //TODO add more tests here
  123.     if (testComplexEquality(Rational(1,1), Rational(1,1))
  124.         && testComplexUnequality(Rational(1,1), Rational(1,2)))
  125.         return true;
  126.     return false;
  127. }
  128.  
  129. int main() {
  130.     using namespace std;
  131.     Rational b(1, 1);
  132.     cin >> b;
  133.     cout << b;
  134.  
  135.     if (complexEqualityTest()) {
  136.         Rational lhs(1,1);
  137.         Rational rhs(5, 5);
  138.  
  139.         testComplexSum(lhs, rhs, Rational(2, 1));
  140.         testComplexSubtracting(lhs, rhs, Rational(0, 1));
  141.         testComplexMultiplication(lhs, rhs, Rational(1,1));
  142.         testComplexDividing(lhs, rhs, Rational(1,1));
  143.     }
  144.     return 0;
  145. }
RAW Paste Data