Atul Mishra
BAN USERI solved it in c++...Please let me know any improvements..
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#define MAXLEN 350
using namespace std;
void reverse(char * str , int len){
int i=0,j=len-1;
char tmp;
while(i<j){
tmp=str[j];
str[j]=str[i];
str[i]=tmp;
j--;i++;
}
}
char * floatToString(double f ,char * str, int fractionPartSize)
{
ofstream fout1 ("float1.out");
int sign=f<0?1:0;
int pos =0;
int intPart = (int)f;
double fractionPart= f-intPart;
//one char at a time , first int part then fraction part
if(intPart==0)
{
str[pos++]='0';
}
else if(sign==1)
{
str[pos++]='-';
intPart=-intPart;
}
while(intPart>0){
str[pos++]='0'+intPart%10;
intPart/=10;
}
int intPartSize=pos-sign;
reverse(str+sign,intPartSize);
//int i=0;
str[pos++]='.';
while(pos<fractionPartSize+intPartSize+1){
f=f-(int)f;
f*=10;
str[pos++]='0'+(int)f;
}
str[pos]='\0';
fout1<<"string"<<str;
}
int main()
{
ifstream fin ("float.in");
ofstream fout("float.out");
char mainStr[MAXLEN];
double f;
fin>>f;
int fractionPartSize=6;
floatToString(f,mainStr,fractionPartSize);
return 0;
}
just remove the semi colons..I don't know how it got there..I copied it directly from my local copy and it's not there. Thanks for pointing out.
- Atul Mishra September 06, 2013