This repository has been archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klib.c
148 lines (135 loc) · 2.06 KB
/
klib.c
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
#include "klib.h"
unsigned nascii(char n)
{
if(n >= '0' && n <= '9')
return (int) n-'0';
else
return (int) n+'0';
}
double pot(double x, unsigned int y)
{
if(!y)
return 1;
else
return x*pot(x,--y);
}
unsigned long fatt(unsigned long x)
{
if(!x)
return 1;
else
return x*fatt(--x);
}
unsigned int strl(const char* str)
{
unsigned int ret= 0;
while(str[ret]!='\0')
++ret;
return ret;
}
long unint(const char* num)
{
int x= 0;
long ret= 0;
while(num[x]!='\0')
{
if(num[x]>='0' && num[x]<='9')
{
ret*= 10;
ret+= nascii(num[x]);
}
++x;
}
ret= num[0]=='-'? -ret: ret;
return ret;
}
long double deci(const char* num)
{
int x= -1;
_Bool t= 0;
long double ret= unint(num);
while((int) ret) ret*= .1;
while(num[++x]!='\0')
{
if(num[x]=='.')
{
while(num[++x]=='0') ret*= .1;
break;
}
if(num[x]>='1' && num[x]<='9') ret*= 10;
}
return ret;
}
unsigned long binario(unsigned long num)
{
long ret= 0;
int x =0;
while(num > 0)
{
ret+= pot(10,x)*(num%2);
++x;
num/= 2;
}
return ret;
}
unsigned int match(const char* str, char n)
{
unsigned int ret= 0;
int x= strl(str);
while(x--)
if(str[x]==n)
++ret;
return ret;
}
void copia(const char* uno, char* due)
{
int x= -1;
while(++x <= strl(uno))
due[x]= uno[x];
}
void scambia(char* uno, char* due)
{
int x= -1;
char temp;
while(++x <= strl(uno))
{
temp= due[x];
due[x]= uno[x];
uno[x]= temp;
}
}
void ordina(long double* num, unsigned int len)
{
long double temp;
while(--len)
{
int x= 0;
while(len-x++)
if(num[x] < num[x-1])
{
temp= num[x];
num[x]= num[x-1];
num[x-1]= temp;
}
}
}
void charbin(long num, char* str, unsigned int len)
{
while(len-- > 0)
{
str[len]= (char) nascii(num%2);
num/=2;
}
}
unsigned int compara(char* uno, char* due)
{
unsigned int match= 0;
int x= 0;
while(x < strl(uno))
{
if(uno[x]==due[x])
match++;
++x;
}
return match;
}