/* This is the program that takes ``dotted decimals'' (characters) and * produces ``decimals'' (unsigned ints). * Use this as example to do the remainder of the programming excercise. */ /* This program uses the fact that the numerical value of '.' is 46, * of '0' is 48, of '1' is 49, etc, of '9' is 57. * See a table of Internal representations of ASCII characters. */ /* Students: Make sure to understand how this program works. * Then use it as example for your programs. */ #include int main() { char dotteddec[20]; /* 16 should be enough! figure out why. */ char bits[33]; /* why 33, not 32 ? */ /* This array is not used in this example program. */ unsigned int decs[4] = {0,0,0,0}; /* the 4 ``decimals'' */ unsigned int a, b ; int i, k, n; i = 0; while (i < 20) { dotteddec[i] = '\0'; /* Not necessary, but. */ i++; } i=0; while (i < 33) { bits[i] = '\0'; /* Not necessary, but. */ i++; } cin >> dotteddec ; /* reading the dotted decimals. Includes 3 dots (!) */ i = 0; k = 0; while (dotteddec[i] != '\0') { a = (int) dotteddec[i]; /* cast as int (!) */ /* A CAST is used when an entity of one type is expressed in an entity of another type. */ if (a == 46) /* if (dotteddec[i] == '.') */ { /* cout << " k = " << k << endl; */ k++; } else { a = a - 48; decs[3-k] = 10*decs[3-k] + a ; /* cout << decs[3-k] << endl; */ } i++; } cout << "Number of characters in the dotted decimal is " << i << endl ; cout << "Decimals, starting at least significant:" << endl ; i = 0; while (i < 4) { cout << decs[i] << " " ; /* print the numeric values in ``least significant first'' order. */ i++; } cout << "." << endl ; }