Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <sstream>
- #pragma hdrstop
- #include "Unit1.h"
- #include "multinominal.h"
- #include "Unit2.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- Multinominal mm[4];
- int mnIndex = 0;
- int mn2Index = 0;
- int opIndex = 0;
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- ListBox1->ItemIndex = 0;
- ListBox2->ItemIndex = 4;
- Multerm m1, m2;
- m1.add(5).add('x', 7);
- m2.add(1).add('y',8).add('z',9);
- mm[0].add(m1);
- mm[0].add(m2);
- mm[1].add(m1);
- mm[1].add(m2);
- mm[1].add(m1);
- mm[2].add(m2);
- Image1Click(nullptr);
- }
- //---------------------------------------------------------------------------
- int drawTextRect(Vcl::Graphics::TCanvas* cv, int x, int y, AnsiString name = "" ) {
- cv->Pen->Color = clBlack;
- int l = cv->TextWidth(name);
- cv->Rectangle(x, y, x+20+l, y+20);
- cv->TextOutW(x+10, y+4, name);
- return x + l + 40;
- }
- void drawArrow(Vcl::Graphics::TCanvas* cv, int x, int y, bool v) {
- cv->Pen->Color = clRed;
- if (v) {
- y = y+20;
- x = x+10;
- cv->MoveTo(x,y);
- cv->LineTo(x,y+19);
- cv->LineTo(x-3,y+16);
- cv->MoveTo(x,y+19);
- cv->LineTo(x+3,y+16);
- } else {
- y = y+10;
- x = x-20;
- cv->MoveTo(x,y);
- cv->LineTo(x+19,y);
- cv->LineTo(x+16,y-3);
- cv->MoveTo(x+19,y);
- cv->LineTo(x+16,y+3);
- }
- }
- AnsiString ItemToString(const Factor& f) {
- std::ostringstream out;
- out << f.varName;
- if (f.power > 1) {
- out << '^' << f.power;
- }
- return AnsiString(out.str().c_str());
- }
- AnsiString ItemToString(const Multinominal& f) {
- std::ostringstream out;
- out << f;
- return AnsiString(out.str().c_str());
- }
- AnsiString ItemToString(const Multerm& f) {
- std::ostringstream out;
- char c = f.print(out, '0');
- out << c;
- return AnsiString(out.str().c_str());
- }
- Multinominal StringToItem(const AnsiString& us) {
- std::stringstream ss;
- ss << us;
- Multinominal m;
- ss >> m;
- return m;
- }
- void __fastcall TForm1::Image1Click(TObject *Sender) {
- if (mnIndex != ListBox1->ItemIndex && ListBox1->ItemIndex>=0&&ListBox1->ItemIndex<4) {
- mnIndex = ListBox1->ItemIndex;
- }
- Vcl::Graphics::TCanvas* cv = Image1->Canvas;
- cv->FillRect(Rect(0,0, Image1->Width, Image1->Height));
- int x = 10;
- int y = 10;
- AnsiString name = ItemToString(mm[mnIndex]);
- drawTextRect(cv, x, y, name);
- Edit2->Text = name;
- for (auto termNode = mm[mnIndex].first; termNode; termNode = termNode->next) {
- x = 10;
- drawArrow(cv, x, y, true);
- y += 40;
- x = drawTextRect(cv, x, y, FloatToStr(termNode->value.multiplier) );
- for (auto factorNode = termNode->value.first; factorNode; factorNode = factorNode->next) {
- drawArrow(cv, x, y, false);
- x = drawTextRect(cv, x, y, ItemToString(factorNode->value));
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::ListBox1Click(TObject *Sender)
- {
- Image1Click(Sender);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- if (mnIndex != ListBox1->ItemIndex && ListBox1->ItemIndex>=0&&ListBox1->ItemIndex<4) {
- mnIndex = ListBox1->ItemIndex;
- }
- mn2Index = ListBox2->ItemIndex;
- opIndex = RadioGroup1->ItemIndex;
- if (mn2Index >= 0 && mn2Index < 4) {
- if (opIndex == 0) {
- mm[mnIndex] = mm[mnIndex] + mm[mn2Index];
- } else if (opIndex == 1) {
- mm[mnIndex] = mm[mnIndex] * mm[mn2Index];
- } else {
- mm[mnIndex] = mm[mn2Index];
- }
- } else {
- Multinominal m = StringToItem( Edit1->Text );
- if (opIndex == 0) {
- mm[mnIndex] = mm[mnIndex] + m;
- } else if (opIndex == 1) {
- mm[mnIndex] = mm[mnIndex] * m;
- } else {
- mm[mnIndex] = m;
- }
- }
- Image1Click(Sender);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormResize(TObject *Sender)
- {
- Image1->Picture->Bitmap->Width = Image1->Width;
- Image1->Picture->Bitmap->Height = Image1->Height;
- Image1Click(Sender);
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement