Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <conio.h>
- using namespace std;
- string bnm_add(string a, string b)
- {
- string wynik;
- string length; // length == najdウuソszy string
- int aln, bln, switcher(0); // switcher -> dodaje 1 w lewej komce, gdy 199 + 001 = 19: -> 1:0 -> 200
- aln = a.length();
- bln = b.length();
- //printf("\na: %i \nb: %i\n\n", aln, bln);
- if (aln > bln)
- { //DODAWANIE ZER : 199 + 1 -> 199 + 001 = 200, (aby 199 + 1 != 299)
- length = a;
- reverse(b.begin(), b.end());
- while (true)
- {
- b += '0';
- bln++;
- if (bln == aln)break;
- }
- reverse(b.begin(), b.end()); // odwracamy 1 -> 100 -> 001 itd.
- }
- else if (bln > aln)
- {
- length = b;
- reverse(a.begin(), a.end());
- while (true)
- {
- a += '0';
- aln++;
- if (aln == bln)break;
- }
- reverse(a.begin(), a.end());
- }
- else if (aln == bln)
- {
- length = a;
- }
- for (size_t x = length.length() - 1; x >= 0; x--)
- {
- char buffor;
- buffor = a[x] + (b[x] - 48) + switcher; // Wstawianie do bufora wartosci, np. 1 + 2 (buff = 49 + (50-48) + 0)
- if (buffor < 48)
- {
- buffor += 48;
- }
- switcher = 0;
- if (buffor > 57) // przypadek np. 96 + 85
- {
- switcher = 1;
- buffor -= 10;
- }
- wynik += buffor; // dodawanie buffora do wyniku
- buffor = { 0 };
- if (x == 0 && switcher == 1)
- {
- wynik += '1'; // Przechodzenie np. 9 -> 10, 999 -> 1249, dodawanie jedynki po lewej.
- }
- if (x == 0)break;
- }
- reverse(wynik.begin(), wynik.end()); // na koniec odwracamy (wszystko byウo odwrotnie naliczane)
- //printf("\n %s\n+ %s\n____________________________________________________\n %s\n\n", a.c_str(), b.c_str(), wynik.c_str());
- return wynik;
- }
- class zliczanie
- {
- bool if_number = false;
- bool czy_palindrom = false;
- string buff, nbuff, i1npocz;
- long long lit = 0;
- string pocz, npocz;
- public:
- bool lel = true;
- void wprowadzanie()
- {
- cout << endl;
- buff.clear();
- cin >> buff;
- cout << endl;
- for (size_t i = 0; i < buff.length(); i++)
- {
- if (buff[i] == '0' || buff[i] == '1' || buff[i] == '2' || buff[i] == '3' || buff[i] == '4' || buff[i] == '5' || buff[i] == '6' || buff[i] == '7' || buff[i] == '8' || buff[i] == '9')
- {
- if_number = true;
- }
- else
- {
- if_number = false;
- cin.clear();
- cout << "GIVE A NUMBER!" << endl;
- return wprowadzanie();
- }
- }
- }
- void check()
- {
- while (czy_palindrom == false)
- {
- if (lit == 0)
- {
- string a = buff;
- pocz = a;
- nbuff = buff;
- reverse(nbuff.begin(), nbuff.end());
- string b = nbuff;
- npocz = b;
- }
- if (lit > 1000)
- {
- cout << "Value your computer and give another number ;) " << endl;
- lel = false;
- break;
- }
- if (nbuff == buff)
- {
- czy_palindrom = true;
- lel = false;
- break;
- }
- else
- {
- buff = bnm_add(buff, nbuff);
- //cout << endl << "buff: " << buff << endl;
- lit++;
- nbuff = buff;
- reverse(nbuff.begin(), nbuff.end());
- if (lit == 1)
- {
- i1npocz = nbuff;
- }
- return check();
- }
- }
- }
- void wynik()
- {
- if (czy_palindrom == true)
- {
- cout << endl << "Number " << pocz << " after " << lit << " iterations is a palindrome: " << buff << endl << endl;
- }
- }
- };
- int main()
- {
- zliczanie a1;
- a1.wprowadzanie();
- while (a1.lel == true)
- {
- a1.check();
- }
- a1.wynik();
- cout << "click 'r' to continue" << endl;
- char t = _getch();
- if (t == 'r')
- {
- cout << endl << endl << "_______" << endl;
- return main();
- }
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement