当我阅读以下文件时
"X","Pa(X)","Time","pa","xixj","Value"
"X",0,5,0,"0-0","123.814935276"
" "," "," "," ","0-1","234"
" "," "," "," ","0-2","100"
" "," "," "," ","1-0","166"
" "," "," "," ","1-1","203.0866414"
" "," "," "," ","1-2","383"
" "," "," "," ","2-0","186"
" "," "," "," ","2-1","338"
" "," "," "," ","2-2","173.0984233"
" "," ",10,0,"0-0","186.221113"
" "," "," "," ","0-1","391"
" "," "," "," ","0-2","64"
" "," "," "," ","1-0","235"
" "," "," "," ","1-1","195.7454998"
" "," "," "," ","1-2","275"
" "," "," "," ","2-0","218"
" "," "," "," ","2-1","121"
" "," "," "," ","2-2","118.0333872"
" "," ",20,0,"0-0","416.36349977"
" "," "," "," ","0-1","282"
" "," "," "," ","0-2","735"
" "," "," "," ","1-0","278"
" "," "," "," ","1-1","211.8960279"
" "," "," "," ","1-2","266"
" "," "," "," ","2-0","743"
" "," "," "," ","2-1","224"
" "," "," "," ","2-2","371.7404745"
我的代码无法读取值列。我使用strtod
但结果是:
X "X"
PaX "Pa(X)"
Time "Time"
pa "pa"
xixj "xixj"
Value 0.000000
start csv
X "X"
PaX 0
Time 5
pa 0
xixj "0-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "0-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "0-2"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-2"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-2"
Value 0.000000
start csv
X " "
PaX " "
Time 10
pa 0
xixj "0-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "0-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "0-2"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-2"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-2"
Value 0.000000
start csv
X " "
PaX " "
Time 20
pa 0
xixj "0-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "0-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "0-2"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "1-2"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-0"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-1"
Value 0.000000
start csv
X " "
PaX " "
Time " "
pa " "
xixj "2-2"
Value 0.000000
并且值始终为0.如何解决此问题? 这里是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <stdbool.h>
#include <ctype.h>
#define BUFSIZ 1024
#define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
double calculateMLpa(const char *Xn_val[], char *traj[], float value[], double alphaxixj, double tauxi, int sz, int dim) {
double mlx = 0;
double v;
double alphaxi;
char *state;
int i;
int p;
int j;
int k;
// int sz = sizeof(Xn_val) / sizeof(int);
// int dim = sizeof(traj) / sizeof(char);
double trns[sz][sz];
double m[sz];
char *trat="-";
// m[xi] values: the number of transitions leaving the state xi
printf("%d %d \n",sz,dim);
int cont=0;
for (i = 0; i < sz; i++) {
m[i] = 0.0;
for (j = 0; j < sz; j++) {
v = 0.0;
int newlength = strlen(Xn_val[i])+strlen(trat)+strlen(Xn_val[j])+1;
state = malloc(sizeof(char)*newlength);
if(state != NULL){
state[0] = '\0';
strcat(state,Xn_val[i]);
strcat(state,trat);
strcat(state,Xn_val[j]);
printf("%s ",state);
}else {
printf(stderr,"malloc failed!\n");
}
// for (k=0; k<=dim;++k){
if (traj[cont] != NULL ){
if (strcmp(traj[cont],state)==0){
v = value[cont+1];
printf("%f \n",v);
}
}
trns[i][j] = v;
printf("%f - \n",trns[i][j]);
if (strcmp(Xn_val[i],Xn_val[j])!=0)
m[i] = m[i] + v;
cont++;
}
}
for (i=0;i<sz;++i){
for(j=0;j<sz;++j){
printf("%f ",trns[i][j]);
}
printf("\n");
}
for (p=0;p<sz;++p){
printf("%f - \n",m[p]);
}
alphaxi = alphaxixj * (((double) sz) - 1.0);
alphaxi = alphaxixj;
//printf("%d ",sz);
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
// xi!=xj
if (strcmp(Xn_val[i], Xn_val[j])!=0) {
mlx = mlx + lgamma(alphaxixj + trns[i][j]) - lgamma(alphaxixj);
}
// xi
else {
mlx = mlx + lgamma(alphaxi) - lgamma(alphaxi + m[i]);
mlx = mlx + lgamma(alphaxi + m[i] + 1.0)+ (alphaxi + 1.0) * log(tauxi);
mlx = mlx - lgamma(alphaxi + 1.0)- (alphaxi + m[i] + 1.0) * log(tauxi + trns[i][j]);
}
}
}
return (mlx);
}
void main() {
printf("inizio\n");
FILE *pf;
int N=20;
int f,kk=0,k=0,i;
bool first=true;
const char *a[]={"0","1","2"};
char *traject[]={"0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2"};
double bs=0;
char *str;
char *trat="-";
int file;
//for (file=0;file<4;++file){
pf=fopen("//home//user//prova0.csv","r");
//float array[10][10];
float *t;
//char *state[];
t = (float *)malloc(N * sizeof(float));
float val;
if (pf)
{
char buffer[BUFSIZ], *ptr;
/*
* Read each line from the file.
*/
while(fgets(buffer, sizeof buffer, pf)){
k++;
}
fclose(pf);
pf=fopen("//home//user//prova0.csv","r");
printf("k=%d\n",k);
char *state[k];
while(fgets(buffer, sizeof buffer, pf)) {
//k=0;
printf("start csv \n");
char *X;
char *PaX;
int Time;
char *pa;
char *xixj;
double Value[k];
char *token;
char *ptr = buffer;
const char end[2]=",";//fgets(buffer, sizeof buffer, pf);
token = strtok(ptr, end);
f=0;
/* walk through other tokens */
while( token != NULL )
{
if(f==0){
X=token;
printf( "X %s\n", token );
}else if(f==1){
PaX=token;
printf( "PaX %s\n", token );
}
else if(f==2){
Time=(token);
printf( "Time %s \n", token );
}
else if(f==3){
pa=token;
printf( "pa %s \n", token );
}
else if(f==4){
xixj=(token);
printf( "xixj %s \n", token );
}
else{
char *str;
Value[kk]=strtod(token, &str);
printf("Value %f \n", Value[kk]);
kk++;
}
token = strtok(NULL, end);
f++;
}
}
fclose(pf);
}
else /* fopen() returned NULL */
{
perror(pf);
}
}
printf("\nstart\n");
int sz=ARRAYSIZE(a);
int dim=ARRAYSIZE(traject);
bs=calculateMLpa(a,traject,t,1.0,0.1,sz,dim);
printf("done file%d \n",file);
printf("%f ",bs);
//}
}
答案 0 :(得分:1)
你用“读取所有的值,你需要在解析为双倍之前摆脱它们,据我所知。
xixj "0-1"
和printf( "xixj %s \n", token );
表示令牌= <"0-1">
,而非<0-1>
,价值相同,为<"383">
而不是<383>
所以应该使用它:
Value[kk]=strtod(&token[1], NULL);
此:
printf("Value %d \n", Value[kk]);
应该是
printf("Value %f \n", Value[kk]);
因为值[k]是双倍的。
对于完全不同的东西 - 你应该考虑使用编码标准,像单字母名称这样的代码使代码更难以维护和读取。
答案 1 :(得分:0)
Value[kk]=strtod(&token[1], NULL);