Advertisement
Jgug

S4.LR1_1

Mar 22nd, 2013
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. //===========================================================
  2. //===[ windowinput.h ]=======================================
  3. //===========================================================
  4. #ifndef WINDOWINPUT_H
  5. #define WINDOWINPUT_H
  6.  
  7. #include <QWidget>
  8. #include <QLabel>
  9. #include <QLineEdit>
  10. #include <QPushButton>
  11. #include <QGridLayout>
  12.  
  13. //===========================================================
  14. class WindowInput : public QWidget
  15. {
  16.   Q_OBJECT
  17. public:
  18.   explicit WindowInput(QWidget *parent = 0);
  19.  
  20.   QLabel* lbl_1;
  21.   QLineEdit* lEd_1;
  22.   QPushButton* pBt_1;
  23.   QPushButton* pBt_2;
  24.   QGridLayout* grLay_1;
  25. signals:
  26.  
  27. public slots:
  28.  
  29. };
  30.  
  31. #endif // WINDOWINPUT_H
  32.  
  33. //===========================================================
  34. //===[ windowinput.cpp ]=====================================
  35. //===========================================================
  36. #include "windowinput.h"
  37. #include "solution.h"
  38.  
  39. WindowInput::WindowInput(QWidget *parent) :
  40.     QWidget(parent)
  41. {
  42.   lbl_1 = new QLabel("Значение переменной:");
  43.  
  44.   lEd_1 = new QLineEdit("0");
  45.     lEd_1->setFixedWidth(40);
  46.     lEd_1->setMaxLength(5);
  47.     lEd_1->selectAll();
  48.  
  49.   pBt_1 = new QPushButton("&Calculate");
  50.   pBt_2 = new QPushButton("&Quit");
  51.  
  52.   grLay_1 = new QGridLayout;
  53.     grLay_1->setMargin(5);
  54.     grLay_1->setSpacing(10);
  55.  
  56.   grLay_1->addWidget(lbl_1, 0, 0);
  57.   grLay_1->addWidget(lEd_1, 0, 1);
  58.   grLay_1->addWidget(pBt_1, 1, 0);
  59.   grLay_1->addWidget(pBt_2, 1, 1);
  60.  
  61.   connect(pBt_2, SIGNAL(clicked()), SLOT(close()));
  62.  
  63.   setLayout(grLay_1);
  64. }
  65.  
  66. //===========================================================
  67. //===[ windowoutput.h ]======================================
  68. //===========================================================
  69. #ifndef WINDOWOUTPUT_H
  70. #define WINDOWOUTPUT_H
  71.  
  72. #include <QWidget>
  73. #include <QLabel>
  74. #include <QLineEdit>
  75. #include <QPushButton>
  76. #include <QGridLayout>
  77.  
  78. class WindowOutput : public QWidget
  79. {
  80.   Q_OBJECT
  81. public:
  82.   explicit WindowOutput(QWidget *parent = 0);
  83.  
  84.   QLabel* lbl_2;
  85.   QLabel* lbl_3;
  86.   QLabel* lbl_4;
  87.   QLabel* lbl_5;
  88.   QLabel* lbl_6;
  89.   QPushButton* pBt_3;
  90.   QPushButton* pBt_4;
  91.   QGridLayout* grLay_2;
  92. signals:
  93.  
  94. public slots:
  95.  
  96. };
  97.  
  98. #endif // WINDOWOUTPUT_H
  99.  
  100.  
  101. //===========================================================
  102. //===[ windowoutput.cpp ]====================================
  103. //===========================================================
  104. #include "windowinput.h"
  105. #include "solution.h"
  106.  
  107. WindowInput::WindowInput(QWidget *parent) :
  108.     QWidget(parent)
  109. {
  110.   lbl_1 = new QLabel("Значение переменной:");
  111.  
  112.   lEd_1 = new QLineEdit("0");
  113.     lEd_1->setFixedWidth(40);
  114.     lEd_1->setMaxLength(5);
  115.     lEd_1->selectAll();
  116.  
  117.   pBt_1 = new QPushButton("&Calculate");
  118.   pBt_2 = new QPushButton("&Quit");
  119.  
  120.   grLay_1 = new QGridLayout;
  121.     grLay_1->setMargin(5);
  122.     grLay_1->setSpacing(10);
  123.  
  124.   grLay_1->addWidget(lbl_1, 0, 0);
  125.   grLay_1->addWidget(lEd_1, 0, 1);
  126.   grLay_1->addWidget(pBt_1, 1, 0);
  127.   grLay_1->addWidget(pBt_2, 1, 1);
  128.  
  129.   connect(pBt_2, SIGNAL(clicked()), SLOT(close()));
  130.  
  131.   setLayout(grLay_1);
  132. }
  133.  
  134. //===========================================================
  135. //===[ solution.h ]==========================================
  136. //===========================================================
  137. #ifndef SOLUTION_H
  138. #define SOLUTION_H
  139.  
  140. #include <QObject>
  141.  
  142. class Solution : public QObject
  143. {
  144.   Q_OBJECT
  145. private:
  146.   double var_1;
  147.   double res_1, res_2;
  148. public:
  149.   Solution();
  150. public slots:
  151.   void Run_1();
  152.   void Run_2();
  153.   void Print_1();
  154.   void Print_2();
  155.   void SetVal(QString);
  156. signals:
  157.   void ResOut_1(QString);
  158.   void ResOut_2(QString);
  159. };
  160.  
  161. #endif // SOLUTION_H
  162.  
  163. //===========================================================
  164. //===[ solution.cpp ]========================================
  165. //===========================================================
  166. #include "solution.h"
  167. #include <QDebug>
  168. #include <math.h>
  169.  
  170. Solution::Solution() : QObject()
  171. {
  172. }
  173.  
  174. void Solution::SetVal(QString a)
  175. {
  176.   var_1 = a.toInt();
  177.   qDebug() << var_1;
  178. }
  179.  
  180. void Solution::Run_1()
  181. {
  182.   res_1 = ((var_1+2)/sqrt(2*var_1) - var_1/(sqrt(2*var_1)+2) + 2/(var_1 - sqrt(2*var_1))) * ((sqrt(var_1) - sqrt(2)) / (var_1+2));
  183.   qDebug() << var_1;
  184.   qDebug() << res_1;
  185. }
  186.  
  187. void Solution::Run_2()
  188. {
  189.   res_2 = 1 / (sqrt(var_1) + sqrt(2));
  190.   qDebug() << var_1;
  191.   qDebug() << res_2;
  192. }
  193.  
  194. void Solution::Print_1()
  195. {
  196.   emit ResOut_1(QString::number(res_1));
  197. }
  198.  
  199. void Solution::Print_2()
  200. {
  201.   emit ResOut_2(QString::number(res_2));
  202. }
  203.  
  204. //===========================================================
  205. //===[ main.cpp ]============================================
  206. //===========================================================
  207. #include "solution.h"
  208. #include "windowinput.h"
  209. #include "windowoutput.h"
  210. #include <QApplication>
  211.  
  212. int main(int argc, char *argv[])
  213. {
  214.   QApplication app(argc, argv);
  215.   WindowInput window_1;
  216.   WindowOutput window_2;
  217.   Solution solution;
  218.  
  219.   window_1.setWindowTitle("Ввод значений");
  220.   window_1.resize(150, 100);
  221.  
  222.   window_2.setWindowTitle("Результаты вычислений");
  223.   window_2.resize(150, 100);
  224.  
  225.   QObject::connect(window_1.lEd_1, SIGNAL(textChanged(QString)),
  226.                    &solution, SLOT(SetVal(QString)));
  227.   //--кнопки--окон-----------------------------------
  228.   QObject::connect(window_1.pBt_1, SIGNAL(clicked()),
  229.                    &window_1, SLOT(hide()));
  230.   QObject::connect(window_1.pBt_1, SIGNAL(clicked()),
  231.                    &window_2, SLOT(show()));
  232.   QObject::connect(window_1.pBt_1, SIGNAL(clicked()),
  233.                    &solution, SLOT(Run_1()));
  234.   QObject::connect(window_1.pBt_1, SIGNAL(clicked()),
  235.                    &solution, SLOT(Run_2()));
  236.   QObject::connect(window_1.pBt_1, SIGNAL(clicked()),
  237.                    &solution, SLOT(Print_1()));
  238.   QObject::connect(window_1.pBt_1, SIGNAL(clicked()),
  239.                    &solution, SLOT(Print_2()));
  240.   QObject::connect(window_2.pBt_3, SIGNAL(clicked()),
  241.                    &window_2, SLOT(hide()));
  242.   QObject::connect(window_2.pBt_3, SIGNAL(clicked()),
  243.                    &window_1, SLOT(show()));
  244.   QObject::connect(window_2.pBt_3, SIGNAL(clicked()),
  245.                    window_1.lEd_1, SLOT(selectAll()));
  246.   //--вывод--результатов-----------------------------
  247.   QObject::connect(&solution, SIGNAL(ResOut_1(QString)),
  248.                    window_2.lbl_4, SLOT(setText(QString)));
  249.   QObject::connect(&solution, SIGNAL(ResOut_2(QString)),
  250.                    window_2.lbl_5, SLOT(setText(QString)));
  251.  
  252.   window_1.show();
  253.  
  254.   return app.exec();
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement