TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ken H
NA
646
361.7k
Double precision data type conversion in c language
Dec 19 2013 4:36 AM
hello friend,
Explanation:The program does not rely on any function provided by the system.
My code is as follows:
#include <stdio.h>
#define ARR_MAX 10
bool IsDig(const char c){
char Cdig[12]={"0123456789."};
for (int i=0;i<11;i++)
{
if(Cdig[i]==c) return true;
}
return false;
}
int str_len(const char *t){
int len=0;
for(;*t!='\0';t++) ++len;
return len;
}
void Initial(double *r){
for(int i=0;i<ARR_MAX;i++,r++){
*r=0.0;
}
}
void main(void)
{
char test[100]={"afadfe545.568adf48dfeea89.51fvae10648.789"};
bool find_mark=false;
double acceptD,temp,accept_dec,E_integer,E_decimal,result[ARR_MAX];
int i,k,k2,m,mark_index,digLen,lens,index;
acceptD=temp=accept_dec=E_integer=E_decimal=0.0;
digLen=index=i=0;
lens=str_len(test);
Initial(&result[0]);
for (i=0;i<lens;i++)
{
if(IsDig(test[i])) ++digLen;
else{
if(digLen>0){
acceptD=test[i-1]-48; // Get single digits.
k=1;
while(k<digLen) // Double-digit or more.
{
E_integer=1;
for(m=1;m<=k;m++) E_integer*=10; // Index Multiplier(Such as: 10,100,1000 and so on...)
temp=test[i-1-k]-48;
acceptD=acceptD+temp*E_integer;
++k;
if(test[i-1-k]=='.') // If there is a decimal point.
{
mark_index=k;
accept_dec=acceptD;
acceptD=0.0;
E_decimal=1;
for(m=1;m<=mark_index;m++)
E_decimal*=0.1;
accept_dec*=E_decimal; // Convert to decimal.
find_mark=true;
}
if(find_mark)
{
++k;
acceptD=test[i-1-k]-48;
++k;
k2=1;
while (k<digLen)
{
E_integer=1;
for(m=1;m<=k2;m++) E_integer*=10;
temp=test[i-1-k]-48;
acceptD+=(temp*E_integer);
++k;
++k2;
}
}
}
digLen=0; // Reset for zero.
if(find_mark){
acceptD+=accept_dec;
result[index++]=acceptD;
find_mark=false; // Reset for false.
}
else result[index++]=acceptD;
}
}
}
// Prevent tail figures:
if(digLen>0){
acceptD=test[i-1]-48;
k=1;
while(k<digLen)
{
E_integer=1;
for(m=1;m<=k;m++) E_integer*=10;
temp=test[i-1-k]-48;
acceptD=acceptD+temp*E_integer;
++k;
if(test[i-1-k]=='.')
{
mark_index=k;
accept_dec=acceptD;
acceptD=0.0;
E_decimal=1;
for(m=1;m<=mark_index;m++)
E_decimal*=0.1;
accept_dec*=E_decimal;
find_mark=true;
}
if(find_mark)
{
++k;
acceptD=test[i-1-k]-48;
++k;
k2=1;
while (k<digLen)
{
E_integer=1;
for(m=1;m<=k2;m++) E_integer*=10;
temp=test[i-1-k]-48;
acceptD+=(temp*E_integer);
++k;
++k2;
}
}
}
digLen=0; // Reset for zero
if(find_mark){
acceptD+=accept_dec;
result[index++]=acceptD;
find_mark=false; // Reset for false.
}
else result[index++]=acceptD;
}
// Print the get result.
for(i=0;i<index;i++) printf("%f4.2\n",result[i]);
}
// It printed result is:
545.5680004.2
48.0000004.2
89.5100004.2
10648.7890004.2
//In fact, I want results should look like this:
545.568
48.0
89.51
10648.789
How to do it?
Thank for you.
Reply
Answers (
2
)
sort of structure in c language
Combinations number in c language