Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <sstream>
- using namespace std;
- //#include "riesenie7.h"
- //Toto je vlozeny hlavickovy subor riesenie7.h
- #if !defined( _RIESENIE_H_ )
- #define _RIESENIE_H_
- //1.uloha
- class VeryLargeInt {
- string cislo;
- const int getCislica(unsigned int i) const;
- public:
- VeryLargeInt(const string &vstup = "0");
- void vloz(const string &vstup);
- const string value() const;
- //2.uloha
- VeryLargeInt(const VeryLargeInt &vstup);
- VeryLargeInt(long long l);
- VeryLargeInt & operator=(const VeryLargeInt &vstup);
- //3.uloha
- bool operator==(const VeryLargeInt &vstup) const;
- bool operator==(const string &vstup) const;
- bool operator<(const VeryLargeInt &vstup) const;
- bool operator<(const string &vstup) const;
- //4.uloha
- const VeryLargeInt operator+(const VeryLargeInt &vstup) const;
- const VeryLargeInt operator+(const string &vstup) const;
- //5.uloha
- const VeryLargeInt operator*(const VeryLargeInt &vstup) const;
- const VeryLargeInt operator*(const string &vstup) const;
- };
- #endif
- bool DUMMY_BOOL = false;
- string DUMMY_STRING = "";
- //1.uloha
- VeryLargeInt::VeryLargeInt(const string &vstup) {
- bool nula = true;
- string c = "";
- for (int i = 0; i < vstup.size() ; i++) {
- if (vstup[i] != '0' && vstup[i] != '1' && vstup[i] != '2' && vstup[i] != '3' && vstup[i] != '4' && vstup[i] != '5' && vstup[i] != '6' && vstup[i] != '7' && vstup[i] != '8' && vstup[i] != '9' && vstup[i] != ' ') {
- nula = true;
- break;
- }
- if (vstup[i] == '0' && nula == false) {
- c += vstup[i];
- }
- if (vstup[i] != '0' && vstup[i] != ' ') {
- nula = false;
- c += vstup[i];
- }
- }
- if (nula == true) {
- cislo = "0";
- }
- else {
- cislo = c;
- }
- }
- void VeryLargeInt::vloz(const string &vstup) {
- bool nula = true;
- string c = "";
- for (int i = 0; i < vstup.size(); i++) {
- if (vstup[i] != '0' && vstup[i] != '1' && vstup[i] != '2' && vstup[i] != '3' && vstup[i] != '4' && vstup[i] != '5' && vstup[i] != '6' && vstup[i] != '7' && vstup[i] != '8' && vstup[i] != '9' && vstup[i] != ' ') {
- nula = true;
- break;
- }
- if (vstup[i] == '0' && nula == false) {
- c += vstup[i];
- }
- if (vstup[i] != '0' && vstup[i] != ' ') {
- nula = false;
- c += vstup[i];
- }
- }
- if (nula == true) {
- cislo = "0";
- }
- else {
- cislo = c;
- }
- }
- const string VeryLargeInt::value() const {
- return cislo;
- }
- //2.uloha
- VeryLargeInt::VeryLargeInt(const VeryLargeInt &vstup){
- vloz(cislo);
- }
- VeryLargeInt::VeryLargeInt(long long l) {
- if (l < 0) {
- vloz("0");
- }
- else {
- string s = static_cast<ostringstream*>(&(ostringstream() << l))->str();
- vloz(s);
- }
- }
- VeryLargeInt & VeryLargeInt::operator=(const VeryLargeInt &vstup) {
- cislo = vstup.cislo;
- return *this;
- }
- //3.uloha
- bool VeryLargeInt::operator==(const VeryLargeInt &vstup) const {
- if (cislo.size() != vstup.cislo.size()) { return false; }
- for (int i = 0; i < cislo.size(); i++) {
- if (cislo[i] != vstup.cislo[i]) {
- return false;
- }
- }
- return true;
- }
- bool VeryLargeInt::operator==(const string &c) const {
- if (cislo.size() != c.size()) { return false; }
- for (int i = 0; i < cislo.size(); i++) {
- if (cislo[i] != c[i]) {
- return false;
- }
- }
- return true;
- }
- bool VeryLargeInt::operator<(const VeryLargeInt &vstup) const {
- if (cislo.size() < vstup.cislo.size()) { return true; }
- if (cislo.size() > vstup.cislo.size()) { return false; }
- if (cislo == vstup.cislo) { return false; }
- for (int i = 0; true; i++) {
- if (cislo[i]-'0' < vstup.cislo[i]-'0') {
- return true;
- }
- }
- return true;
- }
- bool VeryLargeInt::operator<(const string &c) const {
- if (cislo.size() < c.size()) { return true; }
- if (cislo.size() > c.size()) { return false; }
- if (cislo == c) { return false; }
- for (int i = 0; i < cislo.size(); i++) {
- if (cislo[i] - '0' > c[i] - '0') {
- return false;
- }
- }
- return true;
- }
- //4.uloha
- const VeryLargeInt VeryLargeInt::operator+(const VeryLargeInt &vstup) const {
- /* TODO
- * Porovnajte pocet cifier oboch cisel.
- * Tu dlhsiu skopirujte do stringu vacsi a kratsiu do mensi.
- * Spustite cyklus po velkost mensieho. Postupujte odzadu a
- * spocitajte obe hodnoty plus zvysok.
- */
- VeryLargeInt temp;
- return temp;
- }
- const VeryLargeInt VeryLargeInt::operator+(const string &c) const {
- /* TODO
- * Vytvorte dve lokalne instancie velmi velkych cisel.
- * Tieto instancie scitejte a vratte vysledok.
- */
- VeryLargeInt temp;
- return temp;
- }
- const VeryLargeInt VeryLargeInt::operator*(const VeryLargeInt &vstup) const {
- /* TODO
- * Vytvorte si pomocne pole integerov, ktore bude dlhsie o jedno,
- * ako je sucet dlzok oboch cisel a vlozte do neho same 0. Potom
- * chod v cykle od najmensich cisel a vykonajte ich nasobenie.
- * Treba si vymysliet algoritmus, ako to urobit.
- */
- VeryLargeInt d;
- return d;
- }
- const VeryLargeInt VeryLargeInt::operator*(const string &c) const {
- /* TODO
- * Postupujte podobne, ako pri scitani.
- */
- VeryLargeInt temp;
- return temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement