Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #include <sstream>
  5. #pragma hdrstop
  6.  
  7. #include "Unit1.h"
  8. #include "multinominal.h"
  9. #include "Unit2.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. #pragma resource "*.dfm"
  13. TForm1 *Form1;
  14. //---------------------------------------------------------------------------  
  15.  
  16. Multinominal mm[4];
  17. int mnIndex = 0;
  18. int mn2Index = 0;
  19. int opIndex = 0;
  20.  
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22.     : TForm(Owner)
  23. {
  24.  
  25.     ListBox1->ItemIndex = 0;
  26.     ListBox2->ItemIndex = 4;
  27.  
  28.     Multerm m1, m2;
  29.     m1.add(5).add('x', 7);
  30.     m2.add(1).add('y',8).add('z',9);
  31.  
  32.     mm[0].add(m1);
  33.     mm[0].add(m2);
  34.     mm[1].add(m1);
  35.     mm[1].add(m2);
  36.     mm[1].add(m1);
  37.     mm[2].add(m2);
  38.  
  39.    
  40.  
  41.  
  42.  
  43.     Image1Click(nullptr);
  44. }
  45. //---------------------------------------------------------------------------
  46.  
  47.  
  48.  
  49. int drawTextRect(Vcl::Graphics::TCanvas* cv, int x, int y, AnsiString name = "" ) {
  50.     cv->Pen->Color = clBlack;
  51.     int l = cv->TextWidth(name);
  52.            
  53.     cv->Rectangle(x, y, x+20+l, y+20);
  54.     cv->TextOutW(x+10, y+4, name);  
  55.  
  56.     return x + l + 40;
  57. }
  58.  
  59. void drawArrow(Vcl::Graphics::TCanvas* cv, int x, int y, bool v) {  
  60.     cv->Pen->Color = clRed;
  61.  
  62.     if (v) {  
  63.         y = y+20;  
  64.         x = x+10;  
  65.         cv->MoveTo(x,y);
  66.         cv->LineTo(x,y+19);
  67.         cv->LineTo(x-3,y+16);
  68.         cv->MoveTo(x,y+19);
  69.         cv->LineTo(x+3,y+16);
  70.     } else {      
  71.         y = y+10;
  72.         x = x-20;
  73.         cv->MoveTo(x,y);
  74.         cv->LineTo(x+19,y);
  75.         cv->LineTo(x+16,y-3);
  76.         cv->MoveTo(x+19,y);
  77.         cv->LineTo(x+16,y+3);
  78.     }
  79. }
  80.  
  81.  
  82. AnsiString ItemToString(const Factor& f) {
  83.     std::ostringstream out;
  84.     out << f.varName;
  85.     if (f.power > 1) {
  86.         out << '^' << f.power;
  87.     }
  88.     return AnsiString(out.str().c_str());
  89. }
  90. AnsiString ItemToString(const Multinominal& f) {
  91.     std::ostringstream out;
  92.     out << f;
  93.     return AnsiString(out.str().c_str());
  94. }
  95. AnsiString ItemToString(const Multerm& f) {
  96.     std::ostringstream out;
  97.     char c = f.print(out, '0');
  98.     out << c;
  99.     return AnsiString(out.str().c_str());
  100. }
  101. Multinominal StringToItem(const AnsiString& us) {
  102.     std::stringstream ss;
  103.     ss << us;
  104.     Multinominal m;
  105.     ss >> m;
  106.     return m;
  107. }
  108.  
  109. void __fastcall TForm1::Image1Click(TObject *Sender) {
  110.  
  111.     if (mnIndex != ListBox1->ItemIndex && ListBox1->ItemIndex>=0&&ListBox1->ItemIndex<4) {
  112.         mnIndex = ListBox1->ItemIndex;
  113.     }
  114.  
  115.     Vcl::Graphics::TCanvas* cv = Image1->Canvas;  
  116.     cv->FillRect(Rect(0,0, Image1->Width, Image1->Height));
  117.  
  118.    
  119.     int x = 10;
  120.     int y = 10;
  121.     AnsiString name = ItemToString(mm[mnIndex]);
  122.     drawTextRect(cv, x, y, name);
  123.     Edit2->Text = name;
  124.  
  125.     for (auto termNode = mm[mnIndex].first; termNode; termNode = termNode->next) { 
  126.         x = 10;              
  127.         drawArrow(cv, x, y, true);        
  128.         y += 40;  
  129.         x = drawTextRect(cv, x, y, FloatToStr(termNode->value.multiplier) );
  130.  
  131.         for (auto factorNode = termNode->value.first; factorNode; factorNode = factorNode->next) {
  132.             drawArrow(cv, x, y, false);
  133.             x = drawTextRect(cv, x, y, ItemToString(factorNode->value));
  134.         }
  135.     }
  136.  
  137.    
  138.  
  139.    
  140.  
  141. }
  142. //---------------------------------------------------------------------------
  143.  
  144.  
  145. void __fastcall TForm1::ListBox1Click(TObject *Sender)
  146. {
  147.      Image1Click(Sender);
  148. }
  149. //---------------------------------------------------------------------------
  150.  
  151.  
  152.  
  153.  
  154. void __fastcall TForm1::Button1Click(TObject *Sender)
  155. {
  156.          
  157.     if (mnIndex != ListBox1->ItemIndex && ListBox1->ItemIndex>=0&&ListBox1->ItemIndex<4) {
  158.         mnIndex = ListBox1->ItemIndex;
  159.     }
  160.    
  161.     mn2Index = ListBox2->ItemIndex;
  162.  
  163.     opIndex = RadioGroup1->ItemIndex;
  164.  
  165.     if (mn2Index >= 0 && mn2Index < 4) {
  166.         if (opIndex == 0) {
  167.             mm[mnIndex] = mm[mnIndex] + mm[mn2Index];
  168.         } else if (opIndex == 1) {  
  169.             mm[mnIndex] = mm[mnIndex] * mm[mn2Index];                  
  170.         } else {  
  171.             mm[mnIndex] = mm[mn2Index];    
  172.         }
  173.     }  else {
  174.                  
  175.         Multinominal m = StringToItem( Edit1->Text );
  176.         if (opIndex == 0) {
  177.             mm[mnIndex] = mm[mnIndex] + m;
  178.         } else if (opIndex == 1) {  
  179.             mm[mnIndex] = mm[mnIndex] * m;                 
  180.         } else {  
  181.             mm[mnIndex] = m;    
  182.         }  
  183.    
  184.     }
  185.                    
  186.     Image1Click(Sender);
  187.  
  188.    
  189.      
  190.    
  191.  
  192.    
  193. }
  194. //---------------------------------------------------------------------------
  195.  
  196. void __fastcall TForm1::FormResize(TObject *Sender)
  197. {        
  198.     Image1->Picture->Bitmap->Width = Image1->Width;
  199.     Image1->Picture->Bitmap->Height = Image1->Height;
  200.     Image1Click(Sender);       
  201. }
  202. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement