Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 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.  
  37. if (cnt > -1) {
  38. temp = temp / pow(10, cnt);
  39. }
  40. if (neg == 1) {
  41. temp = 0 - temp;
  42. }
  43.  
  44. return temp;
  45. }
  46.  
  47. int main() {
  48. int n = 0;
  49. char broj[11];
  50.  
  51. scanf("%d", &n);
  52.  
  53. for (int i = 0; i < n; i++) {
  54. scanf("%s", broj);
  55. printf("%.3f\n", stringtofloat(broj));
  56. }
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement