Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <complex>
  4. #include <sstream>
  5. #include <iomanip>
  6.  
  7. MainWindow::MainWindow(QWidget *parent) :
  8.     QMainWindow(parent),
  9.     ui(new Ui::MainWindow)
  10. {
  11.     ui->setupUi(this);
  12. }
  13.  
  14. MainWindow::~MainWindow()
  15. {
  16.     delete ui;
  17. }
  18.  
  19.  
  20. void MainWindow::on_sloveButton_clicked()
  21. {
  22.     std::complex<double>a, b, c, D;
  23.     a = ui->avalue->value();
  24.     b = ui->avalue->value();
  25.     c = ui->avalue->value();
  26.     D = b*b-4.0*a*c;
  27.     std::ostringstream out;
  28.     out<< "Equation";
  29.     if (a.real()!=0.0)
  30.     {
  31.         if (a*a == 1.0)
  32.         {
  33.             out<<(a.real()>0.0)?"x2":"-x^2";
  34.         }
  35.         else
  36.         {
  37.             out << std::setprecision(2)<<a.real() << "x^2";
  38.         }
  39.     }
  40.     if (b.real()!=0.0)
  41.     {
  42.         if (b*b == 1.0)
  43.         {
  44.             out<<(b.real()>0.0)?+"+x":"-x";
  45.         }
  46.         else
  47.         {
  48.             out << (b.real()>0.0)?"+":"";
  49.             out <<b.real() << "x";
  50.         }
  51.     }
  52.     if (c.real()!=0.0)
  53.     {
  54.         out << (c.real()>0.0)?"+":"";
  55.         out <<c.real() << "\n";
  56.     }
  57.     out << "Discriminant: " <<D.real() << "\n";
  58.     out << "x1= " << (-b.real() - sqrt(D).real()/(2.0*a.real()));
  59.     if ((sqrt(D).imag() != 0.0)
  60.     {
  61.             out <<((-sqrt(D).imag()/(2*a.real()>0.0)?"+"))))
  62. }
  63.     std::string output;
  64.     output += std::to_string(a.real())+"x^2" +"+";
  65.     output += std::to_string(b.real())+"x"+"+";
  66.     output += std::to_string(c.real())+'\n';
  67.     output += "Discriminant"+std::to_string(D.real()) +'\n';
  68.     output += "x1 =" + std::to_string((-b.real() - sqrt(D).real()/(2.0*a.real()))) +'\n';
  69.     output += " " + std::to_string((-sqrt(D).imag()/(2.0*a.real()))) + '\n';
  70.     output += "x2 =" + std::to_string((-b.real() + sqrt(D).real()/(2.0*a.real())) )+'\n';
  71.     output += " " + std::to_string((sqrt(D).imag()/(2.0*a.real()))) + '\n';
  72.     ui->output_area->setText(output.c_str());
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement