Help!

Help!

Help!
我想把C:/temp下的data.txt中的数据给 Kmeans运算,但是怎么才能把 c中的#include改的能用啊 ?


问题 代码:


#!C:/perl/bin/perl

use CGI;
$query = new CGI;

print $query->header;
print $query->start_html("Save and Restore");
#@@
print '<body bgcolor="#FFFFFF">'."\n" ;
print '<div align="center">'."\n" ;
print "\n" ;
print '<p align="center"><marquee direction=left bgcolor="#66FF66"><font color="#6699FF"><font face="Comic Sans MS">Welcome to K-means</font></font></marquee></p>'."\n" ;
print "\n" ;
print '<p align="center"><marquee direction=right bgcolor="#66FF66"><font color="#6699FF"><font face="Comic Sans MS">Welcome to K-means'."\n" ;
print ",</font></font></marquee></p>\n" ;
#@@
print '<font color="#6699FF"><font face="Comic Sans MS">THE DATA</font></font>'."\n" ;

#@@

# Here's where we take action on the previous request
&save_parameters($query) if $query->param('action') eq 'SAVE';
$query = &restore_parameters($query) if $query->param('action') eq 'RESTORE';

# Here's where we create the form
print $query->start_multipart_form;


print '<table border="3" cellpadding="3" cellspacing="0" width="250">'."\n" ;
print " <tr>\n" ;
print ' <td bgcolor="#008080"><br>'."\n" ;
print '<textarea name=comment rows=5 cols=29></textarea>'."\n";

sub rd_parameters {
local($query) = @_;
local($filename) = "C:/temp/data.txt";
if (open(FILE,">$filename")) {
$query->read<FILE,$data>;
print $data;
close FILE;
}
}




