class Converter{ private: // vector<int> a; // int iriginal; public: //, 2 : , Converter(string str, int original){ this->iriginal = original; // for ( int i=0; i < str.length(); i++ ){ this->a.push_back(charToInt(str[i])); } } // , -1 int charToInt(char c){ if ( c >= '0' && c <= '9' && (c - '0') < this->iriginal ){ return c - '0'; }else{ if ( c >= 'A' && c <= 'Z' && (c - 'A') < this->iriginal ){ return c - 'A' + 10; }else { return -1; } } } // char intToChar(int c){ if ( c >= 0 && c <= 9 ){ return c + '0'; }else{ return c + 'A' - 10; } } // int nextNumber(int final){ int temp = 0; for ( int i = 0; i<this->a.size(); i++){ temp = temp*this->iriginal + this->a[i]; a[i] = temp / final; temp = temp % final; } return temp; } // true - false bool zero(){ for ( int i=0; i<this->a.size(); i++ ){ if ( a[i] != 0 ){ return false; } } return true; } // string convertTo(int final){ vector<int> b; int size = 0; do { b.push_back(this->nextNumber(final)); size++; }while( !this->zero() ); string sTemp=""; for (int i=b.size()-1; i>=0; i--){ sTemp += intToChar(b[i]); } return sTemp; } };
// , string inputFile = argv[1]; // int original = atol(argv[2]); // int final = atol(argv[3]); //, string origNumber; ifstream fin(inputFile.c_str()); if ( fin.is_open() ){ fin >> origNumber; }else{ cout << "File " << inputFile << " not open" << endl; // - , origNumber = inputFile; } fin.close(); Converter conv(origNumber,original); // , if ( argc > 4 ){ // string outputFile = argv[4]; ofstream fout(outputFile.c_str()); if ( fout.is_open() ){ fout << conv.convertTo(final); }else{ cout << "File " << outputFile << " not create" << endl; cout << conv.convertTo(final) << endl; } }else{ cout << conv.convertTo(final) << endl; }
Source: https://habr.com/ru/post/146167/
All Articles