ch2-otoi (Octal to decimal)

Chapter_2     Exercise_2-3 btoi     Exercise_2-4







otoi.c         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 (string) 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 (string) to decimal (integer)
{
if (octal[0] == '\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 i = 0, n = 0, sign = 1;
char temp;

if (octal[i] == '-')
{
sign = -1;
i++; // skip optional sign
}
else if (octal[i] == '+')
{
i++; // skip optional sign
}

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

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

i++;
}

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]+
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
*/





Notes:  Octal numbers are preceded by an optional sign and in our program by an optional 0, so we write [+-]?0[0-7]* as +0, -0, and 0 are valid octal numbers. Here, [0-7]* means zero or more octal digits (in the range 0 ... 7). The other regular_expression, [+-]?[0-7]+ means an optional sign followed by one or more octal digits. A valid number must have at least one digit; in the first case this is 0, and in the second it is in the range 0 ... 7.









Chapter_2     Exercise_2-3 BACK_TO_TOP btoi     Exercise_2-4



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo