Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ООП / Lab4 / Lab4 / Unit1

.cpp
Скачиваний:
13
Добавлен:
18.02.2017
Размер:
6.57 Кб
Скачать
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "Vector"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void TForm1::updatescreen(TObject *Sender){
	FormCreate(Sender);
	for(int i = 0;i < M.size();i++)
		M.at(i)->Draw();
}
bool TForm1::check(double &im,double &re){
	bool check = true;
	try {
		re = StrToFloat(Edit1->Text);
		im = StrToFloat(Edit2->Text);
	} catch(...) {
		ShowMessage("Wrong Format");
		check  = false;
	}
	return check;
}
bool TForm1::check(double &r,int &phi){
	bool check = true;
	try {
		r = StrToFloat(Edit3->Text);
		phi = StrToInt(Edit4->Text);
	} catch(...) {
		ShowMessage("Wrong Format");
		check  = false;
	}
	return check;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{


	Image1->Canvas->Brush->Style = bsSolid;
	Image1->Canvas->FillRect(Rect(0,0,Image1->Width,Image1->Height));
	Image1->Canvas->Pen->Color = clBlack;
	Image1->Canvas->Pen->Width = 3;
	Image1->Canvas->MoveTo(300,0);
	Image1->Canvas->LineTo(300,600);
	Image1->Canvas->MoveTo(0,300);
	Image1->Canvas->LineTo(600,300);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	/*complex *a;
	bool check = true;
	double re,im;
	try {
		re = StrToFloat(Edit1->Text);
		im = StrToFloat(Edit2->Text);
	} catch(...) {
		ShowMessage("Wrong Format");
		check  = false;
	}*/
	double re=0,im=0;
	if(check(im,re) == true){
		complex *a;
		M.push_back(a);
		M.at(M.size()-1) = new complex(re,im,Image1);
		M.at(M.size()-1) -> Draw();
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
	/*
	bool check = true;
	double r;
	int phi;
	try {
		r = StrToFloat(Edit3->Text);
		phi = StrToInt(Edit4->Text);
	} catch(...) {
		ShowMessage("Wrong Format");
		check  = false;
	}*/
	double r=0;
	int phi=0;
	if(check(r,phi) == true){
		complex *a;
		M.push_back(a);
		M.at(M.size()-1) = new complex(r,phi,Image1);
		M.at(M.size()-1) -> Draw();
	}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  int X, int Y)
{
	if(Button ==  mbLeft){
		for(int i = 0;i < M.size();i++){
			if(X >= 300 + M.at(i)->Re*10-3 && X <= 300 + M.at(i)->Re*10+3){
				if(Y >= 300-M.at(i)->Im*10-3 && Y <= 300-M.at(i)->Im*10+3){
					M.at(i)->Mark();
					//M.at(i)->Draw();
				}
			}
		}
	}
	updatescreen(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
	/*bool check = true;
	double re,im;
	try {
		re = StrToFloat(Edit1->Text);
		im = StrToFloat(Edit2->Text);
	} catch(...) {
		ShowMessage("Wrong Format");
		check  = false;
	} */
	double re=0,im=0;
	if(check(im,re) == true){
		complex temp(re,im,Image1);
		for(int i = 0;i < M.size();i++){
			if(M.at(i)->mark == true){
				if(((TButton*)Sender)->Caption == "+")
					*M.at(i) = *M.at(i) + &temp;
				else if(((TButton*)Sender)->Caption == "-")
					*M.at(i) = *M.at(i) - &temp;
				else if(((TButton*)Sender)->Caption == "*")
					*M.at(i) = *M.at(i) * &temp;
			}
		}


	}
	updatescreen(Sender);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
	for(int i = 0;i < M.size();i++){
		delete M.at(i);
	}
	M.clear();
	updatescreen(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
	/*bool check = true;
	double re,im;
	try {
		re = StrToFloat(Edit1->Text);
		im = StrToFloat(Edit2->Text);
	} catch(...) {
		ShowMessage("Wrong Format");
		check  = false;
	}*/
	double re=0,im=0;
	if(check(im,re) == true){
		complex temp(re,im,Image1);
		for(int i = 0;i < M.size();i++){
			if(M.at(i)->mark == true)
				*M.at(i) = &temp;
		}
	}
	updatescreen(Sender);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
	double r=0;
	int phi=0;
	if(check(r,phi) == true){
		complex temp(r,phi,Image1);
		for(int i = 0;i < M.size();i++){
			if(M.at(i)->mark == true)
				*M.at(i) = &temp;
		}
	}
	updatescreen(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
	double r=0;
	int phi=0;
	if(check(r,phi) == true){
		for(int i = 0;i < M.size();i++){
			if(M.at(i)->mark == true)
				M.at(i)->Rotate(phi);
		}
	}
	updatescreen(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
	int a=-1;
	int b=-1;
	int phi;
	int i = 0;
	while(a == -1 && i<M.size()){
		if(M.at(i)->mark == true)
			a=i;
		i++;
	}
	while(b == -1 && i<M.size()){
        if(M.at(i)->mark == true)
			b=i;
		i++;
	}
	if(a!=-1 && b!=-1)
		Edit4->Text = IntToStr(M.at(a)->phi - M.at(b)->phi);
	else
		ShowMessage("Chose vectors");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button11Click(TObject *Sender)
{
		ofstream f;
		f.open("save.txt", ios::out);
		for(int i = 0;i < M.size();i++){
			f << M.at(i)->Re << " " << M.at(i)->Im << "\n";
		}
		f.close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button12Click(TObject *Sender)
{
	ifstream F;
	F.open("save.txt");
	//

	while (!F.eof()){
		int i = M.size();
		double a,b;
		F >> a;
		F >> b;
		complex *temp;//(a,b,Image1);
		M.push_back(temp);
		M.at(i) = new complex(a,b,Image1);
		M.at(i)->Draw();

	}
	F.close();
}
//---------------------------------------------------------------------------

Соседние файлы в папке Lab4