⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 绘制一元函数曲线的工具 输入C语言语法的函数公式, 即可绘制函数的二维曲线 采用内置编译器和虚拟机(这是调整OK的版本)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    try
    {
        xv_min = StrToFloat(this->Edit1->Text);
    }
    catch(...)
    {
        ShowMessage("警告:X变量起点设定错误;");
        return;
    }

    try
    {
        xv_stp = StrToFloat(this->Edit2->Text);
    }
    catch(...)
    {
        ShowMessage("警告:X变量步进设定错误;");
        return;
    }

    try
    {
        xv_max = StrToFloat(this->Edit3->Text);
    }
    catch(...)
    {
        ShowMessage("警告:X变量终点设定错误;");
        return;
    }
                  
    double xr_rect;
    double xr_step;

    try
    {
        xr_rect = StrToFloat(this->Edit9->Text);
    }
    catch(...)
    {
        ShowMessage("警告:X座标范围设定错误;");
        return;
    }

    try
    {
        xr_step = StrToFloat(this->EditA->Text);
    } 
    catch(...)
    {
        ShowMessage("警告:X座标格子设定错误;");
        return;
    }

    double yr_rect;
    double yr_step;
    
    try
    {
        yr_rect = StrToFloat(this->EditC->Text);
    }
    catch(...)
    {
        ShowMessage("警告:Y座标起点设定错误;");
        return;
    }

    try
    {
        yr_step = StrToFloat(this->EditD->Text);
    } 
    catch(...)
    {
        ShowMessage("警告:Y座标格子设定错误;");
        return;
    }

    double xv;
    double yv;

    double xp;
    double yp;
               
    //计算比例;
    yp = (20.0 - h) / (yr_rect * 2);
    xp = (w - 20.0) / (xr_rect * 2);

    //绘制格子;
    if(this->CheckBox1->Checked)
    {
        for(xv = xr_step; xv < xr_rect; xv += xr_step)
        {
            this->Image1->Canvas->MoveTo(xv * xp + (w / 2), (h / 2) - 1);
            this->Image1->Canvas->LineTo(xv * xp + (w / 2), (h / 2) - 3);
        
            this->Image1->Canvas->MoveTo((w / 2) - xv * xp, (h / 2) - 1);
            this->Image1->Canvas->LineTo((w / 2) - xv * xp, (h / 2) - 3);
        }

        for(yv = yr_step; yv < yr_rect; yv += yr_step)
        {
            this->Image1->Canvas->MoveTo(1 + (w / 2), yv * yp + (h / 2));
            this->Image1->Canvas->LineTo(3 + (w / 2), yv * yp + (h / 2));
        
            this->Image1->Canvas->MoveTo(1 + (w / 2), (h / 2) - yv * yp);
            this->Image1->Canvas->LineTo(3 + (w / 2), (h / 2) - yv * yp);
        }
    }

    //编译公式;
    AnsiString app_p;
    AnsiString tmp_o;
    AnsiString tmp_i;
    AnsiString asm_o;

    app_p = ExtractFilePath(Application->ExeName);

    if(app_p.AnsiLastChar()[0] != '\\')
       app_p += "\\";

    tmp_i = app_p + "temp_i.txt";
    tmp_o = app_p + "temp_o.txt";
    asm_o = app_p + "uasm_o.txt";

    this->Memo1->Lines->SaveToFile(tmp_i);

    yyin  = fopen(tmp_i.c_str(), "r+");
    yyout = fopen(tmp_o.c_str(), "w+");
    asout = fopen(asm_o.c_str(), "w+");

    yyparse();

    fclose(yyin);
    fclose(yyout);
    fclose(asout);

    //运算准备;
    double ta, tb, tc;

    my_ucvm9->cln_var();
    my_ucvm9->reg_var("x", UCVM9_TYPE_FLT, &xv);
    my_ucvm9->reg_var("y", UCVM9_TYPE_FLT, &yv);
    my_ucvm9->reg_var("a", UCVM9_TYPE_FLT, &ta);
    my_ucvm9->reg_var("b", UCVM9_TYPE_FLT, &tb);
    my_ucvm9->reg_var("c", UCVM9_TYPE_FLT, &tc);

    int result = my_ucvm9->load_asm_file(asm_o.c_str());

    if( result < 1 )
    {
        ShowMessage("警告:语法错误,无法匹配的标识;");
        return;
    }
    else
    if( result < 0 )
    {                
        ShowMessage("警告:语法错误,无法匹配的指令;");
        return;
    }
    
    //绘制曲线;
    this->Image1->Canvas->Pen->Color = this->Shape1->Brush->Color;

    xv = xv_min;

    my_ucvm9->exe_fun();

    this->Image1->Canvas->MoveTo(
            xv * xp + (w / 2) ,
            yv * yp + (h / 2));

    for(xv = xv_min; xv <= xv_max; xv += xv_stp)
    {
        my_ucvm9->exe_fun();

        this->Image1->Canvas->LineTo(
            xv * xp + (w / 2) ,
            yv * yp + (h / 2));
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    //clean;
    int w = atoi(this->Edit7->Text.c_str());
    int h = atoi(this->Edit8->Text.c_str());

    if(w < 775)
       w = 775;

    if(h < 447)
       h = 447;

    my_bmp_b->Height = h;
    my_bmp_b->Width  = w;

    this->Image1->Tag = 1;

    this->Image1->Height = h;
    this->Image1->Width  = w;

    this->Image1->Picture->Graphic->Assign(my_bmp_b);

    this->Image1->Canvas->Brush = this->Shape2->Brush;
    this->Image1->Canvas->FillRect(TRect(0, 0, w, h));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    double xr;

    try
    {
        xr = StrToFloat(this->Edit9->Text);
    }
    catch(...)
    {
        ShowMessage("警告:X座标范围设定出错;");
        return;
    }

    int w = atoi(this->Edit7->Text.c_str());
    int h = atoi(this->Edit8->Text.c_str());

    if(w < 775)
       w = 775;

    if(h < 447)
       h = 447;

    this->EditC->Text = (xr * h) / w;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    double yr;

    try
    {
        yr = StrToFloat(this->EditC->Text);
    }
    catch(...)
    {
        ShowMessage("警告:Y座标范围设定出错;");
        return;
    }


    int w = atoi(this->Edit7->Text.c_str());
    int h = atoi(this->Edit8->Text.c_str());

    if(w < 775)
       w = 775;

    if(h < 447)
       h = 447;

    this->Edit9->Text = (yr * w) / h;
} 
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
    if(this->SaveDialog1->Execute())
    {
        this->Image1->Picture->SaveToFile(
            this->SaveDialog1->FileName); 
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Shape1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
    this->ColorDialog1->Color = this->Shape1->Brush->Color;

    if(this->ColorDialog1->Execute())
    {
        this->Shape1->Brush->Color = this->ColorDialog1->Color;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Shape2MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
    this->ColorDialog1->Color = this->Shape2->Brush->Color;

    if(this->ColorDialog1->Execute())
    {
        this->Shape2->Brush->Color = this->ColorDialog1->Color;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Shape3MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
    this->ColorDialog1->Color = this->Shape3->Brush->Color;

    if(this->ColorDialog1->Execute())
    {
        this->Shape3->Brush->Color = this->ColorDialog1->Color;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit7Change(TObject *Sender)
{
    this->Image1->Tag = 0;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -