Monday, July 27, 2009

In C Programming,what is the source code/algorithm for knowing if a given input is a decimal or a fraction?

example,:





....


float a;


scanf("%f"%26amp;a);


...





if the user inputs :





a decimal(e.g. 1.00, 3.6)





it will display 1.0000 or 3.60000





but if the user inputs a fraction(e.g.1/2)





it will display 0.50000

In C Programming,what is the source code/algorithm for knowing if a given input is a decimal or a fraction?
#include%26lt;stdio.h%26gt;


#include%26lt;stdlib.h%26gt;





int a,b,present_variable,i);


char str[N];





scanf("%s",%26amp;str);





if( strstr( str,'/')


{





for(i=0; str[i] != NULL ; ++i)


{





if( str[i]== '/')


{


a=present_var;


present_var=0;


continue;


}


present_var=present_var*10+str[i];


}


b=present_variable;


printf("%f",(float) a/b);


}


else


printf("%f", atoi(str) );
Reply:get it from the user as string data, instead of float.





then scan the data for '/' and separate the numbers and store them in some float.





finally process them.





if u need a program, mail me.

order flowers

No comments:

Post a Comment