Advertisement
Guest User

cviko7

a guest
Nov 23rd, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. //#include "riesenie7.h"
  9.  
  10. //Toto je vlozeny hlavickovy subor riesenie7.h
  11. #if !defined( _RIESENIE_H_ )
  12. #define _RIESENIE_H_
  13. //1.uloha
  14. class VeryLargeInt {
  15.     string cislo;
  16.     const int getCislica(unsigned int i) const;
  17.   public:
  18.     VeryLargeInt(const string &vstup = "0");
  19.     void vloz(const string &vstup);
  20.     const string value() const;
  21. //2.uloha
  22.     VeryLargeInt(const VeryLargeInt &vstup);
  23.     VeryLargeInt(long long l);
  24.         VeryLargeInt & operator=(const VeryLargeInt &vstup);
  25. //3.uloha
  26.     bool operator==(const VeryLargeInt &vstup) const;
  27.     bool operator==(const string &vstup) const;
  28.     bool operator<(const VeryLargeInt &vstup) const;
  29.     bool operator<(const string &vstup) const;
  30. //4.uloha
  31.     const VeryLargeInt operator+(const VeryLargeInt &vstup) const;
  32.     const VeryLargeInt operator+(const string &vstup) const;
  33. //5.uloha
  34.     const VeryLargeInt operator*(const VeryLargeInt &vstup) const;
  35.     const VeryLargeInt operator*(const string &vstup) const;
  36.  
  37. };
  38. #endif
  39.  
  40. bool DUMMY_BOOL = false;
  41. string DUMMY_STRING = "";
  42.  
  43. //1.uloha
  44. VeryLargeInt::VeryLargeInt(const string &vstup) {
  45.     bool nula = true;
  46.     string c = "";
  47.     for (int i = 0; i < vstup.size() ; i++) {
  48.         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] != ' ') {
  49.             nula = true;
  50.             break;
  51.         }
  52.         if (vstup[i] == '0' && nula == false) {
  53.             c += vstup[i];
  54.         }
  55.         if (vstup[i] != '0' && vstup[i] != ' ') {
  56.             nula = false;
  57.             c += vstup[i];
  58.         }
  59.     }
  60.     if (nula == true) {
  61.         cislo = "0";
  62.     }
  63.     else {
  64.         cislo = c;
  65.     }
  66. }
  67.  
  68. void VeryLargeInt::vloz(const string &vstup) {
  69.     bool nula = true;
  70.     string c = "";
  71.     for (int i = 0; i < vstup.size(); i++) {
  72.         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] != ' ') {
  73.             nula = true;
  74.             break;
  75.         }
  76.         if (vstup[i] == '0' && nula == false) {
  77.             c += vstup[i];
  78.         }
  79.         if (vstup[i] != '0' && vstup[i] != ' ') {
  80.             nula = false;
  81.             c += vstup[i];
  82.         }
  83.     }
  84.     if (nula == true) {
  85.         cislo = "0";
  86.     }
  87.     else {
  88.         cislo = c;
  89.     }
  90. }
  91. const string VeryLargeInt::value() const {
  92.   return cislo;
  93. }
  94. //2.uloha
  95. VeryLargeInt::VeryLargeInt(const VeryLargeInt &vstup){
  96.     vloz(cislo);
  97. }
  98. VeryLargeInt::VeryLargeInt(long long l) {
  99.     if (l < 0) {
  100.         vloz("0");
  101.     }
  102.     else {
  103.         string s = static_cast<ostringstream*>(&(ostringstream() << l))->str();
  104.         vloz(s);
  105.     }
  106. }
  107. VeryLargeInt & VeryLargeInt::operator=(const VeryLargeInt &vstup) {
  108.     cislo = vstup.cislo;
  109.  
  110.     return *this;
  111. }
  112. //3.uloha
  113. bool VeryLargeInt::operator==(const VeryLargeInt &vstup) const {
  114.     if (cislo.size() != vstup.cislo.size()) { return false; }
  115.     for (int i = 0; i < cislo.size(); i++) {
  116.         if (cislo[i] != vstup.cislo[i]) {
  117.             return false;
  118.         }
  119.     }
  120.     return true;
  121. }
  122. bool VeryLargeInt::operator==(const string &c) const {
  123.     if (cislo.size() != c.size()) { return false; }
  124.     for (int i = 0; i < cislo.size(); i++) {
  125.         if (cislo[i] != c[i]) {
  126.             return false;
  127.         }
  128.     }
  129.     return true;
  130. }
  131. bool VeryLargeInt::operator<(const VeryLargeInt &vstup) const {
  132.     if (cislo.size() < vstup.cislo.size()) { return true; }
  133.     if (cislo.size() > vstup.cislo.size()) { return false; }
  134.     if (cislo == vstup.cislo) { return false; }
  135.     for (int i = 0; true; i++) {
  136.         if (cislo[i]-'0' < vstup.cislo[i]-'0') {
  137.             return true;
  138.         }
  139.     }
  140.     return true;
  141. }
  142. bool VeryLargeInt::operator<(const string &c) const {
  143.     if (cislo.size() < c.size()) { return true; }
  144.     if (cislo.size() > c.size()) { return false; }
  145.     if (cislo == c) { return false; }
  146.     for (int i = 0; i < cislo.size(); i++) {
  147.         if (cislo[i] - '0' > c[i] - '0') {
  148.             return false;
  149.         }
  150.     }
  151.     return true;
  152. }
  153. //4.uloha
  154. const VeryLargeInt VeryLargeInt::operator+(const VeryLargeInt &vstup) const {
  155.  /* TODO
  156.   * Porovnajte pocet cifier oboch cisel.
  157.   * Tu dlhsiu skopirujte do stringu vacsi a kratsiu do mensi.
  158.   * Spustite cyklus po velkost mensieho. Postupujte odzadu a
  159.   * spocitajte obe hodnoty plus zvysok.
  160.   */
  161.   VeryLargeInt temp;
  162.   return temp;
  163. }
  164. const VeryLargeInt VeryLargeInt::operator+(const string &c) const {
  165. /* TODO
  166.   * Vytvorte dve lokalne instancie velmi velkych cisel.
  167.   * Tieto instancie scitejte a vratte vysledok.
  168.   */
  169.     VeryLargeInt temp;
  170.   return temp;
  171. }
  172. const VeryLargeInt VeryLargeInt::operator*(const VeryLargeInt &vstup) const {
  173.  /* TODO
  174.   * Vytvorte si pomocne pole integerov, ktore bude dlhsie o jedno,
  175.   * ako je sucet dlzok oboch cisel a vlozte do neho same 0. Potom
  176.   * chod v cykle od najmensich cisel a vykonajte ich nasobenie.
  177.   * Treba si vymysliet algoritmus, ako to urobit.
  178.   */
  179.   VeryLargeInt d;
  180.   return d;
  181. }
  182. const VeryLargeInt VeryLargeInt::operator*(const string &c) const {
  183. /* TODO
  184.   * Postupujte podobne, ako pri scitani.
  185.   */
  186.   VeryLargeInt temp;
  187.   return temp;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement