Программа вычисляет корни уравнений линейных, кубических ,биквадратных, квадратных,
Метод Кардано
С++ Builder 6
Компиляция и сборка под Windows 10
=
=
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//------------Корень кубический---------------------------------------------------------------
double __fastcall TForm1::croot(double x){
if (x < 0)
return -pow(-x, 1.0/3.0);
return pow(x, 1.0/3.0);
}
//-----------Квадратное-------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
float a, b, c, x1, x2, d;
if( LabeledEdit1->Text != "" && LabeledEdit2->Text != "" && LabeledEdit3->Text != ""){
Label1->Caption ="";
Label2->Caption ="";
a = StrToFloat(LabeledEdit1->Text);
b = StrToFloat(LabeledEdit2->Text);
c = StrToFloat(LabeledEdit3->Text);
d = b*b - 4*a*c; //вычисляем дискриминант
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
Label1 -> Caption = "X1 = " + FloatToStrF(x1,ffFixed, 4, 5);
Label2 -> Caption = "X2 = " + FloatToStrF(x2,ffFixed, 4, 5);
}
else
{
Label1 -> Caption = "";
Label2 -> Caption = "Нет решений";
}
}
else {ShowMessage("Не заполнены поля коэффициенты"); return; }
}
//------------Биквадратное---------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
float a, b, c, x1, x2, x3, x4, t1, t2, d, D; bool log=false;
if( LabeledEdit4->Text != "" && LabeledEdit5->Text != "" && LabeledEdit6->Text != ""){
Label8->Caption ="";
Label6->Caption ="";
Label5->Caption ="";
Label4->Caption ="";
a=StrToFloat( LabeledEdit4->Text ) ;
b=StrToFloat( LabeledEdit5->Text );
c=StrToFloat( LabeledEdit6->Text );
if (a==0) {
if (b!=0) {
if (c==0) {Label8->Caption="X=0";}
else {
if (c/b<0){
x1=sqrt(-(c/b));
x2=-sqrt(-(c/b));
Label8->Caption = "X1 = "+FloatToStrF(x1,ffFixed, 4, 5);
Label6->Caption = "X2 = "+FloatToStrF(x2,ffFixed, 4, 5);
}
else {Label8->Caption="Нет решений";}
}
}
else {
if (c!=0) {Label8->Caption="Нет решений";}
else {Label8->Caption="X любое";}
}
}
else{
D = (b * b - 4 * a * c);
if (D<0) {Label8->Caption="Нет решений";}
else {
d=sqrt(D);
t1=( - b + d )/2.0 / a;
t2=( - b - d )/2.0 / a;
if (t1>0){
x1=sqrt(t1);
x2=-sqrt(t1);
Label8->Caption = "X1 = "+FloatToStrF(x1,ffFixed, 4, 5);
Label6->Caption = "X2 = "+FloatToStrF(x2,ffFixed, 4, 5);
log = true;
}
if (t2>0){
x3=sqrt(t2);
x4=-sqrt(t2);
if (log == true){
Label5->Caption = "X3 = "+FloatToStrF(x3,ffFixed, 4, 5);
Label4->Caption = "X4 = "+FloatToStrF(x4,ffFixed, 4, 5);
}
if (log == false){
Label8->Caption = "X1 = "+FloatToStrF(x3,ffFixed, 4, 5);
Label6->Caption = "X2 = "+FloatToStrF(x4,ffFixed, 4, 5);
}
}
}
}
}
else {ShowMessage("Не заполнены поля коэффициенты"); return; }
}
//-------------Линейное1-------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
float a,b ;
float x;
if( LabeledEdit7->Text != "" && LabeledEdit8->Text != ""){
a = StrToFloat( LabeledEdit7->Text);
b = StrToFloat( LabeledEdit8->Text);
if( a!= 0) { x =-(b/a); Label10->Caption="X = "+FloatToStrF(x,ffFixed, 4, 5 );}
if (a==0 && b==0){ Label10->Caption="X любое"; }
if (b!= 0 && a==0){ Label10->Caption="Нет решений"; }
}
else {ShowMessage("Не заполнены поля коэффициенты"); return; }
}
//--------------Линейное2----------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
float a,b,y,d ;
ListBox1->Items->Clear();
ListBox2->Items->Clear();
ListBox3->Items->Clear();
Label12->Caption="";
if( LabeledEdit9->Text != "" && LabeledEdit10->Text != "" && LabeledEdit12->Text != ""){
a=StrToFloat( LabeledEdit9->Text);
b=StrToFloat( LabeledEdit10->Text);
d=StrToFloat( LabeledEdit12->Text);
if (a==0 && b==0 && d==0){ Label12->Caption="X и Y Любые"; }
if (b== 0 && a==0 && d!=0){ Label12->Caption="Нет решений"; }
if (b!= 0 && a==0){
y=d/b;
Label12->Caption= "Одно решение X Любая Y = " + FloatToStrF(y,ffFixed, 4, 5);
}
if (b== 0 && a!=0){
y=d/a;
Label12->Caption="Одно решение X = "+ FloatToStrF(y,ffFixed, 4, 5)+" Y Любая" ;
}
if( b!= 0) {
for(int i=-100; i<=100;i++){
y = (d-a*i)/b;
ListBox1->Items->Add( "X = "+ IntToStr(i)+" Y= "+FloatToStrF(y,ffFixed, 4, 5) );
if (y - int(y) == 0 && y!=0) {
ListBox2->Items->Add("X = "+ IntToStr(i)+" Y= "+FloatToStrF(y,ffFixed, 4, 5));
}
if (y - int(y) == 0 && y>0 && i>0) {
ListBox3->Items->Add("X = "+ IntToStr(i)+" Y= "+FloatToStrF(y,ffFixed, 4, 5));
}
}
}
}
else {ShowMessage("Не заполнены поля коэффициенты"); return; }
}
//----------Метод Кардано--Кубическое-----------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
if( LabeledEdit11->Text != "" && LabeledEdit13->Text != "" && LabeledEdit14->Text != "" && LabeledEdit15->Text != ""){
double A, B, C, D;
A= LabeledEdit11->Text.ToDouble() ;
B= LabeledEdit13->Text.ToDouble() ;
C= LabeledEdit14->Text.ToDouble() ;
D= LabeledEdit15->Text.ToDouble();
if (A == 0){
Label20->Caption= "X Любое";
return ;
}
//--------------------------------------------------------------------------
double p = (3.0*A*C-B*B)/(3.0*A*A);
double q = (2.0*B*B*B-9.0*A*B*C+27.0*A*A*D)/(27.0*A*A*A);
double S = (q*q/4.0) + (p*p*p/27.0);
double F;
if (q == 0){F = M_PI/2.0; }
if (q < 0){F = atan(-2.0*croot(S)/q); }
if (q > 0){F = atan(-2.0*croot(S)/q) + M_PI; }
//-------------------------------------------------------------------------
double first0 = 0 ;
double first1 = 0 ;
double first2 = 0 ;
double second0 = 0 ;
double second1 = 0 ;
double second2 = 0 ;
for (int i = 0; i < 3; i++) {
if (S < 0){
first0 = 2.0*sqrt(-p/3.0)*cos(F/3.0)-B/(3.0*A);
first1 = 2.0*sqrt(-p/3.0)*cos((F/3.0)+2.0*M_PI/3.0)-B/(3.0*A);
first2 = 2.0*sqrt(-p/3.0)*cos((F/3.0)+4.0*M_PI/3.0)-B/(3.0*A);
}
if (S == 0){
first0 = 2.0*croot(-q/2.0)-B/(3.0*A);
first1 = -croot(-q/2.0)-B/(3.0*A);
first2 = -croot(-q/2.0)-B/(3.0*A);
}
if (S > 0){
double temp1 = croot((-q/2.0)+sqrt(S)) + croot((-q/2.0)-sqrt(S));
double temp2 = croot((-q/2.0)+sqrt(S)) - croot((-q/2.0)-sqrt(S));
first0 = temp1 - B/(3.0*A);
first1 = -temp1/2.0 - B/(3.0*A); second1 = sqrt(3)*temp2/2.0;
first2 = -temp1/2.0 - B/(3.0*A); second2 = -sqrt(3)*temp2/2.0;
}
//----------------------------------------------------------------------------
Label20->Caption = "X1 = "+ FloatToStrF( first0,ffFixed, 4, 5) +" "+ FloatToStrF( second0,ffFixed, 4, 5);
Label21->Caption = "X2 = " +FloatToStrF( first1,ffFixed, 4, 5) +" "+ FloatToStrF( second1,ffFixed, 4, 5);
Label22->Caption = "X3 = "+ FloatToStrF( first2,ffFixed, 4, 5) +" "+ FloatToStrF( second2,ffFixed, 4, 5);
}
}
else {ShowMessage("Не заполнены поля коэффициенты"); return; }
}
//------------только цифры одна запятая забой и минус-----------------------------------------------
void __fastcall TForm1::LabeledEdit1KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit1->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit2KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit2->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit3KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit3->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit4KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit4->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit5KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit5->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit6KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit6->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit7KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit7->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit8KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit8->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit9KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit9->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit10KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit10->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit12KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit12->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit11KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit11->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit13KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit13->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit14KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit14->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit15KeyPress(TObject *Sender, char &Key)
{
if( Key == ',' && LabeledEdit15->Text.Pos(",") > 0 ) { Key=0; return; }
if(Key >= '0' && Key <= '9' || Key == VK_BACK || Key == ',' || Key == '-') return;
Key=0;
}
//----------запрет контекстного меню вставка из буфера обмена-----------------------------------------------------------------
void __fastcall TForm1::LabeledEdit7ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit8ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit9ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit10ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit12ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit1ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit2ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit3ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PageControl1ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit5ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit4ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit6ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit11ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit13ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit14ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabeledEdit15ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled)
{
Handled = true;
}
//---------------------------------------------------------------------------
================================================
=============================================================================================
========================================
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <math.h>
#include <ExtCtrls.hpp>
#include <ComCtrls.hpp>
#include <math.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TPageControl *PageControl1;
TTabSheet *TabSheet1;
TTabSheet *TabSheet2;
TLabeledEdit *LabeledEdit1;
TLabeledEdit *LabeledEdit2;
TLabeledEdit *LabeledEdit3;
TTabSheet *TabSheet3;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TButton *Button1;
TLabeledEdit *LabeledEdit4;
TLabeledEdit *LabeledEdit5;
TLabeledEdit *LabeledEdit6;
TButton *Button2;
TLabel *Label4;
TLabel *Label5;
TLabel *Label7;
TLabel *Label6;
TLabel *Label8;
TLabel *Label9;
TButton *Button3;
TLabeledEdit *LabeledEdit7;
TLabeledEdit *LabeledEdit8;
TLabel *Label10;
TTabSheet *TabSheet4;
TLabel *Label11;
TLabeledEdit *LabeledEdit9;
TLabeledEdit *LabeledEdit10;
TLabeledEdit *LabeledEdit12;
TButton *Button4;
TLabel *Label12;
TListBox *ListBox1;
TListBox *ListBox2;
TListBox *ListBox3;
TLabel *Label13;
TLabel *Label14;
TLabel *Label15;
TLabel *Label16;
TLabel *Label17;
TLabel *Label18;
TBevel *Bevel1;
TBevel *Bevel2;
TBevel *Bevel3;
TTabSheet *TabSheet5;
TLabel *Label19;
TLabeledEdit *LabeledEdit11;
TLabeledEdit *LabeledEdit13;
TLabeledEdit *LabeledEdit14;
TLabeledEdit *LabeledEdit15;
TButton *Button5;
TLabel *Label20;
TLabel *Label21;
TLabel *Label22;
TBevel *Bevel4;
TLabel *Label24;
TPanel *Panel1;
TLabel *Label23;
TLabel *Label25;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall LabeledEdit1KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit2KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit3KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit4KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit5KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit6KeyPress(TObject *Sender, char &Key);
void __fastcall Button3Click(TObject *Sender);
void __fastcall LabeledEdit7KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit8KeyPress(TObject *Sender, char &Key);
void __fastcall Button4Click(TObject *Sender);
void __fastcall LabeledEdit9KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit10KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit12KeyPress(TObject *Sender, char &Key);
void __fastcall Button5Click(TObject *Sender);
void __fastcall LabeledEdit11KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit13KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit14KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit15KeyPress(TObject *Sender, char &Key);
void __fastcall LabeledEdit7ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit8ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit9ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit10ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit12ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit1ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit2ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit3ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall PageControl1ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit5ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit4ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit6ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit11ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit13ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit14ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
void __fastcall LabeledEdit15ContextPopup(TObject *Sender,
TPoint &MousePos, bool &Handled);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
double __fastcall croot(double x);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
=
исходник здесь
=
|