#@@
use Inline C => <<END_C;
$data = **data;
int c_function()
{
#ifndef K_MEANS_H
#define K_MEANS_H

int *k_means(float**, int, int, int, float, float**);

#endif

/*****
** kmeans.c
** Parameters
** - array of data points (float **data)
** - number of data points (int n)
** - dimension (int m)
** - desired number of clusters (int k)
** - error tolerance (float t)
** - used as the stopping criterion, i.e. when the sum of
** squared euclidean distance (standard error for k-means)
** of an iteration is within the tolerable range from that
** of the previous iteration, the clusters are considered
** "stable", and the function returns
** - a suggested value would be 0.0001
** - output address for the final centroids (float **centroids)
** - user must make sure the memory is properly allocated, or
** pass the null pointer if not interested in the centroids

*/
#include <stdlib.h>
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int *k_means (float **data, int n, int m, int k, float t, float **centroids);
int loadDataset (float ***data, int *num, int *dim, char *filename);

int
main (int argc, char **argv)
{
float **data;
int num, dim, ncluster;
float e = 0.0005;
int *c;
int i = 0;

if (argc < 3)
{
printf ("Usage: kmeans -ncluster filename\n");
exit (-1);
}
if (loadDataset (&data, &num, &dim, argv[2]) != 0)
{
printf ("Error in loading dataset");
exit (-1);
}
printf("%d records with %d features has been loaded\n",num,dim);
ncluster = atoi (argv[1]);
c = k_means (data, num, dim, ncluster, e, 0);
for (i = 0; i < num; i++)
{
fprintf (stderr,"data point %d is in cluster %d\n", i, c[i]);
}
free(c);
for (i = 0; i < num; i++)
{
free(data[i]);
}
free(data);
return (0);
}

int *
k_means (float **data, int n, int m, int k, float t, float **centroids)
{
/*
* output cluster label for each data point
*/
int *labels = (int *) malloc (n * sizeof (int));

int h, i, j; /* loop counters, of course :) */
int *counts = (int *) malloc (k * sizeof (int)); /* size of
* each
* cluster
*/
float old_error, error = DBL_MAX; /* sum of squared euclidean
* distance */
float **c =
centroids ? centroids : (float **) malloc (k * sizeof (float *));
float **c1 = (float **) malloc (k * sizeof (float *)); /* temp
* centroids
*/
int nloop = 0;

assert (data && k > 0 && k <= n && m > 0 && t >= 0); /* for debugging */

/****
** initialization */

for (h = i = 0; i < k; h += n / k, i++)
{
c1[i] = (float *) malloc (m * sizeof (float));
if (!centroids)
{
c[i] = (float *) malloc (m * sizeof (float));
}
/*
* pick k points as initial centroids
*/

for (j = 0; j < m; j++)
{
c[i][j] = data[h][j];

};

}

/****
** main loop */

do
{
/*
* save error from last step
*/
nloop++;
printf ("Runing loop %d\n", nloop);
old_error = error, error = 0;

/*
* clear old counts and temp centroids
*/
for (i = 0; i < k; counts[i++] = 0)
{
for (j = 0; j < m; c1[i][j++] = 0);
}

for (h = 0; h < n; h++)
{
/*
* identify the closest cluster
*/
float min_distance = DBL_MAX;
for (i = 0; i < k; i++)
{
float distance = 0;
for (j = m; j-- > 0; distance += pow (data[h][j] - c[i][j], 2));
if (distance < min_distance)
{
labels[h] = i;
min_distance = distance;
}
}
/*
* update size and temp centroid of the destination cluster
*/
for (j = m; j-- > 0; c1[labels[h]][j] += data[h][j]);
counts[labels[h]]++;
/*
* update standard error
*/
error += min_distance;
}

for (i = 0; i < k; i++)
{ /* update all centroids */
for (j = 0; j < m; j++)
{
c[i][j] = counts[i] ? c1[i][j] / counts[i] : c1[i][j];
}
}

}
while (fabs (error - old_error) > t);

/****
** housekeeping */

for (i = 0; i < k; i++)
{
if (!centroids)
{
free (c[i]);
}
free (c1[i]);
}

if (!centroids)
{
free (c);
}
free (c1);

free (counts);
// printf("End of K_means\n");
return labels;
}

#define MAX_LINE_SIZE 2000
int
loadDataset (float ***DATA, int *NUM, int *DIM, char *filename)
{
FILE *fp;
char line[MAX_LINE_SIZE];
int num = 0, dim = 0;
int i=0;
float **data;
fp = fopen (filename, "r");
if (!fp)
{
printf ("Cannot open %s\n", filename);
return (-1);
}
while(fgets(line,MAX_LINE_SIZE,fp))
{
char* pstr,*word;
if((line[0]=='\0')||(line[0]=='#')) continue;
num++;
if(dim==0)
{
pstr=line;
word=strtok(pstr,"\t");
while((word=strtok(NULL,"\t")))
{
dim++;
}
}
}
data=(float**)malloc(num*sizeof(float*));
for(i=0;i<num;i++)
{
data[i]=(float*)malloc(dim*sizeof(float));
}
rewind(fp);
num=0;
dim=0;
while(fgets(line,MAX_LINE_SIZE,fp))
{
char* pstr,*word;
if((line[0]=='\0')||(line[0]=='#')) continue;
pstr=line;
word=strtok(pstr,"\t");
dim=0;
while((word=strtok(NULL,"\t")))
{
data[num][dim]=atoi(word);
dim++;
}
num++;
}
fclose (fp);
*DATA = data;
*NUM = num;
*DIM = dim;
return (0);
}

}

END_C


c_function;

exit 0;
#@@

print "<html>\n" ;
print "\n" ;
print "<head>\n" ;

print "</head>\n" ;
print "\n" ;
#@@
print'<form action="w.pl" method="POST">'."\n";
print '<input type="submit" value="FILE UPLOAD">'."\n";
print"</form>";
#@@
print'<form action="R.pl" method="POST">'."\n";
print '<input type="submit" value="RESULT">'."\n";
print"</form>";
#@@

print "</body>\n" ;
print "</html>\n" ;



jftewrrmxv
f02iF8 <a href="http://dqrplxotqunl.com/">dqrplxotqunl</a>, [url=http://dtfzuwkwhkih.com/]dtfzuwkwhkih[/url], [link=http://romgbhsxbomn.com/]romgbhsxbomn[/link], http://eicomkiglqpy.com/

[img]http://hxrofqeclwfe.com/[/img]
[COLOR=BLUE]URL:[/COLOR][url]HGBSELPmpbIBX[/url]