Exercise 5-6-3 (conversions - Base to decimal)

Chapter_5     Exercise_5-6     Exercise_5-6-2 Exercise_5-6-4     Exercise_5-7







Exercise 5-6     K&R, p. 107


Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline() (Chapters 1 and 4), atoi(), itoa(), and their variants (Chapters 2, 3, and 4), reverse(), (Chapter 3), and strindex() and getop() (Chapter 4).




CONTENTS:     htoi.c     otoi.c     btoi.c




htoi.c     K&R, p. 46  (Ex_2-3)         download


#include <stdio.h> // for printf(), scanf()

#define LENGTH 15 // max length of hex string (2 hex digits per byte)

int htoi(char *hex); // hex to decimal (integer)

int main()
{
char s[LENGTH];

printf("Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+\n");
scanf("%s", s);
printf("htoi(%s): %d\n", s, htoi(s));

return 0;
}

int htoi(char *hex) // hex to decimal (integer)
{
if (*hex == '\0') // empty string
{ // not a number
printf("\"%s\" is not a valid number\n", hex);
return 0; // return value does not signal error, should use an exception
}
// here hex is not empty
int n = 0, sign = 1;
char temp, *p = hex;

if (*p == '-') // negative number
{
sign = -1;
p++; // skip optional sign
}
else if (*p == '+') // positive number
{
p++; // skip optional sign
}

if (*p == '0')
{
p++;
if (*p == '\0')
{
return n; // return 0; // valid hex number
}
if (*p == 'x' || *p == 'X')
{p++;} // skip optional 0x or 0X
}
else if (*p == 'o' || *p == 'O')
{
temp = *p;
p++;
if (*p == 'x' || *p == 'X')
{
printf("\"%s\" is not a valid hex number\n", hex);
*(p-1) = '0'; // changes hex
printf("Did you mean \"%s\"?\n", hex);
*(p-1) = temp; // changes hex back, needed in main()
printf("Note the difference between 'o' or 'O' and '0' (zero)\n");
return 0;
}
}

if (*p == '\0')
{
printf("\"%s\" is not a valid hex number\n", hex);
return 0;
}

while (*p != '\0')
{
if (*p >= '0' && *p <= '9')
{n = n*16 + (*p - '0');}
else if (*p >= 'a' && *p <= 'f')
{n = n*16 + (*p-'a'+10);}
else if (*p >= 'A' && *p <= 'F')
{n = n*16 + (*p-'A'+10);}
else // not a hex number
{
printf("\"%s\" is not a valid hex number\n", hex);
return sign * n;
}

p++;
}

return sign * n;
}
/*
gcc htoi.c -o htoi
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0
htoi(0): 0

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0x
"0x" is not a valid hex number
htoi(0x): 0

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
1.2
"1.2" is not a valid hex number
htoi(1.2): 1

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-2.5
"-2.5" is not a valid hex number
htoi(-2.5): -2

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
10
htoi(10): 16

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0x9
htoi(0x9): 9

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
g
"g" is not a valid hex number
htoi(g): 0

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-25
htoi(-25): -37

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0x-10 // write -0x10
"0x-10" is not a valid hex number
htoi(0x-10): 0

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-0x10
htoi(-0x10): -16

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
+025
htoi(+025): 37

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
oxc // letter 'o' instead of digit '0'
"oxc" is not a valid hex number
Did you mean "0xc"?
Note the difference between 'o' or 'O' and '0' (zero)
htoi(oxc): 0

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0xc
htoi(0xc): 12

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
+Ox14
"+Ox14" is not a valid hex number
Did you mean "+0x14"?
Note the difference between 'o' or 'O' and '0' (zero)
htoi(+Ox14): 0

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
+0x14
htoi(+0x14): 20

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0xa1
htoi(0xa1): 161

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0xaA
htoi(0xaA): 170

/htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0Xaa
htoi(0Xaa): 170

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
aA
htoi(aA): 170

./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-AA
htoi(-AA): -170
*/











otoi.c     (ch2-otoi)         download


#include <stdio.h> // for printf(), scanf()

#define LENGTH 20 // max length of octal string (3 octal digits per byte)

int otoi(char *octal); // octal to decimal (integer)

int main()
{
char s[LENGTH];

printf("Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+\n");
scanf("%s", s);
printf("otoi(%s): %d\n", s, otoi(s));

return 0;
}

