convert.c 2.57 KB
Newer Older
saeed's avatar
saeed committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<stdio.h>
#include<stdlib.h>

char Str[32]  = {'\0'};
char RStr[32] = {'\0'};

static void reverse(char *s, char *t, int sign, int npadzero){
  
  char *tmp = s;
  int len = 0;
  while (*tmp !='\0' ){
    tmp++;
    len++;
  }
  tmp--;

  if (sign < 0){
    *t = '-';
    t++;
  }

  while(npadzero--){
    *t = '0';
    t++;
  }
  
  while (len >0 ){
    *t = *tmp;
    tmp--;
    len--;
    t++;
  }
  *t = '\0';
}

static void intostr(int a, char *st){
  int sign = 1;
  if (a < 0){
    sign = -1;
    a = (-a);
  }
  int index = 0;
  int r = -1 ;
  int q = 0;
  char cl[64] = {'\0'};
  do {
    r = a % 10;
    q = (a - r) / 10;
    *(cl+index) = (char)(r+'0');
    a = q;
    index++;
  }while(a >0);
  
  *(cl+index) = '\0';

  reverse(cl,st,sign,0);
  
}

static void intostr_pad(int a, char *st, int npad){
  int sign = 1;
  if (a < 0){
    sign = -1;
    a = (-a);
  }
  int index = 0;
  int r = -1 ;
  int q = 0;
  char cl[64] = {'\0'};
  do {
    r = a % 10;
    q = (a - r) / 10;
    *(cl+index) = (char)(r+'0');
    a = q;
    index++;
  }while(a >0);
  
  *(cl+index) = '\0';

  reverse(cl,st,sign,npad);
  
}

static void floatostr(float a, char *str, int nafterdecimalpoint){
  int sign = 1;
  long long int ia = (long long int) (a);
  if (a < 0) {
    sign = -1;
    a = (-a);
    ia = (long long int) (a);
  }
  int npadzero = 0;
  long long int n = 1;
  float b = a - (float)ia;
  float bt = b;
  int hitnonzero = 0;
  for(int i=0; i < nafterdecimalpoint;++i){
    n *= 10L;
    bt *= 10L;
    if (((long long int)bt == 0) && (hitnonzero == 0)) npadzero++;
    if ((long long int)bt != 0 ) hitnonzero = 1;
    bt = bt - (long long int)bt;
  }
  if (nafterdecimalpoint == 1) npadzero = 0;

  b *= n;
  long long int ifrac = (long long int) b;
  int index = 0;
  long long int r = -1 ;
  long long int q = 0;
  char cl[64] = {'\0'};
  char ctmp[64] = {'\0'};
  do {
    r = ia % 10;
    q = (ia - r) / 10;
    *(cl+index) = (char)(r+'0');
    ia = q;
    index++;
  }while(ia >0);

  *(cl+index) = '\0';
  reverse(cl,ctmp,sign,0);

  char ck[64]    = {'\0'};
  char ctmp1[64] = {'\0'};
  
  r = -1 ;
  q = 0;
  index = 0;
  do {
    r = ifrac % 10;
    q = (ifrac - r) / 10;
    *(ck+index) = (char)(r+'0');
    ifrac = q;
    index++;
  }while(ifrac >0);

  *(ck+index) = '\0';
  if (sign > 0){
    reverse(ck,ctmp1,sign,npadzero);
  }else{
    reverse(ck,ctmp1,-sign,npadzero);
  }

  char *tmp = ctmp;
  while(*tmp != '\0'){
    *str = *tmp;
    str++;
    tmp++;
  }
  *str = '.';
  str++;
  tmp = ctmp1;

  while(*tmp != '\0'){
    *str = *tmp;
    str++;
    tmp++;
  }
  *str = '\0';
  
  return;
}