-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumManipulate.c
More file actions
187 lines (154 loc) · 2.84 KB
/
Copy pathnumManipulate.c
File metadata and controls
187 lines (154 loc) · 2.84 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* numManipulate.c
*
* Created on: Sep 27, 2016
* Author: Nhat Tan
*/
/*-----system include----------------*/
#include <stdint.h>
//#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
/*-----my include-------------------*/
#include "numManipulate.h"
float Map_y(float x, float x_min, float x_max,float y_min,float y_max)
{
float tan_alpha;
tan_alpha=(y_max-y_min)/(x_max-x_min);
return(tan_alpha*(x-x_min)+y_min);
}
void float2num(float num, char *str){
unsigned char numNeg,numID;
long numDem;
long numInt;
numID=0;
if (num<0)
{
num=-num;
numNeg=1;
}
else
numNeg=0;
char a[10];
numInt=num*100;
a[numID++]=numInt%10+48;
a[numID++]=numInt/10%10+48;
a[numID++]='.';
numInt=num;
if(num<10)
{
a[numID++]=numInt+48;
}
else
{
a[numID++]=numInt%10+48;
numDem=10;
while(num>=numDem)
{
numInt=num/numDem;
numDem*=10;
a[numID++]=numInt%10+48;
}
}
if(numNeg==1)
a[numID++]='-';
a[numID]='\0';
// char b[10];
// signed char i;
// char numInx=0;
// for (i=numID-1;i>=0;i--)
// {
// b[numInx++]=a[i];
// }
// b[numInx]='\0';
// char str[10];
signed char i;
char numInx=0;
for (i=numID-1;i>=0;i--)
{
*(str+(numInx++))=a[i];
// numInx++;
}
*(str+numInx)='\0';
}
// hai lôi trong cu phap, (se bao loi doi voi compiler C99 stric
// + return con tro vao bien cuc bo
// + dung index cho array co kieu la char
void int2num(int num,char *str)
{
unsigned char numNeg,numID;
long numDem;
long numInt;
numID=0;
if (num<0)
{
num=-num;
numNeg=1;
}
else
numNeg=0;
char a[10];
// numInt=num*100;
// a[numID++]=numInt%10+48;
// a[numID++]=numInt/10%10+48;
// a[numID++]='.';
numInt=num;
// if(num<10)
// {
// a[numID++]=numInt+48;
// }
// else
// {
a[numID++]=numInt%10+48;
numDem=10;
while(num>=numDem)
{
numInt=num/numDem;
numDem*=10;
a[numID++]=numInt%10+48;
}
// }
if(numNeg==1)
a[numID++]='-';
a[numID]='\0';
signed char i;
char numInx=0;
for (i=0;i<numID;i++)
{
*(str+(numInx++))=a[numID-1-i];
}
*(str+numInx)='\0';
}
void set_float_value (char *string,float *kp)
{
while (!(isdigit(*string) || *string == '-')) (string++);
*kp=atof(string);
}
void set_int_value (char *string,int32_t *kp)
{
while (! (isdigit(*string) || *string == '-')) (string++);
// while (! (isdigit(*string))) (string++);
*kp=atoi(string);
}
//void set_position (char *string, int16_t *position)
//{
// while(*string!='\0')
// {
//
// switch(*string){
// case 'x':
// *position=atoi(string+1);
// break;
// case 'y':
// *(position+1)=atoi(string+1);
// break;
// case 'z':
// *(position+2)=atoi(string+1);
// break;
// default:
// break;
// }
// string++;
// }
//
//}