Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I will share my whole code here, and I need to code the set base and prime to make it work. if someone can finish
I will share my whole code here, and I need to code the set base and prime to make it work. if someone can finish it, it would be more than awesome. I'm completely blocked
The code has to exectue everything that is given in the main.Main can be changed. so here is my code:
MAIN:
#include#include "Uint.hpp" using namespace std; Uint fibo(size_t n) { Uint f_i_moins_1(0), f_i(1), f_i_plus_1; if (n == 0) return f_i_moins_1; if (n == 1) return f_i; for (size_t i = 2; i <= n; i++){ f_i_plus_1 = f_i_moins_1 + f_i; f_i_moins_1 = f_i; f_i = f_i_plus_1; } return f_i; } Uint factorielle(size_t n) { Uint res(1); for (size_t i = 2; i <= n; i++) res *= i; return res; } int main() { Uint a(10); cout << "Constructeur avec size_t OK "; Uint b; cout << "Constructeur vide OK "; b = 3; cout << "Affectation OK "; const Uint c = 13; cout << uint64_t(c) << " = 13: cast explicite vers uint64_t "; if (a < c) cout << "Operateur < OK "; else cout << "a < c : " << (a < c) << " pas bon!!! "; b += a; cout << "Operateur+= "; if (c == b) cout << "Comparaison == OK "; else cout << "c == b : " << (c == b) << " Operateur == ou += pas bon!!! "; a = fibo(50); cout << "fibo(50) "; b = fibo(51); const Uint d = fibo(52); if (d == a + b) cout << "Operateur+ OK "; else cout << "+ ou == pas bon!!! "; if (d - b != a) cout << "- ou != pas bon!!! "; else cout << "Operateur- OK "; a = factorielle(50); cout << "Factorielle "; b = factorielle(51); if (51 * a == b) cout << "Operateur* et *= OK "; else cout << "* ou *= ou == pas bon!!! "; Uint v=51; if (v != b / a) cout << "/ ou != pas bon!!! "; //else cout << "Operateur/ OK "; cout << "51! en base 10 = " << b << endl; //Le modificateur set_base(...) n'affecte que la prochaine impression d'un Uint cout << set_base(16) << "Modificateur d'impression Uint "; cout << "51! en base 16 = " << b << endl; cout << "52e terme de la suite de Fibonacci, en base 10 = " << d << endl; a = 1; a <<= 99; cout << "Operateur <<= "; cout << "2 ^ 99 = " << a << endl; b = (Uint(1) << 100) -1; cout << "Operateur << "; //cout << set_base(16, LOWER_CASE); cout << "2 ^ 100 - 1 (base 16, bas de casse) = " << b << endl; cout << "10 plus petits nombres premiers > 2^99 "; size_t nb = 0; while (nb < 10) { if (prime(a)) { cout << a << endl; nb++; } ++a; } cout << "10 plus grands nombres premiers < 2^100 "; nb = 0; while (nb < 10) { if (prime(b)) { cout << b << endl; nb++; } --b; }
MY Class Uint.hpp:
// // Created by dinga on 22.12.2022. // #ifndef FINALOLABO11_UINT_HPP #define FINALOLABO11_UINT_HPP #includeusing namespace std; class Uint { private: string nb; public: // Constructors Uint(); // Default constructeur Uint(size_t n); // Constructeur avec size_t explicit operator uint64_t() const; //Cast explicite //oprateur rationnel bool operator<(const Uint &autre) const; bool operator>(const Uint &autre) const; bool operator==(const Uint &autre) const; bool operator!=(const Uint &autre) const; Uint &operator=(const Uint &rhs); //oprateur d'affectation friend Uint operator+(Uint lhs, const Uint &rhs); friend Uint operator*(Uint lhs, const Uint &rhs); friend Uint operator-(Uint lhs, const Uint &rhs); friend Uint operator/(Uint lhs, const Uint &rhs); friend Uint operator%(Uint lhs, const Uint &rhs); Uint &operator<<=(const uint64_t &autre); //Operateurs arithmetiques Uint &operator+=(const Uint &other); Uint &operator-=(const Uint &other); Uint &operator*=(const Uint &other); Uint &operator/=(const Uint &other); Uint &operator%=(const Uint &other); Uint &operator++(); Uint &operator--(); Uint operator++(int); Uint operator--(int); //Constructeur de decalage des bits friend Uint operator<<(Uint lhs, const uint64_t &rhs); friend std::ostream &operator<<(std::ostream &os, const Uint &autre);// Surcharge l'oprateur << pour sortir un Uint friend std::istream &operator>>(std::istream &in, Uint &autre);// Surcharger l'oprateur >> pour lire dans un Uint void eraseZero(); }; #endif //FINALOLABO11_UINT_HPP
And in the end :
Uint.cpp:
// // Created by dinga on 22.12.2022. // #include "Uint.hpp" #includeusing namespace std; using namespace std; uint64_t convert(string n){ uint64_t result = 0; uint64_t base = 1; for (size_t i = n.size() - 1; i != SIZE_MAX; --i) { if (n[i] == '1') result += base; base *= 2; } return result; } Uint::Uint() = default; Uint::Uint(size_t n){ string u; while (n > 0) { if (n % 2 == 0) u.insert(u.begin(), '0'); else u.insert(u.begin(), '1'); n /= 2; } this->nb = u; } Uint::operator uint64_t ()const{ uint64_t result = 0; uint64_t base = 1; for (size_t i = nb.size() - 1; i != SIZE_MAX; --i) { if (nb[i] == '1') result += base; base *= 2; } return result; } bool Uint::operator<(const Uint& rhs) const { if (nb.size() > rhs.nb.size() || nb.size() == rhs.nb.size() && nb > rhs.nb) return false; else return true; } bool Uint::operator>(const Uint& rhs) const { if (nb.size() < rhs.nb.size() || nb.size() == rhs.nb.size() && nb < rhs.nb) return false; else return true; } bool Uint::operator==(const Uint& rhs) const { if(nb == rhs.nb) return true; else return false; } bool Uint::operator!=(const Uint& rhs) const { if (nb == rhs.nb) return false; else return true; } Uint& Uint::operator = (const Uint& rhs) { // do the copy nb = rhs.nb; // return the existing object so we can chain this operator return *this; } Uint& Uint::operator+=(const Uint& autre) { Uint sum; Uint tmp = autre; if (nb.size() > tmp.nb.size()) { while (tmp.nb.size() != nb.size()) { tmp.nb = '0' + tmp.nb; } } else if (tmp.nb.size() > nb.size()) { while (nb.size() != tmp.nb.size()) { nb = '0' + nb; } } int retenue = 0; sum.nb.resize(nb.length()); for (int i = nb.size()-1; i>=0 ; i--) { int n1=nb[i]-'0'; int n2=tmp.nb[i]-'0'; int result = n1 + n2 + retenue; retenue = result / 2; result = result % 2; sum.nb[i] = result + '0'; } if (retenue > 0 ){ sum.nb = '1' + sum.nb; } return *this=sum; } Uint& Uint::operator*=(const Uint& autre) { Uint mult; Uint tmp = autre; string d="0"; if (nb == "0" || tmp.nb == "0") { mult.nb="0"; } else{ for (int i = tmp.nb.size() - 1; i >= 0; i--) { if (tmp.nb[i] == '1') { if(mult.nb=="0") { mult.nb = nb; }else mult+=(*this); } else if (tmp.nb[i] == '0') { mult.nb= '0' + mult.nb; } nb.append(d); } } return *this=mult; } Uint& Uint::operator-=(const Uint& autre) { int x,y; Uint result; Uint tmp=autre; int carry = 0; int i = nb.size() - 1; int j = autre.nb.size() - 1; while (i >= 0 || j >= 0 || carry > 0) { if(i>=0){ x =nb[i--] - '0'; }else x=0; if(j>=0){ y =tmp.nb[j--] - '0'; }else y=0; int sum = x - y - carry; carry = (sum < 0) ? 1 : 0; result.nb.push_back((sum + 2) % 2 + '0'); } reverse(result.nb.begin(), result.nb.end()); result.eraseZero(); return *this=result; } Uint& Uint::operator/=(const Uint& autre) { Uint remainder; Uint div; Uint tmp = autre; if (nb == tmp.nb) { div.nb += '1'; } else { for (int i = 0; i < nb.size(); i++) { remainder.nb += nb[i]; uint64_t w = convert(remainder.nb); uint64_t ww = convert(tmp.nb); if (remainder.nb.size() < tmp.nb.size()) { div.nb += "0"; } else if (remainder.nb.size() >= tmp.nb.size() && (w - ww >= 0)) { div.nb += "1"; remainder -= (tmp); } else div.nb += '0'; } } div.eraseZero(); return*this=div; } Uint &Uint::operator%=(const Uint &autre) { Uint remainder; Uint div; Uint tmp = autre; if (nb == tmp.nb) { div.nb += '1'; } else { for (int i = 0; i < nb.size(); i++) { remainder.nb += nb[i]; uint64_t w = convert(remainder.nb); uint64_t ww = convert(tmp.nb); if (remainder.nb.size() < tmp.nb.size()) { div.nb += "0"; } else if (remainder.nb.size() >= tmp.nb.size() && (w - ww >= 0)) { div.nb += "1"; remainder -= (tmp); } else div.nb += '0'; } } remainder.eraseZero(); return*this=remainder; } Uint& Uint::operator++(){*this+=1; return *this;} Uint& Uint::operator--(){*this-=1; return *this;} Uint Uint::operator++(int) { Uint tmp =*this; ++*this; return tmp; } Uint Uint::operator--(int){ Uint tmp =*this; --*this; return tmp; } Uint &Uint::operator<<=(const uint64_t &autre) { nb.append(autre, '0'); return *this; } Uint operator+(Uint lhs, const Uint &rhs) { lhs += rhs; return lhs; } Uint operator-(Uint lhs, const Uint &rhs) { lhs -= rhs; return lhs; } Uint operator*(Uint lhs, const Uint &rhs) { lhs *= rhs; return lhs; } Uint operator/(Uint lhs, const Uint &rhs) { lhs /= rhs; return lhs; } Uint operator%(Uint lhs, const Uint &rhs) { lhs %= rhs; return lhs; } Uint operator<<(Uint lhs, const uint64_t &rhs) { return lhs <<= rhs; } ostream &operator<<(std::ostream &os, const Uint &autre) { os << autre.nb; return os; } istream &operator>>(std::istream &in, Uint &autre) { in >> autre.nb; return in; } void Uint::eraseZero() { int i; while (this->nb[0] == '0' && this->nb.size() > 1) { i = 1; nb.erase(0, i); } }
Thank you in advance
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started