解决方案 »

  1.   

    http://img07.taobaocdn.com/sns_album/i7/T18NtDXgtIXXb1upjX.jpg
      

  2.   

    就是这里出的错,运行完后出错,但是数据库都已经写进去了
    我自己调试的结果只知道
       /////////////////////////////////////从memo中读取数据并分析   ///////////
    之前出的错,把后面改成函数或者屏蔽都出错function solve(ss: string;bb:string):string;
    var i,i1,j,k,m,n,m2,m4,p,p1,p2,k1:integer;
    a2,a3: array [1..1000] of string;
    s,b:string;
    t:double;
    begin
      ss:=copy(ss,2,length(ss)-1);
      n:=length(ss);
      i:=0;
      k:=1;     //位置
      p:=1;     //数组
      m2:=0;
      while m2=0 do
      begin
       if copy(ss,i,1)=',' then
        begin
          a2[p]:=copy(ss,k,i-k);
          k:=i+1;
          p:=p+1;
        end
        else if copy(ss,i,1)=';' then
          begin
            a2[p]:=copy(ss,k,i-k);
            k:=i+1;
            p:=p+1;
            m2:=1;
          end;
          i:=i+1;
      end;
      for i:=1 to p-1 do
       begin
         s:='';
         for j:=1 to p-1 do
         begin
           if j=i then s:=s
           else
           begin
             s:=s+a2[j]+',';
           end;
         end;
         s:=copy(s,1,length(s)-1);
         if s='' then exit;           //为空则退出
         s:='('+s+';';
         m4:=0;    if length(bb)=0 then   bb:=bb+s
         else
         begin
           for j:=1 to length(bb) do             //判断是否已加入
           begin
             if copy(bb,j,length(s))=s then
               m4:=1;
           end;
           if m4=0 then bb:=bb+s;
         end;   
           solve(s,bb);      //循环调用
       end;    //for
       form1.Memo1.Lines.Add(bb);
    end;procedure TForm1.Button3Click(Sender: TObject);
    var i,i1,i2,i3,j,j2,k,kk,p,n,m,m1,m2,m3,m4,m5,ppp,p1,p2,c1:integer;
    lmax,rmax,b,b1,b2,s,s1,ss,bb,a,strong:string;
    a1,a2,ll: array [1..1000] of string;   //ll为最大项目集
    mm:array [1..1000] of integer;      //记录最大频繁项目集的频数
    t:double;
    //conf:float;
    begin
        kk:=0;     
        //将数据给回变量
        adoquery1.Close();
        adoquery1.SQL.Clear();
        adoquery1.SQL.Add('select max(c) as cc from frequent');
        adoquery1.Open();
        c:=adoquery1.Fields[0].AsInteger;
        for i:=1 to c do
        begin
        adoquery1.Close();
        adoquery1.SQL.Clear();
        adoquery1.SQL.Add('select l,r from frequent where c='+inttostr(i));
        adoquery1.Open();
        l[i]:=adoquery1.Fields[0].AsString;
        r[i]:=adoquery1.Fields[1].AsString;
        end;   //最大频繁项目集
       s:=l[c];
       //lmax:=l[c];             //先把最大的添加到最大频繁项目集
      // rmax:=r[c];            //先把最大的添加到最大频繁项目集次数
       k:=1;    //所至位置
       p:=1;    //数组位数
       i:=1;
       m2:=0;
       while(m2=0) do            //输入到数组中,方便比较
       begin
         if copy(s,i,1)=','  then          //   一组中的一段
         begin
          a1[p]:=copy(s,k,i-k);
          lmax:=lmax+a1[p]+',';
           k:=i+1;
           p:=p+1;
         end
         else if copy(s,i,1)=';'  then       //    一组中最后一个
         begin
           a1[p]:=copy(s,k,i-k);    //获取这一组
           lmax:=lmax+a1[p]+';';
           k:=i+1;
           p:=p+1;                            //得到数组长度
         end
         else if copy(s,i,1)='*' then          //遇到结束符
         begin
           m2:=1;//停止
         end;
           i:=i+1;
       end;   while(c>1) do
       begin
      c:=c-1;
      s:=l[c];
      ppp:=0;////////////////////////////相同项个数限制变量
       k:=1;    //所至位置
       p1:=1;    //数组位数
       i:=1;
       m2:=0;
       while(m2=0) do            //输入到数组中,方便比较
       begin
         if copy(s,i,1)=','  then          //   一组中的一段
         begin
         if copy(s,k,i-k)<>'' then
          a2[p1]:=copy(s,k,i-k);
           k:=i+1;
           p1:=p1+1;
         end
         else if copy(s,i,1)=';'  then       //    一组中最后一个
         begin
           a2[p1]:=copy(s,k,i-k);    //获取这一组
           k:=i+1;
           p1:=p1+1;
           m1:=0;
           for m:=1 to p1-1 do              //判断组中是否c-1个都不存在于现有的不完全的最大频繁项目集
           begin
             for j:=1 to p-1 do
             begin
               if a1[j]=a2[m] then
               m1:=m1+1;                   // 有相同项
             end;
           end;
           if m1=ppp then           //c-1个都不在,就将不存在的添加到现有的最大频繁项目集中
           begin
              for m:=1 to p1-1 do
              begin
                m3:=0;
                for j:=1 to p-1 do
                begin
                if a1[j]=a2[m] then m3:=1;                   // 有相同项
                end;
                if m3=0 then           //不同,则添加进去
                begin
                  a1[p]:=a2[m];
                  p:=p+1;
                end;
               lmax:=lmax+a2[m]+',';        //存入最大频繁项目集
              end;
              lmax:=copy(lmax,1,length(lmax)-1);
              lmax:=lmax+';';
           end;
           p1:=1;
         end
         else if copy(s,i,1)='*' then          //遇到结束符
         begin   
           ppp:=ppp+1;  //限制数量+1,再次循环
           i:=0;       //重新再开始
           if ppp=c then
             m2:=1;//停止
         end;
           i:=i+1;
       end;
      end;
      lmax:=lmax+'*';
    ///////////////////////////////求关联规则//////////////////////
       s:=lmax;
       k:=1;    //所至位置
       p1:=1;    //数组位数
       b2:='select orderID from order_pro where ';
       i:=1;
       m2:=0;
       while(m2=0) do           //遇到结束符结束
       begin
         if copy(s,i,1)=','  then          //   一组中的一段
         begin
           a1[p1]:=copy(s,k,i-k);
           b2:=b2+'['+a1[p1]+']=1 and ';
           k:=i+1;
           p1:=p1+1;
         end
         else if copy(s,i,1)=';'  then       //    一组中最后一个
         begin
           a1[p1]:=copy(s,k,i-k);    //获取这一组
           b:=b2+'['+a1[p1]+']=1';           //动态生成语句
           k:=i+1;
           p1:=p1+1;
           //开始执行这一段
           adoquery1.Close();
           adoquery1.SQL.Clear();
           adoquery1.SQL.Add(b);
           adoquery1.Open();
           m3:=adoquery1.RecordCount;   //算出一个最大频繁项目的个数
           for j:=1 to p1-1 do
           begin
             ss:=ss+a1[j]+',';
           end;
           ss:=copy(ss,1,length(ss)-1);
           ll[kk]:=ss;       //将最大频繁项目存入数组
           mm[kk]:=m3;
           kk:=kk+1;
           ss:='('+ss+';';                  //装饰下ss以便判断
           //////////调用函数solve
             solve(ss,bb);
          memo1.Lines.Add('*');                       //将数据存入memo
            bb:='';
            ss:='';
         b2:='select orderID from order_pro where ';          //重置语句
         p1:=1;
         end
         else if copy(s,i,1)='*' then          //遇到结束符
           begin
             m2:=1;//停止
           end;
           i:=i+1;
       end;
       adoquery1.Close();   /////////////////////////////////////从memo中读取数据并分析   ///////////
       ss:='';
       kk:=-1;
       for i1:=0 to memo1.Lines.Count-2 do
       begin
         if memo1.Lines.Strings[i1]<>'*' then      //将一组带重复数据的字符都给s
         begin                                      //去掉重复项
           if ss='' then ss:=memo1.Lines.Strings[i1]
           else
           begin
             k:=1;    //所至位置
             p:=1;    //数组位数
             s:=memo1.Lines.Strings[i1];
             m2:=length(memo1.Lines.Strings[i1]);
             for i:=1 to m2+1  do            //输入到数组中,方便比较
             begin
               if copy(s,i,1)=','  then          //   一组中的一段
               begin
                 a1[p]:=copy(s,k,i-k);
               //   showmessage(s+'+'+a1[p]);
                 k:=i+1;
                 p:=p+1;
               end
               else if copy(s,i,1)=';'  then       //    一组中最后一个
               begin
                 Setlength(bb,0);          //初始化bb
                 a1[p]:=copy(s,k,i-k);    //获取这一组
                 k:=i+1;
                 p:=p+1;
                 for i2:=1 to p-1 do
                 begin
                   bb:=bb+a1[i2]+',';
                 end;
                 bb:=copy(bb,1,length(bb)-1)+';';             m4:=0;    //判断是否重复
                 for j:=0 to length(ss)  do
                 begin
                   if copy(ss,j,length(bb))=bb then
                     m4:=1;
                 end;
                 if m4=0 then
                   ss:=ss+bb;      //ss中不存在,加入
                 p:=1;                            //初始化p
               end;//else 3
             end;  //for 2
           end;  //else 2
         end  //if
         else        //处理完数据后开始计算   (46,75,73;(75,73;(73;(75;(46,73;(46;(46,75;(23,75,73;(23,73;(23;(23,75;(23,46,73;(23,46;(23,46,75;
         begin
            kk:=kk+1;
             ss:=ss+'*';
             ss:=copy(ss,2,length(ss)-1);
             b2:='select orderID from order_pro where ';
             i3:=1;
             p1:=1;
             k:=1;
             m3:=0;
             while(m3=0) do           //遇到结束符结束
             begin
               if copy(ss,i3,1)=','  then          //   一组中的一段
               begin
                 a1[p1]:=copy(ss,k,i3-k);
                 if copy(a1[p1],1,1)='(' then
                    a1[p1]:=copy(a1[p1],2,length(a1[p1])-1);
                 b2:=b2+'['+a1[p1]+']=1 and ';
                 k:=i3+1;
                 p1:=p1+1;
               end
               else if copy(ss,i3,1)=';'  then       //    一组中最后一个
               begin
               a1[p1]:=copy(ss,k,i3-k);    //获取这一组
               if copy(a1[p1],1,1)='(' then
                    a1[p1]:=copy(a1[p1],2,length(a1[p1])-1);
               b:=b2+'['+a1[p1]+']=1';           //动态生成语句
               k:=i3+1;
               p1:=p1+1;
               s1:='';
               for j2:=1 to p1-1 do
               begin
                 s1:=s1+a1[j2]+',';
               end;
               s1:=copy(s1,1,length(s1)-1);
              //开始执行这一段
               adoquery1.Close();
               adoquery1.SQL.Clear();
               adoquery1.SQL.Add(b);
               adoquery1.Open();
               m5:=adoquery1.RecordCount;   //算出一个最大频繁项目的个数
               t:=mm[kk]/m5;
               if t>=0.6 then strong:='是'
               else  strong:='否';
               b:='insert into rules values('+#39+ll[kk]+#39+','+#39+s1+#39+','+floattostr(t)+','+#39+strong+#39+')';
               adoquery1.Close();
               adoquery1.SQL.Clear();
               adoquery1.SQL.Add(b);
               adoquery1.ExecSQL;     
               b2:='select orderID from order_pro where ';          //重置语句
               p1:=1;
               end
               else if copy(ss,i3,1)='*' then          //遇到结束符
               begin
                 m3:=1;//停止
               end;
               i3:=i3+1;
             end;
             ss:='';
         end;//else 1
         
       end;   //for 1
       memo1.Clear;
       form1.PageControl1.ActivePageIndex:=3;
       form1.ADOTable3.Active:=true;
    end;