Hello,

I need to convert C codes to Java codes.this promram calculates surface areas of a 3D geometry which is read from datas from text file.thanks for help.

text file:

12
0 0 0
0 0 1
4 0 1
4 0 3
5 0 3
5 0 0
0 3 1
4 3 1
4 3 3
5 3 3
5 3 0
0 3 0
18
1 2
2 3
3 4
4 5
5 6
6 1
2 7
7 8
8 3
6 11
11 12
12 1
11 10
10 9
9 4
9 8
5 10
7 12
8
6
-6 -5 -4 -3 -2 -1
4
13 -17 5 10
6
11 -18 8 -16 -14 -13
4
1 7 18 12
4
-12 -11 -10 6
4
-7 -8 -9 2
4
-15 16 9 3
4
14 15 4 17

C syntax:

#include <stdio.h>   
#include <conio.h>  
#include <dos.h>  
#include <math.h>



FILE *fr;            

main()

{
   
   int x[50], y[50], z[50];
   int v=0, i, j, e=0, ex[50], ey[50], l=0, le[20], lcomp[20][20]; // v: vertice xyz: coordinates

   char line[100];
   clrscr();

   printf("File name should be 1.txt"),
   getchar();
   clrscr();
   fr = fopen ("1.txt", "rt");
   fgets(line, 80 , fr);
   sscanf (line, "%d", &v);
   printf ("Number of vertices: %d\n", v);


   for (i=1; i<v+1; i++){

	fgets(line, 80 , fr);
	sscanf (line, "%d %d %d", &x[i], &y[i], &z[i]);
	printf ("V%d - %d %d %d\n", i, x[i], y[i], z[i]);

	}

   fgets(line, 80 , fr);
   sscanf (line, "%d", &e);
   getchar();
   clrscr();
   printf ("Number of egdes: %d\n", e);

   for (i=1; i<e+1; i++){

	fgets(line, 80 , fr);
	sscanf (line, "%d %d", &ex[i], &ey[i]);
	printf ("E%d - %d %d\n", i, ex[i], ey[i]);

	}

   fgets(line, 80 , fr);
   sscanf (line, "%d", &l);
   getchar();
   clrscr();
   printf ("Number of loops: %d\n", l);

   for(i=1; i<l+1; i++){

   	fgets(line, 80, fr);
   	sscanf (line, "%d", &le[i]);
   	fgets(line, 80, fr);

	for(j=1; j<le[i]+1; j++){
		sscanf(line, "%d %d %d %d %d %d %d %d %d %d", &lcomp[i][1], &lcomp[i][2], &lcomp[i][3], &lcomp[i][4], &lcomp[i][5], &lcomp[i][6], &lcomp[i][7], &lcomp[i][8], &lcomp[i][9], &lcomp[i][10]);
        	
		}
   	
	}

   fclose(fr);

      for(i=1; i<l+1; i++){

	printf("L%d: %d\n", i, le[i]);

   	for(j=1; j<le[i]+1; j++){

	printf("%d ", lcomp[i][j]);

	}

   	printf("\n");

	}
getchar();
clrscr();


//Area Calculation
int a[20], nx[20][20], ny[20][20], nz[20][20], ni, nj, nk, ta=0;

   for (i = 1 ; i < l+1; i++) {

   ni=0;
   nj=0;
   nk=0;


   	 for (j = 1 ; j < le[i]+1; j++) {

		if (lcomp[i][j]>0) {

		nx[i][j] = (y[ex[lcomp[i][j]]] - y[ey[lcomp[i][j]]]) * (z[ex[lcomp[i][j]]] + z[ey[lcomp[i][j]]]);
		nz[i][j] = (x[ex[lcomp[i][j]]] - x[ey[lcomp[i][j]]]) * (y[ex[lcomp[i][j]]] + y[ey[lcomp[i][j]]]);
		ny[i][j] = (z[ex[lcomp[i][j]]] - z[ey[lcomp[i][j]]]) * (x[ex[lcomp[i][j]]] + x[ey[lcomp[i][j]]]);

        	}

		else {

		nx[i][j] = (y[ey[(-1)*lcomp[i][j]]] - y[ex[(-1)*lcomp[i][j]]]) * (z[ex[(-1)*lcomp[i][j]]] + z[ey[(-1)*lcomp[i][j]]]);
		nz[i][j] = (x[ey[(-1)*lcomp[i][j]]] - x[ex[(-1)*lcomp[i][j]]]) * (y[ex[(-1)*lcomp[i][j]]] + y[ey[(-1)*lcomp[i][j]]]);
		ny[i][j] = (z[ey[(-1)*lcomp[i][j]]] - z[ex[(-1)*lcomp[i][j]]]) * (x[ex[(-1)*lcomp[i][j]]] + x[ey[(-1)*lcomp[i][j]]]);

		}

		ni = ni + nx[i][j];
		nj = nj + ny[i][j];
		nk = nk + nz[i][j];

	 }

	 a[i] = sqrt((ni*ni) + (nj*nj) + (nk*nk)) / 2;

	 printf ("Area of L%d: %d\n", i, a[i]);
         ta = ta + a[i];

   }

  printf ("Total area is: %d", ta); 
           
  getchar();
	 
  return 0;

   
   
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Attachments
12
0 0 0
0 0 1
4 0 1
4 0 3
5 0 3
5 0 0
0 3 1
4 3 1
4 3 3
5 3 3
5 3 0
0 3 0
18
1 2
2 3
3 4
4 5
5 6
6 1
2 7
7 8
8 3
6 11
11 12
12 1
11 10
10 9
9 4
9 8
5 10
7 12
8
6
-6 -5 -4 -3 -2 -1
4
13 -17 5 10
6
11 -18 8 -16 -14 -13
4
1 7 18 12
4
-12 -11 -10 6
4
-7 -8 -9 2
4
-15 16 9 3
4
14 15 4 17
#include <stdio.h>   
#include <conio.h>  
#include <dos.h>  
#include <math.h>



FILE *fr;            

main()

{
   
   int x[50], y[50], z[50];
   int v=0, i, j, e=0, ex[50], ey[50], l=0, le[20], lcomp[20][20]; // v: vertice xyz: coordinates

   char line[100];
   clrscr();

   printf("File name should be 1.txt"),
   getchar();
   clrscr();
   fr = fopen ("1.txt", "rt");
   fgets(line, 80 , fr);
   sscanf (line, "%d", &v);
   printf ("Number of vertices: %d\n", v);


   for (i=1; i<v+1; i++){

	fgets(line, 80 , fr);
	sscanf (line, "%d %d %d", &x[i], &y[i], &z[i]);
	printf ("V%d - %d %d %d\n", i, x[i], y[i], z[i]);

	}

   fgets(line, 80 , fr);
   sscanf (line, "%d", &e);
   getchar();
   clrscr();
   printf ("Number of egdes: %d\n", e);

   for (i=1; i<e+1; i++){

	fgets(line, 80 , fr);
	sscanf (line, "%d %d", &ex[i], &ey[i]);
	printf ("E%d - %d %d\n", i, ex[i], ey[i]);

	}

   fgets(line, 80 , fr);
   sscanf (line, "%d", &l);
   getchar();
   clrscr();
   printf ("Number of loops: %d\n", l);

   for(i=1; i<l+1; i++){

   	fgets(line, 80, fr);
   	sscanf (line, "%d", &le[i]);
   	fgets(line, 80, fr);

	for(j=1; j<le[i]+1; j++){
		sscanf(line, "%d %d %d %d %d %d %d %d %d %d", &lcomp[i][1], &lcomp[i][2], &lcomp[i][3], &lcomp[i][4], &lcomp[i][5], &lcomp[i][6], &lcomp[i][7], &lcomp[i][8], &lcomp[i][9], &lcomp[i][10]);
        	
		}
   	
	}

   fclose(fr);

      for(i=1; i<l+1; i++){

	printf("L%d: %d\n", i, le[i]);

   	for(j=1; j<le[i]+1; j++){

	printf("%d ", lcomp[i][j]);

	}

   	printf("\n");

	}
getchar();
clrscr();


//Area Calculation
int a[20], nx[20][20], ny[20][20], nz[20][20], ni, nj, nk, ta=0;

   for (i = 1 ; i < l+1; i++) {

   ni=0;
   nj=0;
   nk=0;


   	 for (j = 1 ; j < le[i]+1; j++) {

		if (lcomp[i][j]>0) {

		nx[i][j] = (y[ex[lcomp[i][j]]] - y[ey[lcomp[i][j]]]) * (z[ex[lcomp[i][j]]] + z[ey[lcomp[i][j]]]);
		nz[i][j] = (x[ex[lcomp[i][j]]] - x[ey[lcomp[i][j]]]) * (y[ex[lcomp[i][j]]] + y[ey[lcomp[i][j]]]);
		ny[i][j] = (z[ex[lcomp[i][j]]] - z[ey[lcomp[i][j]]]) * (x[ex[lcomp[i][j]]] + x[ey[lcomp[i][j]]]);

        	}

		else {

		nx[i][j] = (y[ey[(-1)*lcomp[i][j]]] - y[ex[(-1)*lcomp[i][j]]]) * (z[ex[(-1)*lcomp[i][j]]] + z[ey[(-1)*lcomp[i][j]]]);
		nz[i][j] = (x[ey[(-1)*lcomp[i][j]]] - x[ex[(-1)*lcomp[i][j]]]) * (y[ex[(-1)*lcomp[i][j]]] + y[ey[(-1)*lcomp[i][j]]]);
		ny[i][j] = (z[ey[(-1)*lcomp[i][j]]] - z[ex[(-1)*lcomp[i][j]]]) * (x[ex[(-1)*lcomp[i][j]]] + x[ey[(-1)*lcomp[i][j]]]);

		}

		ni = ni + nx[i][j];
		nj = nj + ny[i][j];
		nk = nk + nz[i][j];

	 }

	 a[i] = sqrt((ni*ni) + (nj*nj) + (nk*nk)) / 2;

	 printf ("Area of L%d: %d\n", i, a[i]);
         ta = ta + a[i];

   }

  printf ("Total area is: %d", ta); 
           
  getchar();
	 
  return 0;

   
   
}
This article has been dead for over six months. Start a new discussion instead.