Advertisement
Guest User

stringtofloat

a guest
Apr 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4.  
  5.  
  6. #ifndef DEBUG
  7. #define DEBUG(...)printf(_VA_ARGS_)
  8. #endif
  9.  
  10. float stringtofloat(char *broj) {
  11. float temp = 0;
  12. int cnt = -1, neg = 0;
  13.  
  14. while (*broj) {
  15. if (*broj == '.') {
  16.  
  17. cnt = 0;
  18. }
  19.  
  20. if (*broj == '-') {
  21. neg = 1;
  22. }
  23.  
  24.  
  25. if (isdigit(*broj)) {
  26. temp *= 10;
  27. temp = temp - (*broj - '0');
  28. if (cnt == 0) {
  29. cnt++;
  30. }
  31. }
  32.  
  33. broj++;
  34. }
  35.  
  36. if (cnt > -1) {
  37. temp = temp / pow(10, cnt);
  38. }
  39. if (neg == 1) {
  40. temp = -temp;
  41. }
  42. return temp;
  43. }
  44.  
  45. int main() {
  46. int n = 0;
  47. char broj[11];
  48.  
  49. scanf("%d", &n);
  50.  
  51. for (int i = 0; i < n; i++) {
  52. scanf("%d", broj);
  53. printf("%.3f\n", stringtofloat(broj));
  54. }
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement