Skip to content

Instantly share code, notes, and snippets.

@vg
Created February 22, 2010 09:37
Show Gist options
  • Save vg/310969 to your computer and use it in GitHub Desktop.
Save vg/310969 to your computer and use it in GitHub Desktop.
Can someone rewrite this C code in Java
#include <stdio.h>
#define MAXVAL 255 /* maximum size of an input line */
main ()
{
FILE *fp1, *fp2, *fopen (); /* file pointer declarations */
char line[MAXVAL]; /* line read from the file.max 255 chars */
int a, b, c, result;
char student[100]; /* assume student name is <= 100 characters */
float cgpa;
fp1 = fopen ("in2.dat", "r");
fp2 = fopen ("out2.dat", "w");
while (fgets (line, MAXVAL, fp1) != NULL)
{
sscanf (line, "%d %d %d %s %f", &a, &b, &c, student, &cgpa);
result = (a * a) + (b * b) + (c * c);
fprintf (fp2, " (a*a + b*b + c*c)= %d %s %f\n", result, student, cgpa);
printf (" (a*a + b*b + c*c)= %d %s %f\n", result, student, cgpa);
}
fclose (fp1);
fclose (fp2);
}
0 1 2 John 3.4
1 2 3 Jane 4.5
2 3 4 Alice 5.6
3 4 5 Bob 6.7
4 5 6 Charlie 7.8
(a*a + b*b + c*c)= 5 John 3.400000
(a*a + b*b + c*c)= 14 Jane 4.500000
(a*a + b*b + c*c)= 29 Alice 5.600000
(a*a + b*b + c*c)= 50 Bob 6.700000
(a*a + b*b + c*c)= 77 Charlie 7.800000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment