/*
Main.cpp
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <complex>
#include <fstream>
#include <process.h>
#include <vector>
#include "Graph.h"
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
using namespace std;
TMainForm *MainForm;
Graph *g=NULL;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
Run->Enabled=false;
Button3->Enabled=true;
RadioButton2->Checked=true;
}
//----------------------------------------------------------------------------
void __fastcall TMainForm::Button1Click(TObject *Sender)
{
if((NodeNum->Text!="")&&(g==NULL))
{
if(CheckBox1->Checked==true)
{
g=new Graph(NodeNum->Text.ToInt(),true);
}else
{
g=new Graph(NodeNum->Text.ToInt(),false);
}
Button1->Enabled=false;
CheckBox1->Enabled=false;
ofstream out("data.txt");
out<<"电路图中有"<<NodeNum->Text.ToInt()<<"个节点。"<<endl;
if(CheckBox1->Checked==true)
{
out<<"为二端口电路,节点0和节点1为端口。"<<endl;
}
out<<"--------------------------------------------------"<<endl;
out<<"R(Ω)\tC(F)\tL(H)\tFrom(No.)\tTo(No.)"<<endl;
AnsiString s1("电路中节点数已经设定好了!请向电路中添加数据!\n");
AnsiString s2("电路中节点编号从0到");
AnsiString s3(NodeNum->Text.ToInt()-1);
MessageDlg(s1+s2+s3,mtInformation,TMsgDlgButtons() << mbOK, 0);
NodeNum->ReadOnly=true;
NodeNum->Color=cl3DLight;
}else if(NodeNum->Text=="")
{
MessageDlg("请输入节点中的节点数!",mtInformation,TMsgDlgButtons() << mbOK, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::AddClick(TObject *Sender)
{
if((Begin->Text.ToDouble()>=NodeNum->Text.ToDouble())||(End->Text.ToDouble()>=NodeNum->Text.ToDouble())||(Begin->Text.ToDouble()<0)||(End->Text.ToDouble()<0))
{
MessageDlg("您所添加的该边的两个端点中有非该图中的节点!",mtError,TMsgDlgButtons() << mbOK, 0);
}else if(Begin->Text==""||End->Text==""||R->Text==""||L->Text==""||C->Text=="")
{
MessageDlg("输入错误!输入数据不可以为空!",mtError,TMsgDlgButtons() << mbOK, 0);
}else if(Begin->Text.ToDouble()==End->Text.ToDouble())
{
MessageDlg("边的起点和终点不可以相同!",mtError,TMsgDlgButtons() << mbOK, 0);
}
else if(g)
{
Arcc aa(R->Text.ToDouble(),C->Text.ToDouble(),L->Text.ToDouble(),Begin->Text.ToInt(),End->Text.ToInt()) ;
g->AddArcc(aa);
ofstream out("data.txt",std::ios:ut|std::ios::app);
out<<R->Text.ToDouble()<<"\t"<<C->Text.ToDouble()<<"\t"<<L->Text.ToDouble()<<"\t"<<Begin->Text.ToInt()<<"\t\t"<<End->Text.ToInt()<<endl;
MessageDlg("数据已经添加进去了,请继续添加数据!",mtInformation,TMsgDlgButtons() << mbOK, 0);
R->Text="";
C->Text="";
L->Text="";
Begin->Text="";
End->Text="";
}else if(!g)
{
MessageDlg("请先确定图中节点的个数!",mtError,TMsgDlgButtons() << mbOK, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button3Click(TObject *Sender)
{
if(Result->Text=="")
{
MessageDlg("请输入频率!",mtError,TMsgDlgButtons() << mbOK, 0);
}
else if(g)
{
complex<double> dd(g->CaculateImage(Result->Text.ToDouble()));
AnsiString s1(dd.real());
AnsiString s3(dd.imag());
AnsiString s2("+");
AnsiString s4("i");
if(dd.imag()>=0)
{
ww->Text=s1+s2+s3+s4;
}else if(dd.imag()<0)
{
ww->Text=s1+s3+s4;
}
MessageDlg("计算完成!",mtInformation,TMsgDlgButtons() << mbOK, 0);
}
else
{
MessageDlg("请先输入图中信息!",mtError,TMsgDlgButtons() << mbOK, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::close(TObject *Sender, TCloseAction &Action)
{
if(g)
{
delete g;
}
MessageDlg(" 谢谢使用!\n开发者:倪冉\nKnight-Studio",mtInformation,TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
MessageDlg("欢迎使用电路Calculater2.0!\n开发者:倪冉\nKnight-Studio",mtInformation,TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RadioButton1Click(TObject *Sender)
{
if(RadioButton1->Checked==true)
{
Run->Enabled=true;
Button3->Enabled=false;
ww->Color=cl3DLight;
ww->ReadOnly=true;
delta->ReadOnly=false;
delta->Color=clWindow;
Button4->Enabled=true;
Button5->Enabled=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RadioButton2Click(TObject *Sender)
{
if(RadioButton2->Checked==true)
{
Run->Enabled=false;
Button3->Enabled=true;
ww->ReadOnly=false;
ww->Color=clWindow;
delta->ReadOnly=true;
delta->Color=cl3DLight;
Button4->Enabled=false;
Button5->Enabled=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button2Click(TObject *Sender)
{
if(g)
{
delete g;
g=NULL;
NodeNum->ReadOnly=false;
NodeNum->Color=clWindow;
NodeNum->Text="";
Button1->Enabled=true;
CheckBox1->Enabled=true;
Button4->Enabled=true;
Run->Enabled=false;
Button3->Enabled=true;
RadioButton2->Checked=true;
delta->Text="";
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RunClick(TObject *Sender)
{
if(g)
{
vector<double> data=g->CalculateFrequency();
//Result->Text=g->CalculateFrequency();
if((g->Judge())&&(data.size()==1))
{
Result->Text=data[0];
MessageDlg("该解为唯一正解!",mtInformation,TMsgDlgButtons()<<mbOK,0);
}
else if((g->Judge())&&(data.size()==2))
{
AnsiString s1(data[0]);
AnsiString s3(data[2]);
AnsiString s2("OR");
Result->Text=s1+s2+s3;
MessageDlg("存在两个可以求出的正解!",mtInformation,TMsgDlgButtons()<<mbOK,0);
}
else
{
Result->Text=data[0];
MessageDlg("该解为一个正解,可能还有其他的正解,不能确定!",mtInformation,TMsgDlgButtons()<<mbOK,0);
}
}
else
{
MessageDlg("请先输入电路图中的信息!",mtError,TMsgDlgButtons()<<mbOK,0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button4Click(TObject *Sender)
{
if(g)
{
g->SetDelta(delta->Text.ToDouble());
delta->ReadOnly=true;
delta->Color=cl3DLight;
Button4->Enabled=false;
}
else
{
MessageDlg("请先输入电路图中的信息!",mtError,TMsgDlgButtons()<<mbOK,0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button5Click(TObject *Sender)
{
delta->ReadOnly=false;
delta->Color=clWindow;
delta->Text="";
Button4->Enabled=true;
}
//---------------------------------------------------------------------------
/*
main.h
*/
//---------------------------------------------------------------------------
#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <sysutils.hpp>
#include <windows.hpp>
#include <messages.hpp>
#include <sysutils.hpp>
#include <classes.hpp>
#include <graphics.hpp>
#include <controls.hpp>
#include <forms.hpp>
#include <dialogs.hpp>
#include <stdctrls.hpp>
#include <buttons.hpp>
#include <extctrls.hpp>
#include <menus.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TMainForm : public TForm
{
__published:
TGroupBox *GroupBox1;
TEdit *NodeNum;
TLabel *Label1;
TGroupBox *GroupBox2;
TLabel *Label2;
TEdit *Begin;
TLabel *Label3;
TEdit *End;
TLabel *Label4;
TEdit *R;
TLabel *Label5;
TLabel *Label6;
TButton *Button1;
TEdit *C;
TLabel *Label8;
TEdit *L;
TLabel *Label9;
TButton *Add;
TGroupBox *GroupBox3;
TLabel *Label10;
TEdit *Result;
TButton *Run;
TLabel *Label7;
TEdit *ww;
TButton *Button3;
TLabel *Label11;
TGroupBox *GroupBox4;
TRadioButton *RadioButton1;
TRadioButton *RadioButton2;
TLabel *Label12;
TButton *Button2;
TGroupBox *GroupBox5;
TLabel *Label13;
TEdit *delta;
TButton *Button4;
TButton *Button5;
TCheckBox *CheckBox1;
TLabel *Label14;
void __fastcall Button1Click(TObject *Sender);
void __fastcall AddClick(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall close(TObject *Sender, TCloseAction &Action);
void __fastcall FormCreate(TObject *Sender);
void __fastcall RadioButton1Click(TObject *Sender);
void __fastcall RadioButton2Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall RunClick(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
private: // private user declarations
public: // public user declarations
virtual __fastcall TMainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif
/*
Calculter2.0.cpp
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("Main.cpp", MainForm);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TMainForm), &MainForm);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//---------------------------------------------------------------------------
|