root/trunk/p6b.c

Revision 389, 3.9 kB (checked in by heitor.barbieri, 3 weeks ago)

essage first commit

Line 
1/* Program P6B: dumps the field contents of all active records
2               of a given data base
3*/
4#include <stdio.h>
5#include <string.h>
6#include <math.h>
7
8#include "cisis.h"   /* CISIS Interface header file */
9#include "cirun.h"   /* CISIS Interface runtime declarations */
10
11#define MAXTEMS 40000
12#define TAG20 2
13
14#define MAXRELAT 101
15
16
17//LONGX Q = 0;
18//float QVN[MAXTEMS];
19float QVW[MAXTEMS];
20//LONGX QVMFN[MAXMFNS];
21
22float TABSIM[MAXRELAT];
23LONGX TABSIMMFN[MAXRELAT];
24
25float wrkarea;
26int idx1,idx2,idx3,idx4,point1;
27
28 int *DVN;
29unsigned dvnsize;
30 float *DVW;
31unsigned dvwsize;
32
33
34void main(int argc, char *argv[])
35{
36    RECSTRU *recp;    /* mandatory for defines REC,MFR,DIR,FIELDP */
37
38    LONGX mfn,maxmfn,mfn2,maxmfn2,mfnq,mfnd;
39    int diridx,dirloop,diridx2,dirloop2;
40    char *p;
41
42    char areaval[100+1];
43    int i, g1, g2;
44
45#if 0
46    int c1;
47#endif
48
49    float W, DW;
50    float somap,somaq2,somad2,denom,sim;
51
52    LONGX parmfrom=1,parmto=99999999L,parmloop=1;
53    LONGX parmdfrom=1,parmdto=99999999L;
54
55    if (argc < 2) {
56           printf("P6B <dbname> <parmfrom> <parmto> \n");
57           exit(1);
58    }
59
60   if (argc >= 2) if (sscanf(argv[2],"%"_LD_,&parmfrom) != 1) fatal(argv[2]);
61   if (argc >= 3) if (sscanf(argv[3],"%"_LD_,&parmto) != 1) fatal(argv[3]);
62
63   p=ALLOC((ALLOPARM)(dvnsize=sizeof(int)*MAXTEMS));
64   if (p == NULL) fatal("ALLOC/DVN");
65   DVN=(int *)p;
66   p=ALLOC((ALLOPARM)(dvwsize=sizeof(float)*MAXTEMS));
67   if (p == NULL) fatal("ALLOC/DVW");
68   DVW=(float *)p;
69
70   for (mfn=parmfrom; mfn<=parmto; mfn+=parmloop) {
71    RECORD(0L,argv[1],mfn);
72    if (RECrc == RCEOF) break;
73         if (RECrc != RCNORMAL) continue;
74        memset(QVW,0,dvnsize);
75     mfnq=mfn;
76          for (diridx=0, dirloop=MFRnvf; dirloop--; diridx++) {
77            if (DIRtag(diridx) != TAG20) continue;
78         p=FIELDP(diridx);
79       subfield((UBYTE *)p, DIRlen(diridx), 'f', (UBYTE *)areaval);
80       W=atof(areaval);
81       subfield((UBYTE *)p, DIRlen(diridx), 'd', (UBYTE *)areaval);
82       g1=atoi(areaval);
83 //      printf("1. %6"_LD_"|%6d|%2.9f \n",mfn,g1,W);
84       QVW[g1]=W;
85     }
86      for (mfn2=parmdfrom; mfn2<=parmdto; mfn2+=parmloop) {
87        RECSTRU *recp; /* Torando o registro como "corrente" */
88        RECORD(1L,argv[1],mfn2);
89        if (RECrc == RCEOF) break;
90             if (RECrc != RCNORMAL) continue;
91        memset(DVW,0,dvwsize);
92        mfnd=mfn2;
93        for (diridx2=0, dirloop2=MFRnvf; dirloop2--; diridx2++) {
94              if (DIRtag(diridx2) != TAG20) continue;
95         p=FIELDP(diridx2);
96         subfield((UBYTE *)p, DIRlen(diridx2), 'd', (UBYTE *)areaval);
97         g2=atoi(areaval);
98         subfield((UBYTE *)p, DIRlen(diridx2), 'f', (UBYTE *)areaval);
99         DW=atof(areaval);
100  //     printf("2. %6"_LD_"|%6d|%2.9f \n",mfn2,g2,DW);
101         DVW[g2]=DW;
102        }
103   // Calcula a similaridade
104         for (somap=somaq2=somad2=0, i=0; i<MAXTEMS; i++) {
105              W=QVW[i]; DW= *(DVW+i);
106                     somap += W*DW;
107                        somaq2 += W*W;
108              somad2 += DW*DW;
109            }
110              denom=sqrt(somaq2*somad2);
111               if (denom > 0) sim=somap/denom;
112               else sim=0;
113  //        if (sim > 0)
114  //          printf("==> %6"_LD_"|%6"_LD_"|%9.6f \n",mfn, mfn2,sim );
115
116  //Coloca na fila de MAXRELAT maiores;
117          if (sim == 0) continue;
118
119          for (idx1=1 ; idx1 < MAXRELAT; ++idx1) {
120            if (sim > TABSIM[idx1]) break;
121          }
122          idx1++;
123          for (idx2=MAXRELAT-1; idx2 >= idx1; --idx2) {
124              idx4=idx2-1;
125              TABSIM[idx2]=TABSIM[idx4];
126              TABSIMMFN[idx2]=TABSIMMFN[idx4];
127          }
128          idx3=idx1-1;
129          TABSIM[idx3]=sim;
130          TABSIMMFN[idx3]=mfn2;
131    //      printf(". %"_LD_"|%"_LD_" \n",mfn2,TABSIMMFN[idx3]);
132       }
133  }
134
135  for (i=1; i<MAXRELAT; i++)  {
136             if (TABSIM[i] == 0) break;
137              printf("%"_LD_"|%"_LD_"|%9.6f|",i,mfnq,TABSIM[i]);
138              printf("%"_LD_" ",TABSIMMFN[i]);
139              printf(" \n");
140          }
141  exit;
142}
143/* end of main */
Note: See TracBrowser for help on using the browser.