int otoi(char *octal) // octal to decimal (integer)
{
if (*octal == '\0') // empty string is not a number
{
printf("\"%s\" is not a valid number\n", octal);
return 0; // should handle the error with an exception as we cannot
} // signal the error here with a returned value
// here octal is not empty
int n = 0, sign = 1;
char temp, *p = octal;

if (*p == '-')
{
sign = -1;
p++; // skip optional sign
}
else if (*p == '+')
{
p++; // skip optional sign
}

if (*p == '0')
{
p++; // skip optional '0'
if (*p == '\0')
{return n;} // return 0; // valid octal number
}
else if (*p == 'o' || *p == 'O')
{
printf("\"%s\" is not a valid octal number\n", octal);
temp = *p;
*p = '0'; // change octal
printf("Did you mean \"%s\"?\n", octal);
*p = temp; // change octal back, needed in main()
printf("Note the difference between 'o' or 'O' and '0' (zero)\n");
return 0;
}
else if (*p == '\0')
{
printf("\"%s\" is not a valid octal number\n", octal);
return 0;
}

while (*p != '\0')
{
if (*p >= '0' && *p <= '7')
{n = n*8 + (*p - '0');}
else // not an octal number
{
printf("\"%s\" is not a valid octal number\n", octal);
return sign * n;
}

p++;
}

return sign * n;
}
/*
gcc otoi.c -o otoi
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
0
otoi(0): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
+
"+" is not a valid octal number
otoi(+): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-
"-" is not a valid octal number
otoi(-): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
1.2
"1.2" is not a valid octal number
otoi(1.2): 1

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-2.5
"-2.5" is not a valid octal number
otoi(-2.5): -2

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
o1 // letter 'o' instead of digit '0'
"o1" is not a valid octal number
Did you mean "01"?
Note the difference between 'o' or 'O' and '0' (zero)
otoi(o1): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-o.0
"-o.0" is not a valid octal number
Did you mean "-0.0"?
Note the difference between 'o' or 'O' and '0' (zero)
otoi(-o.0): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-0.0
"-0.0" is not a valid octal number
otoi(-0.0): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
22
otoi(22): 18

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
+022
otoi(+022): 18

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
+0022
otoi(+0022): 18

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-09
"-09" is not a valid octal number
otoi(-09): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
0-7 // write -07
"0-7" is not a valid octal number
otoi(0-7): 0

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-07
otoi(-07): -7

./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-7
otoi(-7): -7
*/











btoi.c     (ch2-btoi)         download


#include <stdio.h> // for printf(), scanf()

#define LENGTH 50 // max length of binary string (8 bits per byte)

int btoi(char *); // binary to decimal (integer)

int main()
{
char s[LENGTH];

printf("Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+\n");
scanf("%s", s);
printf("btoi(%s): %d\n", s, btoi(s));

return 0;
}

int btoi(char *bin) // binary to decimal (integer)
{
if (*bin == '\0') // empty string is not a number
{
printf("\"%s\" is not a valid number\n", bin);
return 0; // should handle the error with an exception as we cannot
} // signal the error here with a returned value
// here bin[] is not empty
int n = 0, sign = 1;
char temp, *p = bin;

if (*p == '-') // negative number
{
sign = -1;
p++; // skip optional sign
}
else if (*p == '+') // positive number
{
p++; // skip optional sign
}

if (*p == '0')
{
p++;
if (*p == '\0')
{return n;} // return 0; // valid binary number
if (*p == 'b' || *p == 'B')
{p++;} // skip optional 0b or 0B
}
else if (*p == 'o' || *p == 'O')
{
temp = *p;
p++;
if (*p == 'b' || *p == 'B')
{
printf("\"%s\" is not a valid binary number\n", bin);
*(p-1) = '0'; // change bin
printf("Did you mean \"%s\"?\n", bin);
*(p-1) = temp; // change bin back, needed in main()
printf("Note the difference between 'o' or 'O' and '0' (zero)\n");
return 0;
}
}

if (*p == '\0')
{ // "0b" or "0B" is not a valid binary number
printf("\"%s\" is not a valid binary number\n", bin);
return 0;
}

while (*p != '\0')
{
if (*p == '0' || *p == '1')
{n = n*2 + (*p - '0');}
else // not a binary number
{
printf("\"%s\" is not a valid binary number\n", bin);
return sign * n;
}

p++;
}

return sign * n;
}
/*
gcc btoi.c -o btoi
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
+
"+" is not a valid binary number
btoi(+): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-
"-" is not a valid binary number
btoi(-): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
+01
btoi(+01): 1

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0b
"0b" is not a valid binary number
btoi(0b): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
a
"a" is not a valid binary number
btoi(a): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
123
"123" is not a valid binary number
btoi(123): 1

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-14
"-14" is not a valid binary number
btoi(-14): -1

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
ob1 // letter 'o', not digit '0'
"ob1" is not a valid binary number
Did you mean "0b1"?
Note the difference between 'o' or 'O' and '0' (zero)
btoi(ob1): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0b1
btoi(0b1): 1

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-0b101
btoi(-0b101): -5

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
11111111
btoi(11111111): 255

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-0B100000000
btoi(-0B100000000): -256

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0b+1 // write +0b1
"0b+1" is not a valid binary number
btoi(0b+1): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
+0b1
btoi(+0b1): 1

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0B-11 // write -0B11
"0B-11" is not a valid binary number
btoi(0B-11): 0

./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-0B11
btoi(-0B11): -3
*/









Chapter_5     Exercise_5-6     Exercise_5-6-2 BACK_TO_TOP Exercise_5-6-4     Exercise_5-7



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo