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

📄 test.~cpp

📁 化学式
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "test.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
static getVar(double mM);
bool checkNumber(AnsiString s);
int checkAllTedit(TEdit *Myedit);

const int mC = 12;
const int mN = 14;
const int mO = 16;
const float mCl = 35.5;
const int mS = 32;
const int mNa = 23;
const int mAl = 27;
//-------------------------------------------------------
int n1,n2,n3,n4,n5,n6,n7,n8;
/*struct myVar
{
 int C;
 int H;
 int O;
 int N;
 int CL;
 int S;
 int Na;
 int Al;
}thisVar;
*/
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 n1=n2=n3=n4=n5=n6=n7=n8=0;   //初值

 ListBox1->Items->Add("规则:");
 ListBox1->Items->Add("M必须在1500以内");
 ListBox1->Items->Add("A用来限定所有元素最大值");
 ListBox1->Items->Add("其它值的含义为:");
 ListBox1->Items->Add("限定0到值的范围,用以减少循环[可以大于A]");
 ListBox1->Items->Add("举例:如果C输入为10,则A的范围为0-10");
}
//---------------------------------------------------------------------------
static getVar(double mM)
{
  //递归算法test
  //if (mmC < 0)
  //    return 0;
  //return getVar((mC*thisVar.C + thisVar.H + mO*thisVar.O + mN*thisVar.N + mCl*thisVar.CL + mS*thisVar.S + mNa*thisVar.Na + mAl*thisVar.Al),mmC--);

  int i1,i2,i3,i4,i5,i6,i7,i8;

  for(i1=n1;i1>=0;i1--)
  {
      for(i2=n2;i2>=0;i2--)
      {
         for(i3=n3;i3>=0;i3--)
         {
           for(i4=n4;i4>=0;i4--)
           {
             for(i5=n5;i5>=0;i5--)
             {
               for(i6=n6;i6>=0;i6--)
               {
                  for(i7=n7;i7>=0;i7--)
                  {
                    for(i8=n8;i8>=0;i8--)
                    {
                       if ((mC*i1 + i2 + mO*i3 + mN*i4 + mCl*i5 + mS*i6 + mNa*i7 + mAl*i8) == mM)
                          Form1->ListBox1->Items->Add((AnsiString)i1 + ":" + (AnsiString)i2 + ":" + (AnsiString)i3 + ":" + (AnsiString)i4 + ":" + (AnsiString)i5 + ":" + (AnsiString)i6 + ":" + (AnsiString)i7 + ":" + (AnsiString)i8);
                    }
                  }
               }
             }
           }
         }
      }
   }

}
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
   double tmM = 0;
   int tmmC = 0;
  //开始检查输入
   switch(checkAllTedit(eM))
   {
     case 1:
        if(eM->Text.ToDouble() < 0 || eM->Text.ToDouble() > 1500 )
          {
           ShowMessage("M输入不符合要求[<1500]");
           return;
          }
         else
          {tmM = eM->Text.ToDouble();}

         if(checkAllTedit(eA)== 0)
            {
              ShowMessage("A输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eA)>1?tmmC=(int)tmM:tmmC=eA->Text.Trim().ToInt();
              }
        //开始检查其它输入
        //n1-----------------------------------------------
          if(checkAllTedit(eC)== 0)
            {
              ShowMessage("C输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eC)>1?n1=tmmC:n1=eC->Text.Trim().ToInt();
              }
           //n2----------------------------------------------
           if(checkAllTedit(eH)== 0)
            {
              ShowMessage("H输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eH)>1?n2=tmmC:n2=eH->Text.Trim().ToInt();
              }

            //n3----------------------------------------------
           if(checkAllTedit(eO)== 0)
            {
              ShowMessage("O输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eO)>1?n3=tmmC:n3=eO->Text.Trim().ToInt();
              }

            //n4----------------------------------------------
           if(checkAllTedit(eN)== 0)
            {
              ShowMessage("N输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eN)>1?n4=tmmC:n4=eN->Text.Trim().ToInt();
              }

            //n5----------------------------------------------
           if(checkAllTedit(eCl)== 0)
            {
              ShowMessage("Cl输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eCl)>1?n5=tmmC:n5=eCl->Text.Trim().ToInt();
              }

           //n6----------------------------------------------
           if(checkAllTedit(eS)== 0)
            {
              ShowMessage("S输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eS)>1?n6=tmmC:n6=eS->Text.Trim().ToInt();
              }

           //n7----------------------------------------------
           if(checkAllTedit(eNa)== 0)
            {
              ShowMessage("Na输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eNa)>1?n7=tmmC:n7=eNa->Text.Trim().ToInt();
              }

           //n8----------------------------------------------
           if(checkAllTedit(eAl)== 0)
            {
              ShowMessage("Al输入不符合要求[必须为数字]");
              return;
            }
            else
              {
                checkAllTedit(eAl)>1?n8=tmmC:n8=eAl->Text.Trim().ToInt();
              }
      break;
     case 2:
         ShowMessage("M输入不符合要求(不是数字)");
         return;
      break;
     case 3:
         ShowMessage("M输入不符合要求(必须输入)");
         return;
      break;
   }

  //开始处理
  Form1->ListBox1->Items->Clear();
  Form1->StatusBar1->Panels->Items[0]->Text = "请等待,正在处理...";
  getVar(tmM);
  Form1->StatusBar1->Panels->Items[0]->Text = "完成! :)";
}
//---------------------------------------------------------------------------

bool checkNumber(AnsiString s)
{
  int ti = 0;
  try
    {
      ti = s.ToDouble();
      return true;
     }
  catch(...)
  {
      return false;
  }
}

int checkAllTedit(TEdit *Myedit)
{
   AnsiString tempS = Myedit->Text.Trim();
   if(tempS != "")
      {
      if(checkNumber(tempS))
        return 1; //是数字
      else
        return 0; //不是数字
      }
   else
      return 3;  //为空
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eMKeyPress(TObject *Sender, char &Key)
{

AnsiString InputKey = Key; 
AnsiString ValidKey = AnsiString("0123456789.");
if (ValidKey.Pos(InputKey) == 0)
{
Key = 0;
}

}
//---------------------------------------------------------------------------

void __fastcall TForm1::eAKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eAlKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eNaKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eSKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eClKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eNKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eOKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eHKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::eCKeyPress(TObject *Sender, char &Key)
{
  if(((Key<=47)||(Key>=58)))
  {
   Key=0;
   return;
  }        
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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