Advertisement
kirya_shkolnik

Кристиночке, дорогой - перевод 10->2

Oct 16th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "math.h"
  4. #include "string"
  5.  
  6. using namespace std;
  7.  
  8. MainWindow::MainWindow(QWidget *parent)
  9. : QMainWindow(parent)
  10. , ui(new Ui::MainWindow)
  11. {
  12. ui->setupUi(this);
  13. }
  14.  
  15. MainWindow::~MainWindow()
  16. {
  17. delete ui;
  18. }
  19.  
  20. int dectobin (int value)
  21. {
  22. int output=0, d=1; // output - итоговое число в 2 СС, d - множитель (разряды)
  23. while(value > 0) // пока число изначальное больше нуля (пока оно есть)
  24. {
  25. output += (value%2) * d; // остаток от деления на два умноженный на разряд
  26. value /= 2; // деление на два
  27. d*=10; // увеличить разряд на 1
  28. }
  29. return output; // вернуть итог туда откуда вызвали
  30. }
  31.  
  32.  
  33. void MainWindow::on_pushButton_Result_clicked()
  34. {
  35. QString str;
  36. int input;
  37.  
  38. str = ui->lineEdit_Decimal->text(); // input number
  39. input=str.toInt();
  40. int outputint = dectobin(input);
  41. if(outputint == 0 && str !='0'){
  42. ui->label_Binary->setText("Error!");
  43. }
  44. else{
  45. QString output = QString::number(outputint); // получение числа в int и перевод в QString
  46. ui->label_Binary->setText(output); // вписать ответ в QString в поле label
  47.  
  48. }
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement