jazykc
2013.3
upoljazykc
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Macros
Pages
funkce
prevody-cisel-do-desitkove-soustavy.c
Go to the documentation of this file.
1
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
//kvuli cemu? strlen()? kvuli strtol() asi ne ;-)
32
33
int
do_desitkove(
char
cislo[],
int
zaklad) {
34
int
i, n;
35
size_t
len=strlen(cislo);
36
int
digit;
37
int
suma = 0;
38
int
an = 1;
39
40
if
(0==len)
return
0;
41
42
//bile znaky na konci/zacatku neresime
43
for
(i=len-1; i>=0; i--, an *= zaklad) {
44
digit = cislo[i];
45
if
(digit >=
'0'
&& digit <=
'9'
) {
46
n = digit -
'0'
;
47
//if (n >= zaklad) ; //ERROR! nebo break?
48
suma += an * n;
49
}
50
else
{
51
digit |= 0x20;
//konverze VELKA->mala
52
n = digit -
'a'
+ 10;
53
//if (n >= zaklad || n > 36 || n < 0) ; //ERROR nebo konec/break
54
suma += an * n;
55
}
56
}
57
return
suma;
58
}
59
60
#define NELEM(pole) (sizeof pole/sizeof pole[0])
61
62
int
main(
void
) {
63
struct
test_s {
char
*cislo;
int
zaklad; } cisla[]={{
"123"
,10},{
"A1B"
,12},{
"1010"
,2},{
"ZZZ"
,36}};
64
int
i;
65
for
(i=0; i<
NELEM
(cisla); i++) {
66
printf(
"Cislo %10s v soustave o zakladu %2d odpovida cislu %10d v desitkove soustave.\n"
,
67
cisla[i].cislo, cisla[i].zaklad, do_desitkove(cisla[i].cislo, cisla[i].zaklad));
68
}
69
return
EXIT_SUCCESS;
70
}
NELEM
#define NELEM(pole)
Definition:
cetnost-znaku-v-poli-retezcu2.c:67
Generated on Sat Dec 7 2013 22:57:26 for jazykc by
1.8.5