File: strings.cpp

package info (click to toggle)
rafkill 1.2.2-3.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 13,268 kB
  • sloc: cpp: 13,508; makefile: 64; sh: 14
file content (195 lines) | stat: -rw-r--r-- 3,731 bytes parent folder | download | duplicates (12)
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
188
189
190
191
192
193
194
195
#include "strings.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>

using namespace std;

int numDig( int q ) {

	int t = 1;
	q = abs( q );
	while( q >= 10 ) {
		t++;
		q /= 10;
	}
	return t;

}

string int2str( int z ) {
	bool neg = z < 0;
	int nd = numDig( z );
	char * str = (char *)malloc( sizeof(char) * (neg + nd + 1) );

	z = abs( z );
	for ( int q = 0; q < nd; q++ ) {
		int h = z % 10;
		str[ neg + nd - q - 1 ] = '0' + h;
		z /= 10;
	}
	if ( neg ) str[0] = '-';
	str[ neg + nd ] = '\0';
	string s = str;
	free( str );
	return s;
}

string int2normal( int z ) {
	bool neg = z < 0;
	int nd = numDig( z );
	int total_char = neg + nd + abs(nd-1)/3+1;
	char * str = (char *)malloc( sizeof(char) * (total_char) );
	for ( int q = 0; q < total_char; q++ )
		str[q] = 'A';
	char * lem = str+(total_char-1);
	*lem = '\0';
	lem--;
	z = abs( z );
	for ( int q = 0; q < nd; q++ ) {
		int h = z % 10;
		*lem = '0'+h;
		lem--;
		if ( (q+1) % 3 == 0 && q != nd-1 ) {
			*lem = ',';
			lem--;
		}
		z /= 10;
	}
	if ( neg ) *str = '-';
	string s = str;
	free( str );
	return s;
}

/*
char * d2normal( double whole, int precise ) {
	int z = (int)whole;
	bool neg = z < 0;
	int nd = numDig( z );
	int total_char = neg + nd + abs(nd-1)/3+1+1;
	char * str = (char *)malloc( sizeof(char) * (total_char) );
	for ( int q = 0; q < total_char; q++ )
		str[q] = 'A';
	char * lem = str+(total_char-1);
	*lem = '\0';
	lem--;
	*lem = '.';
	lem--;
	z = abs( z );
	for ( int q = 0; q < nd; q++ ) {
		int h = z % 10;
		*lem = '0'+h;
		lem--;
		if ( (q+1) % 3 == 0 && q != nd-1 ) {
			*lem = ',';
			lem--;
		}
		z /= 10;
	}

	z = (int)whole;
	whole -= z;
	whole = fabs( whole );
	for ( int q = 0; q < precise; q++ ) {

		char * t = (char *)malloc( sizeof( char ) * 2 );
		whole *= 10;
		z = (int)whole;
		*t = '0' + z;
		whole -= z;
		*(t+1) = '\0';
		char * reveal = append( str, t );
		free( str );
		free( t );
		str = reveal;

	}

	if ( neg ) *str = '-';
	return str;
}
*/

char upcase( char u ) {

	if ( u >= 'a' && u <= 'z' )
		return u-'a'+'A';
	else    return u;

}


void upstring( char * who ) {
	while ( *who != 0 )
		*who++ = upcase( *who );
}


int str2int( char * convert ) {

	int total = 0;
	bool neg = convert[0] == '-';
	for ( int q = neg; q < length( convert ); q++ ) {
		if ( !(convert[q] >= '0' && convert[q] <= '9' ) )
			return 0;
		total = total * 10 + convert[q]-'0';
	}
	if ( neg ) total *= -1;
	return total;

}


double str2d( char * convert ) {

	double front = 0;
	double back = 0;
	int place;
	bool neg = convert[0] == '-';
	for ( place = neg; place < length( convert ) && convert[place] != '.'; place++ ) {
		if ( !( convert[place] >= '0' && convert[place] <= '9' ) )
			return 0;
		front = (front * 10.0) + convert[place]-'0';
	}
	if ( neg ) front *= -1;
	if ( place >= length( convert ) )
		return front;
	if ( convert[place] == '.' ) place++;
	double total = 1.0;
	for ( ; place < length( convert ); place++ ) {
		if ( !(convert[place] >= '0' && convert[place] <= '9' ) )
			return 0;
		total *= 10.0;
		back = (back * 10.0) + convert[place]-'0';
	}
	back /= total;
	if ( neg ) back *= -1;
	front += back;
	return front;

}


int length( char * q ) {
	int z;
	for ( z = 0; *q != '\0'; z++, q++ );
	return z;
}

/*
char * append( const char * first, const char * second ) {
	//printf("Appending [%s] with [%s]\n", first, second );
	int m1 = strlen( first );
	int m2 = strlen( second );
	if ( m1 + m2 == 0 )
		return strdup("");
	char * final = (char *)malloc( sizeof(char) * (m1 + m2 + 1) );
	if ( m1 ) memmove( (void *)final, (void *)first, m1 );
	if ( m2 ) memmove( (void *)(final + sizeof(char)*m1 ), (void *)second, m2 );
	final[ m1 + m2 ] = '\0';
	return final;
}
*/