|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2003
Messaggi: 1041
|
[Help] Codice da C in Java
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?
__________________
Ho trattato con: ))Lexandrus((, abc3d, al2, almar, antonio panaro, bimbo-gio, bruno sorbelli, CLURACAN, cervoale, cristoforobiagrtti, danisevoo, dayz, DrUg@tO, eric654, Feroz, firmus, gigio75, grezzo, icest0rm, leleweb, linears4, madelui, Maverick82^, morpheus3g, mrwinch, obi_wan, PERPAX, piziul, Razorx92, rigolo, robby2002, sagomaccio, sciach, semenzara, slash84, takers, tati29268, twil83, verbania, volcanik & altri... |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2002
Messaggi: 6819
|
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 .
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
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 .....
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
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)
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jun 2003
Messaggi: 1041
|
Scusatemi ragazzi mi sono spiegato male io. Cmq. io devo fare il porting da C a Java ma sto diventando matto
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>
__________________
Ho trattato con: ))Lexandrus((, abc3d, al2, almar, antonio panaro, bimbo-gio, bruno sorbelli, CLURACAN, cervoale, cristoforobiagrtti, danisevoo, dayz, DrUg@tO, eric654, Feroz, firmus, gigio75, grezzo, icest0rm, leleweb, linears4, madelui, Maverick82^, morpheus3g, mrwinch, obi_wan, PERPAX, piziul, Razorx92, rigolo, robby2002, sagomaccio, sciach, semenzara, slash84, takers, tati29268, twil83, verbania, volcanik & altri... |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Jun 2003
Messaggi: 1041
|
x Angel): hai pvt...
__________________
Ho trattato con: ))Lexandrus((, abc3d, al2, almar, antonio panaro, bimbo-gio, bruno sorbelli, CLURACAN, cervoale, cristoforobiagrtti, danisevoo, dayz, DrUg@tO, eric654, Feroz, firmus, gigio75, grezzo, icest0rm, leleweb, linears4, madelui, Maverick82^, morpheus3g, mrwinch, obi_wan, PERPAX, piziul, Razorx92, rigolo, robby2002, sagomaccio, sciach, semenzara, slash84, takers, tati29268, twil83, verbania, volcanik & altri... |
|
|
|
|
|
#7 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
|
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Quote:
Codice:
import java.io.*; import java.util.*;
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Jun 2003
Messaggi: 1041
|
#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];
__________________
Ho trattato con: ))Lexandrus((, abc3d, al2, almar, antonio panaro, bimbo-gio, bruno sorbelli, CLURACAN, cervoale, cristoforobiagrtti, danisevoo, dayz, DrUg@tO, eric654, Feroz, firmus, gigio75, grezzo, icest0rm, leleweb, linears4, madelui, Maverick82^, morpheus3g, mrwinch, obi_wan, PERPAX, piziul, Razorx92, rigolo, robby2002, sagomaccio, sciach, semenzara, slash84, takers, tati29268, twil83, verbania, volcanik & altri... |
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Proviamo in diretta
Codice:
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];
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Ragazzi che ciofeca che ho scritto
Codice:
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);
}
}
Se sputa errori in esecuzione, incolla la traccia completa dell'errore.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:57.




















