Salve ragazzi...
Ho un grande problema. Ho un codice scritto in C e vorrei portarlo in Java. Il problema è che ho provato a vedere come funziona Java ma non ho capito granché.
Qualcuno mi può dare una mano per favore?
Salve ragazzi...
Ho un grande problema. Ho un codice scritto in C e vorrei portarlo in Java. Il problema è che ho provato a vedere come funziona Java ma non ho capito granché.
Qualcuno mi può dare una mano per favore?
Non ho capito: vuoi riscrivere lo stesso codice in Java o vuoi richiamare codice C da Java? Se vuoi fare la seconda cosa, guardati JNI .
Ho un grande problema. Ho un codice scritto in C e vorrei portarlo in Java. Il problema è che ho provato a vedere come funziona Java ma non ho capito granché.Innanzitutto dovresti spiegare meglio di cosa si tratta. Cioè se quello che hai in C sono solo un paio di funzioni, un intero sorgente o un programma completo. E comunque dovresti specificare se il codice in C usa funzionalità o librerie specifiche, come l'utilizzo di framework grafici o altro. Non è nemmeno detto che il porting sia fattibile e/o facile.
Comunque le alternative sono appunto 2: a) riscrivere il tutto in puro Java, b) usare JNI (Java Native Interface) per chiamare codice C da Java.
Poi da quanto dici, Java non lo conosci. Questo complica ulteriormente le cose .....
Salve ragazzi...
Ho un grande problema. Ho un codice scritto in C e vorrei portarlo in Java. Il problema è che ho provato a vedere come funziona Java ma non ho capito granché.
Qualcuno mi può dare una mano per favore?
non penso ti convenga imparare il java solo per tradurre un codice in java... dicci di che tipo di codice si tratta (o, ancora meglio, postalo)
Scusatemi ragazzi mi sono spiegato male io. Cmq. io devo fare il porting da C a Java ma sto diventando matto :mc:
Il programma dovrebbe caricare un file e fare dei calcoli. Sinceramente devo ancora capire bene.
Cominicia cosi:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
afsdfdlecosdfsfdcco
23-07-2007, 23:27
Cominicia cosi:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Come il 99% dei programmi c :stordita:
Sii piu' specifico!
Cominicia cosi:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Dai che proviamo a tradurlo linea per linea :D.
import java.io.*;
import java.util.*;
Dai che proviamo a tradurlo linea per linea :D.
#define MAX_TEMP 10
#define MAX_LEN_VOCABULARY 20
#define MAX_FRAMES 2000
#define PARAM_ORDER 1
#define LMAX_ASCII_BUFFER 50
#define NN 40
float dypr(struct dati *proncep,struct dati *sintcep);
struct dati{
int nfr;
float array[MAX_FRAMES][PARAM_ORDER];
} temp[MAX_TEMP], tok;
int path[MAX_FRAMES][MAX_FRAMES];
float meldattemp[MAX_LEN_VOCABULARY+1][1600];
int ktemp,ktoke,iftoke;
int ifrst,ilast[MAX_LEN_VOCABULARY+1];
int ntemp;
int ntoke;
float radtok;
int nframetemp[MAX_LEN_VOCABULARY+1];
float score[MAX_LEN_VOCABULARY+1][MAX_FRAMES],
aval[MAX_LEN_VOCABULARY+1];
Proviamo in diretta :D.
import java.io.*;
import java.util.*;
public class Main {
static final int MAX_TEMP = 10;
static final int MAX_LEN_VOCABULARY = 20;
static final int MAX_FRAMES = 2000;
static final int PARAM_ORDER = 1;
static final int LMAX_ASCII_BUFFER = 50;
static final int NN = 40;
static class dati {
int nfr;
float[][] array = new float[MAX_FRAMES][PARAM_ORDER];
};
static dati[] temp = new dati[MAX_TEMP];
static { for(int i = 0; i < temp.length; i++) temp[i] = new dati(); }
static dati tok = new dati();
static int[][] path = new int[MAX_FRAMES][MAX_FRAMES];
static float[][] medattemp = new float[MAX_LEN_VOCABULARY + 1][1600];
static int ktemp, ktoke, iftoke;
//int nftoke, nftemp
static int ifrst;
static int[] ilast = new int[MAX_LEN_VOCABULARY + 1];
static int ntemp;
static int ntoke;
static float radtok;
static int[] nframetemp = new int[MAX_LEN_VOCABULARY + 1];
static float[][] score = new float[MAX_LEN_VOCABULARY + 1][MAX_FRAMES];
static float[] aval = new float[MAX_LEN_VOCABULARY + 1];
Ragazzi che ciofeca che ho scritto :D.
import java.io.*;
import java.util.*;
import static java.lang.Math.sqrt;
import static java.lang.Math.max;
import static java.lang.Math.floor;
public class Main {
static final int MAX_TEMP = 10;
static final int MAX_LEN_VOCABULARY = 20;
static final int MAX_FRAMES = 2000;
static final int PARAM_ORDER = 1;
static final int LMAX_ASCII_BUFFER = 50;
static final int NN = 40;
static class dati {
int nfr;
float[][] array = new float[MAX_FRAMES][PARAM_ORDER];
};
static dati[] temp = new dati[MAX_TEMP];
static { for(int i = 0; i < temp.length; i++) temp[i] = new dati(); }
static dati tok = new dati();
static int[][] path = new int[MAX_FRAMES][MAX_FRAMES];
static float[][] meldattemp = new float[MAX_LEN_VOCABULARY + 1][1600];
static int ktemp, ktoke, iftoke;
//int nftoke, nftemp
static int ifrst;
static int[] ilast = new int[MAX_LEN_VOCABULARY + 1];
static int ntemp;
static int ntoke;
static float radtok;
static int[] nframetemp = new int[MAX_LEN_VOCABULARY + 1];
static float[][] score = new float[MAX_LEN_VOCABULARY + 1][MAX_FRAMES];
static float[] aval = new float[MAX_LEN_VOCABULARY + 1];
public static void main(String[] argv) {
/* in c argv ha un argomento in più */
{
String[] temp = new String[argv.length + 1];
temp[0] = "Main";
System.arraycopy(argv, 0, temp, 1, argv.length);
argv = temp;
}
int argc = argv.length;
String dir_out;
String dir_in;
String file_aut;
String file_ris;
String file_ris1;
String linebuf;
int i, j;
long start, end;
float valore;
int[] iord = new int[MAX_LEN_VOCABULARY + 1];
float[] dmatr = new float[MAX_LEN_VOCABULARY + 1];
float[] b = new float[MAX_LEN_VOCABULARY + 1];
int count_ex, global_index;
int count_muc_not_det;
int count_ant_start, count_post_start;
int frame_ritardo, frame_anticipo;
double radtok, sum_rad_tok;
float[] score_min = new float[MAX_LEN_VOCABULARY + 1];
int[] score_min_ind = new int[MAX_LEN_VOCABULARY + 1];
float[] medattok = new float[PARAM_ORDER];
String[] tempName = new String[MAX_LEN_VOCABULARY + 1];
String tokname, file_list, token_file, out_file;
File fp_list, fp_temp, fp_tok;
int nt;
float el;
/* command line interpreter */
if(argc != 4) {
printf("argc=%d\n", argc);
printf(" file name for list of templates>\n");
printf(" file name for token>\n");
printf(" output file >\n");
System.exit(-1);
}
printf("argc=%d\n", argc);
for(i = 0; i < argc; i++) printf("%s ", argv[i]);
/* leggo i file di parametri per ogni tempate (tramite il file lista) e
riempio la matrice dei templates*/
ktemp = 1;
file_list = argv[1]; //file_list è la stringa argv[1]?
printf("\nTEMPLATES list: %s%n", file_list);
token_file = argv[2]; //idem?
printf("\nTOKEN file: %s\n", token_file);
out_file = argv[3];
printf("\nOUTPUT file: %s\n", out_file);
//read token
Scanner scanner = null;
if((scanner = fopen(token_file)) == null) {
printf("\nCan't open file %s\n", token_file);
System.exit(1);
}
nt = 0;
while(scanner.hasNextFloat()) {
el = scanner.nextFloat();
tok.array[nt][0] = el;// carico array di token
nt++;
}
tok.nfr = nt;
scanner.close();
if((scanner = fopen(file_list)) == null) {
printf("\n Can't read from list file");
System.exit(1);
}
ktemp = 0; //nr of template
while(scanner.hasNextLine()) {
linebuf = scanner.nextLine();
//if ?
{
printf("file template:%s\n", linebuf);
Scanner tempScanner = null;
if((tempScanner = fopen(linebuf)) == null) {
printf("\n Can't open file %s\n", linebuf);
System.exit(1);
}
nt = 0;//nr frames template
while(tempScanner.hasNextFloat()) {
el = tempScanner.nextFloat();
temp[ktemp].array[nt][0] = el; //carica array - caso di 1 parametro
nt++;
}
tempScanner.close();
}
temp[ktemp].nfr = nt; //nr frames template ktemp
ktemp++;
}
scanner.close(); //fclose(fp_list)
printf("nr templates ktemp %d\n",ktemp);
printf("nr frame token %d\n",tok.nfr);
for(j = 0; j < ktemp; j++)
printf("nr frame token %d\n", tok.nfr);
///////////////////////////////////////////
//ora i coeff sono caricati.
//chiamiamo la procedura dynamic programming per il confronto
///////////////////////////////////////////
for(i = 0; i < ktemp; i++) {
el = dypr(temp[i], tok);
printf("risultato del confronto=%f\n", el);
}
}
/**************************************************************
COMPUTE PROJECTION DISTANCE BETWEEN FRAMES
***************************************************************/
void dist(int j, float[] meldattok, double radtok, float[] diin) {
int k;
long indtem;
double sum, sumper, distance;
indtem = PARAM_ORDER*(j-1)+1;
sum = 0.0;
sumper = 0.0;
for(k = 0; k < PARAM_ORDER; k++) {
sumper = sumper+(meldattok[k]*meldattemp[ktemp][(int)indtem+k]);
}
distance = 1.0 - (sumper / radtok);
if(distance > 1) {
sum = radtok;
} else {
sum = radtok*sqrt(max(0.0, distance));
}
diin[0] = (float)sum;
}
/**************************************************************
COMPUTE simple euclidean DISTANCE BETWEEN FRAMES
***************************************************************/
static float d(float[] v1, float[] v2) {
int k;
float result;
result = 0;
for(k = 0; k < PARAM_ORDER; k++) {
result = result + (v1[k] - v2[k]) * (v1[k]-v2[k]);
}
return (float)sqrt(result);
}
/**************************************************************************
PERFORMS THE DYNAMIC PROGRAMMING ALGORITHM TO BUILD DISTANCE MATRIX
****************************************************************************/
static float dypr(dati proncep, dati sintcep) {
class elto {
float score;
int cfree;
}
elto[] cell = new elto[MAX_FRAMES];
for(int i = 0; i < cell.length; i++) {
cell[i] = new elto();
}
int first, last, i1, i2, i, j, k;
float aval, bval, cval;
int bfree, cfree;
float dij;
float norm;
int lpercorso;
int nftoke, nftemp;
float result;
nftoke = sintcep.nfr;
nftemp = proncep.nfr;
for(i = 0; i < nftoke; i++) {
for(j = 0; j < nftemp; j++) {
path[i][j] = 4; /* { no-processed cell flag } */
}
}
i1=(int)floor(((float)(3*nftemp-nftoke-1)/8)+0.5);
i2=(int)floor(((float)(9*nftoke-3*nftemp-5)/8)+0.5);
for (i=0;i<nftemp;i++) {
cell[i].score=1e35f;
/*cell[i].free=false;*/
cell[i].cfree=0;
}
aval=0;
bval=1e35f;
bfree=0;
first=0;
last=3;
for(i = 0; i< nftoke;i++) {
first = ((nftemp-NN+1)*i)/nftoke;
last = ((nftemp-NN+2)*i)/nftoke+NN;
if(last > nftemp) last = nftemp;
// for (j=first;j<nftemp;j++)
for (j=first;j<last;j++) {
dij=d(proncep.array[i],sintcep.array[j]);
cval=cell[j].score; cfree=cell[j].cfree;
if (((cval+dij) < (aval+2*dij)) && (cfree>0)) {
if ((cval <= bval) || (bfree==0)) {
bval=cval+dij; /*{ choose C }*/
/*bfree=false;*/
bfree=0;
cfree--;
path[i][j]=1;
} else {
bval=bval+dij; /*{ choose B }*/
/*bfree=false;*/
bfree--;
cfree=0;
path[i][j]=2;
}
} else {
if(((bval+dij) < (aval+2*dij)) && (bfree>0)) {
bval=bval+dij; /*{ choose B }*/
/*bfree=false;*/
bfree--;
cfree=0;
path[i][j]=2;
} else {
bval=aval+2*dij; /*{ choose A }*/
/*bfree=true;*/
bfree=2;
cfree=2;
path[i][j]=0;
}
}
cell[j].score=bval; cell[j].cfree=cfree;
aval=cval;
}
bval=1e35f; /*bfree=false;*/
bfree=0;
if (i>i2)
/*aval=cell[first].score;*/
aval=cell[first+2].score;
/*else if (div(i+1+1,2).rem==1) /*se i+1 dispari if odd(i+1)*/
else if(div(i,3).rem==2)
/*aval=cell[first-1].score;*/
aval=cell[first].score;
else
aval=1e35f;
if (i<(i1-1))
/*last=2*(i+1+1);*/
last=3*(i+1)+2+1;
else
/*last:=nftemp+(i-nftoke+1) div 2;*/
/*last=nftemp+div(i+1-nftoke+1,2).quot;*/
last=nftemp+div(i+1-nftoke+1,3).quot;
if (i<i2)
/*first:=i div 2 +1*/
/*first=div(i+1,2).quot+1;*/
first=div(i+1,3).quot;
else
/*first=nftemp+2*(i+1+1-nftoke)-1;*/
first=(nftemp-1)-2+3*(i+1-(nftoke-1));
}
/*dtw:=cell[nftemp].score/(nftoke+nftemp);*/
/* pat=fopen("path.txt","w");
for (i=0;i<nftemp;i++)
{ for (j=0;j<nftoke;j++)
fprintf(pat,"%i",path[j][i]);
fprintf(pat,"\n");
}
fclose(pat);*/
i=nftoke-1;
j=nftemp-1;
lpercorso=1;
norm=0;
while(i!=0 || j!=0) {
lpercorso++;
//for(k=0;k<BARK_COEF;k++)
// norm+=pow(proncep->ceparray[j][k],2);
switch (path[i][j]) {
case 0: path[i][j]=8;i--; j--; break;
case 1: path[i][j]=8;i--; break;
case 2: path[i][j]=8;j--; break;
case 4: printf("Segnali non nel range 1/3 - 3\n"); return 1e35f;
}
}
result=cell[nftemp-1].score/lpercorso;
return result;
}
static class div_t {
int quot;
int rem;
}
static div_t div(int numerator, int denominator) {
div_t result = new div_t();
result.quot = numerator / denominator;
result.rem = numerator % denominator;
return result;
}
/* Mimano funzioni C */
private static Scanner fopen(String file) {
try {
return new Scanner(new File(file));
} catch(IOException ex) {
System.out.println("Cannot open " + file);
return null;
}
}
private static void printf(String format, Object...params) {
System.out.printf(format, params);
}
}
Compila ma non so se funzioni. Per metà è copia e incolla :D. Bisogna provare e, nel caso in cui non vada a ramengo nel tentativo, confrontare i risultati.
Se sputa errori in esecuzione, incolla la traccia completa dell'errore.
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.