www.pudn.com > FUN_2V.rar > Unit1.cpp


//--------------------------------------------------------------------------- 
#include  
#pragma hdrstop 
 
#include "Unit1.h" 
 
#include "calc_flex.c" 
#include "calc_yacc.c" 
#include "ucvm_type.c" 
 
#include "ucvm9.cpp" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1;  
//--------------------------------------------------------------------------- 
FILE *asout = NULL; 
//--------------------------------------------------------------------------- 
void *func_exp(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT)   
        v_parame = *(long *)(parame_stack->dat_addr); 
    else             
        v_parame = *(char *)(parame_stack->dat_addr);  
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = exp(v_parame); 
 
    return(p_result); 
}                                                           
//--------------------------------------------------------------------------- 
void *func_log(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT)   
        v_parame = *(long *)(parame_stack->dat_addr); 
    else             
        v_parame = *(char *)(parame_stack->dat_addr);  
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = log(v_parame); 
 
    return(p_result); 
} 
//--------------------------------------------------------------------------- 
void *func_sin(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT)   
        v_parame = *(long *)(parame_stack->dat_addr); 
    else             
        v_parame = *(char *)(parame_stack->dat_addr);  
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = sin(v_parame); 
 
    return(p_result); 
}                                   
//--------------------------------------------------------------------------- 
void *func_cos(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT)   
        v_parame = *(long *)(parame_stack->dat_addr); 
    else             
        v_parame = *(char *)(parame_stack->dat_addr);  
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = cos(v_parame); 
 
    return(p_result); 
}                                         
//--------------------------------------------------------------------------- 
void *func_tan(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT)   
        v_parame = *(long *)(parame_stack->dat_addr); 
    else             
        v_parame = *(char *)(parame_stack->dat_addr);  
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = tan(v_parame); 
 
    return(p_result); 
}                                                    
//--------------------------------------------------------------------------- 
void *func_asin(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT) 
        v_parame = *(long *)(parame_stack->dat_addr); 
    else 
        v_parame = *(char *)(parame_stack->dat_addr); 
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = asin(v_parame); 
 
    return(p_result); 
}                                                                     
//--------------------------------------------------------------------------- 
void *func_acos(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT) 
        v_parame = *(long *)(parame_stack->dat_addr); 
    else 
        v_parame = *(char *)(parame_stack->dat_addr); 
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = acos(v_parame); 
 
    return(p_result); 
}                                                                                   
//--------------------------------------------------------------------------- 
void *func_atan(long parame_count, T_DAT *parame_stack) 
{ 
    REAL  v_parame; 
    REAL *p_result; 
 
    if(parame_stack->dat_type == UCVM9_TYPE_FLT) 
        v_parame = *(REAL *)(parame_stack->dat_addr); 
    else 
    if(parame_stack->dat_type == UCVM9_TYPE_INT) 
        v_parame = *(long *)(parame_stack->dat_addr); 
    else 
        v_parame = *(char *)(parame_stack->dat_addr); 
 
    p_result = (REAL *)(new char[sizeof(REAL)]); 
 
    *p_result = atan(v_parame); 
 
    return(p_result); 
} 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
    : TForm(Owner) 
{ 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Button1Click(TObject *Sender) 
{              
    //clean; 
    this->Image1->Canvas->Brush->Color = this->Shape2->Brush->Color; 
    this->Image1->Canvas->FillRect(this->Image1->ClientRect); 
     
    //paint; 
    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->Image1->Width = w; 
    this->Image1->Height= h; 
 
    //绘制轴线; 
    if(this->CheckBox1->Checked) 
    { 
        this->Image1->Canvas->Pen->Color = this->Shape3->Brush->Color; 
         
        this->Image1->Canvas->MoveTo(10, h / 2); 
        this->Image1->Canvas->LineTo(w - 10, h / 2); 
        this->Image1->Canvas->LineTo(w - 20, h / 2 - 2); 
        this->Image1->Canvas->MoveTo(w - 10, h / 2);   
        this->Image1->Canvas->LineTo(w - 20, h / 2 + 2); 
 
        this->Image1->Canvas->MoveTo(w / 2, h - 10); 
        this->Image1->Canvas->LineTo(w / 2, 10); 
        this->Image1->Canvas->LineTo(w / 2 + 2, 20); 
        this->Image1->Canvas->MoveTo(w / 2, 10);    
        this->Image1->Canvas->LineTo(w / 2 - 2, 20); 
    } 
 
    //提取参数; 
    double xv_min; 
    double xv_stp; 
    double xv_max; 
 
    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); 
 
    //运算准备; 
    my_ucvm9->cln_var(); 
    my_ucvm9->reg_var("x", UCVM9_TYPE_FLT, &xv);   
    my_ucvm9->reg_var("y", UCVM9_TYPE_FLT, &yv); 
 
    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; 
    this->Image1->Canvas->Brush->Color = this->Shape2->Brush->Color; 
    this->Image1->Canvas->FillRect(this->Image1->ClientRect); 
} 
//--------------------------------------------------------------------------- 
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::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::FormCreate(TObject *Sender) 
{ 
    my_ucvm9 = new TUCVM9; 
 
    my_ucvm9->reg_fun("exp", UCVM9_TYPE_FLT, func_exp);   
    my_ucvm9->reg_fun("log", UCVM9_TYPE_FLT, func_log); 
 
    my_ucvm9->reg_fun("sin", UCVM9_TYPE_FLT, func_sin);   
    my_ucvm9->reg_fun("cos", UCVM9_TYPE_FLT, func_cos);    
    my_ucvm9->reg_fun("tan", UCVM9_TYPE_FLT, func_tan); 
         
    my_ucvm9->reg_fun("asin", UCVM9_TYPE_FLT, func_asin); 
    my_ucvm9->reg_fun("acos", UCVM9_TYPE_FLT, func_acos);  
    my_ucvm9->reg_fun("atan", UCVM9_TYPE_FLT, func_acos); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::FormDestroy(TObject *Sender) 
{ 
    delete this->my_ucvm9; 
} 
//---------------------------------------------------------------------------