Готови решения > C/C++/Qt/etc
Qt 4.7.4 C++ Class за Бройни Системи dec, bin, hex
(1/1)
haccac:
systems.h
--- Код: C++ (QT) ---#ifndef SYSTEMS_H#define SYSTEMS_H#include <QString>#include <QByteArray> class Systems{public: Systems(); int err[5]; QString rez10_to_2; int rez2_to_10; QByteArray rez10_to_16; int rez16_to_10; void set_num_10_to_2(int number); void set_num_2_to_10(QString number); void set_num_10_to_16(int number); void set_num_16_to_10(QByteArray number);private: int num10_to_2; QString num2_to_10; int num10_to_16; QByteArray num16_to_10; void from_10_to_16(); void from_16_to_10(); void from_10_to_2(); void from_2_to_10();}; #endif // SYSTEMS_H //------------------------------------------------------------------------------------
systems.cpp
--- Код: C++ (QT) ---#include "systems.h"#include <QChar>#include <QString>#include <QByteArray> Systems::Systems(){}void Systems::from_10_to_2(){ int num = num10_to_2; QString n512 = "0"; QString n256 = "0"; QString n128 = "0"; QString n64 = "0"; QString n32 = "0"; QString n16 = "0"; QString n8 = "0"; QString n4 = "0"; QString n2 = "0"; QString n1 = "0"; if(num < 1024 && num >= 512) { num = num - 512; n512 = "1"; } if(num < 512 && num >= 256) { num = num - 256; n256 = "1"; } if(num < 256 && num >= 128) { num = num - 128; n128 = "1"; } if(num < 128 && num >= 64) { num = num - 64; n64 = "1"; } if(num < 64 && num >= 32) { num = num - 32; n32 = "1"; } if(num < 32 && num >= 16) { num = num - 16; n16 = "1"; } if(num < 16 && num >= 8 ) { num = num - 8; n8 = "1"; } if(num < 8 && num >= 4) { num = num - 4; n4 = "1"; } if(num < 4 && num >= 2) { num = num - 2; n2 = "1"; } if(num < 2 && num == 1) { num = num - 1; n1 = "1"; } // Това може да се съкрати, но по-късно разбрах как ства, който се сеща нека си го оправи. 8) rez10_to_2 = n512 + n256 + n128 + n64 + n32 + n16 + n8 + n4 + n2 + n1;} void Systems::set_num_10_to_2(int number){ if(number < 1024 && number >= 0) { num10_to_2 = number; from_10_to_2(); } else { err[1] = 1; }} void Systems::from_2_to_10(){ int rez = 0; int arr_i = 1; int arr[num2_to_10.size() - 1]; QChar ch[num2_to_10.size() - 1]; for(int i = 0; i<num2_to_10.size(); i++) { ch[i] = num2_to_10.at((num2_to_10.size() - 1)- i); arr[i] = arr_i; if(ch[i] == '1') { rez += arr[i]; } else if(ch[i] == '0') { rez += 0; } else { err[2] = 1; break; } arr_i += arr_i; } rez2_to_10 = rez;} void Systems::set_num_2_to_10(QString number){ QChar ch[number.size() - 1]; for(int i = 0; i<number.size(); i++) { ch[i] = number.at((number.size() - 1)- i); if(ch[i] != '1' || ch[i] != '0') { err[2] = 1; break; } } num2_to_10 = number; from_2_to_10();} void Systems::from_10_to_16(){ rez10_to_16.setNum(num10_to_16, 16);} void Systems::set_num_10_to_16(int number){ if(number) { num10_to_16 = number; from_10_to_16(); } else { err[3] = 1; }} void Systems::from_16_to_10(){ bool ok; rez16_to_10 = num16_to_10.toInt(&ok,16);} void Systems::set_num_16_to_10(QByteArray number){ if(!number.isEmpty()) { num16_to_10 = number; from_16_to_10(); } else { err[4] = 1; }} //---------------------------------------------------------------------------------------
main.cpp
--- Код: C++ (QT) ---#include <QtCore/QCoreApplication>#include "systems.h"#include <QDebug>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv);Systems s;s.set_num_10_to_2(10);qDebug() << s.rez10_to_2;s.set_num_2_to_10(s.rez10_to_2);qDebug() << s.rez2_to_10;s.set_num_16_to_10("1f");qDebug() << s.rez16_to_10;s.set_num_10_to_16(31);qDebug() << s.rez10_to_16; return a.exec();}
Навигация
[0] Списък на темите
Премини на пълна версия