#include <iostream>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <string.h>
using namespace std;
const double PI = 3.14159;
int main(int argc, char * const argv[]){
FILE * cfptr;
cfptr = fopen("n123.txt", "r");
FILE * cfptr2;
cfptr2 = fopen("n123_trim.txt", "w");
double Ra = 50.0e4;
double resist;
char line[50];
char character_start = '(';
const char * character_end = ")}";
char * tokenPtr;
char * start;
char * end;
int segment=0;
double x, y, z;
double xnew, ynew, znew, radiusnew, radiusold;
double dx, distance, radius;
double count;
int count_seg = 0;
fscanf(cfptr, "%s", line);
while( !feof( cfptr ) ) {
if ( isdigit( line[0] ) ) {
if( count_seg > 0) fprintf( cfptr2, "%d\n%.2f\n%.2f\n%.2f\n\n", segment, dx, radius, resist );
segment = atoi( line );
count = 0.;
dx = 0.0;
radius = 0.0;
resist = 0.0;
count_seg++;
}
fscanf(cfptr, "%s", line);
while( ! isdigit( line[0]) && (!feof( cfptr )) ){
start = strchr( line, character_start );
if ( start != NULL ){
start++;
int size = strcspn( start, character_end );
if (size > 0 ){
tokenPtr = strtok( start, "," );
xnew = atof( tokenPtr );
tokenPtr = strtok( NULL, "," );
ynew = atof( tokenPtr );
tokenPtr = strtok( NULL, ",");
znew = atof( tokenPtr );
tokenPtr = strtok(NULL, ")");
radiusnew = atof( tokenPtr );
if ( count < 1. ){
x = xnew;
y = ynew;
z = znew;
radius = radiusnew;
radiusold = radiusnew;
}
if ( count > 0.) {
double deltax = sqrt( pow((xnew-x),2) + pow((ynew-y),2) + pow((znew-z),2) );
dx = dx + deltax;
x = xnew;
y = ynew;
z = znew;
radius = radius*(count/(count+1.)) + radiusnew*(1./(count+1.));
resist = resist + Ra*deltax/(PI*radiusnew*radiusold);
radiusold = radiusnew;
}
count++;
}
}
fscanf( cfptr, "%s", line );
}
}
fprintf( cfptr2, "%d\n%.2f\n%.2f\n%.2f\n\n", segment, dx, radius, resist);
fclose(cfptr);
fclose(cfptr2);
return 0;
}