Convertir un programme c en java

denzelboy Messages postés 7 Date d'inscription jeudi 29 mars 2007 Statut Membre Dernière intervention 19 mai 2008 - 23 avril 2008 à 14:43
sheorogath Messages postés 2448 Date d'inscription samedi 21 février 2004 Statut Modérateur Dernière intervention 29 janvier 2010 - 24 avril 2008 à 15:31
Bonjour dans le cadre de mon projet de fin d'étude que je dois en java. J'ai un header (matrix_2d.h) que je souhaite convertir en java. Mais je me trouve perdu par la suite Voici mes implementations:

/* matrix_2d.h*/
#ifndef __MATRIX_2D_H__
#define __MATRIX_2D_H__

typedef int element_type;
#define min_element_type -32768
#define max_element_type 32767

typedef struct __matrix_2d {
int row;
int col;
element_type **m;
} matrix_2d;


matrix_2d *matrix_2d_create(int r, int c);
void matrix_2d_destroy(matrix_2d *m);
void matrix_2d_clear(matrix_2d *m);
void matrix_2d_write(matrix_2d *m);
element_type matrix_2d_abs_max(matrix_2d *m);
#endif /* __MATRIX_2D_H__ */

j'ai remplacé la struct matrix_2d par la classe suivante en java:

/*class matrix_2d*/

public class matrix_2d {

private int row = 0;
private int col = 0;
private int[][] element_type = null;


/**
* Method matrix_2d
*
*
* @param r
* @param c
* @param et
*
*/
public matrix_2d(int r, int c, int[][] et) {
// TODO: Add your code here

row=r;
col=c;
element_type=et;
}

/**
* Method getCol
*
*
* @return
*
*/
public int getCol() {
// TODO: Add your code here
return col;
}

/**
* Method getRow
*
*
* @return
*
*/
public int getRow() {
// TODO: Add your code here

return row;
}

/**
* Method getElement_type
*
*
* @return
*
*/
public int[][] getElement_type() {
// TODO: Add your code here

return element_type;
}


ensuite j'ai crée une interface pour implementer le header matrix2d
/*interface MATRIX2D*/

public interface MATRIX2D {

public matrix_2d[] matrix_2d_create(int r, int c);

public void matrix_2d_destroy(matrix_2d[] m);

public void matrix_2d_clear(matrix_2d[] m);

public void matrix_2d_write(matrix_2d[] m);

public int matrix_2d_abs_max(matrix_2d[] m);


}

voici le matrix2d.c

#include "matrix2d.h"
#include <stdlib.h>
#include <stdio.h>


/*
* Allocate memory for a two-dimensional RxC matrix.
* Returns NULL on failure.
*/
matrix_2d *matrix_2d_create(int r, int c)
{
int row;
matrix_2d *m;

m = malloc(sizeof(matrix_2d));

if (m!=NULL) {
m->row = r;
m->col = c;
m->m = malloc(r*sizeof(element_type*));
if (m->m==NULL) return NULL;
for (row=0; row<r; row++) {
m->m[row] = malloc(c*sizeof(element_type));
if (m->m[row]==NULL) return NULL;
}
}

return m;
}


/*
* Destroy a matrix.
*/
void matrix_2d_destroy(matrix_2d *m)
{
int row;
for (row=0; row<m->row; row++) {
free(m->m[row]);
}
free(m);
}


/*
* Set all matrix elements to zero.
*/
void matrix_2d_clear(matrix_2d *m)
{
int row, col;

for (row=0; row<m->row; row++) {
for (col=0; col<m->col; col++) {
m->m[row][col] = 0;
}
}
}

/*
* Display a matrix.
*/
void matrix_2d_write(matrix_2d *m)
{
int row, col;
for (row=0; row<m->row; row++) {
for (col=0; col<m->col; col++) {
printf("%3d",m->m[row][col]);
}
printf("\n");
}
}


/*
* Returns max(abs(matrix)), the absolute maximum value of a matrix.
*/
element_type matrix_2d_abs_max(matrix_2d *m)
{
int row, col;
element_type max = 0;

for (row=0; row<m->row; row++) {
for (col=0; col<m->col; col++) {
if (abs(m->m[row][col])>max) max = abs(m->m[row][col]);
}
}

return max;
}

5 réponses

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
23 avril 2008 à 15:12
Salut,

bon là c'est super abusé quand même...

import static java.lang.Math.abs;
import static java.lang.System.out;

public class Matrix2D {
    public static final int    MIN_ELEMENT_TYPE    = -32768;
    public static final int    MAX_ELEMENT_TYPE    = 32767;
    private int                row                    = 0;
    private int                column                = 0;
    private int[][]            m                    = null;

    public Matrix2D(final int r, final int c) {
        if (r < 0)
            throw new IllegalArgumentException("Invalid row size");
        if (c < 0)
            throw new IllegalArgumentException("Invalid column size");        this.m new int[(this.row r)][(this.column = c)];
    }

    /*
     * Set all matrix elements to zero.
     */
    public void clear() {
        for (int i = 0; i < row; ++i)
            for (int j = 0; j < column; ++j)
                m[i][j] = 0;
    }

    /*
     * Display a matrix.
     */
    public void write() {
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < column; ++j)
                out.printf("%3d", m[i][j]);
            out.println();
        }
    }

    /*
     * Returns max(abs(matrix)), the absolute maximum value of a matrix.
     */
   public int abs_max() {
        int max = 0;
        for (int i = 0; i < row; ++i)
            for (int j = 0; j < column; ++j)
                if (abs(m[i][j]) > max)
                    max = abs(m[i][j]);
        return max;
    }
}

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
denzelboy Messages postés 7 Date d'inscription jeudi 29 mars 2007 Statut Membre Dernière intervention 19 mai 2008
24 avril 2008 à 03:11
merci pour ta reponse mais imagines que la structure suivante

typedef struct __matrix_2d {
int row;
int col;
element_type **m;
} matrix_2d;

doit etre utilisée ds d'autre programm alors je pense que c'est mieux de passer parb une classe tu en penses koi ?
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
24 avril 2008 à 09:28
Salut,

Matrix2D c'est quoi pour toi sinon une classe ?

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
denzelboy Messages postés 7 Date d'inscription jeudi 29 mars 2007 Statut Membre Dernière intervention 19 mai 2008
24 avril 2008 à 10:47
Salut,

C'est bien sur une classe j'ai compris merci
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
sheorogath Messages postés 2448 Date d'inscription samedi 21 février 2004 Statut Modérateur Dernière intervention 29 janvier 2010 17
24 avril 2008 à 15:31
clos

"n'est pas mort ce qui semble a jamais dormir et en d'etrange temps meme la mort peut mourrir"
0
Rejoignez-nous