#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>

void usage()
{
	printf("Usage: scaleTree intree scale\n");
	exit(1);
}

int main(int argc, char **argv)
{
	int i,j;
	double scale=1.0, totalDist=0.0, scaledTotalDist=0.0;
	char inputString[10000],outputString[10000],distString[100],treeFile[100];
	int index=0,outIndex=0;
	FILE *input;
	if(argc<3) usage();
	else { strcpy(treeFile,argv[1]);
		scale=atof(argv[2]);
	}
	if((input=fopen(treeFile,"r"))==NULL) 
		printf("can not open file: %s\n",treeFile);
	char c;
	while((c=fgetc(input))!=EOF) {
		if(c!='\n' && c!=' ') inputString[index++]=c;
	}
//	printf("%s\n\n",inputString);
//	printf("finish reading tree\n");
	for(i=0;i<index;i++) {
		if(inputString[i]!=':') outputString[outIndex++]=inputString[i];
		else {
			outputString[outIndex++]=inputString[i];
			j=i+1;
			int distIndex=0;
			while(inputString[j]!=',' && inputString[j]!=')'){
				distString[distIndex++]=inputString[j];
				j++;
			}
//			printf("distString: %s j= %d\n",distString,j);
//			getchar();
			i=j-1;
			distString[distIndex]='\0';
			double dist=atof(distString);
			totalDist+=dist;
			dist*=scale;
			sprintf(distString,"%f%c",dist,'\0');
			distIndex=0;
			while(distString[distIndex]!='\0')  {
				outputString[outIndex++]=distString[distIndex];
				distIndex++;
			}
		}
//		printf("finish deal with step %d char: %c\n",i,inputString[i]);
	}
	scaledTotalDist=totalDist*scale;
	outputString[outIndex]='\0';
	printf("%s\n",outputString);
	printf("total distance before scale: %12.6f and after scale: %12.6f\n",totalDist,scaledTotalDist);
}
